EventClass.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # EventClass.py
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This is a library defining some events types classes, which could
  5. # 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, PebsEvent
  9. # is a HW base Intel x86 PEBS event, and user could add more SW/HW
  10. # event classes based on requirements.
  11. from __future__ import print_function
  12. import struct
  13. # Event types, user could add more here
  14. EVTYPE_GENERIC = 0
  15. EVTYPE_PEBS = 1 # Basic PEBS event
  16. EVTYPE_PEBS_LL = 2 # PEBS event with load latency info
  17. EVTYPE_IBS = 3
  18. #
  19. # Currently we don't have good way to tell the event type, but by
  20. # the size of raw buffer, raw PEBS event with load latency data's
  21. # size is 176 bytes, while the pure PEBS event's size is 144 bytes.
  22. #
  23. def create_event(name, comm, dso, symbol, raw_buf):
  24. if (len(raw_buf) == 144):
  25. event = PebsEvent(name, comm, dso, symbol, raw_buf)
  26. elif (len(raw_buf) == 176):
  27. event = PebsNHM(name, comm, dso, symbol, raw_buf)
  28. else:
  29. event = PerfEvent(name, comm, dso, symbol, raw_buf)
  30. return event
  31. class PerfEvent(object):
  32. event_num = 0
  33. def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_GENERIC):
  34. self.name = name
  35. self.comm = comm
  36. self.dso = dso
  37. self.symbol = symbol
  38. self.raw_buf = raw_buf
  39. self.ev_type = ev_type
  40. PerfEvent.event_num += 1
  41. def show(self):
  42. print("PMU event: name=%12s, symbol=%24s, comm=%8s, dso=%12s" %
  43. (self.name, self.symbol, self.comm, self.dso))
  44. #
  45. # Basic Intel PEBS (Precise Event-based Sampling) event, whose raw buffer
  46. # contains the context info when that event happened: the EFLAGS and
  47. # linear IP info, as well as all the registers.
  48. #
  49. class PebsEvent(PerfEvent):
  50. pebs_num = 0
  51. def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS):
  52. tmp_buf=raw_buf[0:80]
  53. flags, ip, ax, bx, cx, dx, si, di, bp, sp = struct.unpack('QQQQQQQQQQ', tmp_buf)
  54. self.flags = flags
  55. self.ip = ip
  56. self.ax = ax
  57. self.bx = bx
  58. self.cx = cx
  59. self.dx = dx
  60. self.si = si
  61. self.di = di
  62. self.bp = bp
  63. self.sp = sp
  64. PerfEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
  65. PebsEvent.pebs_num += 1
  66. del tmp_buf
  67. #
  68. # Intel Nehalem and Westmere support PEBS plus Load Latency info which lie
  69. # in the four 64 bit words write after the PEBS data:
  70. # Status: records the IA32_PERF_GLOBAL_STATUS register value
  71. # DLA: Data Linear Address (EIP)
  72. # DSE: Data Source Encoding, where the latency happens, hit or miss
  73. # in L1/L2/L3 or IO operations
  74. # LAT: the actual latency in cycles
  75. #
  76. class PebsNHM(PebsEvent):
  77. pebs_nhm_num = 0
  78. def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS_LL):
  79. tmp_buf=raw_buf[144:176]
  80. status, dla, dse, lat = struct.unpack('QQQQ', tmp_buf)
  81. self.status = status
  82. self.dla = dla
  83. self.dse = dse
  84. self.lat = lat
  85. PebsEvent.__init__(self, name, comm, dso, symbol, raw_buf, ev_type)
  86. PebsNHM.pebs_nhm_num += 1
  87. del tmp_buf