xref: /freebsd/tools/test/hwpmc/pmctest.py (revision f9166e7c3da08517e9b78191c018ad9ea2439706)
12542e558SGeorge V. Neville-Neil#!/usr/bin/env python
22542e558SGeorge V. Neville-Neil# Copyright (c) 2012, Neville-Neil Consulting
32542e558SGeorge V. Neville-Neil# All rights reserved.
42542e558SGeorge V. Neville-Neil#
52542e558SGeorge V. Neville-Neil# Redistribution and use in source and binary forms, with or without
62542e558SGeorge V. Neville-Neil# modification, are permitted provided that the following conditions are
72542e558SGeorge V. Neville-Neil# met:
82542e558SGeorge V. Neville-Neil#
92542e558SGeorge V. Neville-Neil# Redistributions of source code must retain the above copyright notice,
102542e558SGeorge V. Neville-Neil# this list of conditions and the following disclaimer.
112542e558SGeorge V. Neville-Neil#
122542e558SGeorge V. Neville-Neil# Redistributions in binary form must reproduce the above copyright
132542e558SGeorge V. Neville-Neil# notice, this list of conditions and the following disclaimer in the
142542e558SGeorge V. Neville-Neil# documentation and/or other materials provided with the distribution.
152542e558SGeorge V. Neville-Neil#
162542e558SGeorge V. Neville-Neil# Neither the name of Neville-Neil Consulting nor the names of its
172542e558SGeorge V. Neville-Neil# contributors may be used to endorse or promote products derived from
182542e558SGeorge V. Neville-Neil# this software without specific prior written permission.
192542e558SGeorge V. Neville-Neil#
202542e558SGeorge V. Neville-Neil# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
212542e558SGeorge V. Neville-Neil# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
222542e558SGeorge V. Neville-Neil# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
232542e558SGeorge V. Neville-Neil# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
242542e558SGeorge V. Neville-Neil# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
252542e558SGeorge V. Neville-Neil# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
262542e558SGeorge V. Neville-Neil# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272542e558SGeorge V. Neville-Neil# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282542e558SGeorge V. Neville-Neil# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
292542e558SGeorge V. Neville-Neil# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
302542e558SGeorge V. Neville-Neil# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312542e558SGeorge V. Neville-Neil#
322542e558SGeorge V. Neville-Neil# Author: George V. Neville-Neil
332542e558SGeorge V. Neville-Neil#
342542e558SGeorge V. Neville-Neil# $FreeBSD$
352542e558SGeorge V. Neville-Neil
362542e558SGeorge V. Neville-Neil# Description: A program to run a simple program against every available
372542e558SGeorge V. Neville-Neil# pmc counter present in a system.
382542e558SGeorge V. Neville-Neil#
392542e558SGeorge V. Neville-Neil# To use:
402542e558SGeorge V. Neville-Neil#
41*f9166e7cSGeorge V. Neville-Neil# pmctest.py -p ls > /dev/null
422542e558SGeorge V. Neville-Neil#
432542e558SGeorge V. Neville-Neil# This should result in ls being run with every available counter
442542e558SGeorge V. Neville-Neil# and the system should neither lock up nor panic.
45*f9166e7cSGeorge V. Neville-Neil#
46*f9166e7cSGeorge V. Neville-Neil# The default is to wait after each counter is tested.  Since the
47*f9166e7cSGeorge V. Neville-Neil# prompt would go to stdout you won't see it, just press return
48*f9166e7cSGeorge V. Neville-Neil# to continue or Ctrl-D to stop.
492542e558SGeorge V. Neville-Neil
502542e558SGeorge V. Neville-Neilimport sys
512542e558SGeorge V. Neville-Neilimport subprocess
522542e558SGeorge V. Neville-Neilfrom subprocess import PIPE
532542e558SGeorge V. Neville-Neil
542542e558SGeorge V. Neville-Neil# A list of strings that are not really counters, just
552542e558SGeorge V. Neville-Neil# name tags that are output by pmccontrol -L
562542e558SGeorge V. Neville-Neilnotcounter = ["IAF", "IAP", "TSC", "UNC", "UCF"]
572542e558SGeorge V. Neville-Neil
582542e558SGeorge V. Neville-Neildef main():
592542e558SGeorge V. Neville-Neil
60*f9166e7cSGeorge V. Neville-Neil    from optparse import OptionParser
612542e558SGeorge V. Neville-Neil
62*f9166e7cSGeorge V. Neville-Neil    parser = OptionParser()
63*f9166e7cSGeorge V. Neville-Neil    parser.add_option("-p", "--program", dest="program",
64*f9166e7cSGeorge V. Neville-Neil                      help="program to execute")
65*f9166e7cSGeorge V. Neville-Neil    parser.add_option("-w", "--wait", action="store_true", dest="wait",
66*f9166e7cSGeorge V. Neville-Neil                      default=True, help="wait after each execution")
67*f9166e7cSGeorge V. Neville-Neil
68*f9166e7cSGeorge V. Neville-Neil    (options, args) = parser.parse_args()
692542e558SGeorge V. Neville-Neil
702542e558SGeorge V. Neville-Neil    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
712542e558SGeorge V. Neville-Neil    counters = p.communicate()[0]
722542e558SGeorge V. Neville-Neil
732542e558SGeorge V. Neville-Neil    if len(counters) <= 0:
742542e558SGeorge V. Neville-Neil        print "no counters found"
752542e558SGeorge V. Neville-Neil        sys.exit()
762542e558SGeorge V. Neville-Neil
772542e558SGeorge V. Neville-Neil    for counter in counters.split():
782542e558SGeorge V. Neville-Neil        if counter in notcounter:
792542e558SGeorge V. Neville-Neil            continue
80*f9166e7cSGeorge V. Neville-Neil        p = subprocess.Popen(["pmcstat", "-p", counter, options.program],
81*f9166e7cSGeorge V. Neville-Neil                             stdout=PIPE)
822542e558SGeorge V. Neville-Neil        result = p.communicate()[0]
832542e558SGeorge V. Neville-Neil        print result
84*f9166e7cSGeorge V. Neville-Neil        if (options.wait == True):
85*f9166e7cSGeorge V. Neville-Neil            try:
86*f9166e7cSGeorge V. Neville-Neil                value = raw_input("next?")
87*f9166e7cSGeorge V. Neville-Neil            except EOFError:
88*f9166e7cSGeorge V. Neville-Neil                sys.exit()
892542e558SGeorge V. Neville-Neil
902542e558SGeorge V. Neville-Neil# The canonical way to make a python module into a script.
912542e558SGeorge V. Neville-Neil# Remove if unnecessary.
922542e558SGeorge V. Neville-Neil
932542e558SGeorge V. Neville-Neilif __name__ == "__main__":
942542e558SGeorge V. Neville-Neil    main()
95