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
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-sqlite for Qt sqlite3 support.
22 # An example of using this script with Intel PT:
24 # $ perf record -e intel_pt//u ls
25 # $ perf script -s ~/libexec/perf-core/scripts/python/export-to-sqlite.py pt_example branches calls
26 # 2017-07-31 14:26:07.326913 Creating database...
27 # 2017-07-31 14:26:07.538097 Writing records...
28 # 2017-07-31 14:26:09.889292 Adding indexes
29 # 2017-07-31 14:26:09.958746 Done
31 # To browse the database, sqlite3 can be used e.g.
33 # $ sqlite3 pt_example
35 # sqlite> select * from samples_view where id < 10;
36 # sqlite> .mode column
37 # sqlite> select * from samples_view where id < 10;
39 # sqlite> .schema samples_view
42 # An example of using the database is provided by the script
43 # call-graph-from-sql.py. Refer to that script for details.
45 # The database structure is practically the same as created by the script
46 # export-to-postgresql.py. Refer to that script for details. A notable
47 # difference is the 'transaction' column of the 'samples' table which is
48 # renamed 'transaction_' in sqlite because 'transaction' is a reserved word.
50 from PySide
.QtSql
import *
52 sys
.path
.append(os
.environ
['PERF_EXEC_PATH'] + \
53 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
55 # These perf imports are not used at present
56 #from perf_trace_context import *
59 perf_db_export_mode
= True
60 perf_db_export_calls
= False
61 perf_db_export_callchains
= False
64 print >> sys
.stderr
, "Usage is: export-to-sqlite.py <database name> [<columns>] [<calls>] [<callchains>]"
65 print >> sys
.stderr
, "where: columns 'all' or 'branches'"
66 print >> sys
.stderr
, " calls 'calls' => create calls and call_paths table"
67 print >> sys
.stderr
, " callchains 'callchains' => create call_paths table"
68 raise Exception("Too few arguments")
70 if (len(sys
.argv
) < 2):
75 if (len(sys
.argv
) >= 3):
80 if columns
not in ("all", "branches"):
83 branches
= (columns
== "branches")
85 for i
in range(3,len(sys
.argv
)):
86 if (sys
.argv
[i
] == "calls"):
87 perf_db_export_calls
= True
88 elif (sys
.argv
[i
] == "callchains"):
89 perf_db_export_callchains
= True
96 raise Exception("Query failed: " + q
.lastError().text())
101 raise Exception("Query failed: " + q
.lastError().text())
103 print datetime
.datetime
.today(), "Creating database..."
114 raise Exception(dbname
+ " already exists")
116 db
= QSqlDatabase
.addDatabase('QSQLITE')
117 db
.setDatabaseName(dbname
)
120 query
= QSqlQuery(db
)
122 do_query(query
, 'PRAGMA journal_mode = OFF')
123 do_query(query
, 'BEGIN TRANSACTION')
125 do_query(query
, 'CREATE TABLE selected_events ('
126 'id integer NOT NULL PRIMARY KEY,'
128 do_query(query
, 'CREATE TABLE machines ('
129 'id integer NOT NULL PRIMARY KEY,'
131 'root_dir varchar(4096))')
132 do_query(query
, 'CREATE TABLE threads ('
133 'id integer NOT NULL PRIMARY KEY,'
138 do_query(query
, 'CREATE TABLE comms ('
139 'id integer NOT NULL PRIMARY KEY,'
141 do_query(query
, 'CREATE TABLE comm_threads ('
142 'id integer NOT NULL PRIMARY KEY,'
145 do_query(query
, 'CREATE TABLE dsos ('
146 'id integer NOT NULL PRIMARY KEY,'
148 'short_name varchar(256),'
149 'long_name varchar(4096),'
150 'build_id varchar(64))')
151 do_query(query
, 'CREATE TABLE symbols ('
152 'id integer NOT NULL PRIMARY KEY,'
157 'name varchar(2048))')
158 do_query(query
, 'CREATE TABLE branch_types ('
159 'id integer NOT NULL PRIMARY KEY,'
163 do_query(query
, 'CREATE TABLE samples ('
164 'id integer NOT NULL PRIMARY KEY,'
176 'to_symbol_id bigint,'
177 'to_sym_offset bigint,'
179 'branch_type integer,'
181 'call_path_id bigint)')
183 do_query(query
, 'CREATE TABLE samples ('
184 'id integer NOT NULL PRIMARY KEY,'
196 'to_symbol_id bigint,'
197 'to_sym_offset bigint,'
201 'transaction_ bigint,'
203 'branch_type integer,'
205 'call_path_id bigint)')
207 if perf_db_export_calls
or perf_db_export_callchains
:
208 do_query(query
, 'CREATE TABLE call_paths ('
209 'id integer NOT NULL PRIMARY KEY,'
213 if perf_db_export_calls
:
214 do_query(query
, 'CREATE TABLE calls ('
215 'id integer NOT NULL PRIMARY KEY,'
218 'call_path_id bigint,'
220 'return_time bigint,'
221 'branch_count bigint,'
224 'parent_call_path_id bigint,'
227 # printf was added to sqlite in version 3.8.3
228 sqlite_has_printf
= False
230 do_query(query
, 'SELECT printf("") FROM machines')
231 sqlite_has_printf
= True
236 if sqlite_has_printf
:
237 return 'printf("%x", ' + x
+ ')'
241 do_query(query
, 'CREATE VIEW machines_view AS '
246 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
249 do_query(query
, 'CREATE VIEW dsos_view AS '
253 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
259 do_query(query
, 'CREATE VIEW symbols_view AS '
263 '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
267 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
270 do_query(query
, 'CREATE VIEW threads_view AS '
274 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
280 do_query(query
, 'CREATE VIEW comm_threads_view AS '
283 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
285 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
286 '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
287 ' FROM comm_threads')
289 if perf_db_export_calls
or perf_db_export_callchains
:
290 do_query(query
, 'CREATE VIEW call_paths_view AS '
293 + emit_to_hex('c.ip') + ' AS ip,'
295 '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
296 '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
297 '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
299 + emit_to_hex('p.ip') + ' AS parent_ip,'
300 'p.symbol_id AS parent_symbol_id,'
301 '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
302 '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
303 '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
304 ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
305 if perf_db_export_calls
:
306 do_query(query
, 'CREATE VIEW calls_view AS '
310 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
311 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
312 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
314 + emit_to_hex('ip') + ' AS ip,'
316 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
319 'return_time - call_time AS elapsed_time,'
323 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,'
324 'parent_call_path_id'
325 ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
327 do_query(query
, 'CREATE VIEW samples_view AS '
332 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
333 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
334 '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
335 '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
336 + emit_to_hex('ip') + ' AS ip_hex,'
337 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
339 '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
340 + emit_to_hex('to_ip') + ' AS to_ip_hex,'
341 '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
343 '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
344 '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
348 do_query(query
, 'END TRANSACTION')
350 evsel_query
= QSqlQuery(db
)
351 evsel_query
.prepare("INSERT INTO selected_events VALUES (?, ?)")
352 machine_query
= QSqlQuery(db
)
353 machine_query
.prepare("INSERT INTO machines VALUES (?, ?, ?)")
354 thread_query
= QSqlQuery(db
)
355 thread_query
.prepare("INSERT INTO threads VALUES (?, ?, ?, ?, ?)")
356 comm_query
= QSqlQuery(db
)
357 comm_query
.prepare("INSERT INTO comms VALUES (?, ?)")
358 comm_thread_query
= QSqlQuery(db
)
359 comm_thread_query
.prepare("INSERT INTO comm_threads VALUES (?, ?, ?)")
360 dso_query
= QSqlQuery(db
)
361 dso_query
.prepare("INSERT INTO dsos VALUES (?, ?, ?, ?, ?)")
362 symbol_query
= QSqlQuery(db
)
363 symbol_query
.prepare("INSERT INTO symbols VALUES (?, ?, ?, ?, ?, ?)")
364 branch_type_query
= QSqlQuery(db
)
365 branch_type_query
.prepare("INSERT INTO branch_types VALUES (?, ?)")
366 sample_query
= QSqlQuery(db
)
368 sample_query
.prepare("INSERT INTO samples VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
370 sample_query
.prepare("INSERT INTO samples VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
371 if perf_db_export_calls
or perf_db_export_callchains
:
372 call_path_query
= QSqlQuery(db
)
373 call_path_query
.prepare("INSERT INTO call_paths VALUES (?, ?, ?, ?)")
374 if perf_db_export_calls
:
375 call_query
= QSqlQuery(db
)
376 call_query
.prepare("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
379 print datetime
.datetime
.today(), "Writing records..."
380 do_query(query
, 'BEGIN TRANSACTION')
381 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
382 evsel_table(0, "unknown")
383 machine_table(0, 0, "unknown")
384 thread_table(0, 0, 0, -1, -1)
385 comm_table(0, "unknown")
386 dso_table(0, 0, "unknown", "unknown", "")
387 symbol_table(0, 0, 0, 0, 0, "unknown")
388 sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
389 if perf_db_export_calls
or perf_db_export_callchains
:
390 call_path_table(0, 0, 0, 0)
395 do_query(query
, 'END TRANSACTION')
397 print datetime
.datetime
.today(), "Adding indexes"
398 if perf_db_export_calls
:
399 do_query(query
, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
401 if (unhandled_count
):
402 print datetime
.datetime
.today(), "Warning: ", unhandled_count
, " unhandled events"
403 print datetime
.datetime
.today(), "Done"
405 def trace_unhandled(event_name
, context
, event_fields_dict
):
406 global unhandled_count
409 def sched__sched_switch(*x
):
412 def bind_exec(q
, n
, x
):
414 q
.addBindValue(str(xx
))
418 bind_exec(evsel_query
, 2, x
)
420 def machine_table(*x
):
421 bind_exec(machine_query
, 3, x
)
423 def thread_table(*x
):
424 bind_exec(thread_query
, 5, x
)
427 bind_exec(comm_query
, 2, x
)
429 def comm_thread_table(*x
):
430 bind_exec(comm_thread_query
, 3, x
)
433 bind_exec(dso_query
, 5, x
)
435 def symbol_table(*x
):
436 bind_exec(symbol_query
, 6, x
)
438 def branch_type_table(*x
):
439 bind_exec(branch_type_query
, 2, x
)
441 def sample_table(*x
):
444 sample_query
.addBindValue(str(xx
))
446 sample_query
.addBindValue(str(xx
))
447 do_query_(sample_query
)
449 bind_exec(sample_query
, 22, x
)
451 def call_path_table(*x
):
452 bind_exec(call_path_query
, 4, x
)
454 def call_return_table(*x
):
455 bind_exec(call_query
, 11, x
)