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