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))') 357do_query(query, 'CREATE TABLE comm_threads (' 358 'id bigint NOT NULL,' 359 'comm_id bigint,' 360 'thread_id bigint)') 361do_query(query, 'CREATE TABLE dsos (' 362 'id bigint NOT NULL,' 363 'machine_id bigint,' 364 'short_name varchar(256),' 365 'long_name varchar(4096),' 366 'build_id varchar(64))') 367do_query(query, 'CREATE TABLE symbols (' 368 'id bigint NOT NULL,' 369 'dso_id bigint,' 370 'sym_start bigint,' 371 'sym_end bigint,' 372 'binding integer,' 373 'name varchar(2048))') 374do_query(query, 'CREATE TABLE branch_types (' 375 'id integer NOT NULL,' 376 'name varchar(80))') 377 378if branches: 379 do_query(query, 'CREATE TABLE samples (' 380 'id bigint NOT NULL,' 381 'evsel_id bigint,' 382 'machine_id bigint,' 383 'thread_id bigint,' 384 'comm_id bigint,' 385 'dso_id bigint,' 386 'symbol_id bigint,' 387 'sym_offset bigint,' 388 'ip bigint,' 389 'time bigint,' 390 'cpu integer,' 391 'to_dso_id bigint,' 392 'to_symbol_id bigint,' 393 'to_sym_offset bigint,' 394 'to_ip bigint,' 395 'branch_type integer,' 396 'in_tx boolean,' 397 'call_path_id bigint,' 398 'insn_count bigint,' 399 'cyc_count bigint)') 400else: 401 do_query(query, 'CREATE TABLE samples (' 402 'id bigint NOT NULL,' 403 'evsel_id bigint,' 404 'machine_id bigint,' 405 'thread_id bigint,' 406 'comm_id bigint,' 407 'dso_id bigint,' 408 'symbol_id bigint,' 409 'sym_offset bigint,' 410 'ip bigint,' 411 'time bigint,' 412 'cpu integer,' 413 'to_dso_id bigint,' 414 'to_symbol_id bigint,' 415 'to_sym_offset bigint,' 416 'to_ip bigint,' 417 'period bigint,' 418 'weight bigint,' 419 'transaction bigint,' 420 'data_src bigint,' 421 'branch_type integer,' 422 'in_tx boolean,' 423 'call_path_id bigint,' 424 'insn_count bigint,' 425 'cyc_count bigint)') 426 427if perf_db_export_calls or perf_db_export_callchains: 428 do_query(query, 'CREATE TABLE call_paths (' 429 'id bigint NOT NULL,' 430 'parent_id bigint,' 431 'symbol_id bigint,' 432 'ip bigint)') 433if perf_db_export_calls: 434 do_query(query, 'CREATE TABLE calls (' 435 'id bigint NOT NULL,' 436 'thread_id bigint,' 437 'comm_id bigint,' 438 'call_path_id bigint,' 439 'call_time bigint,' 440 'return_time bigint,' 441 'branch_count bigint,' 442 'call_id bigint,' 443 'return_id bigint,' 444 'parent_call_path_id bigint,' 445 'flags integer,' 446 'parent_id bigint,' 447 'insn_count bigint,' 448 'cyc_count bigint)') 449 450do_query(query, 'CREATE VIEW machines_view AS ' 451 'SELECT ' 452 'id,' 453 'pid,' 454 'root_dir,' 455 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest' 456 ' FROM machines') 457 458do_query(query, 'CREATE VIEW dsos_view AS ' 459 'SELECT ' 460 'id,' 461 'machine_id,' 462 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,' 463 'short_name,' 464 'long_name,' 465 'build_id' 466 ' FROM dsos') 467 468do_query(query, 'CREATE VIEW symbols_view AS ' 469 'SELECT ' 470 'id,' 471 'name,' 472 '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,' 473 'dso_id,' 474 'sym_start,' 475 'sym_end,' 476 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding' 477 ' FROM symbols') 478 479do_query(query, 'CREATE VIEW threads_view AS ' 480 'SELECT ' 481 'id,' 482 'machine_id,' 483 '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,' 484 'process_id,' 485 'pid,' 486 'tid' 487 ' FROM threads') 488 489do_query(query, 'CREATE VIEW comm_threads_view AS ' 490 'SELECT ' 491 'comm_id,' 492 '(SELECT comm FROM comms WHERE id = comm_id) AS command,' 493 'thread_id,' 494 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,' 495 '(SELECT tid FROM threads WHERE id = thread_id) AS tid' 496 ' FROM comm_threads') 497 498if perf_db_export_calls or perf_db_export_callchains: 499 do_query(query, 'CREATE VIEW call_paths_view AS ' 500 'SELECT ' 501 'c.id,' 502 'to_hex(c.ip) AS ip,' 503 'c.symbol_id,' 504 '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,' 505 '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,' 506 '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,' 507 'c.parent_id,' 508 'to_hex(p.ip) AS parent_ip,' 509 'p.symbol_id AS parent_symbol_id,' 510 '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,' 511 '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,' 512 '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name' 513 ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id') 514if perf_db_export_calls: 515 do_query(query, 'CREATE VIEW calls_view AS ' 516 'SELECT ' 517 'calls.id,' 518 'thread_id,' 519 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,' 520 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,' 521 '(SELECT comm FROM comms WHERE id = comm_id) AS command,' 522 'call_path_id,' 523 'to_hex(ip) AS ip,' 524 'symbol_id,' 525 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,' 526 'call_time,' 527 'return_time,' 528 'return_time - call_time AS elapsed_time,' 529 'branch_count,' 530 'insn_count,' 531 'cyc_count,' 532 '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,' 533 'call_id,' 534 'return_id,' 535 '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,' 536 'parent_call_path_id,' 537 'calls.parent_id' 538 ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id') 539 540do_query(query, 'CREATE VIEW samples_view AS ' 541 'SELECT ' 542 'id,' 543 'time,' 544 'cpu,' 545 '(SELECT pid FROM threads WHERE id = thread_id) AS pid,' 546 '(SELECT tid FROM threads WHERE id = thread_id) AS tid,' 547 '(SELECT comm FROM comms WHERE id = comm_id) AS command,' 548 '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,' 549 'to_hex(ip) AS ip_hex,' 550 '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,' 551 'sym_offset,' 552 '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,' 553 'to_hex(to_ip) AS to_ip_hex,' 554 '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,' 555 'to_sym_offset,' 556 '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,' 557 '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,' 558 'in_tx,' 559 'insn_count,' 560 'cyc_count,' 561 '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' 562 ' FROM samples') 563 564 565file_header = struct.pack("!11sii", b"PGCOPY\n\377\r\n\0", 0, 0) 566file_trailer = b"\377\377" 567 568def open_output_file(file_name): 569 path_name = output_dir_name + "/" + file_name 570 file = open(path_name, "wb+") 571 file.write(file_header) 572 return file 573 574def close_output_file(file): 575 file.write(file_trailer) 576 file.close() 577 578def copy_output_file_direct(file, table_name): 579 close_output_file(file) 580 sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')" 581 do_query(query, sql) 582 583# Use COPY FROM STDIN because security may prevent postgres from accessing the files directly 584def copy_output_file(file, table_name): 585 conn = PQconnectdb(toclientstr("dbname = " + dbname)) 586 if (PQstatus(conn)): 587 raise Exception("COPY FROM STDIN PQconnectdb failed") 588 file.write(file_trailer) 589 file.seek(0) 590 sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')" 591 res = PQexec(conn, toclientstr(sql)) 592 if (PQresultStatus(res) != 4): 593 raise Exception("COPY FROM STDIN PQexec failed") 594 data = file.read(65536) 595 while (len(data)): 596 ret = PQputCopyData(conn, data, len(data)) 597 if (ret != 1): 598 raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret)) 599 data = file.read(65536) 600 ret = PQputCopyEnd(conn, None) 601 if (ret != 1): 602 raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret)) 603 PQfinish(conn) 604 605def remove_output_file(file): 606 name = file.name 607 file.close() 608 os.unlink(name) 609 610evsel_file = open_output_file("evsel_table.bin") 611machine_file = open_output_file("machine_table.bin") 612thread_file = open_output_file("thread_table.bin") 613comm_file = open_output_file("comm_table.bin") 614comm_thread_file = open_output_file("comm_thread_table.bin") 615dso_file = open_output_file("dso_table.bin") 616symbol_file = open_output_file("symbol_table.bin") 617branch_type_file = open_output_file("branch_type_table.bin") 618sample_file = open_output_file("sample_table.bin") 619if perf_db_export_calls or perf_db_export_callchains: 620 call_path_file = open_output_file("call_path_table.bin") 621if perf_db_export_calls: 622 call_file = open_output_file("call_table.bin") 623 624def trace_begin(): 625 printdate("Writing to intermediate files...") 626 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs 627 evsel_table(0, "unknown") 628 machine_table(0, 0, "unknown") 629 thread_table(0, 0, 0, -1, -1) 630 comm_table(0, "unknown") 631 dso_table(0, 0, "unknown", "unknown", "") 632 symbol_table(0, 0, 0, 0, 0, "unknown") 633 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) 634 if perf_db_export_calls or perf_db_export_callchains: 635 call_path_table(0, 0, 0, 0) 636 call_return_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 637 638unhandled_count = 0 639 640def trace_end(): 641 printdate("Copying to database...") 642 copy_output_file(evsel_file, "selected_events") 643 copy_output_file(machine_file, "machines") 644 copy_output_file(thread_file, "threads") 645 copy_output_file(comm_file, "comms") 646 copy_output_file(comm_thread_file, "comm_threads") 647 copy_output_file(dso_file, "dsos") 648 copy_output_file(symbol_file, "symbols") 649 copy_output_file(branch_type_file, "branch_types") 650 copy_output_file(sample_file, "samples") 651 if perf_db_export_calls or perf_db_export_callchains: 652 copy_output_file(call_path_file, "call_paths") 653 if perf_db_export_calls: 654 copy_output_file(call_file, "calls") 655 656 printdate("Removing intermediate files...") 657 remove_output_file(evsel_file) 658 remove_output_file(machine_file) 659 remove_output_file(thread_file) 660 remove_output_file(comm_file) 661 remove_output_file(comm_thread_file) 662 remove_output_file(dso_file) 663 remove_output_file(symbol_file) 664 remove_output_file(branch_type_file) 665 remove_output_file(sample_file) 666 if perf_db_export_calls or perf_db_export_callchains: 667 remove_output_file(call_path_file) 668 if perf_db_export_calls: 669 remove_output_file(call_file) 670 os.rmdir(output_dir_name) 671 printdate("Adding primary keys") 672 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)') 673 do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)') 674 do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)') 675 do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)') 676 do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)') 677 do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)') 678 do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)') 679 do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)') 680 do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)') 681 if perf_db_export_calls or perf_db_export_callchains: 682 do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)') 683 if perf_db_export_calls: 684 do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)') 685 686 printdate("Adding foreign keys") 687 do_query(query, 'ALTER TABLE threads ' 688 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),' 689 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)') 690 do_query(query, 'ALTER TABLE comm_threads ' 691 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),' 692 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)') 693 do_query(query, 'ALTER TABLE dsos ' 694 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)') 695 do_query(query, 'ALTER TABLE symbols ' 696 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)') 697 do_query(query, 'ALTER TABLE samples ' 698 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),' 699 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),' 700 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),' 701 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),' 702 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),' 703 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),' 704 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),' 705 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)') 706 if perf_db_export_calls or perf_db_export_callchains: 707 do_query(query, 'ALTER TABLE call_paths ' 708 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),' 709 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)') 710 if perf_db_export_calls: 711 do_query(query, 'ALTER TABLE calls ' 712 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),' 713 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),' 714 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),' 715 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),' 716 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),' 717 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)') 718 do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)') 719 do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)') 720 721 if (unhandled_count): 722 printdate("Warning: ", unhandled_count, " unhandled events") 723 printdate("Done") 724 725def trace_unhandled(event_name, context, event_fields_dict): 726 global unhandled_count 727 unhandled_count += 1 728 729def sched__sched_switch(*x): 730 pass 731 732def evsel_table(evsel_id, evsel_name, *x): 733 evsel_name = toserverstr(evsel_name) 734 n = len(evsel_name) 735 fmt = "!hiqi" + str(n) + "s" 736 value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name) 737 evsel_file.write(value) 738 739def machine_table(machine_id, pid, root_dir, *x): 740 root_dir = toserverstr(root_dir) 741 n = len(root_dir) 742 fmt = "!hiqiii" + str(n) + "s" 743 value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir) 744 machine_file.write(value) 745 746def thread_table(thread_id, machine_id, process_id, pid, tid, *x): 747 value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid) 748 thread_file.write(value) 749 750def comm_table(comm_id, comm_str, *x): 751 comm_str = toserverstr(comm_str) 752 n = len(comm_str) 753 fmt = "!hiqi" + str(n) + "s" 754 value = struct.pack(fmt, 2, 8, comm_id, n, comm_str) 755 comm_file.write(value) 756 757def comm_thread_table(comm_thread_id, comm_id, thread_id, *x): 758 fmt = "!hiqiqiq" 759 value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id) 760 comm_thread_file.write(value) 761 762def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x): 763 short_name = toserverstr(short_name) 764 long_name = toserverstr(long_name) 765 build_id = toserverstr(build_id) 766 n1 = len(short_name) 767 n2 = len(long_name) 768 n3 = len(build_id) 769 fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s" 770 value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id) 771 dso_file.write(value) 772 773def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x): 774 symbol_name = toserverstr(symbol_name) 775 n = len(symbol_name) 776 fmt = "!hiqiqiqiqiii" + str(n) + "s" 777 value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name) 778 symbol_file.write(value) 779 780def branch_type_table(branch_type, name, *x): 781 name = toserverstr(name) 782 n = len(name) 783 fmt = "!hiii" + str(n) + "s" 784 value = struct.pack(fmt, 2, 4, branch_type, n, name) 785 branch_type_file.write(value) 786 787def 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, *x): 788 if branches: 789 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiiiBiqiqiq", 20, 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) 790 else: 791 value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiqiqiqiqiiiBiqiqiq", 24, 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) 792 sample_file.write(value) 793 794def call_path_table(cp_id, parent_id, symbol_id, ip, *x): 795 fmt = "!hiqiqiqiq" 796 value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip) 797 call_path_file.write(value) 798 799def 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): 800 fmt = "!hiqiqiqiqiqiqiqiqiqiqiiiqiqiq" 801 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) 802 call_file.write(value) 803