Blame src/libpfm4/python/src/session.py

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
Packit Service a1973e
from perfmon import *
Packit Service a1973e
import os
Packit Service a1973e
import sys
Packit Service a1973e
Packit Service a1973e
# Common base class
Packit Service a1973e
class Session:
Packit Service a1973e
  def __init__(self, events):
Packit Service a1973e
    self.system = System()
Packit Service a1973e
    self.event_names = events
Packit Service a1973e
    self.events = []
Packit Service a1973e
    self.fds = []
Packit Service a1973e
    for e in events:
Packit Service a1973e
      err, encoding = pfm_get_perf_event_encoding(e, PFM_PLM0 | PFM_PLM3,
Packit Service a1973e
                                                  None, None)
Packit Service a1973e
      self.events.append(encoding)
Packit Service a1973e
Packit Service a1973e
  def __del__(self):
Packit Service a1973e
    pass
Packit Service a1973e
Packit Service a1973e
  def read(self, fd):
Packit Service a1973e
    # TODO: determine counter width
Packit Service a1973e
    return os.read(fd, 8)
Packit Service a1973e
Packit Service a1973e
class SystemWideSession(Session):
Packit Service a1973e
  def __init__(self, cpus, events):
Packit Service a1973e
    self.cpus = cpus
Packit Service a1973e
    Session.__init__(self, events)
Packit Service a1973e
Packit Service a1973e
  def __del__(self):
Packit Service a1973e
    Session.__del__(self)
Packit Service a1973e
Packit Service a1973e
  def start(self):
Packit Service a1973e
    self.cpu_fds = []
Packit Service a1973e
    for c in self.cpus:
Packit Service a1973e
      self.cpu_fds.append([])
Packit Service a1973e
      cur_cpu_fds = self.cpu_fds[-1]
Packit Service a1973e
      for e in self.events:
Packit Service a1973e
        cur_cpu_fds.append(perf_event_open(e, -1, c, -1, 0))
Packit Service a1973e
Packit Service a1973e
  def read(self, c, i):
Packit Service a1973e
    index = self.cpus.index(c)
Packit Service a1973e
    return Session.read(self, self.cpu_fds[index][i])
Packit Service a1973e
Packit Service a1973e
class PerThreadSession(Session):
Packit Service a1973e
  def __init__(self, pid, events):
Packit Service a1973e
    self.pid = pid
Packit Service a1973e
    Session.__init__(self, events)
Packit Service a1973e
Packit Service a1973e
  def __del__(self):
Packit Service a1973e
    Session.__del__(self)
Packit Service a1973e
Packit Service a1973e
  def start(self):
Packit Service a1973e
    for e in self.events:
Packit Service a1973e
      self.fds.append(perf_event_open(e, self.pid, -1, -1, 0))
Packit Service a1973e
Packit Service a1973e
  def read(self, i):
Packit Service a1973e
    return Session.read(self, self.fds[i])