Blame src/libpfm4/python/sys.py

Packit Service a1973e
#!/usr/bin/env python
Packit Service a1973e
#
Packit Service a1973e
# Copyright (c) 2008 Google, Inc.
Packit Service a1973e
# Contributed by Arun Sharma <arun.sharma@google.com>
Packit Service a1973e
# 
Packit Service a1973e
# Permission is hereby granted, free of charge, to any person obtaining a
Packit Service a1973e
# copy of this software and associated documentation files (the "Software"),
Packit Service a1973e
# to deal in the Software without restriction, including without limitation
Packit Service a1973e
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service a1973e
# and/or sell copies of the Software, and to permit persons to whom the
Packit Service a1973e
# Software is furnished to do so, subject to the following conditions:
Packit Service a1973e
# 
Packit Service a1973e
# The above copyright notice and this permission notice shall be included
Packit Service a1973e
# in all copies or substantial portions of the Software.
Packit Service a1973e
# 
Packit Service a1973e
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service a1973e
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service a1973e
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Packit Service a1973e
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit Service a1973e
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit Service a1973e
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Packit Service a1973e
# OTHER DEALINGS IN THE SOFTWARE.
Packit Service a1973e
# 
Packit Service a1973e
# System wide monitoring example. Copied from syst.c
Packit Service a1973e
#
Packit Service a1973e
# Run as: ./sys.py -c cpulist -e eventlist
Packit Service a1973e
Packit Service a1973e
import sys
Packit Service a1973e
import os
Packit Service a1973e
import optparse
Packit Service a1973e
import time 
Packit Service a1973e
import struct
Packit Service a1973e
import perfmon
Packit Service a1973e
Packit Service a1973e
if __name__ == '__main__':
Packit Service a1973e
    parser = optparse.OptionParser()
Packit Service a1973e
    parser.add_option("-e", "--events", help="Events to use",
Packit Service a1973e
		       action="store", dest="events")
Packit Service a1973e
    parser.add_option("-c", "--cpulist", help="CPUs to monitor",
Packit Service a1973e
		       action="store", dest="cpulist")
Packit Service a1973e
    parser.set_defaults(cpulist="0")
Packit Service a1973e
    parser.set_defaults(events="PERF_COUNT_HW_CPU_CYCLES")
Packit Service a1973e
    (options, args) = parser.parse_args()
Packit Service a1973e
Packit Service a1973e
    cpus = options.cpulist.split(',')
Packit Service a1973e
    cpus = [ int(c) for c in cpus ] 
Packit Service a1973e
Packit Service a1973e
    if options.events:
Packit Service a1973e
      events = options.events.split(",")
Packit Service a1973e
    else:
Packit Service a1973e
      raise "You need to specify events to monitor"
Packit Service a1973e
Packit Service a1973e
    s = perfmon.SystemWideSession(cpus, events)
Packit Service a1973e
Packit Service a1973e
    s.start()
Packit Service a1973e
    # Measuring loop
Packit Service a1973e
    while 1:
Packit Service a1973e
      time.sleep(1)
Packit Service a1973e
      # read the counts
Packit Service a1973e
      for c in cpus:
Packit Service a1973e
        for i in range(0, len(events)):
Packit Service a1973e
          count = struct.unpack("L", s.read(c, i))[0]
Packit Service a1973e
          print """CPU%d: %s\t%lu""" % (c, events[i], count)