2 # SPDX-License-Identifier: GPL-2.0
4 # This is a library defining some events types classes, which could
5 # be used by other scripts to analyzing the perf samples.
7 # Currently there are just a few classes defined for examples,
8 # PerfEvent is the base class for all perf event sample, PebsEvent
9 # is a HW base Intel x86 PEBS event, and user could add more SW/HW
10 # event classes based on requirements.
14 # Event types, user could add more here
16 EVTYPE_PEBS = 1 # Basic PEBS event
17 EVTYPE_PEBS_LL = 2 # PEBS event with load latency info
21 # Currently we don't have good way to tell the event type, but by
22 # the size of raw buffer, raw PEBS event with load latency data's
23 # size is 176 bytes, while the pure PEBS event's size is 144 bytes.
25 def create_event(name, comm, dso, symbol, raw_buf):
26 if (len(raw_buf) == 144):
27 event = PebsEvent(name, comm, dso, symbol, raw_buf)
28 elif (len(raw_buf) == 176):
29 event = PebsNHM(name, comm, dso, symbol, raw_buf)
31 event = PerfEvent(name, comm, dso, symbol, raw_buf)
35 class PerfEvent(object):
37 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_GENERIC):
42 self.raw_buf = raw_buf
43 self.ev_type = ev_type
44 PerfEvent.event_num += 1
47 print "PMU event: name=%12s, symbol=%24s, comm=%8s, dso=%12s" % (self.name, self.symbol, self.comm, self.dso)
50 # Basic Intel PEBS (Precise Event-based Sampling) event, whose raw buffer
51 # contains the context info when that event happened: the EFLAGS and
52 # linear IP info, as well as all the registers.
54 class PebsEvent(PerfEvent):
56 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS):
58 flags, ip, ax, bx, cx, dx, si, di, bp, sp = struct.unpack('QQQQQQQQQQ', tmp_buf)
70 PerfEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
71 PebsEvent.pebs_num += 1
75 # Intel Nehalem and Westmere support PEBS plus Load Latency info which lie
76 # in the four 64 bit words write after the PEBS data:
77 # Status: records the IA32_PERF_GLOBAL_STATUS register value
78 # DLA: Data Linear Address (EIP)
79 # DSE: Data Source Encoding, where the latency happens, hit or miss
80 # in L1/L2/L3 or IO operations
81 # LAT: the actual latency in cycles
83 class PebsNHM(PebsEvent):
85 def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS_LL):
86 tmp_buf=raw_buf[144:176]
87 status, dla, dse, lat = struct.unpack('QQQQ', tmp_buf)
93 PebsEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
94 PebsNHM.pebs_nhm_num += 1