| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 |
|
| 13 | import os
|
| 14 | import sys
|
| 15 | import struct
|
| 16 | import datetime
|
| 17 |
|
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 |
|
| 200 | from PySide.QtSql import *
|
| 201 |
|
| 202 | |
| 203 | from ctypes import *
|
| 204 | libpq = CDLL("libpq.so.5")
|
| 205 | PQconnectdb = libpq.PQconnectdb
|
| 206 | PQconnectdb.restype = c_void_p
|
| 207 | PQfinish = libpq.PQfinish
|
| 208 | PQstatus = libpq.PQstatus
|
| 209 | PQexec = libpq.PQexec
|
| 210 | PQexec.restype = c_void_p
|
| 211 | PQresultStatus = libpq.PQresultStatus
|
| 212 | PQputCopyData = libpq.PQputCopyData
|
| 213 | PQputCopyData.argtypes = [ c_void_p, c_void_p, c_int ]
|
| 214 | PQputCopyEnd = libpq.PQputCopyEnd
|
| 215 | PQputCopyEnd.argtypes = [ c_void_p, c_void_p ]
|
| 216 |
|
| 217 | sys.path.append(os.environ['PERF_EXEC_PATH'] + \
|
| 218 | '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
|
| 219 |
|
| 220 | |
| 221 | |
| 222 | |
| 223 |
|
| 224 | perf_db_export_mode = True
|
| 225 | perf_db_export_calls = False
|
| 226 | perf_db_export_callchains = False
|
| 227 |
|
| 228 |
|
| 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 |
|
| 236 | if (len(sys.argv) < 2):
|
| 237 | usage()
|
| 238 |
|
| 239 | dbname = sys.argv[1]
|
| 240 |
|
| 241 | if (len(sys.argv) >= 3):
|
| 242 | columns = sys.argv[2]
|
| 243 | else:
|
| 244 | columns = "all"
|
| 245 |
|
| 246 | if columns not in ("all", "branches"):
|
| 247 | usage()
|
| 248 |
|
| 249 | branches = (columns == "branches")
|
| 250 |
|
| 251 | for i in range(3,len(sys.argv)):
|
| 252 | if (sys.argv[i] == "calls"):
|
| 253 | perf_db_export_calls = True
|
| 254 | elif (sys.argv[i] == "callchains"):
|
| 255 | perf_db_export_callchains = True
|
| 256 | else:
|
| 257 | usage()
|
| 258 |
|
| 259 | output_dir_name = os.getcwd() + "/" + dbname + "-perf-data"
|
| 260 | os.mkdir(output_dir_name)
|
| 261 |
|
| 262 | def do_query(q, s):
|
| 263 | if (q.exec_(s)):
|
| 264 | return
|
| 265 | raise Exception("Query failed: " + q.lastError().text())
|
| 266 |
|
| 267 | print datetime.datetime.today(), "Creating database..."
|
| 268 |
|
| 269 | db = QSqlDatabase.addDatabase('QPSQL')
|
| 270 | query = QSqlQuery(db)
|
| 271 | db.setDatabaseName('postgres')
|
| 272 | db.open()
|
| 273 | try:
|
| 274 | do_query(query, 'CREATE DATABASE ' + dbname)
|
| 275 | except:
|
| 276 | os.rmdir(output_dir_name)
|
| 277 | raise
|
| 278 | query.finish()
|
| 279 | query.clear()
|
| 280 | db.close()
|
| 281 |
|
| 282 | db.setDatabaseName(dbname)
|
| 283 | db.open()
|
| 284 |
|
| 285 | query = QSqlQuery(db)
|
| 286 | do_query(query, 'SET client_min_messages TO WARNING')
|
| 287 |
|
| 288 | do_query(query, 'CREATE TABLE selected_events ('
|
| 289 | 'id bigint NOT NULL,'
|
| 290 | 'name varchar(80))')
|
| 291 | do_query(query, 'CREATE TABLE machines ('
|
| 292 | 'id bigint NOT NULL,'
|
| 293 | 'pid integer,'
|
| 294 | 'root_dir varchar(4096))')
|
| 295 | do_query(query, 'CREATE TABLE threads ('
|
| 296 | 'id bigint NOT NULL,'
|
| 297 | 'machine_id bigint,'
|
| 298 | 'process_id bigint,'
|
| 299 | 'pid integer,'
|
| 300 | 'tid integer)')
|
| 301 | do_query(query, 'CREATE TABLE comms ('
|
| 302 | 'id bigint NOT NULL,'
|
| 303 | 'comm varchar(16))')
|
| 304 | do_query(query, 'CREATE TABLE comm_threads ('
|
| 305 | 'id bigint NOT NULL,'
|
| 306 | 'comm_id bigint,'
|
| 307 | 'thread_id bigint)')
|
| 308 | do_query(query, 'CREATE TABLE dsos ('
|
| 309 | 'id bigint NOT NULL,'
|
| 310 | 'machine_id bigint,'
|
| 311 | 'short_name varchar(256),'
|
| 312 | 'long_name varchar(4096),'
|
| 313 | 'build_id varchar(64))')
|
| 314 | do_query(query, 'CREATE TABLE symbols ('
|
| 315 | 'id bigint NOT NULL,'
|
| 316 | 'dso_id bigint,'
|
| 317 | 'sym_start bigint,'
|
| 318 | 'sym_end bigint,'
|
| 319 | 'binding integer,'
|
| 320 | 'name varchar(2048))')
|
| 321 | do_query(query, 'CREATE TABLE branch_types ('
|
| 322 | 'id integer NOT NULL,'
|
| 323 | 'name varchar(80))')
|
| 324 |
|
| 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 | else:
|
| 345 | do_query(query, 'CREATE TABLE samples ('
|
| 346 | 'id bigint NOT NULL,'
|
| 347 | 'evsel_id bigint,'
|
| 348 | 'machine_id bigint,'
|
| 349 | 'thread_id bigint,'
|
| 350 | 'comm_id bigint,'
|
| 351 | 'dso_id bigint,'
|
| 352 | 'symbol_id bigint,'
|
| 353 | 'sym_offset bigint,'
|
| 354 | 'ip bigint,'
|
| 355 | 'time bigint,'
|
| 356 | 'cpu integer,'
|
| 357 | 'to_dso_id bigint,'
|
| 358 | 'to_symbol_id bigint,'
|
| 359 | 'to_sym_offset bigint,'
|
| 360 | 'to_ip bigint,'
|
| 361 | 'period bigint,'
|
| 362 | 'weight bigint,'
|
| 363 | 'transaction bigint,'
|
| 364 | 'data_src bigint,'
|
| 365 | 'branch_type integer,'
|
| 366 | 'in_tx boolean,'
|
| 367 | 'call_path_id bigint)')
|
| 368 |
|
| 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 |
|
| 389 | do_query(query, 'CREATE VIEW machines_view AS '
|
| 390 | 'SELECT '
|
| 391 | 'id,'
|
| 392 | 'pid,'
|
| 393 | 'root_dir,'
|
| 394 | 'CASE WHEN id=0 THEN \'unknown\' WHEN pid=-1 THEN \'host\' ELSE \'guest\' END AS host_or_guest'
|
| 395 | ' FROM machines')
|
| 396 |
|
| 397 | do_query(query, 'CREATE VIEW dsos_view AS '
|
| 398 | 'SELECT '
|
| 399 | 'id,'
|
| 400 | 'machine_id,'
|
| 401 | '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
|
| 402 | 'short_name,'
|
| 403 | 'long_name,'
|
| 404 | 'build_id'
|
| 405 | ' FROM dsos')
|
| 406 |
|
| 407 | do_query(query, 'CREATE VIEW symbols_view AS '
|
| 408 | 'SELECT '
|
| 409 | 'id,'
|
| 410 | 'name,'
|
| 411 | '(SELECT short_name FROM dsos WHERE id=dso_id) AS dso,'
|
| 412 | 'dso_id,'
|
| 413 | 'sym_start,'
|
| 414 | 'sym_end,'
|
| 415 | 'CASE WHEN binding=0 THEN \'local\' WHEN binding=1 THEN \'global\' ELSE \'weak\' END AS binding'
|
| 416 | ' FROM symbols')
|
| 417 |
|
| 418 | do_query(query, 'CREATE VIEW threads_view AS '
|
| 419 | 'SELECT '
|
| 420 | 'id,'
|
| 421 | 'machine_id,'
|
| 422 | '(SELECT host_or_guest FROM machines_view WHERE id = machine_id) AS host_or_guest,'
|
| 423 | 'process_id,'
|
| 424 | 'pid,'
|
| 425 | 'tid'
|
| 426 | ' FROM threads')
|
| 427 |
|
| 428 | do_query(query, 'CREATE VIEW comm_threads_view AS '
|
| 429 | 'SELECT '
|
| 430 | 'comm_id,'
|
| 431 | '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
|
| 432 | 'thread_id,'
|
| 433 | '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
|
| 434 | '(SELECT tid FROM threads WHERE id = thread_id) AS tid'
|
| 435 | ' FROM comm_threads')
|
| 436 |
|
| 437 | if perf_db_export_calls or perf_db_export_callchains:
|
| 438 | do_query(query, 'CREATE VIEW call_paths_view AS '
|
| 439 | 'SELECT '
|
| 440 | 'c.id,'
|
| 441 | 'to_hex(c.ip) AS ip,'
|
| 442 | 'c.symbol_id,'
|
| 443 | '(SELECT name FROM symbols WHERE id = c.symbol_id) AS symbol,'
|
| 444 | '(SELECT dso_id FROM symbols WHERE id = c.symbol_id) AS dso_id,'
|
| 445 | '(SELECT dso FROM symbols_view WHERE id = c.symbol_id) AS dso_short_name,'
|
| 446 | 'c.parent_id,'
|
| 447 | 'to_hex(p.ip) AS parent_ip,'
|
| 448 | 'p.symbol_id AS parent_symbol_id,'
|
| 449 | '(SELECT name FROM symbols WHERE id = p.symbol_id) AS parent_symbol,'
|
| 450 | '(SELECT dso_id FROM symbols WHERE id = p.symbol_id) AS parent_dso_id,'
|
| 451 | '(SELECT dso FROM symbols_view WHERE id = p.symbol_id) AS parent_dso_short_name'
|
| 452 | ' FROM call_paths c INNER JOIN call_paths p ON p.id = c.parent_id')
|
| 453 | if perf_db_export_calls:
|
| 454 | do_query(query, 'CREATE VIEW calls_view AS '
|
| 455 | 'SELECT '
|
| 456 | 'calls.id,'
|
| 457 | 'thread_id,'
|
| 458 | '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
|
| 459 | '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
|
| 460 | '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
|
| 461 | 'call_path_id,'
|
| 462 | 'to_hex(ip) AS ip,'
|
| 463 | 'symbol_id,'
|
| 464 | '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
|
| 465 | 'call_time,'
|
| 466 | 'return_time,'
|
| 467 | 'return_time - call_time AS elapsed_time,'
|
| 468 | 'branch_count,'
|
| 469 | 'call_id,'
|
| 470 | 'return_id,'
|
| 471 | 'CASE WHEN flags=1 THEN \'no call\' WHEN flags=2 THEN \'no return\' WHEN flags=3 THEN \'no call/return\' ELSE \'\' END AS flags,'
|
| 472 | 'parent_call_path_id'
|
| 473 | ' FROM calls INNER JOIN call_paths ON call_paths.id = call_path_id')
|
| 474 |
|
| 475 | do_query(query, 'CREATE VIEW samples_view AS '
|
| 476 | 'SELECT '
|
| 477 | 'id,'
|
| 478 | 'time,'
|
| 479 | 'cpu,'
|
| 480 | '(SELECT pid FROM threads WHERE id = thread_id) AS pid,'
|
| 481 | '(SELECT tid FROM threads WHERE id = thread_id) AS tid,'
|
| 482 | '(SELECT comm FROM comms WHERE id = comm_id) AS command,'
|
| 483 | '(SELECT name FROM selected_events WHERE id = evsel_id) AS event,'
|
| 484 | 'to_hex(ip) AS ip_hex,'
|
| 485 | '(SELECT name FROM symbols WHERE id = symbol_id) AS symbol,'
|
| 486 | 'sym_offset,'
|
| 487 | '(SELECT short_name FROM dsos WHERE id = dso_id) AS dso_short_name,'
|
| 488 | 'to_hex(to_ip) AS to_ip_hex,'
|
| 489 | '(SELECT name FROM symbols WHERE id = to_symbol_id) AS to_symbol,'
|
| 490 | 'to_sym_offset,'
|
| 491 | '(SELECT short_name FROM dsos WHERE id = to_dso_id) AS to_dso_short_name,'
|
| 492 | '(SELECT name FROM branch_types WHERE id = branch_type) AS branch_type_name,'
|
| 493 | 'in_tx'
|
| 494 | ' FROM samples')
|
| 495 |
|
| 496 |
|
| 497 | file_header = struct.pack("!11sii", "PGCOPY\n\377\r\n\0", 0, 0)
|
| 498 | file_trailer = "\377\377"
|
| 499 |
|
| 500 | def open_output_file(file_name):
|
| 501 | path_name = output_dir_name + "/" + file_name
|
| 502 | file = open(path_name, "w+")
|
| 503 | file.write(file_header)
|
| 504 | return file
|
| 505 |
|
| 506 | def close_output_file(file):
|
| 507 | file.write(file_trailer)
|
| 508 | file.close()
|
| 509 |
|
| 510 | def copy_output_file_direct(file, table_name):
|
| 511 | close_output_file(file)
|
| 512 | sql = "COPY " + table_name + " FROM '" + file.name + "' (FORMAT 'binary')"
|
| 513 | do_query(query, sql)
|
| 514 |
|
| 515 | |
| 516 | def copy_output_file(file, table_name):
|
| 517 | conn = PQconnectdb("dbname = " + dbname)
|
| 518 | if (PQstatus(conn)):
|
| 519 | raise Exception("COPY FROM STDIN PQconnectdb failed")
|
| 520 | file.write(file_trailer)
|
| 521 | file.seek(0)
|
| 522 | sql = "COPY " + table_name + " FROM STDIN (FORMAT 'binary')"
|
| 523 | res = PQexec(conn, sql)
|
| 524 | if (PQresultStatus(res) != 4):
|
| 525 | raise Exception("COPY FROM STDIN PQexec failed")
|
| 526 | data = file.read(65536)
|
| 527 | while (len(data)):
|
| 528 | ret = PQputCopyData(conn, data, len(data))
|
| 529 | if (ret != 1):
|
| 530 | raise Exception("COPY FROM STDIN PQputCopyData failed, error " + str(ret))
|
| 531 | data = file.read(65536)
|
| 532 | ret = PQputCopyEnd(conn, None)
|
| 533 | if (ret != 1):
|
| 534 | raise Exception("COPY FROM STDIN PQputCopyEnd failed, error " + str(ret))
|
| 535 | PQfinish(conn)
|
| 536 |
|
| 537 | def remove_output_file(file):
|
| 538 | name = file.name
|
| 539 | file.close()
|
| 540 | os.unlink(name)
|
| 541 |
|
| 542 | evsel_file = open_output_file("evsel_table.bin")
|
| 543 | machine_file = open_output_file("machine_table.bin")
|
| 544 | thread_file = open_output_file("thread_table.bin")
|
| 545 | comm_file = open_output_file("comm_table.bin")
|
| 546 | comm_thread_file = open_output_file("comm_thread_table.bin")
|
| 547 | dso_file = open_output_file("dso_table.bin")
|
| 548 | symbol_file = open_output_file("symbol_table.bin")
|
| 549 | branch_type_file = open_output_file("branch_type_table.bin")
|
| 550 | sample_file = open_output_file("sample_table.bin")
|
| 551 | if perf_db_export_calls or perf_db_export_callchains:
|
| 552 | call_path_file = open_output_file("call_path_table.bin")
|
| 553 | if perf_db_export_calls:
|
| 554 | call_file = open_output_file("call_table.bin")
|
| 555 |
|
| 556 | def trace_begin():
|
| 557 | print datetime.datetime.today(), "Writing to intermediate files..."
|
| 558 | |
| 559 | evsel_table(0, "unknown")
|
| 560 | machine_table(0, 0, "unknown")
|
| 561 | thread_table(0, 0, 0, -1, -1)
|
| 562 | comm_table(0, "unknown")
|
| 563 | dso_table(0, 0, "unknown", "unknown", "")
|
| 564 | symbol_table(0, 0, 0, 0, 0, "unknown")
|
| 565 | sample_table(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
| 566 | if perf_db_export_calls or perf_db_export_callchains:
|
| 567 | call_path_table(0, 0, 0, 0)
|
| 568 |
|
| 569 | unhandled_count = 0
|
| 570 |
|
| 571 | def trace_end():
|
| 572 | print datetime.datetime.today(), "Copying to database..."
|
| 573 | copy_output_file(evsel_file, "selected_events")
|
| 574 | copy_output_file(machine_file, "machines")
|
| 575 | copy_output_file(thread_file, "threads")
|
| 576 | copy_output_file(comm_file, "comms")
|
| 577 | copy_output_file(comm_thread_file, "comm_threads")
|
| 578 | copy_output_file(dso_file, "dsos")
|
| 579 | copy_output_file(symbol_file, "symbols")
|
| 580 | copy_output_file(branch_type_file, "branch_types")
|
| 581 | copy_output_file(sample_file, "samples")
|
| 582 | if perf_db_export_calls or perf_db_export_callchains:
|
| 583 | copy_output_file(call_path_file, "call_paths")
|
| 584 | if perf_db_export_calls:
|
| 585 | copy_output_file(call_file, "calls")
|
| 586 |
|
| 587 | print datetime.datetime.today(), "Removing intermediate files..."
|
| 588 | remove_output_file(evsel_file)
|
| 589 | remove_output_file(machine_file)
|
| 590 | remove_output_file(thread_file)
|
| 591 | remove_output_file(comm_file)
|
| 592 | remove_output_file(comm_thread_file)
|
| 593 | remove_output_file(dso_file)
|
| 594 | remove_output_file(symbol_file)
|
| 595 | remove_output_file(branch_type_file)
|
| 596 | remove_output_file(sample_file)
|
| 597 | if perf_db_export_calls or perf_db_export_callchains:
|
| 598 | remove_output_file(call_path_file)
|
| 599 | if perf_db_export_calls:
|
| 600 | remove_output_file(call_file)
|
| 601 | os.rmdir(output_dir_name)
|
| 602 | print datetime.datetime.today(), "Adding primary keys"
|
| 603 | do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
|
| 604 | do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
|
| 605 | do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
|
| 606 | do_query(query, 'ALTER TABLE comms ADD PRIMARY KEY (id)')
|
| 607 | do_query(query, 'ALTER TABLE comm_threads ADD PRIMARY KEY (id)')
|
| 608 | do_query(query, 'ALTER TABLE dsos ADD PRIMARY KEY (id)')
|
| 609 | do_query(query, 'ALTER TABLE symbols ADD PRIMARY KEY (id)')
|
| 610 | do_query(query, 'ALTER TABLE branch_types ADD PRIMARY KEY (id)')
|
| 611 | do_query(query, 'ALTER TABLE samples ADD PRIMARY KEY (id)')
|
| 612 | if perf_db_export_calls or perf_db_export_callchains:
|
| 613 | do_query(query, 'ALTER TABLE call_paths ADD PRIMARY KEY (id)')
|
| 614 | if perf_db_export_calls:
|
| 615 | do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
|
| 616 |
|
| 617 | print datetime.datetime.today(), "Adding foreign keys"
|
| 618 | do_query(query, 'ALTER TABLE threads '
|
| 619 | 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
|
| 620 | 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
|
| 621 | do_query(query, 'ALTER TABLE comm_threads '
|
| 622 | 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
|
| 623 | 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id)')
|
| 624 | do_query(query, 'ALTER TABLE dsos '
|
| 625 | 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id)')
|
| 626 | do_query(query, 'ALTER TABLE symbols '
|
| 627 | 'ADD CONSTRAINT dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id)')
|
| 628 | do_query(query, 'ALTER TABLE samples '
|
| 629 | 'ADD CONSTRAINT evselfk FOREIGN KEY (evsel_id) REFERENCES selected_events (id),'
|
| 630 | 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
|
| 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 dsofk FOREIGN KEY (dso_id) REFERENCES dsos (id),'
|
| 634 | 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id),'
|
| 635 | 'ADD CONSTRAINT todsofk FOREIGN KEY (to_dso_id) REFERENCES dsos (id),'
|
| 636 | 'ADD CONSTRAINT tosymbolfk FOREIGN KEY (to_symbol_id) REFERENCES symbols (id)')
|
| 637 | if perf_db_export_calls or perf_db_export_callchains:
|
| 638 | do_query(query, 'ALTER TABLE call_paths '
|
| 639 | 'ADD CONSTRAINT parentfk FOREIGN KEY (parent_id) REFERENCES call_paths (id),'
|
| 640 | 'ADD CONSTRAINT symbolfk FOREIGN KEY (symbol_id) REFERENCES symbols (id)')
|
| 641 | if perf_db_export_calls:
|
| 642 | do_query(query, 'ALTER TABLE calls '
|
| 643 | 'ADD CONSTRAINT threadfk FOREIGN KEY (thread_id) REFERENCES threads (id),'
|
| 644 | 'ADD CONSTRAINT commfk FOREIGN KEY (comm_id) REFERENCES comms (id),'
|
| 645 | 'ADD CONSTRAINT call_pathfk FOREIGN KEY (call_path_id) REFERENCES call_paths (id),'
|
| 646 | 'ADD CONSTRAINT callfk FOREIGN KEY (call_id) REFERENCES samples (id),'
|
| 647 | 'ADD CONSTRAINT returnfk FOREIGN KEY (return_id) REFERENCES samples (id),'
|
| 648 | 'ADD CONSTRAINT parent_call_pathfk FOREIGN KEY (parent_call_path_id) REFERENCES call_paths (id)')
|
| 649 | do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
|
| 650 |
|
| 651 | if (unhandled_count):
|
| 652 | print datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events"
|
| 653 | print datetime.datetime.today(), "Done"
|
| 654 |
|
| 655 | def trace_unhandled(event_name, context, event_fields_dict):
|
| 656 | global unhandled_count
|
| 657 | unhandled_count += 1
|
| 658 |
|
| 659 | def sched__sched_switch(*x):
|
| 660 | pass
|
| 661 |
|
| 662 | def evsel_table(evsel_id, evsel_name, *x):
|
| 663 | n = len(evsel_name)
|
| 664 | fmt = "!hiqi" + str(n) + "s"
|
| 665 | value = struct.pack(fmt, 2, 8, evsel_id, n, evsel_name)
|
| 666 | evsel_file.write(value)
|
| 667 |
|
| 668 | def machine_table(machine_id, pid, root_dir, *x):
|
| 669 | n = len(root_dir)
|
| 670 | fmt = "!hiqiii" + str(n) + "s"
|
| 671 | value = struct.pack(fmt, 3, 8, machine_id, 4, pid, n, root_dir)
|
| 672 | machine_file.write(value)
|
| 673 |
|
| 674 | def thread_table(thread_id, machine_id, process_id, pid, tid, *x):
|
| 675 | value = struct.pack("!hiqiqiqiiii", 5, 8, thread_id, 8, machine_id, 8, process_id, 4, pid, 4, tid)
|
| 676 | thread_file.write(value)
|
| 677 |
|
| 678 | def comm_table(comm_id, comm_str, *x):
|
| 679 | n = len(comm_str)
|
| 680 | fmt = "!hiqi" + str(n) + "s"
|
| 681 | value = struct.pack(fmt, 2, 8, comm_id, n, comm_str)
|
| 682 | comm_file.write(value)
|
| 683 |
|
| 684 | def comm_thread_table(comm_thread_id, comm_id, thread_id, *x):
|
| 685 | fmt = "!hiqiqiq"
|
| 686 | value = struct.pack(fmt, 3, 8, comm_thread_id, 8, comm_id, 8, thread_id)
|
| 687 | comm_thread_file.write(value)
|
| 688 |
|
| 689 | def dso_table(dso_id, machine_id, short_name, long_name, build_id, *x):
|
| 690 | n1 = len(short_name)
|
| 691 | n2 = len(long_name)
|
| 692 | n3 = len(build_id)
|
| 693 | fmt = "!hiqiqi" + str(n1) + "si" + str(n2) + "si" + str(n3) + "s"
|
| 694 | value = struct.pack(fmt, 5, 8, dso_id, 8, machine_id, n1, short_name, n2, long_name, n3, build_id)
|
| 695 | dso_file.write(value)
|
| 696 |
|
| 697 | def symbol_table(symbol_id, dso_id, sym_start, sym_end, binding, symbol_name, *x):
|
| 698 | n = len(symbol_name)
|
| 699 | fmt = "!hiqiqiqiqiii" + str(n) + "s"
|
| 700 | value = struct.pack(fmt, 6, 8, symbol_id, 8, dso_id, 8, sym_start, 8, sym_end, 4, binding, n, symbol_name)
|
| 701 | symbol_file.write(value)
|
| 702 |
|
| 703 | def branch_type_table(branch_type, name, *x):
|
| 704 | n = len(name)
|
| 705 | fmt = "!hiii" + str(n) + "s"
|
| 706 | value = struct.pack(fmt, 2, 4, branch_type, n, name)
|
| 707 | branch_type_file.write(value)
|
| 708 |
|
| 709 | 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):
|
| 710 | if branches:
|
| 711 | 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)
|
| 712 | else:
|
| 713 | 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)
|
| 714 | sample_file.write(value)
|
| 715 |
|
| 716 | def call_path_table(cp_id, parent_id, symbol_id, ip, *x):
|
| 717 | fmt = "!hiqiqiqiq"
|
| 718 | value = struct.pack(fmt, 4, 8, cp_id, 8, parent_id, 8, symbol_id, 8, ip)
|
| 719 | call_path_file.write(value)
|
| 720 |
|
| 721 | 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):
|
| 722 | fmt = "!hiqiqiqiqiqiqiqiqiqiqii"
|
| 723 | 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)
|
| 724 | call_file.write(value)
|