#!/usr/bin/env python1# SPDX-License-Identifier: BSD-3-Clause2#3# Copyright (c) 2012, Neville-Neil Consulting4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are8# met:9#10# Redistributions of source code must retain the above copyright notice,11# this list of conditions and the following disclaimer.12#13# Redistributions in binary form must reproduce the above copyright14# notice, this list of conditions and the following disclaimer in the15# documentation and/or other materials provided with the distribution.16#17# Neither the name of Neville-Neil Consulting nor the names of its18# contributors may be used to endorse or promote products derived from19# this software without specific prior written permission.20#21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32#33# Author: George V. Neville-Neil34#3536# Description: A program to run a simple program against every available37# pmc counter present in a system.38#39# To use:40#41# pmctest.py -p ls > /dev/null42#43# This should result in ls being run with every available counter44# and the system should neither lock up nor panic.45#46# The default is to wait after each counter is tested. Since the47# prompt would go to stdout you won't see it, just press return48# to continue or Ctrl-D to stop.4950import sys51import subprocess52from subprocess import PIPE5354# Use input() for Python version 355if sys.version_info[0] == 3:56raw_input = input5758# A list of strings that are not really counters, just59# name tags that are output by pmccontrol -L60notcounter = ["IAF", "IAP", "TSC", "UNC", "UCF", "UCP", "SOFT" ]6162def main():6364from optparse import OptionParser6566parser = OptionParser()67parser.add_option("-p", "--program", dest="program",68help="program to execute")69parser.add_option("-w", "--wait", action="store_true", dest="wait",70default=True, help="wait after each execution")7172(options, args) = parser.parse_args()7374if (options.program == None):75print("specify program, such as ls, with -p/--program")76sys.exit()7778p = subprocess.Popen(["pmccontrol", "-L"], stdout=PIPE)79counters = p.communicate()[0]8081if len(counters) <= 0:82print("no counters found")83sys.exit()8485for counter in counters.split():86if counter in notcounter:87continue88p = subprocess.Popen(["pmcstat", "-p", counter, options.program],89stdout=PIPE)90result = p.communicate()[0]91print(result)92if (options.wait == True):93try:94value = raw_input("next?")95except EOFError:96sys.exit()9798# The canonical way to make a python module into a script.99# Remove if unnecessary.100101if __name__ == "__main__":102main()103104105