export-to-postgresql.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. from __future__ import print_function
  13. import os
  14. import sys
  15. import struct
  16. import datetime
  17. # To use this script you will need to have installed package python-pyside which
  18. # provides LGPL-licensed Python bindings for Qt. You will also need the package
  19. # libqt4-sql-psql for Qt postgresql support.
  20. #
  21. # The script assumes postgresql is running on the local machine and that the
  22. # user has postgresql permissions to create databases. Examples of installing
  23. # postgresql and adding such a user are:
  24. #
  25. # fedora:
  26. #
  27. # $ sudo yum install postgresql postgresql-server python-pyside qt-postgresql
  28. # $ sudo su - postgres -c initdb
  29. # $ sudo service postgresql start
  30. # $ sudo su - postgres
  31. # $ createuser <your user id here>
  32. # Shall the new role be a superuser? (y/n) y
  33. #
  34. # ubuntu:
  35. #
  36. # $ sudo apt-get install postgresql python-pyside.qtsql libqt4-sql-psql
  37. # $ sudo su - postgres
  38. # $ createuser -s <your user id here>
  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-sql.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. if sys.version_info < (3, 0):
  200. def toserverstr(str):
  201. return str
  202. def toclientstr(str):
  203. return str
  204. else:
  205. # Assume UTF-8 server_encoding and client_encoding
  206. def toserverstr(str):
  207. return bytes(str, "UTF_8")
  208. def toclientstr(str):
  209. return bytes(str, "UTF_8")
  210. # Need to access PostgreSQL C library directly to use COPY FROM STDIN
  211. from ctypes import *
  212. libpq = CDLL("libpq.so.5")
  213. PQconnectdb = libpq.PQconnectdb
  214. PQconnectdb.restype = c_void_p
  215. PQconnectdb.argtypes = [ c_char_p ]
  216. PQfinish = libpq.PQfinish
  217. PQfinish.argtypes = [ c_void_p ]
  218. PQstatus = libpq.PQstatus
  219. PQstatus.restype = c_int
  220. PQstatus.argtypes = [ c_void_p ]
  221. PQexec = libpq.PQexec
  222. PQexec.restype = c_void_p
  223. PQexec.argtypes = [ c_void_p, c_char_p ]
  224. PQresultStatus = libpq.PQresultStatus
  225. PQresultStatus.restype = c_int
  226. PQresultStatus.argtypes = [ c_void_p ]
  227. PQputCopyData = libpq.PQputCopyData
  228. PQputCopyData.restype = c_int
  229. PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
  230. PQputCopyEnd = libpq.PQputCopyEnd
  231. PQputCopyEnd.restype = c_int
  232. PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
  233. sys.path.append(os.environ['PERF_EXEC_PATH'] + \
  234. '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
  235. # These perf imports are not used at present
  236. #from perf_trace_context import *
  237. #from Core import *
  238. perf_db_export_mode = True
  239. perf_db_export_calls = False
  240. perf_db_export_callchains = False
  241. def printerr(*args, **kw_args):
  242. print(*args, file=sys.stderr, **kw_args)
  243. def usage():
  244. printerr("Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>] [<callchains>]")
  245. printerr("where: columns 'all' or 'branches'")
  246. printerr(" calls 'calls' => create calls and call_paths table")
  247. printerr(" callchains 'callchains' => create call_paths table")
  248. raise Exception("Too few arguments")
  249. if (len(sys.argv) < 2):
  250. usage()
  251. dbname = sys.argv[1]
  252. if (len(sys.argv) >= 3):
  253. columns = sys.argv[2]
  254. else:
  255. columns = "all"
  256. if columns not in ("all", "branches"):
  257. usage()
  258. branches = (columns == "branches")
  259. for i in range(3,len(sys.argv)):
  260. if (sys.argv[i] == "calls"):
  261. perf_db_export_calls = True
  262. elif (sys.argv[i] == "callchains"):
  263. perf_db_export_callchains = True
  264. else:
  265. usage()
  266. output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
  267. os.mkdir(output_dir_name)
  268. def do_query(q, s):
  269. if (q.exec_(s)):
  270. return
  271. raise Exception("Query failed: " + q.lastError().text())
  272. print(datetime.datetime.today(), "Creating database...")
  273. db = QSqlDatabase.addDatabase('QPSQL')
  274. query = QSqlQuery(db)
  275. db.setDatabaseName('postgres')
  276. db.open()
  277. try:
  278. do_query(query, 'CREATE DATABASE ' + dbname)
  279. except:
  280. os.rmdir(output_dir_name)
  281. raise
  282. query.finish()
  283. query.clear()
  284. db.close()
  285. db.setDatabaseName(dbname)
  286. db.open()
  287. query = QSqlQuery(db)
  288. do_query(query, 'SET client_min_messages TO WARNING')
  289. do_query(query, 'CREATE TABLE selected_events ('
  290. 'id bigint NOT NULL,'
  291. 'name varchar(80))')
  292. do_query(query, 'CREATE TABLE machines ('
  293. 'id bigint NOT NULL,'
  294. 'pid integer,'
  295. 'root_dir varchar(4096))')
  296. do_query(query, 'CREATE TABLE threads ('
  297. 'id bigint NOT NULL,'
  298. 'machine_id bigint,'
  299. 'process_id bigint,'
  300. 'pid integer,'
  301. 'tid integer)')
  302. do_query(query, 'CREATE TABLE comms ('
  303. 'id bigint NOT NULL,'
  304. 'comm varchar(16))')
  305. do_query(query, 'CREATE TABLE comm_threads ('
  306. 'id bigint NOT NULL,'
  307. 'comm_id bigint,'
  308. 'thread_id bigint)')
  309. do_query(query, 'CREATE TABLE dsos ('
  310. 'id bigint NOT NULL,'
  311. 'machine_id bigint,'
  312. 'short_name varchar(256),'
  313. 'long_name varchar(4096),'
  314. 'build_id varchar(64))')
  315. do_query(query, 'CREATE TABLE symbols ('
  316. 'id bigint NOT NULL,'
  317. 'dso_id bigint,'
  318. 'sym_start bigint,'
  319. 'sym_end bigint,'
  320. 'binding integer,'
  321. 'name varchar(2048))')
  322. do_query(query, 'CREATE TABLE branch_types ('
  323. 'id integer NOT NULL,'
  324. 'name varchar(80))')
  325. if branches:
  326. do_query(query, 'CREATE TABLE samples ('
  327. 'id bigint NOT NULL,'
  328. 'evsel_id bigint,'
  329. 'machine_id bigint,'
  330. 'thread_id bigint,'
  331. 'comm_id bigint,'
  332. 'dso_id bigint,'
  333. 'symbol_id bigint,'
  334. 'sym_offset bigint,'
  335. 'ip bigint,'
  336. 'time bigint,'
  337. 'cpu integer,'
  338. 'to_dso_id bigint,'
  339. 'to_symbol_id bigint,'
  340. 'to_sym_offset bigint,'
  341. 'to_ip bigint,'
  342. 'branch_type integer,'
  343. 'in_tx boolean,'
  344. 'call_path_id bigint)')
  345. else:
  346. do_query(query, 'CREATE TABLE samples ('
  347. 'id bigint NOT NULL,'
  348. 'evsel_id bigint,'
  349. 'machine_id bigint,'
  350. 'thread_id bigint,'
  351. 'comm_id bigint,'
  352. 'dso_id bigint,'
  353. 'symbol_id bigint,'
  354. 'sym_offset bigint,'
  355. 'ip bigint,'
  356. 'time bigint,'
  357. 'cpu integer,'
  358. 'to_dso_id bigint,'
  359. 'to_symbol_id bigint,'
  360. 'to_sym_offset bigint,'
  361. 'to_ip bigint,'
  362. 'period bigint,'
  363. 'weight bigint,'
  364. 'transaction bigint,'
  365. 'data_src bigint,'
  366. 'branch_type integer,'
  367. 'in_tx boolean,'
  368. 'call_path_id bigint)')
  369. if perf_db_export_calls or perf_db_export_callchains:
  370. do_query(query, 'CREATE TABLE call_paths ('
  371. 'id bigint NOT NULL,'
  372. 'parent_id bigint,'
  373. 'symbol_id bigint,'
  374. 'ip bigint)')
  375. if perf_db_export_calls:
  376. do_query(query, 'CREATE TABLE calls ('
  377. 'id bigint NOT NULL,'
  378. 'thread_id bigint,'
  379. 'comm_id bigint,'
  380. 'call_path_id bigint,'
  381. 'call_time bigint,'
  382. 'return_time bigint,'
  383. 'branch_count bigint,'
  384. 'call_id bigint,'
  385. 'return_id bigint,'
  386. 'parent_call_path_id bigint,'
  387. 'flags integer)')
  388. do_query(query, 'CREATE VIEW machines_view AS '
  389. 'SELECT '
  390. 'id,'
  391. 'pid,'
  392. 'root_dir,'
  393. 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
  394. ' FROM machines')
  395. do_query(query, 'CREATE VIEW dsos_view AS '
  396. 'SELECT '
  397. 'id,'
  398. 'machine_id,'
  399. '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
  400. 'short_name,'
  401. 'long_name,'
  402. 'build_id'
  403. ' FROM dsos')
  404. do_query(query, 'CREATE VIEW symbols_view AS '
  405. 'SELECT '
  406. 'id,'
  407. 'name,'
  408. '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
  409. 'dso_id,'
  410. 'sym_start,'
  411. 'sym_end,'
  412. 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
  413. ' FROM symbols')
  414. do_query(query, 'CREATE VIEW threads_view AS '
  415. 'SELECT '
  416. 'id,'
  417. 'machine_id,'
  418. '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
  419. 'process_id,'
  420. 'pid,'
  421. 'tid'
  422. ' FROM threads')
  423. do_query(query, 'CREATE VIEW comm_threads_view AS '
  424. 'SELECT '
  425. 'comm_id,'
  426. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  427. 'thread_id,'
  428. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  429. '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
  430. ' FROM comm_threads')
  431. if perf_db_export_calls or perf_db_export_callchains:
  432. do_query(query, 'CREATE VIEW call_paths_view AS '
  433. 'SELECT '
  434. 'c.id,'
  435. 'to_hex(c.ip) AS ip,'
  436. 'c.symbol_id,'
  437. '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
  438. '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
  439. '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
  440. 'c.parent_id,'
  441. 'to_hex(p.ip) AS parent_ip,'
  442. 'p.symbol_id AS parent_symbol_id,'
  443. '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
  444. '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
  445. '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
  446. ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
  447. if perf_db_export_calls:
  448. do_query(query, 'CREATE VIEW calls_view AS '
  449. 'SELECT '
  450. 'calls.id,'
  451. 'thread_id,'
  452. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  453. '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
  454. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  455. 'call_path_id,'
  456. 'to_hex(ip) AS ip,'
  457. 'symbol_id,'
  458. '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
  459. 'call_time,'
  460. 'return_time,'
  461. 'return_time - call_time AS elapsed_time,'
  462. 'branch_count,'
  463. 'call_id,'
  464. 'return_id,'
  465. 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,'
  466. 'parent_call_path_id'
  467. ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
  468. do_query(query, 'CREATE VIEW samples_view AS '
  469. 'SELECT '
  470. 'id,'
  471. 'time,'
  472. 'cpu,'
  473. '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
  474. '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
  475. '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
  476. '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
  477. 'to_hex(ip) AS ip_hex,'
  478. '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
  479. 'sym_offset,'
  480. '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
  481. 'to_hex(to_ip) AS to_ip_hex,'
  482. '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
  483. 'to_sym_offset,'
  484. '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
  485. '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
  486. 'in_tx'
  487. ' FROM samples')
  488. file_header = struct.pack("!11sii", b"PGCOPY\n\377\r\n\0", 0, 0)
  489. file_trailer = b"\377\377"
  490. def open_output_file(file_name):
  491. path_name = output_dir_name + "/" + file_name
  492. file = open(path_name, "wb+")
  493. file.write(file_header)
  494. return file
  495. def close_output_file(file):
  496. file.write(file_trailer)
  497. file.close()
  498. def copy_output_file_direct(file, table_name):
  499. close_output_file(file)
  500. sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')"
  501. do_query(query, sql)
  502. # Use COPY FROM STDIN because security may prevent postgres from accessing the files directly
  503. def copy_output_file(file, table_name):
  504. conn = PQconnectdb(toclientstr("dbname = " + dbname))
  505. if (PQstatus(conn)):
  506. raise Exception("COPY FROM STDIN PQconnectdb failed")
  507. file.write(file_trailer)
  508. file.seek(0)
  509. sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')"
  510. res = PQexec(conn, toclientstr(sql))
  511. if (PQresultStatus(res) != 4):
  512. raise Exception("COPY FROM STDIN PQexec failed")
  513. data = file.read(65536)
  514. while (len(data)):
  515. ret = PQputCopyData(conn, data, len(data))
  516. if (ret != 1):
  517. raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret))
  518. data = file.read(65536)
  519. ret = PQputCopyEnd(conn, None)
  520. if (ret != 1):
  521. raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret))
  522. PQfinish(conn)
  523. def remove_output_file(file):
  524. name = file.name
  525. file.close()
  526. os.unlink(name)
  527. evsel_file = open_output_file("evsel_table.bin")
  528. machine_file = open_output_file("machine_table.bin")
  529. thread_file = open_output_file("thread_table.bin")
  530. comm_file = open_output_file("comm_table.bin")
  531. comm_thread_file = open_output_file("comm_thread_table.bin")
  532. dso_file = open_output_file("dso_table.bin")
  533. symbol_file = open_output_file("symbol_table.bin")
  534. branch_type_file = open_output_file("branch_type_table.bin")
  535. sample_file = open_output_file("sample_table.bin")
  536. if perf_db_export_calls or perf_db_export_callchains:
  537. call_path_file = open_output_file("call_path_table.bin")
  538. if perf_db_export_calls:
  539. call_file = open_output_file("call_table.bin")
  540. def trace_begin():
  541. print(datetime.datetime.today(), "Writing to intermediate files...")
  542. # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
  543. evsel_table(0, "unknown")
  544. machine_table(0, 0, "unknown")
  545. thread_table(0, 0, 0, -1, -1)
  546. comm_table(0, "unknown")
  547. dso_table(0, 0, "unknown", "unknown", "")
  548. symbol_table(0, 0, 0, 0, 0, "unknown")
  549. sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  550. if perf_db_export_calls or perf_db_export_callchains:
  551. call_path_table(0, 0, 0, 0)
  552. unhandled_count = 0
  553. def trace_end():
  554. print(datetime.datetime.today(), "Copying to database...")
  555. copy_output_file(evsel_file, "selected_events")
  556. copy_output_file(machine_file, "machines")
  557. copy_output_file(thread_file, "threads")
  558. copy_output_file(comm_file, "comms")
  559. copy_output_file(comm_thread_file, "comm_threads")
  560. copy_output_file(dso_file, "dsos")
  561. copy_output_file(symbol_file, "symbols")
  562. copy_output_file(branch_type_file, "branch_types")
  563. copy_output_file(sample_file, "samples")
  564. if perf_db_export_calls or perf_db_export_callchains:
  565. copy_output_file(call_path_file, "call_paths")
  566. if perf_db_export_calls:
  567. copy_output_file(call_file, "calls")
  568. print(datetime.datetime.today(), "Removing intermediate files...")
  569. remove_output_file(evsel_file)
  570. remove_output_file(machine_file)
  571. remove_output_file(thread_file)
  572. remove_output_file(comm_file)
  573. remove_output_file(comm_thread_file)
  574. remove_output_file(dso_file)
  575. remove_output_file(symbol_file)
  576. remove_output_file(branch_type_file)
  577. remove_output_file(sample_file)
  578. if perf_db_export_calls or perf_db_export_callchains:
  579. remove_output_file(call_path_file)
  580. if perf_db_export_calls:
  581. remove_output_file(call_file)
  582. os.rmdir(output_dir_name)
  583. print(datetime.datetime.today(), "Adding primary keys")
  584. do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
  585. do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
  586. do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
  587. do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)')
  588. do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)')
  589. do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)')
  590. do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)')
  591. do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)')
  592. do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)')
  593. if perf_db_export_calls or perf_db_export_callchains:
  594. do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)')
  595. if perf_db_export_calls:
  596. do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
  597. print(datetime.datetime.today(), "Adding foreign keys")
  598. do_query(query, 'ALTER TABLE threads '
  599. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
  600. 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
  601. do_query(query, 'ALTER TABLE comm_threads '
  602. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  603. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)')
  604. do_query(query, 'ALTER TABLE dsos '
  605. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)')
  606. do_query(query, 'ALTER TABLE symbols '
  607. 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)')
  608. do_query(query, 'ALTER TABLE samples '
  609. 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),'
  610. 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
  611. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
  612. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  613. 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),'
  614. 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),'
  615. 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),'
  616. 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)')
  617. if perf_db_export_calls or perf_db_export_callchains:
  618. do_query(query, 'ALTER TABLE call_paths '
  619. 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),'
  620. 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)')
  621. if perf_db_export_calls:
  622. do_query(query, 'ALTER TABLE calls '
  623. 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
  624. 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
  625. 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),'
  626. 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),'
  627. 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),'
  628. 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)')
  629. do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
  630. if (unhandled_count):
  631. print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events")
  632. print(datetime.datetime.today(), "Done")
  633. def trace_unhandled(event_name, context, event_fields_dict):
  634. global unhandled_count
  635. unhandled_count += 1
  636. def sched__sched_switch(*x):
  637. pass
  638. def evsel_table(evsel_id, evsel_name, *x):
  639. evsel_name = toserverstr(evsel_name)
  640. n = len(evsel_name)
  641. fmt = "!hiqi" + str(n) + "s"
  642. value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name)
  643. evsel_file.write(value)
  644. def machine_table(machine_id, pid, root_dir, *x):
  645. root_dir = toserverstr(root_dir)
  646. n = len(root_dir)
  647. fmt = "!hiqiii" + str(n) + "s"
  648. value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir)
  649. machine_file.write(value)
  650. def thread_table(thread_id, machine_id, process_id, pid, tid, *x):
  651. value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid)
  652. thread_file.write(value)
  653. def comm_table(comm_id, comm_str, *x):
  654. comm_str = toserverstr(comm_str)
  655. n = len(comm_str)
  656. fmt = "!hiqi" + str(n) + "s"
  657. value = struct.pack(fmt, 2, 8, comm_id, n, comm_str)
  658. comm_file.write(value)
  659. def comm_thread_table(comm_thread_id, comm_id, thread_id, *x):
  660. fmt = "!hiqiqiq"
  661. value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id)
  662. comm_thread_file.write(value)
  663. def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x):
  664. short_name = toserverstr(short_name)
  665. long_name = toserverstr(long_name)
  666. build_id = toserverstr(build_id)
  667. n1 = len(short_name)
  668. n2 = len(long_name)
  669. n3 = len(build_id)
  670. fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s"
  671. value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id)
  672. dso_file.write(value)
  673. def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x):
  674. symbol_name = toserverstr(symbol_name)
  675. n = len(symbol_name)
  676. fmt = "!hiqiqiqiqiii" + str(n) + "s"
  677. value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name)
  678. symbol_file.write(value)
  679. def branch_type_table(branch_type, name, *x):
  680. name = toserverstr(name)
  681. n = len(name)
  682. fmt = "!hiii" + str(n) + "s"
  683. value = struct.pack(fmt, 2, 4, branch_type, n, name)
  684. branch_type_file.write(value)
  685. 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):
  686. if branches:
  687. 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)
  688. else:
  689. 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)
  690. sample_file.write(value)
  691. def call_path_table(cp_id, parent_id, symbol_id, ip, *x):
  692. fmt = "!hiqiqiqiq"
  693. value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip)
  694. call_path_file.write(value)
  695. 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):
  696. fmt = "!hiqiqiqiqiqiqiqiqiqiqii"
  697. 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)
  698. call_file.write(value)