Blame src/libpfm4/python/src/pmu.py

Packit 577717
#
Packit 577717
# Copyright (c) 2008 Google, Inc.
Packit 577717
# Contributed by Arun Sharma <arun.sharma@google.com>
Packit 577717
#
Packit 577717
# Permission is hereby granted, free of charge, to any person obtaining a
Packit 577717
# copy of this software and associated documentation files (the "Software"),
Packit 577717
# to deal in the Software without restriction, including without limitation
Packit 577717
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit 577717
# and/or sell copies of the Software, and to permit persons to whom the
Packit 577717
# Software is furnished to do so, subject to the following conditions:
Packit 577717
#
Packit 577717
# The above copyright notice and this permission notice shall be included
Packit 577717
# in all copies or substantial portions of the Software.
Packit 577717
#
Packit 577717
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 577717
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 577717
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Packit 577717
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit 577717
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit 577717
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Packit 577717
# OTHER DEALINGS IN THE SOFTWARE.
Packit 577717
#
Packit 577717
Packit 577717
import os
Packit 577717
from perfmon import *
Packit 577717
Packit 577717
def public_members(self):
Packit 577717
    s = "{ "
Packit 577717
    for k, v in self.__dict__.iteritems():
Packit 577717
      if not k[0] == '_':
Packit 577717
        s += "%s : %s, " % (k, v)
Packit 577717
    s += " }"
Packit 577717
    return s
Packit 577717
Packit 577717
class System:
Packit 577717
  # Use the os that gives us everything
Packit 577717
  os = PFM_OS_PERF_EVENT_EXT
Packit 577717
Packit 577717
  def __init__(self):
Packit 577717
    self.ncpus = os.sysconf('SC_NPROCESSORS_ONLN')
Packit 577717
    self.pmus = []
Packit 577717
    for i in range(0, PFM_PMU_MAX):
Packit 577717
      try:
Packit 577717
        pmu = PMU(i)
Packit 577717
      except:
Packit 577717
        pass
Packit 577717
      else:
Packit 577717
        self.pmus.append(pmu)
Packit 577717
Packit 577717
  def __repr__(self):
Packit 577717
    return public_members(self)
Packit 577717
Packit 577717
class Event:
Packit 577717
  def __init__(self, info):
Packit 577717
    self.info = info
Packit 577717
    self.__attrs = []
Packit 577717
Packit 577717
  def __repr__(self):
Packit 577717
    return '\n' + public_members(self)
Packit 577717
Packit 577717
  def __parse_attrs(self):
Packit 577717
    info = self.info
Packit 577717
    for index in range(0, info.nattrs):
Packit 577717
      self.__attrs.append(pfm_get_event_attr_info(info.idx, index,
Packit 577717
                                                  System.os)[1])
Packit 577717
Packit 577717
  def attrs(self):
Packit 577717
    if not self.__attrs:
Packit 577717
      self.__parse_attrs()
Packit 577717
    return self.__attrs
Packit 577717
Packit 577717
class PMU:
Packit 577717
  def __init__(self, i):
Packit 577717
    self.info = pfm_get_pmu_info(i)[1]
Packit 577717
    self.__events = []
Packit 577717
Packit 577717
  def __parse_events(self):
Packit 577717
    index = self.info.first_event
Packit 577717
    while index != -1:
Packit 577717
      self.__events.append(Event(pfm_get_event_info(index, System.os)[1]))
Packit 577717
      index = pfm_get_event_next(index)
Packit 577717
Packit 577717
  def events(self):
Packit 577717
    if not self.__events:
Packit 577717
      self.__parse_events()
Packit 577717
    return self.__events
Packit 577717
Packit 577717
  def __repr__(self):
Packit 577717
    return public_members(self)
Packit 577717
Packit 577717
if __name__ == '__main__':
Packit 577717
  from perfmon import *
Packit 577717
  s = System()
Packit 577717
  for pmu in s.pmus:
Packit 577717
    info = pmu.info
Packit 577717
    if info.flags.is_present:
Packit 577717
      print info.name, info.size, info.nevents
Packit 577717
      for e in pmu.events():
Packit 577717
        print e.info.name, e.info.code
Packit 577717
        for a in e.attrs():
Packit 577717
	  print '\t\t', a.name, a.code