1231634Sgnn#!/usr/bin/env python
2231634Sgnn# Copyright (c) 2012, Neville-Neil Consulting
3231634Sgnn# All rights reserved.
4231634Sgnn#
5231634Sgnn# Redistribution and use in source and binary forms, with or without
6231634Sgnn# modification, are permitted provided that the following conditions are
7231634Sgnn# met:
8231634Sgnn#
9231634Sgnn# Redistributions of source code must retain the above copyright notice,
10231634Sgnn# this list of conditions and the following disclaimer.
11231634Sgnn#
12231634Sgnn# Redistributions in binary form must reproduce the above copyright
13231634Sgnn# notice, this list of conditions and the following disclaimer in the
14231634Sgnn# documentation and/or other materials provided with the distribution.
15231634Sgnn#
16231634Sgnn# Neither the name of Neville-Neil Consulting nor the names of its
17231634Sgnn# contributors may be used to endorse or promote products derived from
18231634Sgnn# this software without specific prior written permission.
19231634Sgnn#
20231634Sgnn# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21231634Sgnn# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22231634Sgnn# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23231634Sgnn# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24231634Sgnn# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25231634Sgnn# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26231634Sgnn# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27231634Sgnn# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28231634Sgnn# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29231634Sgnn# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30231634Sgnn# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31231634Sgnn#
32231634Sgnn# Author: George V. Neville-Neil
33231634Sgnn#
34231634Sgnn# $FreeBSD: releng/10.3/tools/test/hwpmc/pmctest.py 250612 2013-05-13 19:53:19Z hiren $
35231634Sgnn
36231634Sgnn# Description: A program to run a simple program against every available
37231634Sgnn# pmc counter present in a system.
38231634Sgnn#
39231634Sgnn# To use:
40231634Sgnn#
41231698Sgnn# pmctest.py -p ls > /dev/null
42231634Sgnn#
43231634Sgnn# This should result in ls being run with every available counter
44231634Sgnn# and the system should neither lock up nor panic.
45231698Sgnn#
46231698Sgnn# The default is to wait after each counter is tested.  Since the
47231698Sgnn# prompt would go to stdout you won't see it, just press return
48231698Sgnn# to continue or Ctrl-D to stop.
49231634Sgnn
50231634Sgnnimport sys
51231634Sgnnimport subprocess
52231634Sgnnfrom subprocess import PIPE
53231634Sgnn
54250612Shiren# Use input() for Python version 3
55250612Shirenif sys.version_info[0] == 3:
56250612Shiren    raw_input = input
57250612Shiren
58231634Sgnn# A list of strings that are not really counters, just
59231634Sgnn# name tags that are output by pmccontrol -L
60240323Sfabientnotcounter = ["IAF", "IAP", "TSC", "UNC", "UCF", "UCP", "SOFT" ]
61231634Sgnn
62231634Sgnndef main():
63231634Sgnn
64231698Sgnn    from optparse import OptionParser
65231698Sgnn
66231698Sgnn    parser = OptionParser()
67231698Sgnn    parser.add_option("-p", "--program", dest="program",
68231698Sgnn                      help="program to execute")
69231698Sgnn    parser.add_option("-w", "--wait", action="store_true", dest="wait",
70231698Sgnn                      default=True, help="wait after each execution")
71231634Sgnn
72231698Sgnn    (options, args) = parser.parse_args()
73231634Sgnn
74240101Sgnn    if (options.program == None):
75241826Seadler        print("specify program, such as ls, with -p/--program")
76240101Sgnn        sys.exit()
77240101Sgnn
78231634Sgnn    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
79231634Sgnn    counters = p.communicate()[0]
80231634Sgnn
81231634Sgnn    if len(counters) <= 0:
82241826Seadler        print("no counters found")
83231634Sgnn        sys.exit()
84231634Sgnn
85231634Sgnn    for counter in counters.split():
86231634Sgnn        if counter in notcounter:
87231634Sgnn            continue
88231698Sgnn        p = subprocess.Popen(["pmcstat", "-p", counter, options.program],
89231698Sgnn                             stdout=PIPE)
90231634Sgnn        result = p.communicate()[0]
91241826Seadler        print(result)
92231698Sgnn        if (options.wait == True):
93231698Sgnn            try:
94250612Shiren                value = raw_input("next?")
95231698Sgnn            except EOFError:
96231698Sgnn                sys.exit()
97231634Sgnn
98231634Sgnn# The canonical way to make a python module into a script.
99231634Sgnn# Remove if unnecessary.
100231634Sgnn
101231634Sgnnif __name__ == "__main__":
102231634Sgnn    main()
103