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