xref: /freebsd/tools/test/hwpmc/pmctest.py (revision 2542e55879f88791c74e54f501469f339f95849b)
1*2542e558SGeorge V. Neville-Neil#!/usr/bin/env python
2*2542e558SGeorge V. Neville-Neil# Copyright (c) 2012, Neville-Neil Consulting
3*2542e558SGeorge V. Neville-Neil# All rights reserved.
4*2542e558SGeorge V. Neville-Neil#
5*2542e558SGeorge V. Neville-Neil# Redistribution and use in source and binary forms, with or without
6*2542e558SGeorge V. Neville-Neil# modification, are permitted provided that the following conditions are
7*2542e558SGeorge V. Neville-Neil# met:
8*2542e558SGeorge V. Neville-Neil#
9*2542e558SGeorge V. Neville-Neil# Redistributions of source code must retain the above copyright notice,
10*2542e558SGeorge V. Neville-Neil# this list of conditions and the following disclaimer.
11*2542e558SGeorge V. Neville-Neil#
12*2542e558SGeorge V. Neville-Neil# Redistributions in binary form must reproduce the above copyright
13*2542e558SGeorge V. Neville-Neil# notice, this list of conditions and the following disclaimer in the
14*2542e558SGeorge V. Neville-Neil# documentation and/or other materials provided with the distribution.
15*2542e558SGeorge V. Neville-Neil#
16*2542e558SGeorge V. Neville-Neil# Neither the name of Neville-Neil Consulting nor the names of its
17*2542e558SGeorge V. Neville-Neil# contributors may be used to endorse or promote products derived from
18*2542e558SGeorge V. Neville-Neil# this software without specific prior written permission.
19*2542e558SGeorge V. Neville-Neil#
20*2542e558SGeorge V. Neville-Neil# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*2542e558SGeorge V. Neville-Neil# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*2542e558SGeorge V. Neville-Neil# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23*2542e558SGeorge V. Neville-Neil# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24*2542e558SGeorge V. Neville-Neil# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25*2542e558SGeorge V. Neville-Neil# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26*2542e558SGeorge V. Neville-Neil# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27*2542e558SGeorge V. Neville-Neil# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28*2542e558SGeorge V. Neville-Neil# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29*2542e558SGeorge V. Neville-Neil# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30*2542e558SGeorge V. Neville-Neil# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*2542e558SGeorge V. Neville-Neil#
32*2542e558SGeorge V. Neville-Neil# Author: George V. Neville-Neil
33*2542e558SGeorge V. Neville-Neil#
34*2542e558SGeorge V. Neville-Neil# $FreeBSD$
35*2542e558SGeorge V. Neville-Neil
36*2542e558SGeorge V. Neville-Neil# Description: A program to run a simple program against every available
37*2542e558SGeorge V. Neville-Neil# pmc counter present in a system.
38*2542e558SGeorge V. Neville-Neil#
39*2542e558SGeorge V. Neville-Neil# To use:
40*2542e558SGeorge V. Neville-Neil#
41*2542e558SGeorge V. Neville-Neil# pmctest.py ls > /dev/null
42*2542e558SGeorge V. Neville-Neil#
43*2542e558SGeorge V. Neville-Neil# This should result in ls being run with every available counter
44*2542e558SGeorge V. Neville-Neil# and the system should neither lock up nor panic.
45*2542e558SGeorge V. Neville-Neil
46*2542e558SGeorge V. Neville-Neilimport sys
47*2542e558SGeorge V. Neville-Neilimport subprocess
48*2542e558SGeorge V. Neville-Neilfrom subprocess import PIPE
49*2542e558SGeorge V. Neville-Neil
50*2542e558SGeorge V. Neville-Neil# A list of strings that are not really counters, just
51*2542e558SGeorge V. Neville-Neil# name tags that are output by pmccontrol -L
52*2542e558SGeorge V. Neville-Neilnotcounter = ["IAF", "IAP", "TSC", "UNC", "UCF"]
53*2542e558SGeorge V. Neville-Neil
54*2542e558SGeorge V. Neville-Neildef main():
55*2542e558SGeorge V. Neville-Neil
56*2542e558SGeorge V. Neville-Neil    if (len(sys.argv) != 2):
57*2542e558SGeorge V. Neville-Neil        print ("usage: pmctest.py program")
58*2542e558SGeorge V. Neville-Neil
59*2542e558SGeorge V. Neville-Neil    program = sys.argv[1]
60*2542e558SGeorge V. Neville-Neil
61*2542e558SGeorge V. Neville-Neil    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
62*2542e558SGeorge V. Neville-Neil    counters = p.communicate()[0]
63*2542e558SGeorge V. Neville-Neil
64*2542e558SGeorge V. Neville-Neil    if len(counters) <= 0:
65*2542e558SGeorge V. Neville-Neil        print "no counters found"
66*2542e558SGeorge V. Neville-Neil        sys.exit()
67*2542e558SGeorge V. Neville-Neil
68*2542e558SGeorge V. Neville-Neil    for counter in counters.split():
69*2542e558SGeorge V. Neville-Neil        if counter in notcounter:
70*2542e558SGeorge V. Neville-Neil            continue
71*2542e558SGeorge V. Neville-Neil        p = subprocess.Popen(["pmcstat", "-p", counter, program], stdout=PIPE)
72*2542e558SGeorge V. Neville-Neil        result = p.communicate()[0]
73*2542e558SGeorge V. Neville-Neil        print result
74*2542e558SGeorge V. Neville-Neil
75*2542e558SGeorge V. Neville-Neil# The canonical way to make a python module into a script.
76*2542e558SGeorge V. Neville-Neil# Remove if unnecessary.
77*2542e558SGeorge V. Neville-Neil
78*2542e558SGeorge V. Neville-Neilif __name__ == "__main__":
79*2542e558SGeorge V. Neville-Neil    main()
80