Blame src/libpfm4/python/self.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
# Self monitoring example. Copied from self.c
Packit Service a1973e
Packit Service a1973e
import os
Packit Service a1973e
import optparse
Packit Service a1973e
import random
Packit Service a1973e
import errno
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.set_defaults(events="PERF_COUNT_HW_CPU_CYCLES")
Packit Service a1973e
  (options, args) = parser.parse_args()
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.PerThreadSession(int(os.getpid()), events)
Packit Service a1973e
  s.start()
Packit Service a1973e
Packit Service a1973e
  # code to be measured
Packit Service a1973e
  #
Packit Service a1973e
  # note that this is not identical to what examples/self.c does
Packit Service a1973e
  # thus counts will be different in the end
Packit Service a1973e
  for i in range(1, 1000000):
Packit Service a1973e
    random.random()
Packit Service a1973e
Packit Service a1973e
  # read the counts
Packit Service a1973e
  for i in range(0, len(events)):
Packit Service a1973e
    count = struct.unpack("L", s.read(i))[0]
Packit Service a1973e
    print """%s\t%lu""" % (events[i], count)