xref: /freebsd/tools/test/hwpmc/pmctest.py (revision 345ddfcfe96ec7cc2bbe8b0217eea546fdab200a)
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#
41f9166e7cSGeorge 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.
45f9166e7cSGeorge V. Neville-Neil#
46f9166e7cSGeorge V. Neville-Neil# The default is to wait after each counter is tested.  Since the
47f9166e7cSGeorge V. Neville-Neil# prompt would go to stdout you won't see it, just press return
48f9166e7cSGeorge 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
56*345ddfcfSFabien Thomasnotcounter = ["IAF", "IAP", "TSC", "UNC", "UCF", "UCP", "SOFT" ]
572542e558SGeorge V. Neville-Neil
582542e558SGeorge V. Neville-Neildef main():
592542e558SGeorge V. Neville-Neil
60f9166e7cSGeorge V. Neville-Neil    from optparse import OptionParser
612542e558SGeorge V. Neville-Neil
62f9166e7cSGeorge V. Neville-Neil    parser = OptionParser()
63f9166e7cSGeorge V. Neville-Neil    parser.add_option("-p", "--program", dest="program",
64f9166e7cSGeorge V. Neville-Neil                      help="program to execute")
65f9166e7cSGeorge V. Neville-Neil    parser.add_option("-w", "--wait", action="store_true", dest="wait",
66f9166e7cSGeorge V. Neville-Neil                      default=True, help="wait after each execution")
67f9166e7cSGeorge V. Neville-Neil
68f9166e7cSGeorge V. Neville-Neil    (options, args) = parser.parse_args()
692542e558SGeorge V. Neville-Neil
70d2c10b2aSGeorge V. Neville-Neil    if (options.program == None):
71d2c10b2aSGeorge V. Neville-Neil        print "specify program, such as ls, with -p/--program"
72d2c10b2aSGeorge V. Neville-Neil        sys.exit()
73d2c10b2aSGeorge V. Neville-Neil
742542e558SGeorge V. Neville-Neil    p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)
752542e558SGeorge V. Neville-Neil    counters = p.communicate()[0]
762542e558SGeorge V. Neville-Neil
772542e558SGeorge V. Neville-Neil    if len(counters) <= 0:
782542e558SGeorge V. Neville-Neil        print "no counters found"
792542e558SGeorge V. Neville-Neil        sys.exit()
802542e558SGeorge V. Neville-Neil
812542e558SGeorge V. Neville-Neil    for counter in counters.split():
822542e558SGeorge V. Neville-Neil        if counter in notcounter:
832542e558SGeorge V. Neville-Neil            continue
84f9166e7cSGeorge V. Neville-Neil        p = subprocess.Popen(["pmcstat", "-p", counter, options.program],
85f9166e7cSGeorge V. Neville-Neil                             stdout=PIPE)
862542e558SGeorge V. Neville-Neil        result = p.communicate()[0]
872542e558SGeorge V. Neville-Neil        print result
88f9166e7cSGeorge V. Neville-Neil        if (options.wait == True):
89f9166e7cSGeorge V. Neville-Neil            try:
90f9166e7cSGeorge V. Neville-Neil                value = raw_input("next?")
91f9166e7cSGeorge V. Neville-Neil            except EOFError:
92f9166e7cSGeorge V. Neville-Neil                sys.exit()
932542e558SGeorge V. Neville-Neil
942542e558SGeorge V. Neville-Neil# The canonical way to make a python module into a script.
952542e558SGeorge V. Neville-Neil# Remove if unnecessary.
962542e558SGeorge V. Neville-Neil
972542e558SGeorge V. Neville-Neilif __name__ == "__main__":
982542e558SGeorge V. Neville-Neil    main()
99