export-to-postgresql.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. import os
  13. import sys
  14. import struct
  15. import datetime
  16. # To use this script you will need to have installed package python-pyside which
  17. # provides LGPL-licensed Python bindings for Qt. You will also need the package
  18. # libqt4-sql-psql for Qt postgresql support.
  19. #
  20. # The script assumes postgresql is running on the local machine and that the
  21. # user has postgresql permissions to create databases. Examples of installing
  22. # postgresql and adding such a user are:
  23. #
  24. # fedora:
  25. #
  26. # $ sudo yum install postgresql postgresql-server python-pyside qt-postgresql
  27. # $ sudo su - postgres -c initdb
  28. # $ sudo service postgresql start
  29. # $ sudo su - postgres
  30. # $ createuser <your user id here>
  31. # Shall the new role be a superuser? (y/n) y
  32. #
  33. # ubuntu:
  34. #
  35. # $ sudo apt-get install postgresql
  36. # $ sudo su - postgres
  37. # $ createuser <your user id here>
  38. # Shall the new role be a superuser? (y/n) y
  39. #
  40. # An example of using this script with Intel PT:
  41. #
  42. # $ perf record -e intel_pt//u ls
  43. # $ perf script -s ~/libexec/perf-core/scripts/python/export-to-postgresql.py pt_example branches calls
  44. # 2015-05-29 12:49:23.464364 Creating database...
  45. # 2015-05-29 12:49:26.281717 Writing to intermediate files...
  46. # 2015-05-29 12:49:27.190383 Copying to database...
  47. # 2015-05-29 12:49:28.140451 Removing intermediate files...
  48. # 2015-05-29 12:49:28.147451 Adding primary keys
  49. # 2015-05-29 12:49:28.655683 Adding foreign keys
  50. # 2015-05-29 12:49:29.365350 Done
  51. #
  52. # To browse the database, psql can be used e.g.
  53. #
  54. # $ psql pt_example
  55. # pt_example=# select * from samples_view where id < 100;
  56. # pt_example=# \d+
  57. # pt_example=# \d+ samples_view
  58. # pt_example=# \q
  59. #
  60. # An example of using the database is provided by the script
  61. # call-graph-from-postgresql.py. Refer to that script for details.
  62. #
  63. # Tables:
  64. #
  65. # The tables largely correspond to perf tools' data structures. They are largely self-explanatory.
  66. #
  67. # samples
  68. #
  69. # 'samples' is the main table. It represents what instruction was executing at a point in time
  70. # when something (a selected event) happened. The memory address is the instruction pointer or 'ip'.
  71. #
  72. # calls
  73. #
  74. # 'calls' represents function calls and is related to 'samples' by 'call_id' and 'return_id'.
  75. # 'calls' is only created when the 'calls' option to this script is specified.
  76. #
  77. # call_paths
  78. #
  79. # 'call_paths' represents all the call stacks. Each 'call' has an associated record in 'call_paths'.
  80. # 'calls_paths' is only created when the 'calls' option to this script is specified.
  81. #
  82. # branch_types
  83. #
  84. # 'branch_types' provides descriptions for each type of branch.
  85. #
  86. # comm_threads
  87. #
  88. # 'comm_threads' shows how 'comms' relates to 'threads'.
  89. #
  90. # comms
  91. #
  92. # 'comms' contains a record for each 'comm' - the name given to the executable that is running.
  93. #
  94. # dsos
  95. #
  96. # 'dsos' contains a record for each executable file or library.
  97. #
  98. # machines
  99. #
  100. # 'machines' can be used to distinguish virtual machines if virtualization is supported.
  101. #
  102. # selected_events
  103. #
  104. # 'selected_events' contains a record for each kind of event that has been sampled.
  105. #
  106. # symbols
  107. #
  108. # 'symbols' contains a record for each symbol. Only symbols that have samples are present.
  109. #
  110. # threads
  111. #
  112. # 'threads' contains a record for each thread.
  113. #
  114. # Views:
  115. #
  116. # Most of the tables have views for more friendly display. The views are:
  117. #
  118. # calls_view
  119. # call_paths_view
  120. # comm_threads_view
  121. # dsos_view
  122. # machines_view
  123. # samples_view
  124. # symbols_view
  125. # threads_view
  126. #
  127. # More examples of browsing the database with psql:
  128. # Note that some of the examples are not the most optimal SQL query.
  129. # Note that call information is only available if the script's 'calls' option has been used.
  130. #
  131. # Top 10 function calls (not aggregated by symbol):
  132. #
  133. # SELECT * FROM calls_view ORDER BY elapsed_time DESC LIMIT 10;
  134. #
  135. # Top 10 function calls (aggregated by symbol):
  136. #
  137. # SELECT symbol_id,(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,
  138. # SUM(elapsed_time) AS tot_elapsed_time,SUM(branch_count) AS tot_branch_count
  139. # FROM calls_view GROUP BY symbol_id ORDER BY tot_elapsed_time DESC LIMIT 10;
  140. #
  141. # Note that the branch count gives a rough estimation of cpu usage, so functions
  142. # that took a long time but have a relatively low branch count must have spent time
  143. # waiting.
  144. #
  145. # Find symbols by pattern matching on part of the name (e.g. names containing 'alloc'):
  146. #
  147. # SELECT * FROM symbols_view WHERE name LIKE '%alloc%';
  148. #
  149. # Top 10 function calls for a specific symbol (e.g. whose symbol_id is 187):
  150. #
  151. # SELECT * FROM calls_view WHERE symbol_id = 187 ORDER BY elapsed_time DESC LIMIT 10;
  152. #
  153. # Show function calls made by function in the same context (i.e. same call path) (e.g. one with call_path_id 254):
  154. #
  155. # SELECT * FROM calls_view WHERE parent_call_path_id = 254;
  156. #
  157. # Show branches made during a function call (e.g. where call_id is 29357 and return_id is 29370 and tid is 29670)
  158. #
  159. # SELECT * FROM samples_view WHERE id >= 29357 AND id <= 29370 AND tid = 29670 AND event LIKE 'branches%';
  160. #
  161. # Show transactions:
  162. #
  163. # SELECT * FROM samples_view WHERE event = 'transactions';
  164. #
  165. # Note transaction start has 'in_tx' true whereas, transaction end has 'in_tx' false.
  166. # Transaction aborts have branch_type_name 'transaction abort'
  167. #
  168. # Show transaction aborts:
  169. #
  170. # SELECT * FROM samples_view WHERE event = 'transactions' AND branch_type_name = 'transaction abort';
  171. #
  172. # To print a call stack requires walking the call_paths table. For example this python script:
  173. # #!/usr/bin/python2
  174. #
  175. # import sys
  176. # from PySide.QtSql import *
  177. #
  178. # if __name__ == '__main__':
  179. # if (len(sys.argv) < 3):
  180. # print >> sys.stderr, "Usage is: printcallstack.py <database name> <call_path_id>"
  181. # raise Exception("Too few arguments")
  182. # dbname = sys.argv[1]
  183. # call_path_id = sys.argv[2]
  184. # db = QSqlDatabase.addDatabase('QPSQL')
  185. # db.setDatabaseName(dbname)
  186. # if not db.open():
  187. # raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text())
  188. # query = QSqlQuery(db)
  189. # print " id ip symbol_id symbol dso_id dso_short_name"
  190. # while call_path_id != 0 and call_path_id != 1:
  191. # ret = query.exec_('SELECT * FROM call_paths_view WHERE id = ' + str(call_path_id))
  192. # if not ret:
  193. # raise Exception("Query failed: " + query.lastError().text())
  194. # if not query.next():
  195. # raise Exception("Query failed")
  196. # 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))
  197. # call_path_id = query.value(6)
  198. from PySide.QtSql import *
  199. # Need to access PostgreSQL C library directly to use COPY FROM STDIN
  200. from ctypes import *
  201. libpq = CDLL("libpq.so.5")
  202. PQconnectdb = libpq.PQconnectdb
  203. PQconnectdb.restype = c_void_p
  204. PQfinish = libpq.PQfinish
  205. PQstatus = libpq.PQstatus
  206. PQexec = libpq.PQexec
  207. PQexec.restype = c_void_p
  208. PQresultStatus = libpq.PQresultStatus
  209. PQputCopyData = libpq.PQputCopyData
  210. PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
  211. PQputCopyEnd = libpq.PQputCopyEnd
  212. PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
  213. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  214. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  215. # These perf imports are not used at present
  216. #from perf_trace_context import *
  217. #from Core import *
  218. perf_db_export_mode = True
  219. perf_db_export_calls = False
  220. def usage():
  221. print >> sys.stderr, "Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>]"
  222. print >> sys.stderr, "where: columns 'all' or 'branches'"
  223. print >> sys.stderr, " calls 'calls' => create calls table"
  224. raise Exception("Too few arguments")
  225. if (len(sys.argv) < 2):
  226. usage()
  227. dbname = sys.argv[1]
  228. if (len(sys.argv) >= 3):
  229. columns = sys.argv[2]
  230. else:
  231. columns = "all"
  232. if columns not in ("all", "branches"):
  233. usage()
  234. branches = (columns == "branches")
  235. if (len(sys.argv) >= 4):
  236. if (sys.argv[3] == "calls"):
  237. perf_db_export_calls = True
  238. else:
  239. usage()
  240. output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
  241. os.mkdir(output_dir_name)
  242. def do_query(q, s):
  243. if (q.exec_(s)):
  244. return
  245. raise Exception("Query failed: " + q.lastError().text())
  246. print datetime.datetime.today(), "Creating database..."
  247. db = QSqlDatabase.addDatabase('QPSQL')
  248. query = QSqlQuery(db)
  249. db.setDatabaseName('postgres')
  250. db.open()
  251. try:
  252. do_query(query, 'CREATE DATABASE ' + dbname)
  253. except:
  254. os.rmdir(output_dir_name)
  255. raise
  256. query.finish()
  257. query.clear()
  258. db.close()
  259. db.setDatabaseName(dbname)
  260. db.open()
  261. query = QSqlQuery(db)
  262. do_query(query, 'SET client_min_messages TO WARNING')
  263. do_query(query, 'CREATE TABLE selected_events ('
  264. 'id bigint NOT NULL,'
  265. 'name varchar(80))')
  266. do_query(query, 'CREATE TABLE machines ('
  267. 'id bigint NOT NULL,'
  268. 'pid integer,'
  269. 'root_dir varchar(4096))')
  270. do_query(query, 'CREATE TABLE threads ('
  271. 'id bigint NOT NULL,'
  272. 'machine_id bigint,'
  273. 'process_id bigint,'
  274. 'pid integer,'
  275. 'tid integer)')
  276. do_query(query, 'CREATE TABLE comms ('
  277. 'id bigint NOT NULL,'
  278. 'comm varchar(16))')
  279. do_query(query, 'CREATE TABLE comm_threads ('
  280. 'id bigint NOT NULL,'
  281. 'comm_id bigint,'
  282. 'thread_id bigint)')
  283. do_query(query, 'CREATE TABLE dsos ('
  284. 'id bigint NOT NULL,'
  285. 'machine_id bigint,'
  286. 'short_name varchar(256),'
  287. 'long_name varchar(4096),'
  288. 'build_id varchar(64))')
  289. do_query(query, 'CREATE TABLE symbols ('
  290. 'id bigint NOT NULL,'
  291. 'dso_id bigint,'
  292. 'sym_start bigint,'
  293. 'sym_end bigint,'
  294. 'binding integer,'
  295. 'name varchar(2048))')
  296. do_query(query, 'CREATE TABLE branch_types ('
  297. 'id integer NOT NULL,'
  298. 'name varchar(80))')
  299. if branches:
  300. do_query(query, 'CREATE TABLE samples ('
  301. 'id bigint NOT NULL,'
  302. 'evsel_id bigint,'
  303. 'machine_id bigint,'
  304. 'thread_id bigint,'
  305. 'comm_id bigint,'
  306. 'dso_id bigint,'
  307. 'symbol_id bigint,'
  308. 'sym_offset bigint,'
  309. 'ip bigint,'
  310. 'time bigint,'
  311. 'cpu integer,'
  312. 'to_dso_id bigint,'
  313. 'to_symbol_id bigint,'
  314. 'to_sym_offset bigint,'
  315. 'to_ip bigint,'
  316. 'branch_type integer,'
  317. 'in_tx boolean)')
  318. else:
  319. do_query(query, 'CREATE TABLE samples ('
  320. 'id bigint NOT NULL,'
  321. 'evsel_id bigint,'
  322. 'machine_id bigint,'
  323. 'thread_id bigint,'
  324. 'comm_id bigint,'
  325. 'dso_id bigint,'
  326. 'symbol_id bigint,'
  327. 'sym_offset bigint,'
  328. 'ip bigint,'
  329. 'time bigint,'
  330. 'cpu integer,'
  331. 'to_dso_id bigint,'
  332. 'to_symbol_id bigint,'
  333. 'to_sym_offset bigint,'
  334. 'to_ip bigint,'
  335. 'period bigint,'
  336. 'weight bigint,'
  337. 'transaction bigint,'
  338. 'data_src bigint,'
  339. 'branch_type integer,'
  340. 'in_tx boolean)')
  341. if perf_db_export_calls:
  342. do_query(query, 'CREATE TABLE call_paths ('
  343. 'id bigint NOT NULL,'
  344. 'parent_id bigint,'
  345. 'symbol_id bigint,'
  346. 'ip bigint)')
  347. do_query(query, 'CREATE TABLE calls ('
  348. 'id bigint NOT NULL,'
  349. 'thread_id bigint,'
  350. 'comm_id bigint,'
  351. 'call_path_id bigint,'
  352. 'call_time bigint,'
  353. 'return_time bigint,'
  354. 'branch_count bigint,'
  355. 'call_id bigint,'
  356. 'return_id bigint,'
  357. 'parent_call_path_id bigint,'
  358. 'flags integer)')
  359. do_query(query, 'CREATE VIEW machines_view AS '
  360. 'SELECT '
  361. 'id,'
  362. 'pid,'
  363. 'root_dir,'
  364. 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
  365. ' FROM machines')
  366. do_query(query, 'CREATE VIEW dsos_view AS '
  367. 'SELECT '
  368. 'id,'
  369. 'machine_id,'
  370. '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
  371. 'short_name,'
  372. 'long_name,'
  373. 'build_id'
  374. ' FROM dsos')
  375. do_query(query, 'CREATE VIEW symbols_view AS '
  376. 'SELECT '
  377. 'id,'
  378. 'name,'
  379. '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
  380. 'dso_id,'
  381. 'sym_start,'
  382. 'sym_end,'
  383. 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
  384. ' FROM symbols')
  385. do_query(query, 'CREATE VIEW threads_view AS '
  386. 'SELECT '
  387. 'id,'
  388. 'machine_id,'
  389. '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
  390. 'process_id,'
  391. 'pid,'
  392. 'tid'
  393. ' FROM threads')
  394. do_query(query, 'CREATE VIEW comm_threads_view AS '
  395. 'SELECT '
  396. 'comm_id,'
  397. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  398. 'thread_id,'
  399. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  400. '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
  401. ' FROM comm_threads')
  402. if perf_db_export_calls:
  403. do_query(query, 'CREATE VIEW call_paths_view AS '
  404. 'SELECT '
  405. 'c.id,'
  406. 'to_hex(c.ip) AS ip,'
  407. 'c.symbol_id,'
  408. '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
  409. '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
  410. '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
  411. 'c.parent_id,'
  412. 'to_hex(p.ip) AS parent_ip,'
  413. 'p.symbol_id AS parent_symbol_id,'
  414. '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
  415. '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
  416. '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
  417. ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
  418. do_query(query, 'CREATE VIEW calls_view AS '
  419. 'SELECT '
  420. 'calls.id,'
  421. 'thread_id,'
  422. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  423. '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
  424. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  425. 'call_path_id,'
  426. 'to_hex(ip) AS ip,'
  427. 'symbol_id,'
  428. '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
  429. 'call_time,'
  430. 'return_time,'
  431. 'return_time - call_time AS elapsed_time,'
  432. 'branch_count,'
  433. 'call_id,'
  434. 'return_id,'
  435. 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,'
  436. 'parent_call_path_id'
  437. ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
  438. do_query(query, 'CREATE VIEW samples_view AS '
  439. 'SELECT '
  440. 'id,'
  441. 'time,'
  442. 'cpu,'
  443. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  444. '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
  445. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  446. '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
  447. 'to_hex(ip) AS ip_hex,'
  448. '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
  449. 'sym_offset,'
  450. '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
  451. 'to_hex(to_ip) AS to_ip_hex,'
  452. '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
  453. 'to_sym_offset,'
  454. '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
  455. '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
  456. 'in_tx'
  457. ' FROM samples')
  458. file_header = struct.pack("!11sii", "PGCOPY\n\377\r\n\0", 0, 0)
  459. file_trailer = "\377\377"
  460. def open_output_file(file_name):
  461. path_name = output_dir_name + "/" + file_name
  462. file = open(path_name, "w+")
  463. file.write(file_header)
  464. return file
  465. def close_output_file(file):
  466. file.write(file_trailer)
  467. file.close()
  468. def copy_output_file_direct(file, table_name):
  469. close_output_file(file)
  470. sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')"
  471. do_query(query, sql)
  472. # Use COPY FROM STDIN because security may prevent postgres from accessing the files directly
  473. def copy_output_file(file, table_name):
  474. conn = PQconnectdb("dbname = " + dbname)
  475. if (PQstatus(conn)):
  476. raise Exception("COPY FROM STDIN PQconnectdb failed")
  477. file.write(file_trailer)
  478. file.seek(0)
  479. sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')"
  480. res = PQexec(conn, sql)
  481. if (PQresultStatus(res) != 4):
  482. raise Exception("COPY FROM STDIN PQexec failed")
  483. data = file.read(65536)
  484. while (len(data)):
  485. ret = PQputCopyData(conn, data, len(data))
  486. if (ret != 1):
  487. raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret))
  488. data = file.read(65536)
  489. ret = PQputCopyEnd(conn, None)
  490. if (ret != 1):
  491. raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret))
  492. PQfinish(conn)
  493. def remove_output_file(file):
  494. name = file.name
  495. file.close()
  496. os.unlink(name)
  497. evsel_file = open_output_file("evsel_table.bin")
  498. machine_file = open_output_file("machine_table.bin")
  499. thread_file = open_output_file("thread_table.bin")
  500. comm_file = open_output_file("comm_table.bin")
  501. comm_thread_file = open_output_file("comm_thread_table.bin")
  502. dso_file = open_output_file("dso_table.bin")
  503. symbol_file = open_output_file("symbol_table.bin")
  504. branch_type_file = open_output_file("branch_type_table.bin")
  505. sample_file = open_output_file("sample_table.bin")
  506. if perf_db_export_calls:
  507. call_path_file = open_output_file("call_path_table.bin")
  508. call_file = open_output_file("call_table.bin")
  509. def trace_begin():
  510. print datetime.datetime.today(), "Writing to intermediate files..."
  511. # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
  512. evsel_table(0, "unknown")
  513. machine_table(0, 0, "unknown")
  514. thread_table(0, 0, 0, -1, -1)
  515. comm_table(0, "unknown")
  516. dso_table(0, 0, "unknown", "unknown", "")
  517. symbol_table(0, 0, 0, 0, 0, "unknown")
  518. sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  519. if perf_db_export_calls:
  520. call_path_table(0, 0, 0, 0)
  521. unhandled_count = 0
  522. def trace_end():
  523. print datetime.datetime.today(), "Copying to database..."
  524. copy_output_file(evsel_file, "selected_events")
  525. copy_output_file(machine_file, "machines")
  526. copy_output_file(thread_file, "threads")
  527. copy_output_file(comm_file, "comms")
  528. copy_output_file(comm_thread_file, "comm_threads")
  529. copy_output_file(dso_file, "dsos")
  530. copy_output_file(symbol_file, "symbols")
  531. copy_output_file(branch_type_file, "branch_types")
  532. copy_output_file(sample_file, "samples")
  533. if perf_db_export_calls:
  534. copy_output_file(call_path_file, "call_paths")
  535. copy_output_file(call_file, "calls")
  536. print datetime.datetime.today(), "Removing intermediate files..."
  537. remove_output_file(evsel_file)
  538. remove_output_file(machine_file)
  539. remove_output_file(thread_file)
  540. remove_output_file(comm_file)
  541. remove_output_file(comm_thread_file)
  542. remove_output_file(dso_file)
  543. remove_output_file(symbol_file)
  544. remove_output_file(branch_type_file)
  545. remove_output_file(sample_file)
  546. if perf_db_export_calls:
  547. remove_output_file(call_path_file)
  548. remove_output_file(call_file)
  549. os.rmdir(output_dir_name)
  550. print datetime.datetime.today(), "Adding primary keys"
  551. do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
  552. do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
  553. do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
  554. do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)')
  555. do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)')
  556. do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)')
  557. do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)')
  558. do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)')
  559. do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)')
  560. if perf_db_export_calls:
  561. do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)')
  562. do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
  563. print datetime.datetime.today(), "Adding foreign keys"
  564. do_query(query, 'ALTER TABLE threads '
  565. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
  566. 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
  567. do_query(query, 'ALTER TABLE comm_threads '
  568. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  569. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)')
  570. do_query(query, 'ALTER TABLE dsos '
  571. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)')
  572. do_query(query, 'ALTER TABLE symbols '
  573. 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)')
  574. do_query(query, 'ALTER TABLE samples '
  575. 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),'
  576. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
  577. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
  578. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  579. 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),'
  580. 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),'
  581. 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),'
  582. 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)')
  583. if perf_db_export_calls:
  584. do_query(query, 'ALTER TABLE call_paths '
  585. 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),'
  586. 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)')
  587. do_query(query, 'ALTER TABLE calls '
  588. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
  589. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  590. 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),'
  591. 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),'
  592. 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),'
  593. 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)')
  594. do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
  595. if (unhandled_count):
  596. print datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events"
  597. print datetime.datetime.today(), "Done"
  598. def trace_unhandled(event_name, context, event_fields_dict):
  599. global unhandled_count
  600. unhandled_count += 1
  601. def sched__sched_switch(*x):
  602. pass
  603. def evsel_table(evsel_id, evsel_name, *x):
  604. n = len(evsel_name)
  605. fmt = "!hiqi" + str(n) + "s"
  606. value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name)
  607. evsel_file.write(value)
  608. def machine_table(machine_id, pid, root_dir, *x):
  609. n = len(root_dir)
  610. fmt = "!hiqiii" + str(n) + "s"
  611. value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir)
  612. machine_file.write(value)
  613. def thread_table(thread_id, machine_id, process_id, pid, tid, *x):
  614. value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid)
  615. thread_file.write(value)
  616. def comm_table(comm_id, comm_str, *x):
  617. n = len(comm_str)
  618. fmt = "!hiqi" + str(n) + "s"
  619. value = struct.pack(fmt, 2, 8, comm_id, n, comm_str)
  620. comm_file.write(value)
  621. def comm_thread_table(comm_thread_id, comm_id, thread_id, *x):
  622. fmt = "!hiqiqiq"
  623. value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id)
  624. comm_thread_file.write(value)
  625. def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x):
  626. n1 = len(short_name)
  627. n2 = len(long_name)
  628. n3 = len(build_id)
  629. fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s"
  630. value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id)
  631. dso_file.write(value)
  632. def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x):
  633. n = len(symbol_name)
  634. fmt = "!hiqiqiqiqiii" + str(n) + "s"
  635. value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name)
  636. symbol_file.write(value)
  637. def branch_type_table(branch_type, name, *x):
  638. n = len(name)
  639. fmt = "!hiii" + str(n) + "s"
  640. value = struct.pack(fmt, 2, 4, branch_type, n, name)
  641. branch_type_file.write(value)
  642. def 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, *x):
  643. if branches:
  644. value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiiiB", 17, 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)
  645. else:
  646. value = struct.pack("!hiqiqiqiqiqiqiqiqiqiqiiiqiqiqiqiqiqiqiqiiiB", 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, 8, period, 8, weight, 8, transaction, 8, data_src, 4, branch_type, 1, in_tx)
  647. sample_file.write(value)
  648. def call_path_table(cp_id, parent_id, symbol_id, ip, *x):
  649. fmt = "!hiqiqiqiq"
  650. value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip)
  651. call_path_file.write(value)
  652. def 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):
  653. fmt = "!hiqiqiqiqiqiqiqiqiqiqii"
  654. 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)
  655. call_file.write(value)