1*87b6a3adSFeng Tang# event_analyzing_sample.py: general event handler in python 20076d546SFeng Tang# 3*87b6a3adSFeng Tang# Current perf report is already very powerful with the annotation integrated, 40076d546SFeng Tang# and this script is not trying to be as powerful as perf report, but 50076d546SFeng Tang# providing end user/developer a flexible way to analyze the events other 60076d546SFeng Tang# than trace points. 70076d546SFeng Tang# 80076d546SFeng Tang# The 2 database related functions in this script just show how to gather 90076d546SFeng Tang# the basic information, and users can modify and write their own functions 10*87b6a3adSFeng Tang# according to their specific requirement. 110076d546SFeng Tang# 12*87b6a3adSFeng Tang# The first function "show_general_events" just does a basic grouping for all 130076d546SFeng Tang# generic events with the help of sqlite, and the 2nd one "show_pebs_ll" is 140076d546SFeng Tang# for a x86 HW PMU event: PEBS with load latency data. 150076d546SFeng Tang# 160076d546SFeng Tang 170076d546SFeng Tangimport os 180076d546SFeng Tangimport sys 190076d546SFeng Tangimport math 200076d546SFeng Tangimport struct 210076d546SFeng Tangimport sqlite3 220076d546SFeng Tang 230076d546SFeng Tangsys.path.append(os.environ['PERF_EXEC_PATH'] + \ 240076d546SFeng Tang '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') 250076d546SFeng Tang 260076d546SFeng Tangfrom perf_trace_context import * 270076d546SFeng Tangfrom EventClass import * 280076d546SFeng Tang 290076d546SFeng Tang# 300076d546SFeng Tang# If the perf.data has a big number of samples, then the insert operation 310076d546SFeng Tang# will be very time consuming (about 10+ minutes for 10000 samples) if the 320076d546SFeng Tang# .db database is on disk. Move the .db file to RAM based FS to speedup 330076d546SFeng Tang# the handling, which will cut the time down to several seconds. 340076d546SFeng Tang# 350076d546SFeng Tangcon = sqlite3.connect("/dev/shm/perf.db") 360076d546SFeng Tangcon.isolation_level = None 370076d546SFeng Tang 380076d546SFeng Tangdef trace_begin(): 390076d546SFeng Tang print "In trace_begin:\n" 400076d546SFeng Tang 410076d546SFeng Tang # 420076d546SFeng Tang # Will create several tables at the start, pebs_ll is for PEBS data with 430076d546SFeng Tang # load latency info, while gen_events is for general event. 440076d546SFeng Tang # 450076d546SFeng Tang con.execute(""" 460076d546SFeng Tang create table if not exists gen_events ( 470076d546SFeng Tang name text, 480076d546SFeng Tang symbol text, 490076d546SFeng Tang comm text, 500076d546SFeng Tang dso text 510076d546SFeng Tang );""") 520076d546SFeng Tang con.execute(""" 530076d546SFeng Tang create table if not exists pebs_ll ( 540076d546SFeng Tang name text, 550076d546SFeng Tang symbol text, 560076d546SFeng Tang comm text, 570076d546SFeng Tang dso text, 580076d546SFeng Tang flags integer, 590076d546SFeng Tang ip integer, 600076d546SFeng Tang status integer, 610076d546SFeng Tang dse integer, 620076d546SFeng Tang dla integer, 630076d546SFeng Tang lat integer 640076d546SFeng Tang );""") 650076d546SFeng Tang 660076d546SFeng Tang# 670076d546SFeng Tang# Create and insert event object to a database so that user could 680076d546SFeng Tang# do more analysis with simple database commands. 690076d546SFeng Tang# 700076d546SFeng Tangdef process_event(param_dict): 710076d546SFeng Tang event_attr = param_dict["attr"] 720076d546SFeng Tang sample = param_dict["sample"] 730076d546SFeng Tang raw_buf = param_dict["raw_buf"] 740076d546SFeng Tang comm = param_dict["comm"] 750076d546SFeng Tang name = param_dict["ev_name"] 760076d546SFeng Tang 770076d546SFeng Tang # Symbol and dso info are not always resolved 780076d546SFeng Tang if (param_dict.has_key("dso")): 790076d546SFeng Tang dso = param_dict["dso"] 800076d546SFeng Tang else: 810076d546SFeng Tang dso = "Unknown_dso" 820076d546SFeng Tang 830076d546SFeng Tang if (param_dict.has_key("symbol")): 840076d546SFeng Tang symbol = param_dict["symbol"] 850076d546SFeng Tang else: 860076d546SFeng Tang symbol = "Unknown_symbol" 870076d546SFeng Tang 88*87b6a3adSFeng Tang # Create the event object and insert it to the right table in database 890076d546SFeng Tang event = create_event(name, comm, dso, symbol, raw_buf) 900076d546SFeng Tang insert_db(event) 910076d546SFeng Tang 920076d546SFeng Tangdef insert_db(event): 930076d546SFeng Tang if event.ev_type == EVTYPE_GENERIC: 940076d546SFeng Tang con.execute("insert into gen_events values(?, ?, ?, ?)", 950076d546SFeng Tang (event.name, event.symbol, event.comm, event.dso)) 960076d546SFeng Tang elif event.ev_type == EVTYPE_PEBS_LL: 970076d546SFeng Tang event.ip &= 0x7fffffffffffffff 980076d546SFeng Tang event.dla &= 0x7fffffffffffffff 990076d546SFeng Tang con.execute("insert into pebs_ll values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 1000076d546SFeng Tang (event.name, event.symbol, event.comm, event.dso, event.flags, 1010076d546SFeng Tang event.ip, event.status, event.dse, event.dla, event.lat)) 1020076d546SFeng Tang 1030076d546SFeng Tangdef trace_end(): 1040076d546SFeng Tang print "In trace_end:\n" 1050076d546SFeng Tang # We show the basic info for the 2 type of event classes 1060076d546SFeng Tang show_general_events() 1070076d546SFeng Tang show_pebs_ll() 1080076d546SFeng Tang con.close() 1090076d546SFeng Tang 1100076d546SFeng Tang# 1110076d546SFeng Tang# As the event number may be very big, so we can't use linear way 112*87b6a3adSFeng Tang# to show the histogram in real number, but use a log2 algorithm. 1130076d546SFeng Tang# 1140076d546SFeng Tang 1150076d546SFeng Tangdef num2sym(num): 1160076d546SFeng Tang # Each number will have at least one '#' 1170076d546SFeng Tang snum = '#' * (int)(math.log(num, 2) + 1) 1180076d546SFeng Tang return snum 1190076d546SFeng Tang 1200076d546SFeng Tangdef show_general_events(): 1210076d546SFeng Tang 1220076d546SFeng Tang # Check the total record number in the table 1230076d546SFeng Tang count = con.execute("select count(*) from gen_events") 1240076d546SFeng Tang for t in count: 1250076d546SFeng Tang print "There is %d records in gen_events table" % t[0] 1260076d546SFeng Tang if t[0] == 0: 1270076d546SFeng Tang return 1280076d546SFeng Tang 1290076d546SFeng Tang print "Statistics about the general events grouped by thread/symbol/dso: \n" 1300076d546SFeng Tang 1310076d546SFeng Tang # Group by thread 1320076d546SFeng Tang commq = con.execute("select comm, count(comm) from gen_events group by comm order by -count(comm)") 133*87b6a3adSFeng Tang print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42) 1340076d546SFeng Tang for row in commq: 1350076d546SFeng Tang print "%16s %8d %s" % (row[0], row[1], num2sym(row[1])) 1360076d546SFeng Tang 1370076d546SFeng Tang # Group by symbol 138*87b6a3adSFeng Tang print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58) 1390076d546SFeng Tang symbolq = con.execute("select symbol, count(symbol) from gen_events group by symbol order by -count(symbol)") 1400076d546SFeng Tang for row in symbolq: 1410076d546SFeng Tang print "%32s %8d %s" % (row[0], row[1], num2sym(row[1])) 1420076d546SFeng Tang 1430076d546SFeng Tang # Group by dso 144*87b6a3adSFeng Tang print "\n%40s %8s %16s\n%s" % ("dso", "number", "histogram", "="*74) 1450076d546SFeng Tang dsoq = con.execute("select dso, count(dso) from gen_events group by dso order by -count(dso)") 1460076d546SFeng Tang for row in dsoq: 1470076d546SFeng Tang print "%40s %8d %s" % (row[0], row[1], num2sym(row[1])) 1480076d546SFeng Tang 1490076d546SFeng Tang# 1500076d546SFeng Tang# This function just shows the basic info, and we could do more with the 1510076d546SFeng Tang# data in the tables, like checking the function parameters when some 1520076d546SFeng Tang# big latency events happen. 1530076d546SFeng Tang# 1540076d546SFeng Tangdef show_pebs_ll(): 1550076d546SFeng Tang 1560076d546SFeng Tang count = con.execute("select count(*) from pebs_ll") 1570076d546SFeng Tang for t in count: 1580076d546SFeng Tang print "There is %d records in pebs_ll table" % t[0] 1590076d546SFeng Tang if t[0] == 0: 1600076d546SFeng Tang return 1610076d546SFeng Tang 1620076d546SFeng Tang print "Statistics about the PEBS Load Latency events grouped by thread/symbol/dse/latency: \n" 1630076d546SFeng Tang 1640076d546SFeng Tang # Group by thread 1650076d546SFeng Tang commq = con.execute("select comm, count(comm) from pebs_ll group by comm order by -count(comm)") 166*87b6a3adSFeng Tang print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42) 1670076d546SFeng Tang for row in commq: 1680076d546SFeng Tang print "%16s %8d %s" % (row[0], row[1], num2sym(row[1])) 1690076d546SFeng Tang 1700076d546SFeng Tang # Group by symbol 171*87b6a3adSFeng Tang print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58) 1720076d546SFeng Tang symbolq = con.execute("select symbol, count(symbol) from pebs_ll group by symbol order by -count(symbol)") 1730076d546SFeng Tang for row in symbolq: 1740076d546SFeng Tang print "%32s %8d %s" % (row[0], row[1], num2sym(row[1])) 1750076d546SFeng Tang 1760076d546SFeng Tang # Group by dse 1770076d546SFeng Tang dseq = con.execute("select dse, count(dse) from pebs_ll group by dse order by -count(dse)") 178*87b6a3adSFeng Tang print "\n%32s %8s %16s\n%s" % ("dse", "number", "histogram", "="*58) 1790076d546SFeng Tang for row in dseq: 1800076d546SFeng Tang print "%32s %8d %s" % (row[0], row[1], num2sym(row[1])) 1810076d546SFeng Tang 1820076d546SFeng Tang # Group by latency 1830076d546SFeng Tang latq = con.execute("select lat, count(lat) from pebs_ll group by lat order by lat") 184*87b6a3adSFeng Tang print "\n%32s %8s %16s\n%s" % ("latency", "number", "histogram", "="*58) 1850076d546SFeng Tang for row in latq: 1860076d546SFeng Tang print "%32s %8d %s" % (row[0], row[1], num2sym(row[1])) 1870076d546SFeng Tang 1880076d546SFeng Tangdef trace_unhandled(event_name, context, event_fields_dict): 1890076d546SFeng Tang print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]) 190