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