Back to home page

OSCL-LXR

 
 

    


0001 # EventClass.py
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # This is a library defining some events types classes, which could
0005 # be used by other scripts to analyzing the perf samples.
0006 #
0007 # Currently there are just a few classes defined for examples,
0008 # PerfEvent is the base class for all perf event sample, PebsEvent
0009 # is a HW base Intel x86 PEBS event, and user could add more SW/HW
0010 # event classes based on requirements.
0011 from __future__ import print_function
0012 
0013 import struct
0014 
0015 # Event types, user could add more here
0016 EVTYPE_GENERIC  = 0
0017 EVTYPE_PEBS     = 1     # Basic PEBS event
0018 EVTYPE_PEBS_LL  = 2     # PEBS event with load latency info
0019 EVTYPE_IBS      = 3
0020 
0021 #
0022 # Currently we don't have good way to tell the event type, but by
0023 # the size of raw buffer, raw PEBS event with load latency data's
0024 # size is 176 bytes, while the pure PEBS event's size is 144 bytes.
0025 #
0026 def create_event(name, comm, dso, symbol, raw_buf):
0027         if (len(raw_buf) == 144):
0028                 event = PebsEvent(name, comm, dso, symbol, raw_buf)
0029         elif (len(raw_buf) == 176):
0030                 event = PebsNHM(name, comm, dso, symbol, raw_buf)
0031         else:
0032                 event = PerfEvent(name, comm, dso, symbol, raw_buf)
0033 
0034         return event
0035 
0036 class PerfEvent(object):
0037         event_num = 0
0038         def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_GENERIC):
0039                 self.name       = name
0040                 self.comm       = comm
0041                 self.dso        = dso
0042                 self.symbol     = symbol
0043                 self.raw_buf    = raw_buf
0044                 self.ev_type    = ev_type
0045                 PerfEvent.event_num += 1
0046 
0047         def show(self):
0048                 print("PMU event: name=%12s, symbol=%24s, comm=%8s, dso=%12s" %
0049                       (self.name, self.symbol, self.comm, self.dso))
0050 
0051 #
0052 # Basic Intel PEBS (Precise Event-based Sampling) event, whose raw buffer
0053 # contains the context info when that event happened: the EFLAGS and
0054 # linear IP info, as well as all the registers.
0055 #
0056 class PebsEvent(PerfEvent):
0057         pebs_num = 0
0058         def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS):
0059                 tmp_buf=raw_buf[0:80]
0060                 flags, ip, ax, bx, cx, dx, si, di, bp, sp = struct.unpack('QQQQQQQQQQ', tmp_buf)
0061                 self.flags = flags
0062                 self.ip    = ip
0063                 self.ax    = ax
0064                 self.bx    = bx
0065                 self.cx    = cx
0066                 self.dx    = dx
0067                 self.si    = si
0068                 self.di    = di
0069                 self.bp    = bp
0070                 self.sp    = sp
0071 
0072                 PerfEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
0073                 PebsEvent.pebs_num += 1
0074                 del tmp_buf
0075 
0076 #
0077 # Intel Nehalem and Westmere support PEBS plus Load Latency info which lie
0078 # in the four 64 bit words write after the PEBS data:
0079 #       Status: records the IA32_PERF_GLOBAL_STATUS register value
0080 #       DLA:    Data Linear Address (EIP)
0081 #       DSE:    Data Source Encoding, where the latency happens, hit or miss
0082 #               in L1/L2/L3 or IO operations
0083 #       LAT:    the actual latency in cycles
0084 #
0085 class PebsNHM(PebsEvent):
0086         pebs_nhm_num = 0
0087         def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS_LL):
0088                 tmp_buf=raw_buf[144:176]
0089                 status, dla, dse, lat = struct.unpack('QQQQ', tmp_buf)
0090                 self.status = status
0091                 self.dla = dla
0092                 self.dse = dse
0093                 self.lat = lat
0094 
0095                 PebsEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
0096                 PebsNHM.pebs_nhm_num += 1
0097                 del tmp_buf