xref: /freebsd/tools/test/hwpmc/pmctest.py (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1#!/usr/bin/env python
2# SPDX-License-Identifier: BSD-3-Clause
3#
4# Copyright (c) 2012, Neville-Neil Consulting
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11# Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# Redistributions in binary form must reproduce the above copyright
15# notice, this list of conditions and the following disclaimer in the
16# documentation and/or other materials provided with the distribution.
17#
18# Neither the name of Neville-Neil Consulting nor the names of its
19# contributors may be used to endorse or promote products derived from
20# this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34# Author: George V. Neville-Neil
35#
36# $FreeBSD$
37
38# Description: A program to run a simple program against every available
39# pmc counter present in a system.
40#
41# To use:
42#
43# pmctest.py -p ls > /dev/null
44#
45# This should result in ls being run with every available counter
46# and the system should neither lock up nor panic.
47#
48# The default is to wait after each counter is tested.  Since the
49# prompt would go to stdout you won't see it, just press return
50# to continue or Ctrl-D to stop.
51
52import sys
53import subprocess
54from subprocess import PIPE
55
56# Use input() for Python version 3
57if sys.version_info[0] == 3:
58    raw_input = input
59
60# A list of strings that are not really counters, just
61# name tags that are output by pmccontrol -L
62notcounter = ["IAF", "IAP", "TSC", "UNC", "UCF", "UCP", "SOFT" ]
63
64def main():
65
66    from optparse import OptionParser
67
68    parser = OptionParser()
69    parser.add_option("-p", "--program", dest="program",
70                      help="program to execute")
71    parser.add_option("-w", "--wait", action="store_true", dest="wait",
72                      default=True, help="wait after each execution")
73
74    (options, args) = parser.parse_args()
75
76    if (options.program == None):
77        print("specify program, such as ls, with -p/--program")
78        sys.exit()
79
80    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
81    counters = p.communicate()[0]
82
83    if len(counters) <= 0:
84        print("no counters found")
85        sys.exit()
86
87    for counter in counters.split():
88        if counter in notcounter:
89            continue
90        p = subprocess.Popen(["pmcstat", "-p", counter, options.program],
91                             stdout=PIPE)
92        result = p.communicate()[0]
93        print(result)
94        if (options.wait == True):
95            try:
96                value = raw_input("next?")
97            except EOFError:
98                sys.exit()
99
100# The canonical way to make a python module into a script.
101# Remove if unnecessary.
102
103if __name__ == "__main__":
104    main()
105