xref: /freebsd/tools/test/hwpmc/pmctest.py (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
12542e558SGeorge V. Neville-Neil#!/usr/bin/env python
2*8a16b7a1SPedro F. Giffuni# SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni#
42542e558SGeorge V. Neville-Neil# Copyright (c) 2012, Neville-Neil Consulting
52542e558SGeorge V. Neville-Neil# All rights reserved.
62542e558SGeorge V. Neville-Neil#
72542e558SGeorge V. Neville-Neil# Redistribution and use in source and binary forms, with or without
82542e558SGeorge V. Neville-Neil# modification, are permitted provided that the following conditions are
92542e558SGeorge V. Neville-Neil# met:
102542e558SGeorge V. Neville-Neil#
112542e558SGeorge V. Neville-Neil# Redistributions of source code must retain the above copyright notice,
122542e558SGeorge V. Neville-Neil# this list of conditions and the following disclaimer.
132542e558SGeorge V. Neville-Neil#
142542e558SGeorge V. Neville-Neil# Redistributions in binary form must reproduce the above copyright
152542e558SGeorge V. Neville-Neil# notice, this list of conditions and the following disclaimer in the
162542e558SGeorge V. Neville-Neil# documentation and/or other materials provided with the distribution.
172542e558SGeorge V. Neville-Neil#
182542e558SGeorge V. Neville-Neil# Neither the name of Neville-Neil Consulting nor the names of its
192542e558SGeorge V. Neville-Neil# contributors may be used to endorse or promote products derived from
202542e558SGeorge V. Neville-Neil# this software without specific prior written permission.
212542e558SGeorge V. Neville-Neil#
222542e558SGeorge V. Neville-Neil# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
232542e558SGeorge V. Neville-Neil# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
242542e558SGeorge V. Neville-Neil# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
252542e558SGeorge V. Neville-Neil# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
262542e558SGeorge V. Neville-Neil# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
272542e558SGeorge V. Neville-Neil# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
282542e558SGeorge V. Neville-Neil# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
292542e558SGeorge V. Neville-Neil# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
302542e558SGeorge V. Neville-Neil# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
312542e558SGeorge V. Neville-Neil# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
322542e558SGeorge V. Neville-Neil# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
332542e558SGeorge V. Neville-Neil#
342542e558SGeorge V. Neville-Neil# Author: George V. Neville-Neil
352542e558SGeorge V. Neville-Neil#
362542e558SGeorge V. Neville-Neil
372542e558SGeorge V. Neville-Neil# Description: A program to run a simple program against every available
382542e558SGeorge V. Neville-Neil# pmc counter present in a system.
392542e558SGeorge V. Neville-Neil#
402542e558SGeorge V. Neville-Neil# To use:
412542e558SGeorge V. Neville-Neil#
42f9166e7cSGeorge V. Neville-Neil# pmctest.py -p ls > /dev/null
432542e558SGeorge V. Neville-Neil#
442542e558SGeorge V. Neville-Neil# This should result in ls being run with every available counter
452542e558SGeorge V. Neville-Neil# and the system should neither lock up nor panic.
46f9166e7cSGeorge V. Neville-Neil#
47f9166e7cSGeorge V. Neville-Neil# The default is to wait after each counter is tested.  Since the
48f9166e7cSGeorge V. Neville-Neil# prompt would go to stdout you won't see it, just press return
49f9166e7cSGeorge V. Neville-Neil# to continue or Ctrl-D to stop.
502542e558SGeorge V. Neville-Neil
512542e558SGeorge V. Neville-Neilimport sys
522542e558SGeorge V. Neville-Neilimport subprocess
532542e558SGeorge V. Neville-Neilfrom subprocess import PIPE
542542e558SGeorge V. Neville-Neil
5505864d89SHiren Panchasara# Use input() for Python version 3
5605864d89SHiren Panchasaraif sys.version_info[0] == 3:
5705864d89SHiren Panchasara    raw_input = input
5805864d89SHiren Panchasara
592542e558SGeorge V. Neville-Neil# A list of strings that are not really counters, just
602542e558SGeorge V. Neville-Neil# name tags that are output by pmccontrol -L
61345ddfcfSFabien Thomasnotcounter = ["IAF", "IAP", "TSC", "UNC", "UCF", "UCP", "SOFT" ]
622542e558SGeorge V. Neville-Neil
632542e558SGeorge V. Neville-Neildef main():
642542e558SGeorge V. Neville-Neil
65f9166e7cSGeorge V. Neville-Neil    from optparse import OptionParser
662542e558SGeorge V. Neville-Neil
67f9166e7cSGeorge V. Neville-Neil    parser = OptionParser()
68f9166e7cSGeorge V. Neville-Neil    parser.add_option("-p", "--program", dest="program",
69f9166e7cSGeorge V. Neville-Neil                      help="program to execute")
70f9166e7cSGeorge V. Neville-Neil    parser.add_option("-w", "--wait", action="store_true", dest="wait",
71f9166e7cSGeorge V. Neville-Neil                      default=True, help="wait after each execution")
72f9166e7cSGeorge V. Neville-Neil
73f9166e7cSGeorge V. Neville-Neil    (options, args) = parser.parse_args()
742542e558SGeorge V. Neville-Neil
75d2c10b2aSGeorge V. Neville-Neil    if (options.program == None):
76168ff9abSEitan Adler        print("specify program, such as ls, with -p/--program")
77d2c10b2aSGeorge V. Neville-Neil        sys.exit()
78d2c10b2aSGeorge V. Neville-Neil
792542e558SGeorge V. Neville-Neil    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
802542e558SGeorge V. Neville-Neil    counters = p.communicate()[0]
812542e558SGeorge V. Neville-Neil
822542e558SGeorge V. Neville-Neil    if len(counters) <= 0:
83168ff9abSEitan Adler        print("no counters found")
842542e558SGeorge V. Neville-Neil        sys.exit()
852542e558SGeorge V. Neville-Neil
862542e558SGeorge V. Neville-Neil    for counter in counters.split():
872542e558SGeorge V. Neville-Neil        if counter in notcounter:
882542e558SGeorge V. Neville-Neil            continue
89f9166e7cSGeorge V. Neville-Neil        p = subprocess.Popen(["pmcstat", "-p", counter, options.program],
90f9166e7cSGeorge V. Neville-Neil                             stdout=PIPE)
912542e558SGeorge V. Neville-Neil        result = p.communicate()[0]
92168ff9abSEitan Adler        print(result)
93f9166e7cSGeorge V. Neville-Neil        if (options.wait == True):
94f9166e7cSGeorge V. Neville-Neil            try:
9505864d89SHiren Panchasara                value = raw_input("next?")
96f9166e7cSGeorge V. Neville-Neil            except EOFError:
97f9166e7cSGeorge V. Neville-Neil                sys.exit()
982542e558SGeorge V. Neville-Neil
992542e558SGeorge V. Neville-Neil# The canonical way to make a python module into a script.
1002542e558SGeorge V. Neville-Neil# Remove if unnecessary.
1012542e558SGeorge V. Neville-Neil
1022542e558SGeorge V. Neville-Neilif __name__ == "__main__":
1032542e558SGeorge V. Neville-Neil    main()
104