event_analyzing_sample.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # event_analyzing_sample.py: general event handler in python
  2. #
  3. # Current perf report is already very powerful with the annotation integrated,
  4. # and this script is not trying to be as powerful as perf report, but
  5. # providing end user/developer a flexible way to analyze the events other
  6. # than trace points.
  7. #
  8. # The 2 database related functions in this script just show how to gather
  9. # the basic information, and users can modify and write their own functions
  10. # according to their specific requirement.
  11. #
  12. # The first function "show_general_events" just does a basic grouping for all
  13. # generic events with the help of sqlite, and the 2nd one "show_pebs_ll" is
  14. # for a x86 HW PMU event: PEBS with load latency data.
  15. #
  16. import os
  17. import sys
  18. import math
  19. import struct
  20. import sqlite3
  21. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  22. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  23. from perf_trace_context import *
  24. from EventClass import *
  25. #
  26. # If the perf.data has a big number of samples, then the insert operation
  27. # will be very time consuming (about 10+ minutes for 10000 samples) if the
  28. # .db database is on disk. Move the .db file to RAM based FS to speedup
  29. # the handling, which will cut the time down to several seconds.
  30. #
  31. con = sqlite3.connect("/dev/shm/perf.db")
  32. con.isolation_level = None
  33. def trace_begin():
  34. print "In trace_begin:\n"
  35. #
  36. # Will create several tables at the start, pebs_ll is for PEBS data with
  37. # load latency info, while gen_events is for general event.
  38. #
  39. con.execute("""
  40. create table if not exists gen_events (
  41. name text,
  42. symbol text,
  43. comm text,
  44. dso text
  45. );""")
  46. con.execute("""
  47. create table if not exists pebs_ll (
  48. name text,
  49. symbol text,
  50. comm text,
  51. dso text,
  52. flags integer,
  53. ip integer,
  54. status integer,
  55. dse integer,
  56. dla integer,
  57. lat integer
  58. );""")
  59. #
  60. # Create and insert event object to a database so that user could
  61. # do more analysis with simple database commands.
  62. #
  63. def process_event(param_dict):
  64. event_attr = param_dict["attr"]
  65. sample = param_dict["sample"]
  66. raw_buf = param_dict["raw_buf"]
  67. comm = param_dict["comm"]
  68. name = param_dict["ev_name"]
  69. # Symbol and dso info are not always resolved
  70. if (param_dict.has_key("dso")):
  71. dso = param_dict["dso"]
  72. else:
  73. dso = "Unknown_dso"
  74. if (param_dict.has_key("symbol")):
  75. symbol = param_dict["symbol"]
  76. else:
  77. symbol = "Unknown_symbol"
  78. # Create the event object and insert it to the right table in database
  79. event = create_event(name, comm, dso, symbol, raw_buf)
  80. insert_db(event)
  81. def insert_db(event):
  82. if event.ev_type == EVTYPE_GENERIC:
  83. con.execute("insert into gen_events values(?, ?, ?, ?)",
  84. (event.name, event.symbol, event.comm, event.dso))
  85. elif event.ev_type == EVTYPE_PEBS_LL:
  86. event.ip &= 0x7fffffffffffffff
  87. event.dla &= 0x7fffffffffffffff
  88. con.execute("insert into pebs_ll values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  89. (event.name, event.symbol, event.comm, event.dso, event.flags,
  90. event.ip, event.status, event.dse, event.dla, event.lat))
  91. def trace_end():
  92. print "In trace_end:\n"
  93. # We show the basic info for the 2 type of event classes
  94. show_general_events()
  95. show_pebs_ll()
  96. con.close()
  97. #
  98. # As the event number may be very big, so we can't use linear way
  99. # to show the histogram in real number, but use a log2 algorithm.
  100. #
  101. def num2sym(num):
  102. # Each number will have at least one '#'
  103. snum = '#' * (int)(math.log(num, 2) + 1)
  104. return snum
  105. def show_general_events():
  106. # Check the total record number in the table
  107. count = con.execute("select count(*) from gen_events")
  108. for t in count:
  109. print "There is %d records in gen_events table" % t[0]
  110. if t[0] == 0:
  111. return
  112. print "Statistics about the general events grouped by thread/symbol/dso: \n"
  113. # Group by thread
  114. commq = con.execute("select comm, count(comm) from gen_events group by comm order by -count(comm)")
  115. print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42)
  116. for row in commq:
  117. print "%16s %8d %s" % (row[0], row[1], num2sym(row[1]))
  118. # Group by symbol
  119. print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58)
  120. symbolq = con.execute("select symbol, count(symbol) from gen_events group by symbol order by -count(symbol)")
  121. for row in symbolq:
  122. print "%32s %8d %s" % (row[0], row[1], num2sym(row[1]))
  123. # Group by dso
  124. print "\n%40s %8s %16s\n%s" % ("dso", "number", "histogram", "="*74)
  125. dsoq = con.execute("select dso, count(dso) from gen_events group by dso order by -count(dso)")
  126. for row in dsoq:
  127. print "%40s %8d %s" % (row[0], row[1], num2sym(row[1]))
  128. #
  129. # This function just shows the basic info, and we could do more with the
  130. # data in the tables, like checking the function parameters when some
  131. # big latency events happen.
  132. #
  133. def show_pebs_ll():
  134. count = con.execute("select count(*) from pebs_ll")
  135. for t in count:
  136. print "There is %d records in pebs_ll table" % t[0]
  137. if t[0] == 0:
  138. return
  139. print "Statistics about the PEBS Load Latency events grouped by thread/symbol/dse/latency: \n"
  140. # Group by thread
  141. commq = con.execute("select comm, count(comm) from pebs_ll group by comm order by -count(comm)")
  142. print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42)
  143. for row in commq:
  144. print "%16s %8d %s" % (row[0], row[1], num2sym(row[1]))
  145. # Group by symbol
  146. print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58)
  147. symbolq = con.execute("select symbol, count(symbol) from pebs_ll group by symbol order by -count(symbol)")
  148. for row in symbolq:
  149. print "%32s %8d %s" % (row[0], row[1], num2sym(row[1]))
  150. # Group by dse
  151. dseq = con.execute("select dse, count(dse) from pebs_ll group by dse order by -count(dse)")
  152. print "\n%32s %8s %16s\n%s" % ("dse", "number", "histogram", "="*58)
  153. for row in dseq:
  154. print "%32s %8d %s" % (row[0], row[1], num2sym(row[1]))
  155. # Group by latency
  156. latq = con.execute("select lat, count(lat) from pebs_ll group by lat order by lat")
  157. print "\n%32s %8s %16s\n%s" % ("latency", "number", "histogram", "="*58)
  158. for row in latq:
  159. print "%32s %8d %s" % (row[0], row[1], num2sym(row[1]))
  160. def trace_unhandled(event_name, context, event_fields_dict):
  161. print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])