export-to-postgresql.py 26 KB

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