Loading...
1# export-to-postgresql.py: export perf data to a postgresql database
2# Copyright (c) 2014, Intel Corporation.
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms and conditions of the GNU General Public License,
6# version 2, as published by the Free Software Foundation.
7#
8# This program is distributed in the hope it will be useful, but WITHOUT
9# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11# more details.
12
13import os
14import sys
15import struct
16import datetime
17
18# To use this script you will need to have installed package python-pyside which
19# provides LGPL-licensed Python bindings for Qt. You will also need the package
20# libqt4-sql-psql for Qt postgresql support.
21#
22# The script assumes postgresql is running on the local machine and that the
23# user has postgresql permissions to create databases. Examples of installing
24# postgresql and adding such a user are:
25#
26# fedora:
27#
28# $ sudo yum install postgresql postgresql-server python-pyside qt-postgresql
29# $ sudo su - postgres -c initdb
30# $ sudo service postgresql start
31# $ sudo su - postgres
32# $ createuser <your user id here>
33# Shall the new role be a superuser? (y/n) y
34#
35# ubuntu:
36#
37# $ sudo apt-get install postgresql python-pyside.qtsql libqt4-sql-psql
38# $ sudo su - postgres
39# $ createuser -s <your user id here>
40#
41# An example of using this script with Intel PT:
42#
43# $ perf record -e intel_pt//u ls
44# $ perf script -s ~/libexec/perf-core/scripts/python/export-to-postgresql.py pt_example branches calls
45# 2015-05-29 12:49:23.464364 Creating database...
46# 2015-05-29 12:49:26.281717 Writing to intermediate files...
47# 2015-05-29 12:49:27.190383 Copying to database...
48# 2015-05-29 12:49:28.140451 Removing intermediate files...
49# 2015-05-29 12:49:28.147451 Adding primary keys
50# 2015-05-29 12:49:28.655683 Adding foreign keys
51# 2015-05-29 12:49:29.365350 Done
52#
53# To browse the database, psql can be used e.g.
54#
55# $ psql pt_example
56# pt_example=# select * from samples_view where id < 100;
57# pt_example=# \d+
58# pt_example=# \d+ samples_view
59# pt_example=# \q
60#
61# An example of using the database is provided by the script
62# call-graph-from-postgresql.py. Refer to that script for details.
63#
64# Tables:
65#
66# The tables largely correspond to perf tools' data structures. They are largely self-explanatory.
67#
68# samples
69#
70# 'samples' is the main table. It represents what instruction was executing at a point in time
71# when something (a selected event) happened. The memory address is the instruction pointer or 'ip'.
72#
73# calls
74#
75# 'calls' represents function calls and is related to 'samples' by 'call_id' and 'return_id'.
76# 'calls' is only created when the 'calls' option to this script is specified.
77#
78# call_paths
79#
80# 'call_paths' represents all the call stacks. Each 'call' has an associated record in 'call_paths'.
81# 'calls_paths' is only created when the 'calls' option to this script is specified.
82#
83# branch_types
84#
85# 'branch_types' provides descriptions for each type of branch.
86#
87# comm_threads
88#
89# 'comm_threads' shows how 'comms' relates to 'threads'.
90#
91# comms
92#
93# 'comms' contains a record for each 'comm' - the name given to the executable that is running.
94#
95# dsos
96#
97# 'dsos' contains a record for each executable file or library.
98#
99# machines
100#
101# 'machines' can be used to distinguish virtual machines if virtualization is supported.
102#
103# selected_events
104#
105# 'selected_events' contains a record for each kind of event that has been sampled.
106#
107# symbols
108#
109# 'symbols' contains a record for each symbol. Only symbols that have samples are present.
110#
111# threads
112#
113# 'threads' contains a record for each thread.
114#
115# Views:
116#
117# Most of the tables have views for more friendly display. The views are:
118#
119# calls_view
120# call_paths_view
121# comm_threads_view
122# dsos_view
123# machines_view
124# samples_view
125# symbols_view
126# threads_view
127#
128# More examples of browsing the database with psql:
129# Note that some of the examples are not the most optimal SQL query.
130# Note that call information is only available if the script's 'calls' option has been used.
131#
132# Top 10 function calls (not aggregated by symbol):
133#
134# SELECT * FROM calls_view ORDER BY elapsed_time DESC LIMIT 10;
135#
136# Top 10 function calls (aggregated by symbol):
137#
138# SELECT symbol_id,(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,
139# SUM(elapsed_time) AS tot_elapsed_time,SUM(branch_count) AS tot_branch_count
140# FROM calls_view GROUP BY symbol_id ORDER BY tot_elapsed_time DESC LIMIT 10;
141#
142# Note that the branch count gives a rough estimation of cpu usage, so functions
143# that took a long time but have a relatively low branch count must have spent time
144# waiting.
145#
146# Find symbols by pattern matching on part of the name (e.g. names containing 'alloc'):
147#
148# SELECT * FROM symbols_view WHERE name LIKE '%alloc%';
149#
150# Top 10 function calls for a specific symbol (e.g. whose symbol_id is 187):
151#
152# SELECT * FROM calls_view WHERE symbol_id = 187 ORDER BY elapsed_time DESC LIMIT 10;
153#
154# Show function calls made by function in the same context (i.e. same call path) (e.g. one with call_path_id 254):
155#
156# SELECT * FROM calls_view WHERE parent_call_path_id = 254;
157#
158# Show branches made during a function call (e.g. where call_id is 29357 and return_id is 29370 and tid is 29670)
159#
160# SELECT * FROM samples_view WHERE id >= 29357 AND id <= 29370 AND tid = 29670 AND event LIKE 'branches%';
161#
162# Show transactions:
163#
164# SELECT * FROM samples_view WHERE event = 'transactions';
165#
166# Note transaction start has 'in_tx' true whereas, transaction end has 'in_tx' false.
167# Transaction aborts have branch_type_name 'transaction abort'
168#
169# Show transaction aborts:
170#
171# SELECT * FROM samples_view WHERE event = 'transactions' AND branch_type_name = 'transaction abort';
172#
173# To print a call stack requires walking the call_paths table. For example this python script:
174# #!/usr/bin/python2
175#
176# import sys
177# from PySide.QtSql import *
178#
179# if __name__ == '__main__':
180# if (len(sys.argv) < 3):
181# print >> sys.stderr, "Usage is: printcallstack.py <database name> <call_path_id>"
182# raise Exception("Too few arguments")
183# dbname = sys.argv[1]
184# call_path_id = sys.argv[2]
185# db = QSqlDatabase.addDatabase('QPSQL')
186# db.setDatabaseName(dbname)
187# if not db.open():
188# raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text())
189# query = QSqlQuery(db)
190# print " id ip symbol_id symbol dso_id dso_short_name"
191# while call_path_id != 0 and call_path_id != 1:
192# ret = query.exec_('SELECT * FROM call_paths_view WHERE id = ' + str(call_path_id))
193# if not ret:
194# raise Exception("Query failed: " + query.lastError().text())
195# if not query.next():
196# raise Exception("Query failed")
197# print "{0:>6} {1:>10} {2:>9} {3:<30} {4:>6} {5:<30}".format(query.value(0), query.value(1), query.value(2), query.value(3), query.value(4), query.value(5))
198# call_path_id = query.value(6)
199
200from PySide.QtSql import *
201
202# Need to access PostgreSQL C library directly to use COPY FROM STDIN
203from ctypes import *
204libpq = CDLL("libpq.so.5")
205PQconnectdb = libpq.PQconnectdb
206PQconnectdb.restype = c_void_p
207PQfinish = libpq.PQfinish
208PQstatus = libpq.PQstatus
209PQexec = libpq.PQexec
210PQexec.restype = c_void_p
211PQresultStatus = libpq.PQresultStatus
212PQputCopyData = libpq.PQputCopyData
213PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
214PQputCopyEnd = libpq.PQputCopyEnd
215PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
216
217sys.path.append(os.environ['PERF_EXEC_PATH'] + \
218 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
219
220# These perf imports are not used at present
221#from perf_trace_context import *
222#from Core import *
223
224perf_db_export_mode = True
225perf_db_export_calls = False
226perf_db_export_callchains = False
227
228
229def usage():
230 print >> sys.stderr, "Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>] [<callchains>]"
231 print >> sys.stderr, "where: columns 'all' or 'branches'"
232 print >> sys.stderr, " calls 'calls' => create calls and call_paths table"
233 print >> sys.stderr, " callchains 'callchains' => create call_paths table"
234 raise Exception("Too few arguments")
235
236if (len(sys.argv) < 2):
237 usage()
238
239dbname = sys.argv[1]
240
241if (len(sys.argv) >= 3):
242 columns = sys.argv[2]
243else:
244 columns = "all"
245
246if columns not in ("all", "branches"):
247 usage()
248
249branches = (columns == "branches")
250
251for i in range(3,len(sys.argv)):
252 if (sys.argv[i] == "calls"):
253 perf_db_export_calls = True
254 elif (sys.argv[i] == "callchains"):
255 perf_db_export_callchains = True
256 else:
257 usage()
258
259output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
260os.mkdir(output_dir_name)
261
262def do_query(q, s):
263 if (q.exec_(s)):
264 return
265 raise Exception("Query failed: " + q.lastError().text())
266
267print datetime.datetime.today(), "Creating database..."
268
269db = QSqlDatabase.addDatabase('QPSQL')
270query = QSqlQuery(db)
271db.setDatabaseName('postgres')
272db.open()
273try:
274 do_query(query, 'CREATE DATABASE ' + dbname)
275except:
276 os.rmdir(output_dir_name)
277 raise
278query.finish()
279query.clear()
280db.close()
281
282db.setDatabaseName(dbname)
283db.open()
284
285query = QSqlQuery(db)
286do_query(query, 'SET client_min_messages TO WARNING')
287
288do_query(query, 'CREATE TABLE selected_events ('
289 'id bigint NOT NULL,'
290 'name varchar(80))')
291do_query(query, 'CREATE TABLE machines ('
292 'id bigint NOT NULL,'
293 'pid integer,'
294 'root_dir varchar(4096))')
295do_query(query, 'CREATE TABLE threads ('
296 'id bigint NOT NULL,'
297 'machine_id bigint,'
298 'process_id bigint,'
299 'pid integer,'
300 'tid integer)')
301do_query(query, 'CREATE TABLE comms ('
302 'id bigint NOT NULL,'
303 'comm varchar(16))')
304do_query(query, 'CREATE TABLE comm_threads ('
305 'id bigint NOT NULL,'
306 'comm_id bigint,'
307 'thread_id bigint)')
308do_query(query, 'CREATE TABLE dsos ('
309 'id bigint NOT NULL,'
310 'machine_id bigint,'
311 'short_name varchar(256),'
312 'long_name varchar(4096),'
313 'build_id varchar(64))')
314do_query(query, 'CREATE TABLE symbols ('
315 'id bigint NOT NULL,'
316 'dso_id bigint,'
317 'sym_start bigint,'
318 'sym_end bigint,'
319 'binding integer,'
320 'name varchar(2048))')
321do_query(query, 'CREATE TABLE branch_types ('
322 'id integer NOT NULL,'
323 'name varchar(80))')
324
325if branches:
326 do_query(query, 'CREATE TABLE samples ('
327 'id bigint NOT NULL,'
328 'evsel_id bigint,'
329 'machine_id bigint,'
330 'thread_id bigint,'
331 'comm_id bigint,'
332 'dso_id bigint,'
333 'symbol_id bigint,'
334 'sym_offset bigint,'
335 'ip bigint,'
336 'time bigint,'
337 'cpu integer,'
338 'to_dso_id bigint,'
339 'to_symbol_id bigint,'
340 'to_sym_offset bigint,'
341 'to_ip bigint,'
342 'branch_type integer,'
343 'in_tx boolean)')
344else:
345 do_query(query, 'CREATE TABLE samples ('
346 'id bigint NOT NULL,'
347 'evsel_id bigint,'
348 'machine_id bigint,'
349 'thread_id bigint,'
350 'comm_id bigint,'
351 'dso_id bigint,'
352 'symbol_id bigint,'
353 'sym_offset bigint,'
354 'ip bigint,'
355 'time bigint,'
356 'cpu integer,'
357 'to_dso_id bigint,'
358 'to_symbol_id bigint,'
359 'to_sym_offset bigint,'
360 'to_ip bigint,'
361 'period bigint,'
362 'weight bigint,'
363 'transaction bigint,'
364 'data_src bigint,'
365 'branch_type integer,'
366 'in_tx boolean,'
367 'call_path_id bigint)')
368
369if perf_db_export_calls or perf_db_export_callchains:
370 do_query(query, 'CREATE TABLE call_paths ('
371 'id bigint NOT NULL,'
372 'parent_id bigint,'
373 'symbol_id bigint,'
374 'ip bigint)')
375if perf_db_export_calls:
376 do_query(query, 'CREATE TABLE calls ('
377 'id bigint NOT NULL,'
378 'thread_id bigint,'
379 'comm_id bigint,'
380 'call_path_id bigint,'
381 'call_time bigint,'
382 'return_time bigint,'
383 'branch_count bigint,'
384 'call_id bigint,'
385 'return_id bigint,'
386 'parent_call_path_id bigint,'
387 'flags integer)')
388
389do_query(query, 'CREATE VIEW machines_view AS '
390 'SELECT '
391 'id,'
392 'pid,'
393 'root_dir,'
394 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
395 ' FROM machines')
396
397do_query(query, 'CREATE VIEW dsos_view AS '
398 'SELECT '
399 'id,'
400 'machine_id,'
401 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
402 'short_name,'
403 'long_name,'
404 'build_id'
405 ' FROM dsos')
406
407do_query(query, 'CREATE VIEW symbols_view AS '
408 'SELECT '
409 'id,'
410 'name,'
411 '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
412 'dso_id,'
413 'sym_start,'
414 'sym_end,'
415 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
416 ' FROM symbols')
417
418do_query(query, 'CREATE VIEW threads_view AS '
419 'SELECT '
420 'id,'
421 'machine_id,'
422 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
423 'process_id,'
424 'pid,'
425 'tid'
426 ' FROM threads')
427
428do_query(query, 'CREATE VIEW comm_threads_view AS '
429 'SELECT '
430 'comm_id,'
431 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
432 'thread_id,'
433 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
434 '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
435 ' FROM comm_threads')
436
437if perf_db_export_calls or perf_db_export_callchains:
438 do_query(query, 'CREATE VIEW call_paths_view AS '
439 'SELECT '
440 'c.id,'
441 'to_hex(c.ip) AS ip,'
442 'c.symbol_id,'
443 '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
444 '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
445 '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
446 'c.parent_id,'
447 'to_hex(p.ip) AS parent_ip,'
448 'p.symbol_id AS parent_symbol_id,'
449 '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
450 '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
451 '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
452 ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
453if perf_db_export_calls:
454 do_query(query, 'CREATE VIEW calls_view AS '
455 'SELECT '
456 'calls.id,'
457 'thread_id,'
458 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
459 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
460 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
461 'call_path_id,'
462 'to_hex(ip) AS ip,'
463 'symbol_id,'
464 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
465 'call_time,'
466 'return_time,'
467 'return_time - call_time AS elapsed_time,'
468 'branch_count,'
469 'call_id,'
470 'return_id,'
471 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,'
472 'parent_call_path_id'
473 ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
474
475do_query(query, 'CREATE VIEW samples_view AS '
476 'SELECT '
477 'id,'
478 'time,'
479 'cpu,'
480 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
481 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
482 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
483 '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
484 'to_hex(ip) AS ip_hex,'
485 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
486 'sym_offset,'
487 '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
488 'to_hex(to_ip) AS to_ip_hex,'
489 '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
490 'to_sym_offset,'
491 '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
492 '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
493 'in_tx'
494 ' FROM samples')
495
496
497file_header = struct.pack("!11sii", "PGCOPY\n\377\r\n\0", 0, 0)
498file_trailer = "\377\377"
499
500def open_output_file(file_name):
501 path_name = output_dir_name + "/" + file_name
502 file = open(path_name, "w+")
503 file.write(file_header)
504 return file
505
506def close_output_file(file):
507 file.write(file_trailer)
508 file.close()
509
510def copy_output_file_direct(file, table_name):
511 close_output_file(file)
512 sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')"
513 do_query(query, sql)
514
515# Use COPY FROM STDIN because security may prevent postgres from accessing the files directly
516def copy_output_file(file, table_name):
517 conn = PQconnectdb("dbname = " + dbname)
518 if (PQstatus(conn)):
519 raise Exception("COPY FROM STDIN PQconnectdb failed")
520 file.write(file_trailer)
521 file.seek(0)
522 sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')"
523 res = PQexec(conn, sql)
524 if (PQresultStatus(res) != 4):
525 raise Exception("COPY FROM STDIN PQexec failed")
526 data = file.read(65536)
527 while (len(data)):
528 ret = PQputCopyData(conn, data, len(data))
529 if (ret != 1):
530 raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret))
531 data = file.read(65536)
532 ret = PQputCopyEnd(conn, None)
533 if (ret != 1):
534 raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret))
535 PQfinish(conn)
536
537def remove_output_file(file):
538 name = file.name
539 file.close()
540 os.unlink(name)
541
542evsel_file = open_output_file("evsel_table.bin")
543machine_file = open_output_file("machine_table.bin")
544thread_file = open_output_file("thread_table.bin")
545comm_file = open_output_file("comm_table.bin")
546comm_thread_file = open_output_file("comm_thread_table.bin")
547dso_file = open_output_file("dso_table.bin")
548symbol_file = open_output_file("symbol_table.bin")
549branch_type_file = open_output_file("branch_type_table.bin")
550sample_file = open_output_file("sample_table.bin")
551if perf_db_export_calls or perf_db_export_callchains:
552 call_path_file = open_output_file("call_path_table.bin")
553if perf_db_export_calls:
554 call_file = open_output_file("call_table.bin")
555
556def trace_begin():
557 print datetime.datetime.today(), "Writing to intermediate files..."
558 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
559 evsel_table(0, "unknown")
560 machine_table(0, 0, "unknown")
561 thread_table(0, 0, 0, -1, -1)
562 comm_table(0, "unknown")
563 dso_table(0, 0, "unknown", "unknown", "")
564 symbol_table(0, 0, 0, 0, 0, "unknown")
565 sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
566 if perf_db_export_calls or perf_db_export_callchains:
567 call_path_table(0, 0, 0, 0)
568
569unhandled_count = 0
570
571def trace_end():
572 print datetime.datetime.today(), "Copying to database..."
573 copy_output_file(evsel_file, "selected_events")
574 copy_output_file(machine_file, "machines")
575 copy_output_file(thread_file, "threads")
576 copy_output_file(comm_file, "comms")
577 copy_output_file(comm_thread_file, "comm_threads")
578 copy_output_file(dso_file, "dsos")
579 copy_output_file(symbol_file, "symbols")
580 copy_output_file(branch_type_file, "branch_types")
581 copy_output_file(sample_file, "samples")
582 if perf_db_export_calls or perf_db_export_callchains:
583 copy_output_file(call_path_file, "call_paths")
584 if perf_db_export_calls:
585 copy_output_file(call_file, "calls")
586
587 print datetime.datetime.today(), "Removing intermediate files..."
588 remove_output_file(evsel_file)
589 remove_output_file(machine_file)
590 remove_output_file(thread_file)
591 remove_output_file(comm_file)
592 remove_output_file(comm_thread_file)
593 remove_output_file(dso_file)
594 remove_output_file(symbol_file)
595 remove_output_file(branch_type_file)
596 remove_output_file(sample_file)
597 if perf_db_export_calls or perf_db_export_callchains:
598 remove_output_file(call_path_file)
599 if perf_db_export_calls:
600 remove_output_file(call_file)
601 os.rmdir(output_dir_name)
602 print datetime.datetime.today(), "Adding primary keys"
603 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
604 do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
605 do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
606 do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)')
607 do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)')
608 do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)')
609 do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)')
610 do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)')
611 do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)')
612 if perf_db_export_calls or perf_db_export_callchains:
613 do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)')
614 if perf_db_export_calls:
615 do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
616
617 print datetime.datetime.today(), "Adding foreign keys"
618 do_query(query, 'ALTER TABLE threads '
619 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
620 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
621 do_query(query, 'ALTER TABLE comm_threads '
622 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
623 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)')
624 do_query(query, 'ALTER TABLE dsos '
625 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)')
626 do_query(query, 'ALTER TABLE symbols '
627 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)')
628 do_query(query, 'ALTER TABLE samples '
629 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),'
630 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
631 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
632 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
633 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),'
634 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),'
635 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),'
636 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)')
637 if perf_db_export_calls or perf_db_export_callchains:
638 do_query(query, 'ALTER TABLE call_paths '
639 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),'
640 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)')
641 if perf_db_export_calls:
642 do_query(query, 'ALTER TABLE calls '
643 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
644 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
645 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),'
646 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),'
647 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),'
648 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)')
649 do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
650
651 if (unhandled_count):
652 print datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events"
653 print datetime.datetime.today(), "Done"
654
655def trace_unhandled(event_name, context, event_fields_dict):
656 global unhandled_count
657 unhandled_count += 1
658
659def sched__sched_switch(*x):
660 pass
661
662def evsel_table(evsel_id, evsel_name, *x):
663 n = len(evsel_name)
664 fmt = "!hiqi" + str(n) + "s"
665 value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name)
666 evsel_file.write(value)
667
668def machine_table(machine_id, pid, root_dir, *x):
669 n = len(root_dir)
670 fmt = "!hiqiii" + str(n) + "s"
671 value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir)
672 machine_file.write(value)
673
674def thread_table(thread_id, machine_id, process_id, pid, tid, *x):
675 value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid)
676 thread_file.write(value)
677
678def comm_table(comm_id, comm_str, *x):
679 n = len(comm_str)
680 fmt = "!hiqi" + str(n) + "s"
681 value = struct.pack(fmt, 2, 8, comm_id, n, comm_str)
682 comm_file.write(value)
683
684def comm_thread_table(comm_thread_id, comm_id, thread_id, *x):
685 fmt = "!hiqiqiq"
686 value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id)
687 comm_thread_file.write(value)
688
689def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x):
690 n1 = len(short_name)
691 n2 = len(long_name)
692 n3 = len(build_id)
693 fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s"
694 value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id)
695 dso_file.write(value)
696
697def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x):
698 n = len(symbol_name)
699 fmt = "!hiqiqiqiqiii" + str(n) + "s"
700 value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name)
701 symbol_file.write(value)
702
703def branch_type_table(branch_type, name, *x):
704 n = len(name)
705 fmt = "!hiii" + str(n) + "s"
706 value = struct.pack(fmt, 2, 4, branch_type, n, name)
707 branch_type_file.write(value)
708
709def sample_table(sample_id, evsel_id, machine_id, thread_id, comm_id, dso_id, symbol_id, sym_offset, ip, time, cpu, to_dso_id, to_symbol_id, to_sym_offset, to_ip, period, weight, transaction, data_src, branch_type, in_tx, call_path_id, *x):
710 if branches:
711 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiiiBiq", 18, 8, sample_id, 8, evsel_id, 8, machine_id, 8, thread_id, 8, comm_id, 8, dso_id, 8, symbol_id, 8, sym_offset, 8, ip, 8, time, 4, cpu, 8, to_dso_id, 8, to_symbol_id, 8, to_sym_offset, 8, to_ip, 4, branch_type, 1, in_tx, 8, call_path_id)
712 else:
713 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiqiqiqiqiiiBiq", 22, 8, sample_id, 8, evsel_id, 8, machine_id, 8, thread_id, 8, comm_id, 8, dso_id, 8, symbol_id, 8, sym_offset, 8, ip, 8, time, 4, cpu, 8, to_dso_id, 8, to_symbol_id, 8, to_sym_offset, 8, to_ip, 8, period, 8, weight, 8, transaction, 8, data_src, 4, branch_type, 1, in_tx, 8, call_path_id)
714 sample_file.write(value)
715
716def call_path_table(cp_id, parent_id, symbol_id, ip, *x):
717 fmt = "!hiqiqiqiq"
718 value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip)
719 call_path_file.write(value)
720
721def call_return_table(cr_id, thread_id, comm_id, call_path_id, call_time, return_time, branch_count, call_id, return_id, parent_call_path_id, flags, *x):
722 fmt = "!hiqiqiqiqiqiqiqiqiqiqii"
723 value = struct.pack(fmt, 11, 8, cr_id, 8, thread_id, 8, comm_id, 8, call_path_id, 8, call_time, 8, return_time, 8, branch_count, 8, call_id, 8, return_id, 8, parent_call_path_id, 4, flags)
724 call_file.write(value)
1# export-to-postgresql.py: export perf data to a postgresql database
2# Copyright (c) 2014, Intel Corporation.
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms and conditions of the GNU General Public License,
6# version 2, as published by the Free Software Foundation.
7#
8# This program is distributed in the hope it will be useful, but WITHOUT
9# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11# more details.
12
13from __future__ import print_function
14
15import os
16import sys
17import struct
18import datetime
19
20# To use this script you will need to have installed package python-pyside which
21# provides LGPL-licensed Python bindings for Qt. You will also need the package
22# libqt4-sql-psql for Qt postgresql support.
23#
24# The script assumes postgresql is running on the local machine and that the
25# user has postgresql permissions to create databases. Examples of installing
26# postgresql and adding such a user are:
27#
28# fedora:
29#
30# $ sudo yum install postgresql postgresql-server qt-postgresql
31# $ sudo su - postgres -c initdb
32# $ sudo service postgresql start
33# $ sudo su - postgres
34# $ createuser -s <your user id here> # Older versions may not support -s, in which case answer the prompt below:
35# Shall the new role be a superuser? (y/n) y
36# $ sudo yum install python-pyside
37#
38# Alternately, to use Python3 and/or pyside 2, one of the following:
39# $ sudo yum install python3-pyside
40# $ pip install --user PySide2
41# $ pip3 install --user PySide2
42#
43# ubuntu:
44#
45# $ sudo apt-get install postgresql
46# $ sudo su - postgres
47# $ createuser -s <your user id here>
48# $ sudo apt-get install python-pyside.qtsql libqt4-sql-psql
49#
50# Alternately, to use Python3 and/or pyside 2, one of the following:
51#
52# $ sudo apt-get install python3-pyside.qtsql libqt4-sql-psql
53# $ sudo apt-get install python-pyside2.qtsql libqt5sql5-psql
54# $ sudo apt-get install python3-pyside2.qtsql libqt5sql5-psql
55#
56# An example of using this script with Intel PT:
57#
58# $ perf record -e intel_pt//u ls
59# $ perf script -s ~/libexec/perf-core/scripts/python/export-to-postgresql.py pt_example branches calls
60# 2015-05-29 12:49:23.464364 Creating database...
61# 2015-05-29 12:49:26.281717 Writing to intermediate files...
62# 2015-05-29 12:49:27.190383 Copying to database...
63# 2015-05-29 12:49:28.140451 Removing intermediate files...
64# 2015-05-29 12:49:28.147451 Adding primary keys
65# 2015-05-29 12:49:28.655683 Adding foreign keys
66# 2015-05-29 12:49:29.365350 Done
67#
68# To browse the database, psql can be used e.g.
69#
70# $ psql pt_example
71# pt_example=# select * from samples_view where id < 100;
72# pt_example=# \d+
73# pt_example=# \d+ samples_view
74# pt_example=# \q
75#
76# An example of using the database is provided by the script
77# exported-sql-viewer.py. Refer to that script for details.
78#
79# Tables:
80#
81# The tables largely correspond to perf tools' data structures. They are largely self-explanatory.
82#
83# samples
84#
85# 'samples' is the main table. It represents what instruction was executing at a point in time
86# when something (a selected event) happened. The memory address is the instruction pointer or 'ip'.
87#
88# calls
89#
90# 'calls' represents function calls and is related to 'samples' by 'call_id' and 'return_id'.
91# 'calls' is only created when the 'calls' option to this script is specified.
92#
93# call_paths
94#
95# 'call_paths' represents all the call stacks. Each 'call' has an associated record in 'call_paths'.
96# 'calls_paths' is only created when the 'calls' option to this script is specified.
97#
98# branch_types
99#
100# 'branch_types' provides descriptions for each type of branch.
101#
102# comm_threads
103#
104# 'comm_threads' shows how 'comms' relates to 'threads'.
105#
106# comms
107#
108# 'comms' contains a record for each 'comm' - the name given to the executable that is running.
109#
110# dsos
111#
112# 'dsos' contains a record for each executable file or library.
113#
114# machines
115#
116# 'machines' can be used to distinguish virtual machines if virtualization is supported.
117#
118# selected_events
119#
120# 'selected_events' contains a record for each kind of event that has been sampled.
121#
122# symbols
123#
124# 'symbols' contains a record for each symbol. Only symbols that have samples are present.
125#
126# threads
127#
128# 'threads' contains a record for each thread.
129#
130# Views:
131#
132# Most of the tables have views for more friendly display. The views are:
133#
134# calls_view
135# call_paths_view
136# comm_threads_view
137# dsos_view
138# machines_view
139# samples_view
140# symbols_view
141# threads_view
142#
143# More examples of browsing the database with psql:
144# Note that some of the examples are not the most optimal SQL query.
145# Note that call information is only available if the script's 'calls' option has been used.
146#
147# Top 10 function calls (not aggregated by symbol):
148#
149# SELECT * FROM calls_view ORDER BY elapsed_time DESC LIMIT 10;
150#
151# Top 10 function calls (aggregated by symbol):
152#
153# SELECT symbol_id,(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,
154# SUM(elapsed_time) AS tot_elapsed_time,SUM(branch_count) AS tot_branch_count
155# FROM calls_view GROUP BY symbol_id ORDER BY tot_elapsed_time DESC LIMIT 10;
156#
157# Note that the branch count gives a rough estimation of cpu usage, so functions
158# that took a long time but have a relatively low branch count must have spent time
159# waiting.
160#
161# Find symbols by pattern matching on part of the name (e.g. names containing 'alloc'):
162#
163# SELECT * FROM symbols_view WHERE name LIKE '%alloc%';
164#
165# Top 10 function calls for a specific symbol (e.g. whose symbol_id is 187):
166#
167# SELECT * FROM calls_view WHERE symbol_id = 187 ORDER BY elapsed_time DESC LIMIT 10;
168#
169# Show function calls made by function in the same context (i.e. same call path) (e.g. one with call_path_id 254):
170#
171# SELECT * FROM calls_view WHERE parent_call_path_id = 254;
172#
173# Show branches made during a function call (e.g. where call_id is 29357 and return_id is 29370 and tid is 29670)
174#
175# SELECT * FROM samples_view WHERE id >= 29357 AND id <= 29370 AND tid = 29670 AND event LIKE 'branches%';
176#
177# Show transactions:
178#
179# SELECT * FROM samples_view WHERE event = 'transactions';
180#
181# Note transaction start has 'in_tx' true whereas, transaction end has 'in_tx' false.
182# Transaction aborts have branch_type_name 'transaction abort'
183#
184# Show transaction aborts:
185#
186# SELECT * FROM samples_view WHERE event = 'transactions' AND branch_type_name = 'transaction abort';
187#
188# To print a call stack requires walking the call_paths table. For example this python script:
189# #!/usr/bin/python2
190#
191# import sys
192# from PySide.QtSql import *
193#
194# if __name__ == '__main__':
195# if (len(sys.argv) < 3):
196# print >> sys.stderr, "Usage is: printcallstack.py <database name> <call_path_id>"
197# raise Exception("Too few arguments")
198# dbname = sys.argv[1]
199# call_path_id = sys.argv[2]
200# db = QSqlDatabase.addDatabase('QPSQL')
201# db.setDatabaseName(dbname)
202# if not db.open():
203# raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text())
204# query = QSqlQuery(db)
205# print " id ip symbol_id symbol dso_id dso_short_name"
206# while call_path_id != 0 and call_path_id != 1:
207# ret = query.exec_('SELECT * FROM call_paths_view WHERE id = ' + str(call_path_id))
208# if not ret:
209# raise Exception("Query failed: " + query.lastError().text())
210# if not query.next():
211# raise Exception("Query failed")
212# print "{0:>6} {1:>10} {2:>9} {3:<30} {4:>6} {5:<30}".format(query.value(0), query.value(1), query.value(2), query.value(3), query.value(4), query.value(5))
213# call_path_id = query.value(6)
214
215pyside_version_1 = True
216if not "pyside-version-1" in sys.argv:
217 try:
218 from PySide2.QtSql import *
219 pyside_version_1 = False
220 except:
221 pass
222
223if pyside_version_1:
224 from PySide.QtSql import *
225
226if sys.version_info < (3, 0):
227 def toserverstr(str):
228 return str
229 def toclientstr(str):
230 return str
231else:
232 # Assume UTF-8 server_encoding and client_encoding
233 def toserverstr(str):
234 return bytes(str, "UTF_8")
235 def toclientstr(str):
236 return bytes(str, "UTF_8")
237
238# Need to access PostgreSQL C library directly to use COPY FROM STDIN
239from ctypes import *
240libpq = CDLL("libpq.so.5")
241PQconnectdb = libpq.PQconnectdb
242PQconnectdb.restype = c_void_p
243PQconnectdb.argtypes = [ c_char_p ]
244PQfinish = libpq.PQfinish
245PQfinish.argtypes = [ c_void_p ]
246PQstatus = libpq.PQstatus
247PQstatus.restype = c_int
248PQstatus.argtypes = [ c_void_p ]
249PQexec = libpq.PQexec
250PQexec.restype = c_void_p
251PQexec.argtypes = [ c_void_p, c_char_p ]
252PQresultStatus = libpq.PQresultStatus
253PQresultStatus.restype = c_int
254PQresultStatus.argtypes = [ c_void_p ]
255PQputCopyData = libpq.PQputCopyData
256PQputCopyData.restype = c_int
257PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
258PQputCopyEnd = libpq.PQputCopyEnd
259PQputCopyEnd.restype = c_int
260PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
261
262sys.path.append(os.environ['PERF_EXEC_PATH'] + \
263 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
264
265# These perf imports are not used at present
266#from perf_trace_context import *
267#from Core import *
268
269perf_db_export_mode = True
270perf_db_export_calls = False
271perf_db_export_callchains = False
272
273def printerr(*args, **kw_args):
274 print(*args, file=sys.stderr, **kw_args)
275
276def printdate(*args, **kw_args):
277 print(datetime.datetime.today(), *args, sep=' ', **kw_args)
278
279def usage():
280 printerr("Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>] [<callchains>] [<pyside-version-1>]");
281 printerr("where: columns 'all' or 'branches'");
282 printerr(" calls 'calls' => create calls and call_paths table");
283 printerr(" callchains 'callchains' => create call_paths table");
284 printerr(" pyside-version-1 'pyside-version-1' => use pyside version 1");
285 raise Exception("Too few or bad arguments")
286
287if (len(sys.argv) < 2):
288 usage()
289
290dbname = sys.argv[1]
291
292if (len(sys.argv) >= 3):
293 columns = sys.argv[2]
294else:
295 columns = "all"
296
297if columns not in ("all", "branches"):
298 usage()
299
300branches = (columns == "branches")
301
302for i in range(3,len(sys.argv)):
303 if (sys.argv[i] == "calls"):
304 perf_db_export_calls = True
305 elif (sys.argv[i] == "callchains"):
306 perf_db_export_callchains = True
307 elif (sys.argv[i] == "pyside-version-1"):
308 pass
309 else:
310 usage()
311
312output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
313os.mkdir(output_dir_name)
314
315def do_query(q, s):
316 if (q.exec_(s)):
317 return
318 raise Exception("Query failed: " + q.lastError().text())
319
320printdate("Creating database...")
321
322db = QSqlDatabase.addDatabase('QPSQL')
323query = QSqlQuery(db)
324db.setDatabaseName('postgres')
325db.open()
326try:
327 do_query(query, 'CREATE DATABASE ' + dbname)
328except:
329 os.rmdir(output_dir_name)
330 raise
331query.finish()
332query.clear()
333db.close()
334
335db.setDatabaseName(dbname)
336db.open()
337
338query = QSqlQuery(db)
339do_query(query, 'SET client_min_messages TO WARNING')
340
341do_query(query, 'CREATE TABLE selected_events ('
342 'id bigint NOT NULL,'
343 'name varchar(80))')
344do_query(query, 'CREATE TABLE machines ('
345 'id bigint NOT NULL,'
346 'pid integer,'
347 'root_dir varchar(4096))')
348do_query(query, 'CREATE TABLE threads ('
349 'id bigint NOT NULL,'
350 'machine_id bigint,'
351 'process_id bigint,'
352 'pid integer,'
353 'tid integer)')
354do_query(query, 'CREATE TABLE comms ('
355 'id bigint NOT NULL,'
356 'comm varchar(16),'
357 'c_thread_id bigint,'
358 'c_time bigint,'
359 'exec_flag boolean)')
360do_query(query, 'CREATE TABLE comm_threads ('
361 'id bigint NOT NULL,'
362 'comm_id bigint,'
363 'thread_id bigint)')
364do_query(query, 'CREATE TABLE dsos ('
365 'id bigint NOT NULL,'
366 'machine_id bigint,'
367 'short_name varchar(256),'
368 'long_name varchar(4096),'
369 'build_id varchar(64))')
370do_query(query, 'CREATE TABLE symbols ('
371 'id bigint NOT NULL,'
372 'dso_id bigint,'
373 'sym_start bigint,'
374 'sym_end bigint,'
375 'binding integer,'
376 'name varchar(2048))')
377do_query(query, 'CREATE TABLE branch_types ('
378 'id integer NOT NULL,'
379 'name varchar(80))')
380
381if branches:
382 do_query(query, 'CREATE TABLE samples ('
383 'id bigint NOT NULL,'
384 'evsel_id bigint,'
385 'machine_id bigint,'
386 'thread_id bigint,'
387 'comm_id bigint,'
388 'dso_id bigint,'
389 'symbol_id bigint,'
390 'sym_offset bigint,'
391 'ip bigint,'
392 'time bigint,'
393 'cpu integer,'
394 'to_dso_id bigint,'
395 'to_symbol_id bigint,'
396 'to_sym_offset bigint,'
397 'to_ip bigint,'
398 'branch_type integer,'
399 'in_tx boolean,'
400 'call_path_id bigint,'
401 'insn_count bigint,'
402 'cyc_count bigint,'
403 'flags integer)')
404else:
405 do_query(query, 'CREATE TABLE samples ('
406 'id bigint NOT NULL,'
407 'evsel_id bigint,'
408 'machine_id bigint,'
409 'thread_id bigint,'
410 'comm_id bigint,'
411 'dso_id bigint,'
412 'symbol_id bigint,'
413 'sym_offset bigint,'
414 'ip bigint,'
415 'time bigint,'
416 'cpu integer,'
417 'to_dso_id bigint,'
418 'to_symbol_id bigint,'
419 'to_sym_offset bigint,'
420 'to_ip bigint,'
421 'period bigint,'
422 'weight bigint,'
423 'transaction bigint,'
424 'data_src bigint,'
425 'branch_type integer,'
426 'in_tx boolean,'
427 'call_path_id bigint,'
428 'insn_count bigint,'
429 'cyc_count bigint,'
430 'flags integer)')
431
432if perf_db_export_calls or perf_db_export_callchains:
433 do_query(query, 'CREATE TABLE call_paths ('
434 'id bigint NOT NULL,'
435 'parent_id bigint,'
436 'symbol_id bigint,'
437 'ip bigint)')
438if perf_db_export_calls:
439 do_query(query, 'CREATE TABLE calls ('
440 'id bigint NOT NULL,'
441 'thread_id bigint,'
442 'comm_id bigint,'
443 'call_path_id bigint,'
444 'call_time bigint,'
445 'return_time bigint,'
446 'branch_count bigint,'
447 'call_id bigint,'
448 'return_id bigint,'
449 'parent_call_path_id bigint,'
450 'flags integer,'
451 'parent_id bigint,'
452 'insn_count bigint,'
453 'cyc_count bigint)')
454
455do_query(query, 'CREATE TABLE ptwrite ('
456 'id bigint NOT NULL,'
457 'payload bigint,'
458 'exact_ip boolean)')
459
460do_query(query, 'CREATE TABLE cbr ('
461 'id bigint NOT NULL,'
462 'cbr integer,'
463 'mhz integer,'
464 'percent integer)')
465
466do_query(query, 'CREATE TABLE mwait ('
467 'id bigint NOT NULL,'
468 'hints integer,'
469 'extensions integer)')
470
471do_query(query, 'CREATE TABLE pwre ('
472 'id bigint NOT NULL,'
473 'cstate integer,'
474 'subcstate integer,'
475 'hw boolean)')
476
477do_query(query, 'CREATE TABLE exstop ('
478 'id bigint NOT NULL,'
479 'exact_ip boolean)')
480
481do_query(query, 'CREATE TABLE pwrx ('
482 'id bigint NOT NULL,'
483 'deepest_cstate integer,'
484 'last_cstate integer,'
485 'wake_reason integer)')
486
487do_query(query, 'CREATE TABLE context_switches ('
488 'id bigint NOT NULL,'
489 'machine_id bigint,'
490 'time bigint,'
491 'cpu integer,'
492 'thread_out_id bigint,'
493 'comm_out_id bigint,'
494 'thread_in_id bigint,'
495 'comm_in_id bigint,'
496 'flags integer)')
497
498do_query(query, 'CREATE VIEW machines_view AS '
499 'SELECT '
500 'id,'
501 'pid,'
502 'root_dir,'
503 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
504 ' FROM machines')
505
506do_query(query, 'CREATE VIEW dsos_view AS '
507 'SELECT '
508 'id,'
509 'machine_id,'
510 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
511 'short_name,'
512 'long_name,'
513 'build_id'
514 ' FROM dsos')
515
516do_query(query, 'CREATE VIEW symbols_view AS '
517 'SELECT '
518 'id,'
519 'name,'
520 '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
521 'dso_id,'
522 'sym_start,'
523 'sym_end,'
524 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
525 ' FROM symbols')
526
527do_query(query, 'CREATE VIEW threads_view AS '
528 'SELECT '
529 'id,'
530 'machine_id,'
531 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
532 'process_id,'
533 'pid,'
534 'tid'
535 ' FROM threads')
536
537do_query(query, 'CREATE VIEW comm_threads_view AS '
538 'SELECT '
539 'comm_id,'
540 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
541 'thread_id,'
542 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
543 '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
544 ' FROM comm_threads')
545
546if perf_db_export_calls or perf_db_export_callchains:
547 do_query(query, 'CREATE VIEW call_paths_view AS '
548 'SELECT '
549 'c.id,'
550 'to_hex(c.ip) AS ip,'
551 'c.symbol_id,'
552 '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
553 '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
554 '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
555 'c.parent_id,'
556 'to_hex(p.ip) AS parent_ip,'
557 'p.symbol_id AS parent_symbol_id,'
558 '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
559 '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
560 '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
561 ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
562if perf_db_export_calls:
563 do_query(query, 'CREATE VIEW calls_view AS '
564 'SELECT '
565 'calls.id,'
566 'thread_id,'
567 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
568 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
569 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
570 'call_path_id,'
571 'to_hex(ip) AS ip,'
572 'symbol_id,'
573 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
574 'call_time,'
575 'return_time,'
576 'return_time - call_time AS elapsed_time,'
577 'branch_count,'
578 'insn_count,'
579 'cyc_count,'
580 'CASE WHEN cyc_count=0 THEN CAST(0 AS NUMERIC(20, 2)) ELSE CAST((CAST(insn_count AS FLOAT) / cyc_count) AS NUMERIC(20, 2)) END AS IPC,'
581 'call_id,'
582 'return_id,'
583 'CASE WHEN flags=0 THEN \'\' WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' WHEN flags=6 THEN \'jump\' ELSE CAST ( flags AS VARCHAR(6) ) END AS flags,'
584 'parent_call_path_id,'
585 'calls.parent_id'
586 ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
587
588do_query(query, 'CREATE VIEW samples_view AS '
589 'SELECT '
590 'id,'
591 'time,'
592 'cpu,'
593 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
594 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
595 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
596 '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
597 'to_hex(ip) AS ip_hex,'
598 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
599 'sym_offset,'
600 '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
601 'to_hex(to_ip) AS to_ip_hex,'
602 '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
603 'to_sym_offset,'
604 '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
605 '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
606 'in_tx,'
607 'insn_count,'
608 'cyc_count,'
609 'CASE WHEN cyc_count=0 THEN CAST(0 AS NUMERIC(20, 2)) ELSE CAST((CAST(insn_count AS FLOAT) / cyc_count) AS NUMERIC(20, 2)) END AS IPC,'
610 'flags'
611 ' FROM samples')
612
613do_query(query, 'CREATE VIEW ptwrite_view AS '
614 'SELECT '
615 'ptwrite.id,'
616 'time,'
617 'cpu,'
618 'to_hex(payload) AS payload_hex,'
619 'CASE WHEN exact_ip=FALSE THEN \'False\' ELSE \'True\' END AS exact_ip'
620 ' FROM ptwrite'
621 ' INNER JOIN samples ON samples.id = ptwrite.id')
622
623do_query(query, 'CREATE VIEW cbr_view AS '
624 'SELECT '
625 'cbr.id,'
626 'time,'
627 'cpu,'
628 'cbr,'
629 'mhz,'
630 'percent'
631 ' FROM cbr'
632 ' INNER JOIN samples ON samples.id = cbr.id')
633
634do_query(query, 'CREATE VIEW mwait_view AS '
635 'SELECT '
636 'mwait.id,'
637 'time,'
638 'cpu,'
639 'to_hex(hints) AS hints_hex,'
640 'to_hex(extensions) AS extensions_hex'
641 ' FROM mwait'
642 ' INNER JOIN samples ON samples.id = mwait.id')
643
644do_query(query, 'CREATE VIEW pwre_view AS '
645 'SELECT '
646 'pwre.id,'
647 'time,'
648 'cpu,'
649 'cstate,'
650 'subcstate,'
651 'CASE WHEN hw=FALSE THEN \'False\' ELSE \'True\' END AS hw'
652 ' FROM pwre'
653 ' INNER JOIN samples ON samples.id = pwre.id')
654
655do_query(query, 'CREATE VIEW exstop_view AS '
656 'SELECT '
657 'exstop.id,'
658 'time,'
659 'cpu,'
660 'CASE WHEN exact_ip=FALSE THEN \'False\' ELSE \'True\' END AS exact_ip'
661 ' FROM exstop'
662 ' INNER JOIN samples ON samples.id = exstop.id')
663
664do_query(query, 'CREATE VIEW pwrx_view AS '
665 'SELECT '
666 'pwrx.id,'
667 'time,'
668 'cpu,'
669 'deepest_cstate,'
670 'last_cstate,'
671 'CASE WHEN wake_reason=1 THEN \'Interrupt\''
672 ' WHEN wake_reason=2 THEN \'Timer Deadline\''
673 ' WHEN wake_reason=4 THEN \'Monitored Address\''
674 ' WHEN wake_reason=8 THEN \'HW\''
675 ' ELSE CAST ( wake_reason AS VARCHAR(2) )'
676 'END AS wake_reason'
677 ' FROM pwrx'
678 ' INNER JOIN samples ON samples.id = pwrx.id')
679
680do_query(query, 'CREATE VIEW power_events_view AS '
681 'SELECT '
682 'samples.id,'
683 'samples.time,'
684 'samples.cpu,'
685 'selected_events.name AS event,'
686 'FORMAT(\'%6s\', cbr.cbr) AS cbr,'
687 'FORMAT(\'%6s\', cbr.mhz) AS MHz,'
688 'FORMAT(\'%5s\', cbr.percent) AS percent,'
689 'to_hex(mwait.hints) AS hints_hex,'
690 'to_hex(mwait.extensions) AS extensions_hex,'
691 'FORMAT(\'%3s\', pwre.cstate) AS cstate,'
692 'FORMAT(\'%3s\', pwre.subcstate) AS subcstate,'
693 'CASE WHEN pwre.hw=FALSE THEN \'False\' WHEN pwre.hw=TRUE THEN \'True\' ELSE NULL END AS hw,'
694 'CASE WHEN exstop.exact_ip=FALSE THEN \'False\' WHEN exstop.exact_ip=TRUE THEN \'True\' ELSE NULL END AS exact_ip,'
695 'FORMAT(\'%3s\', pwrx.deepest_cstate) AS deepest_cstate,'
696 'FORMAT(\'%3s\', pwrx.last_cstate) AS last_cstate,'
697 'CASE WHEN pwrx.wake_reason=1 THEN \'Interrupt\''
698 ' WHEN pwrx.wake_reason=2 THEN \'Timer Deadline\''
699 ' WHEN pwrx.wake_reason=4 THEN \'Monitored Address\''
700 ' WHEN pwrx.wake_reason=8 THEN \'HW\''
701 ' ELSE FORMAT(\'%2s\', pwrx.wake_reason)'
702 'END AS wake_reason'
703 ' FROM cbr'
704 ' FULL JOIN mwait ON mwait.id = cbr.id'
705 ' FULL JOIN pwre ON pwre.id = cbr.id'
706 ' FULL JOIN exstop ON exstop.id = cbr.id'
707 ' FULL JOIN pwrx ON pwrx.id = cbr.id'
708 ' INNER JOIN samples ON samples.id = coalesce(cbr.id, mwait.id, pwre.id, exstop.id, pwrx.id)'
709 ' INNER JOIN selected_events ON selected_events.id = samples.evsel_id'
710 ' ORDER BY samples.id')
711
712do_query(query, 'CREATE VIEW context_switches_view AS '
713 'SELECT '
714 'context_switches.id,'
715 'context_switches.machine_id,'
716 'context_switches.time,'
717 'context_switches.cpu,'
718 'th_out.pid AS pid_out,'
719 'th_out.tid AS tid_out,'
720 'comm_out.comm AS comm_out,'
721 'th_in.pid AS pid_in,'
722 'th_in.tid AS tid_in,'
723 'comm_in.comm AS comm_in,'
724 'CASE WHEN context_switches.flags = 0 THEN \'in\''
725 ' WHEN context_switches.flags = 1 THEN \'out\''
726 ' WHEN context_switches.flags = 3 THEN \'out preempt\''
727 ' ELSE CAST ( context_switches.flags AS VARCHAR(11) )'
728 'END AS flags'
729 ' FROM context_switches'
730 ' INNER JOIN threads AS th_out ON th_out.id = context_switches.thread_out_id'
731 ' INNER JOIN threads AS th_in ON th_in.id = context_switches.thread_in_id'
732 ' INNER JOIN comms AS comm_out ON comm_out.id = context_switches.comm_out_id'
733 ' INNER JOIN comms AS comm_in ON comm_in.id = context_switches.comm_in_id')
734
735file_header = struct.pack("!11sii", b"PGCOPY\n\377\r\n\0", 0, 0)
736file_trailer = b"\377\377"
737
738def open_output_file(file_name):
739 path_name = output_dir_name + "/" + file_name
740 file = open(path_name, "wb+")
741 file.write(file_header)
742 return file
743
744def close_output_file(file):
745 file.write(file_trailer)
746 file.close()
747
748def copy_output_file_direct(file, table_name):
749 close_output_file(file)
750 sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')"
751 do_query(query, sql)
752
753# Use COPY FROM STDIN because security may prevent postgres from accessing the files directly
754def copy_output_file(file, table_name):
755 conn = PQconnectdb(toclientstr("dbname = " + dbname))
756 if (PQstatus(conn)):
757 raise Exception("COPY FROM STDIN PQconnectdb failed")
758 file.write(file_trailer)
759 file.seek(0)
760 sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')"
761 res = PQexec(conn, toclientstr(sql))
762 if (PQresultStatus(res) != 4):
763 raise Exception("COPY FROM STDIN PQexec failed")
764 data = file.read(65536)
765 while (len(data)):
766 ret = PQputCopyData(conn, data, len(data))
767 if (ret != 1):
768 raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret))
769 data = file.read(65536)
770 ret = PQputCopyEnd(conn, None)
771 if (ret != 1):
772 raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret))
773 PQfinish(conn)
774
775def remove_output_file(file):
776 name = file.name
777 file.close()
778 os.unlink(name)
779
780evsel_file = open_output_file("evsel_table.bin")
781machine_file = open_output_file("machine_table.bin")
782thread_file = open_output_file("thread_table.bin")
783comm_file = open_output_file("comm_table.bin")
784comm_thread_file = open_output_file("comm_thread_table.bin")
785dso_file = open_output_file("dso_table.bin")
786symbol_file = open_output_file("symbol_table.bin")
787branch_type_file = open_output_file("branch_type_table.bin")
788sample_file = open_output_file("sample_table.bin")
789if perf_db_export_calls or perf_db_export_callchains:
790 call_path_file = open_output_file("call_path_table.bin")
791if perf_db_export_calls:
792 call_file = open_output_file("call_table.bin")
793ptwrite_file = open_output_file("ptwrite_table.bin")
794cbr_file = open_output_file("cbr_table.bin")
795mwait_file = open_output_file("mwait_table.bin")
796pwre_file = open_output_file("pwre_table.bin")
797exstop_file = open_output_file("exstop_table.bin")
798pwrx_file = open_output_file("pwrx_table.bin")
799context_switches_file = open_output_file("context_switches_table.bin")
800
801def trace_begin():
802 printdate("Writing to intermediate files...")
803 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
804 evsel_table(0, "unknown")
805 machine_table(0, 0, "unknown")
806 thread_table(0, 0, 0, -1, -1)
807 comm_table(0, "unknown", 0, 0, 0)
808 dso_table(0, 0, "unknown", "unknown", "")
809 symbol_table(0, 0, 0, 0, 0, "unknown")
810 sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
811 if perf_db_export_calls or perf_db_export_callchains:
812 call_path_table(0, 0, 0, 0)
813 call_return_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
814
815unhandled_count = 0
816
817def is_table_empty(table_name):
818 do_query(query, 'SELECT * FROM ' + table_name + ' LIMIT 1');
819 if query.next():
820 return False
821 return True
822
823def drop(table_name):
824 do_query(query, 'DROP VIEW ' + table_name + '_view');
825 do_query(query, 'DROP TABLE ' + table_name);
826
827def trace_end():
828 printdate("Copying to database...")
829 copy_output_file(evsel_file, "selected_events")
830 copy_output_file(machine_file, "machines")
831 copy_output_file(thread_file, "threads")
832 copy_output_file(comm_file, "comms")
833 copy_output_file(comm_thread_file, "comm_threads")
834 copy_output_file(dso_file, "dsos")
835 copy_output_file(symbol_file, "symbols")
836 copy_output_file(branch_type_file, "branch_types")
837 copy_output_file(sample_file, "samples")
838 if perf_db_export_calls or perf_db_export_callchains:
839 copy_output_file(call_path_file, "call_paths")
840 if perf_db_export_calls:
841 copy_output_file(call_file, "calls")
842 copy_output_file(ptwrite_file, "ptwrite")
843 copy_output_file(cbr_file, "cbr")
844 copy_output_file(mwait_file, "mwait")
845 copy_output_file(pwre_file, "pwre")
846 copy_output_file(exstop_file, "exstop")
847 copy_output_file(pwrx_file, "pwrx")
848 copy_output_file(context_switches_file, "context_switches")
849
850 printdate("Removing intermediate files...")
851 remove_output_file(evsel_file)
852 remove_output_file(machine_file)
853 remove_output_file(thread_file)
854 remove_output_file(comm_file)
855 remove_output_file(comm_thread_file)
856 remove_output_file(dso_file)
857 remove_output_file(symbol_file)
858 remove_output_file(branch_type_file)
859 remove_output_file(sample_file)
860 if perf_db_export_calls or perf_db_export_callchains:
861 remove_output_file(call_path_file)
862 if perf_db_export_calls:
863 remove_output_file(call_file)
864 remove_output_file(ptwrite_file)
865 remove_output_file(cbr_file)
866 remove_output_file(mwait_file)
867 remove_output_file(pwre_file)
868 remove_output_file(exstop_file)
869 remove_output_file(pwrx_file)
870 remove_output_file(context_switches_file)
871 os.rmdir(output_dir_name)
872 printdate("Adding primary keys")
873 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
874 do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
875 do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
876 do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)')
877 do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)')
878 do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)')
879 do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)')
880 do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)')
881 do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)')
882 if perf_db_export_calls or perf_db_export_callchains:
883 do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)')
884 if perf_db_export_calls:
885 do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
886 do_query(query, 'ALTER TABLE ptwrite ADD PRIMARY KEY (id)')
887 do_query(query, 'ALTER TABLE cbr ADD PRIMARY KEY (id)')
888 do_query(query, 'ALTER TABLE mwait ADD PRIMARY KEY (id)')
889 do_query(query, 'ALTER TABLE pwre ADD PRIMARY KEY (id)')
890 do_query(query, 'ALTER TABLE exstop ADD PRIMARY KEY (id)')
891 do_query(query, 'ALTER TABLE pwrx ADD PRIMARY KEY (id)')
892 do_query(query, 'ALTER TABLE context_switches ADD PRIMARY KEY (id)')
893
894 printdate("Adding foreign keys")
895 do_query(query, 'ALTER TABLE threads '
896 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
897 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
898 do_query(query, 'ALTER TABLE comms '
899 'ADD CONSTRAINT threadfk FOREIGN KEY (c_thread_id) REFERENCES threads (id)')
900 do_query(query, 'ALTER TABLE comm_threads '
901 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
902 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)')
903 do_query(query, 'ALTER TABLE dsos '
904 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)')
905 do_query(query, 'ALTER TABLE symbols '
906 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)')
907 do_query(query, 'ALTER TABLE samples '
908 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),'
909 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
910 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
911 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
912 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),'
913 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),'
914 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),'
915 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)')
916 if perf_db_export_calls or perf_db_export_callchains:
917 do_query(query, 'ALTER TABLE call_paths '
918 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),'
919 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)')
920 if perf_db_export_calls:
921 do_query(query, 'ALTER TABLE calls '
922 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
923 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
924 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),'
925 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),'
926 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),'
927 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)')
928 do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
929 do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)')
930 do_query(query, 'ALTER TABLE comms ADD has_calls boolean')
931 do_query(query, 'UPDATE comms SET has_calls = TRUE WHERE comms.id IN (SELECT DISTINCT comm_id FROM calls)')
932 do_query(query, 'ALTER TABLE ptwrite '
933 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
934 do_query(query, 'ALTER TABLE cbr '
935 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
936 do_query(query, 'ALTER TABLE mwait '
937 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
938 do_query(query, 'ALTER TABLE pwre '
939 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
940 do_query(query, 'ALTER TABLE exstop '
941 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
942 do_query(query, 'ALTER TABLE pwrx '
943 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
944 do_query(query, 'ALTER TABLE context_switches '
945 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
946 'ADD CONSTRAINT toutfk FOREIGN KEY (thread_out_id) REFERENCES threads (id),'
947 'ADD CONSTRAINT tinfk FOREIGN KEY (thread_in_id) REFERENCES threads (id),'
948 'ADD CONSTRAINT coutfk FOREIGN KEY (comm_out_id) REFERENCES comms (id),'
949 'ADD CONSTRAINT cinfk FOREIGN KEY (comm_in_id) REFERENCES comms (id)')
950
951 printdate("Dropping unused tables")
952 if is_table_empty("ptwrite"):
953 drop("ptwrite")
954 if is_table_empty("mwait") and is_table_empty("pwre") and is_table_empty("exstop") and is_table_empty("pwrx"):
955 do_query(query, 'DROP VIEW power_events_view');
956 drop("mwait")
957 drop("pwre")
958 drop("exstop")
959 drop("pwrx")
960 if is_table_empty("cbr"):
961 drop("cbr")
962 if is_table_empty("context_switches"):
963 drop("context_switches")
964
965 if (unhandled_count):
966 printdate("Warning: ", unhandled_count, " unhandled events")
967 printdate("Done")
968
969def trace_unhandled(event_name, context, event_fields_dict):
970 global unhandled_count
971 unhandled_count += 1
972
973def sched__sched_switch(*x):
974 pass
975
976def evsel_table(evsel_id, evsel_name, *x):
977 evsel_name = toserverstr(evsel_name)
978 n = len(evsel_name)
979 fmt = "!hiqi" + str(n) + "s"
980 value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name)
981 evsel_file.write(value)
982
983def machine_table(machine_id, pid, root_dir, *x):
984 root_dir = toserverstr(root_dir)
985 n = len(root_dir)
986 fmt = "!hiqiii" + str(n) + "s"
987 value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir)
988 machine_file.write(value)
989
990def thread_table(thread_id, machine_id, process_id, pid, tid, *x):
991 value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid)
992 thread_file.write(value)
993
994def comm_table(comm_id, comm_str, thread_id, time, exec_flag, *x):
995 comm_str = toserverstr(comm_str)
996 n = len(comm_str)
997 fmt = "!hiqi" + str(n) + "s" + "iqiqiB"
998 value = struct.pack(fmt, 5, 8, comm_id, n, comm_str, 8, thread_id, 8, time, 1, exec_flag)
999 comm_file.write(value)
1000
1001def comm_thread_table(comm_thread_id, comm_id, thread_id, *x):
1002 fmt = "!hiqiqiq"
1003 value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id)
1004 comm_thread_file.write(value)
1005
1006def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x):
1007 short_name = toserverstr(short_name)
1008 long_name = toserverstr(long_name)
1009 build_id = toserverstr(build_id)
1010 n1 = len(short_name)
1011 n2 = len(long_name)
1012 n3 = len(build_id)
1013 fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s"
1014 value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id)
1015 dso_file.write(value)
1016
1017def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x):
1018 symbol_name = toserverstr(symbol_name)
1019 n = len(symbol_name)
1020 fmt = "!hiqiqiqiqiii" + str(n) + "s"
1021 value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name)
1022 symbol_file.write(value)
1023
1024def branch_type_table(branch_type, name, *x):
1025 name = toserverstr(name)
1026 n = len(name)
1027 fmt = "!hiii" + str(n) + "s"
1028 value = struct.pack(fmt, 2, 4, branch_type, n, name)
1029 branch_type_file.write(value)
1030
1031def sample_table(sample_id, evsel_id, machine_id, thread_id, comm_id, dso_id, symbol_id, sym_offset, ip, time, cpu, to_dso_id, to_symbol_id, to_sym_offset, to_ip, period, weight, transaction, data_src, branch_type, in_tx, call_path_id, insn_cnt, cyc_cnt, flags, *x):
1032 if branches:
1033 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiiiBiqiqiqii", 21, 8, sample_id, 8, evsel_id, 8, machine_id, 8, thread_id, 8, comm_id, 8, dso_id, 8, symbol_id, 8, sym_offset, 8, ip, 8, time, 4, cpu, 8, to_dso_id, 8, to_symbol_id, 8, to_sym_offset, 8, to_ip, 4, branch_type, 1, in_tx, 8, call_path_id, 8, insn_cnt, 8, cyc_cnt, 4, flags)
1034 else:
1035 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiqiqiqiqiiiBiqiqiqii", 25, 8, sample_id, 8, evsel_id, 8, machine_id, 8, thread_id, 8, comm_id, 8, dso_id, 8, symbol_id, 8, sym_offset, 8, ip, 8, time, 4, cpu, 8, to_dso_id, 8, to_symbol_id, 8, to_sym_offset, 8, to_ip, 8, period, 8, weight, 8, transaction, 8, data_src, 4, branch_type, 1, in_tx, 8, call_path_id, 8, insn_cnt, 8, cyc_cnt, 4, flags)
1036 sample_file.write(value)
1037
1038def call_path_table(cp_id, parent_id, symbol_id, ip, *x):
1039 fmt = "!hiqiqiqiq"
1040 value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip)
1041 call_path_file.write(value)
1042
1043def call_return_table(cr_id, thread_id, comm_id, call_path_id, call_time, return_time, branch_count, call_id, return_id, parent_call_path_id, flags, parent_id, insn_cnt, cyc_cnt, *x):
1044 fmt = "!hiqiqiqiqiqiqiqiqiqiqiiiqiqiq"
1045 value = struct.pack(fmt, 14, 8, cr_id, 8, thread_id, 8, comm_id, 8, call_path_id, 8, call_time, 8, return_time, 8, branch_count, 8, call_id, 8, return_id, 8, parent_call_path_id, 4, flags, 8, parent_id, 8, insn_cnt, 8, cyc_cnt)
1046 call_file.write(value)
1047
1048def ptwrite(id, raw_buf):
1049 data = struct.unpack_from("<IQ", raw_buf)
1050 flags = data[0]
1051 payload = data[1]
1052 exact_ip = flags & 1
1053 value = struct.pack("!hiqiqiB", 3, 8, id, 8, payload, 1, exact_ip)
1054 ptwrite_file.write(value)
1055
1056def cbr(id, raw_buf):
1057 data = struct.unpack_from("<BBBBII", raw_buf)
1058 cbr = data[0]
1059 MHz = (data[4] + 500) / 1000
1060 percent = ((cbr * 1000 / data[2]) + 5) / 10
1061 value = struct.pack("!hiqiiiiii", 4, 8, id, 4, cbr, 4, int(MHz), 4, int(percent))
1062 cbr_file.write(value)
1063
1064def mwait(id, raw_buf):
1065 data = struct.unpack_from("<IQ", raw_buf)
1066 payload = data[1]
1067 hints = payload & 0xff
1068 extensions = (payload >> 32) & 0x3
1069 value = struct.pack("!hiqiiii", 3, 8, id, 4, hints, 4, extensions)
1070 mwait_file.write(value)
1071
1072def pwre(id, raw_buf):
1073 data = struct.unpack_from("<IQ", raw_buf)
1074 payload = data[1]
1075 hw = (payload >> 7) & 1
1076 cstate = (payload >> 12) & 0xf
1077 subcstate = (payload >> 8) & 0xf
1078 value = struct.pack("!hiqiiiiiB", 4, 8, id, 4, cstate, 4, subcstate, 1, hw)
1079 pwre_file.write(value)
1080
1081def exstop(id, raw_buf):
1082 data = struct.unpack_from("<I", raw_buf)
1083 flags = data[0]
1084 exact_ip = flags & 1
1085 value = struct.pack("!hiqiB", 2, 8, id, 1, exact_ip)
1086 exstop_file.write(value)
1087
1088def pwrx(id, raw_buf):
1089 data = struct.unpack_from("<IQ", raw_buf)
1090 payload = data[1]
1091 deepest_cstate = payload & 0xf
1092 last_cstate = (payload >> 4) & 0xf
1093 wake_reason = (payload >> 8) & 0xf
1094 value = struct.pack("!hiqiiiiii", 4, 8, id, 4, deepest_cstate, 4, last_cstate, 4, wake_reason)
1095 pwrx_file.write(value)
1096
1097def synth_data(id, config, raw_buf, *x):
1098 if config == 0:
1099 ptwrite(id, raw_buf)
1100 elif config == 1:
1101 mwait(id, raw_buf)
1102 elif config == 2:
1103 pwre(id, raw_buf)
1104 elif config == 3:
1105 exstop(id, raw_buf)
1106 elif config == 4:
1107 pwrx(id, raw_buf)
1108 elif config == 5:
1109 cbr(id, raw_buf)
1110
1111def context_switch_table(id, machine_id, time, cpu, thread_out_id, comm_out_id, thread_in_id, comm_in_id, flags, *x):
1112 fmt = "!hiqiqiqiiiqiqiqiqii"
1113 value = struct.pack(fmt, 9, 8, id, 8, machine_id, 8, time, 4, cpu, 8, thread_out_id, 8, comm_out_id, 8, thread_in_id, 8, comm_in_id, 4, flags)
1114 context_switches_file.write(value)