98 lines · python
1# EventClass.py2# SPDX-License-Identifier: GPL-2.03#4# This is a library defining some events types classes, which could5# be used by other scripts to analyzing the perf samples.6#7# Currently there are just a few classes defined for examples,8# PerfEvent is the base class for all perf event sample, PebsEvent9# is a HW base Intel x86 PEBS event, and user could add more SW/HW10# event classes based on requirements.11from __future__ import print_function12 13import struct14 15# Event types, user could add more here16EVTYPE_GENERIC = 017EVTYPE_PEBS = 1 # Basic PEBS event18EVTYPE_PEBS_LL = 2 # PEBS event with load latency info19EVTYPE_IBS = 320 21#22# Currently we don't have good way to tell the event type, but by23# the size of raw buffer, raw PEBS event with load latency data's24# size is 176 bytes, while the pure PEBS event's size is 144 bytes.25#26def create_event(name, comm, dso, symbol, raw_buf):27 if (len(raw_buf) == 144):28 event = PebsEvent(name, comm, dso, symbol, raw_buf)29 elif (len(raw_buf) == 176):30 event = PebsNHM(name, comm, dso, symbol, raw_buf)31 else:32 event = PerfEvent(name, comm, dso, symbol, raw_buf)33 34 return event35 36class PerfEvent(object):37 event_num = 038 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_GENERIC):39 self.name = name40 self.comm = comm41 self.dso = dso42 self.symbol = symbol43 self.raw_buf = raw_buf44 self.ev_type = ev_type45 PerfEvent.event_num += 146 47 def show(self):48 print("PMU event: name=%12s, symbol=%24s, comm=%8s, dso=%12s" %49 (self.name, self.symbol, self.comm, self.dso))50 51#52# Basic Intel PEBS (Precise Event-based Sampling) event, whose raw buffer53# contains the context info when that event happened: the EFLAGS and54# linear IP info, as well as all the registers.55#56class PebsEvent(PerfEvent):57 pebs_num = 058 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS):59 tmp_buf=raw_buf[0:80]60 flags, ip, ax, bx, cx, dx, si, di, bp, sp = struct.unpack('QQQQQQQQQQ', tmp_buf)61 self.flags = flags62 self.ip = ip63 self.ax = ax64 self.bx = bx65 self.cx = cx66 self.dx = dx67 self.si = si68 self.di = di69 self.bp = bp70 self.sp = sp71 72 PerfEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)73 PebsEvent.pebs_num += 174 del tmp_buf75 76#77# Intel Nehalem and Westmere support PEBS plus Load Latency info which lie78# in the four 64 bit words write after the PEBS data:79# Status: records the IA32_PERF_GLOBAL_STATUS register value80# DLA: Data Linear Address (EIP)81# DSE: Data Source Encoding, where the latency happens, hit or miss82# in L1/L2/L3 or IO operations83# LAT: the actual latency in cycles84#85class PebsNHM(PebsEvent):86 pebs_nhm_num = 087 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS_LL):88 tmp_buf=raw_buf[144:176]89 status, dla, dse, lat = struct.unpack('QQQQ', tmp_buf)90 self.status = status91 self.dla = dla92 self.dse = dse93 self.lat = lat94 95 PebsEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)96 PebsNHM.pebs_nhm_num += 197 del tmp_buf98