1 # export-to-sqlite.py: export perf data to a sqlite3 database
2 # Copyright (c) 2017, Intel Corporation.
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.
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
13 from __future__ import print_function
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-sqlite for Qt sqlite3 support.
24 # An example of using this script with Intel PT:
26 # $ perf record -e intel_pt//u ls
27 # $ perf script -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py pt_example branches calls
28 # 2017-07-31 14:26:07.326913 Creating database...
29 # 2017-07-31 14:26:07.538097 Writing records...
30 # 2017-07-31 14:26:09.889292 Adding indexes
31 # 2017-07-31 14:26:09.958746 Done
33 # To browse the database, sqlite3 can be used e.g.
35 # $ sqlite3 pt_example
37 # sqlite> select * from samples_view where id < 10;
38 # sqlite> .mode column
39 # sqlite> select * from samples_view where id < 10;
41 # sqlite> .schema samples_view
44 # An example of using the database is provided by the script
45 # exported-sql-viewer.py. Refer to that script for details.
47 # The database structure is practically the same as created by the script
48 # export-to-postgresql.py. Refer to that script for details. A notable
49 # difference is the 'transaction' column of the 'samples' table which is
50 # renamed 'transaction_' in sqlite because 'transaction' is a reserved word.
52 from PySide.QtSql import *
54 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
55 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
57 # These perf imports are not used at present
58 #from perf_trace_context import *
61 perf_db_export_mode = True
62 perf_db_export_calls = False
63 perf_db_export_callchains = False
65 def printerr(*args, **keyword_args):
66 print(*args, file=sys.stderr, **keyword_args)
69 printerr("Usage is: export-to-sqlite.py <database name> [<columns>] [<calls>] [<callchains>]");
70 printerr("where: columns 'all' or 'branches'");
71 printerr(" calls 'calls' => create calls and call_paths table");
72 printerr(" callchains 'callchains' => create call_paths table");
73 raise Exception("Too few arguments")
75 if (len(sys.argv) < 2):
80 if (len(sys.argv) >= 3):
85 if columns not in ("all", "branches"):
88 branches = (columns == "branches")
90 for i in range(3,len(sys.argv)):
91 if (sys.argv[i] == "calls"):
92 perf_db_export_calls = True
93 elif (sys.argv[i] == "callchains"):
94 perf_db_export_callchains = True
101 raise Exception("Query failed: " + q.lastError().text())
106 raise Exception("Query failed: " + q.lastError().text())
108 print(datetime.datetime.today(), "Creating database ...")
119 raise Exception(dbname + " already exists")
121 db = QSqlDatabase.addDatabase('QSQLITE')
122 db.setDatabaseName(dbname)
125 query = QSqlQuery(db)
127 do_query(query, 'PRAGMA journal_mode = OFF')
128 do_query(query, 'BEGIN TRANSACTION')
130 do_query(query, 'CREATE TABLE selected_events ('
131 'id integer NOT NULL PRIMARY KEY,'
133 do_query(query, 'CREATE TABLE machines ('
134 'id integer NOT NULL PRIMARY KEY,'
136 'root_dir varchar(4096))')
137 do_query(query, 'CREATE TABLE threads ('
138 'id integer NOT NULL PRIMARY KEY,'
143 do_query(query, 'CREATE TABLE comms ('
144 'id integer NOT NULL PRIMARY KEY,'
146 do_query(query, 'CREATE TABLE comm_threads ('
147 'id integer NOT NULL PRIMARY KEY,'
150 do_query(query, 'CREATE TABLE dsos ('
151 'id integer NOT NULL PRIMARY KEY,'
153 'short_name varchar(256),'
154 'long_name varchar(4096),'
155 'build_id varchar(64))')
156 do_query(query, 'CREATE TABLE symbols ('
157 'id integer NOT NULL PRIMARY KEY,'
162 'name varchar(2048))')
163 do_query(query, 'CREATE TABLE branch_types ('
164 'id integer NOT NULL PRIMARY KEY,'
168 do_query(query, 'CREATE TABLE samples ('
169 'id integer NOT NULL PRIMARY KEY,'
181 'to_symbol_id bigint,'
182 'to_sym_offset bigint,'
184 'branch_type integer,'
186 'call_path_id bigint)')
188 do_query(query, 'CREATE TABLE samples ('
189 'id integer NOT NULL PRIMARY KEY,'
201 'to_symbol_id bigint,'
202 'to_sym_offset bigint,'
206 'transaction_ bigint,'
208 'branch_type integer,'
210 'call_path_id bigint)')
212 if perf_db_export_calls or perf_db_export_callchains:
213 do_query(query, 'CREATE TABLE call_paths ('
214 'id integer NOT NULL PRIMARY KEY,'
218 if perf_db_export_calls:
219 do_query(query, 'CREATE TABLE calls ('
220 'id integer NOT NULL PRIMARY KEY,'
223 'call_path_id bigint,'
225 'return_time bigint,'
226 'branch_count bigint,'
229 'parent_call_path_id bigint,'
233 # printf was added to sqlite in version 3.8.3
234 sqlite_has_printf = False
236 do_query(query, 'SELECT printf("") FROM machines')
237 sqlite_has_printf = True
242 if sqlite_has_printf:
243 return 'printf("%x", ' + x + ')'
247 do_query(query, 'CREATE VIEW machines_view AS '
252 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
255 do_query(query, 'CREATE VIEW dsos_view AS '
259 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
265 do_query(query, 'CREATE VIEW symbols_view AS '
269 '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
273 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
276 do_query(query, 'CREATE VIEW threads_view AS '
280 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
286 do_query(query, 'CREATE VIEW comm_threads_view AS '
289 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
291 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
292 '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
293 ' FROM comm_threads')
295 if perf_db_export_calls or perf_db_export_callchains:
296 do_query(query, 'CREATE VIEW call_paths_view AS '
299 + emit_to_hex('c.ip') + ' AS ip,'
301 '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
302 '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
303 '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
305 + emit_to_hex('p.ip') + ' AS parent_ip,'
306 'p.symbol_id AS parent_symbol_id,'
307 '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
308 '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
309 '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
310 ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
311 if perf_db_export_calls:
312 do_query(query, 'CREATE VIEW calls_view AS '
316 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
317 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
318 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
320 + emit_to_hex('ip') + ' AS ip,'
322 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
325 'return_time - call_time AS elapsed_time,'
329 '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 flags END AS flags,'
330 'parent_call_path_id,'
332 ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
334 do_query(query, 'CREATE VIEW samples_view AS '
339 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
340 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
341 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
342 '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
343 + emit_to_hex('ip') + ' AS ip_hex,'
344 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
346 '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
347 + emit_to_hex('to_ip') + ' AS to_ip_hex,'
348 '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
350 '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
351 '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
355 do_query(query, 'END TRANSACTION')
357 evsel_query = QSqlQuery(db)
358 evsel_query.prepare("INSERT INTO selected_events VALUES (?, ?)")
359 machine_query = QSqlQuery(db)
360 machine_query.prepare("INSERT INTO machines VALUES (?, ?, ?)")
361 thread_query = QSqlQuery(db)
362 thread_query.prepare("INSERT INTO threads VALUES (?, ?, ?, ?, ?)")
363 comm_query = QSqlQuery(db)
364 comm_query.prepare("INSERT INTO comms VALUES (?, ?)")
365 comm_thread_query = QSqlQuery(db)
366 comm_thread_query.prepare("INSERT INTO comm_threads VALUES (?, ?, ?)")
367 dso_query = QSqlQuery(db)
368 dso_query.prepare("INSERT INTO dsos VALUES (?, ?, ?, ?, ?)")
369 symbol_query = QSqlQuery(db)
370 symbol_query.prepare("INSERT INTO symbols VALUES (?, ?, ?, ?, ?, ?)")
371 branch_type_query = QSqlQuery(db)
372 branch_type_query.prepare("INSERT INTO branch_types VALUES (?, ?)")
373 sample_query = QSqlQuery(db)
375 sample_query.prepare("INSERT INTO samples VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
377 sample_query.prepare("INSERT INTO samples VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
378 if perf_db_export_calls or perf_db_export_callchains:
379 call_path_query = QSqlQuery(db)
380 call_path_query.prepare("INSERT INTO call_paths VALUES (?, ?, ?, ?)")
381 if perf_db_export_calls:
382 call_query = QSqlQuery(db)
383 call_query.prepare("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
386 print(datetime.datetime.today(), "Writing records...")
387 do_query(query, 'BEGIN TRANSACTION')
388 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
389 evsel_table(0, "unknown")
390 machine_table(0, 0, "unknown")
391 thread_table(0, 0, 0, -1, -1)
392 comm_table(0, "unknown")
393 dso_table(0, 0, "unknown", "unknown", "")
394 symbol_table(0, 0, 0, 0, 0, "unknown")
395 sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
396 if perf_db_export_calls or perf_db_export_callchains:
397 call_path_table(0, 0, 0, 0)
398 call_return_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
403 do_query(query, 'END TRANSACTION')
405 print(datetime.datetime.today(), "Adding indexes")
406 if perf_db_export_calls:
407 do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
408 do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)')
410 if (unhandled_count):
411 print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events")
412 print(datetime.datetime.today(), "Done")
414 def trace_unhandled(event_name, context, event_fields_dict):
415 global unhandled_count
418 def sched__sched_switch(*x):
421 def bind_exec(q, n, x):
423 q.addBindValue(str(xx))
427 bind_exec(evsel_query, 2, x)
429 def machine_table(*x):
430 bind_exec(machine_query, 3, x)
432 def thread_table(*x):
433 bind_exec(thread_query, 5, x)
436 bind_exec(comm_query, 2, x)
438 def comm_thread_table(*x):
439 bind_exec(comm_thread_query, 3, x)
442 bind_exec(dso_query, 5, x)
444 def symbol_table(*x):
445 bind_exec(symbol_query, 6, x)
447 def branch_type_table(*x):
448 bind_exec(branch_type_query, 2, x)
450 def sample_table(*x):
453 sample_query.addBindValue(str(xx))
455 sample_query.addBindValue(str(xx))
456 do_query_(sample_query)
458 bind_exec(sample_query, 22, x)
460 def call_path_table(*x):
461 bind_exec(call_path_query, 4, x)
463 def call_return_table(*x):
464 bind_exec(call_query, 12, x)