Introduce a printdate function to eliminate the repetitive use of datetime.datetime.today() in the SQL exporting scripts.
Showing the changes to tools/perf/scripts/python (2 of 2 files). Show all
Unified · Split
| @@ -251,6 +251,9 @@ perf_db_export_callchains = False · 250 unmodified lines | ||
| 251 | 251 | def printerr(*args, **kw_args): |
| 252 | 252 | print(*args, file=sys.stderr, **kw_args) |
| 253 | 253 | |
| 254 | +def printdate(*args, **kw_args): | |
| 255 | + print(datetime.datetime.today(), *args, sep=' ', **kw_args) | |
| 256 | + | |
| 254 | 257 | def usage(): |
| 255 | 258 | printerr("Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>] [<callchains>]") |
| 256 | 259 | printerr("where: columns 'all' or 'branches'") |
| @@ -289,7 +292,7 @@ def do_query(q, s): · 32 unmodified lines | ||
| 289 | 292 | return |
| 290 | 293 | raise Exception("Query failed: " + q.lastError().text()) |
| 291 | 294 | |
| 292 | −print(datetime.datetime.today(), "Creating database...") | |
| 295 | +printdate("Creating database...") | |
| 293 | 296 | |
| 294 | 297 | db = QSqlDatabase.addDatabase('QPSQL') |
| 295 | 298 | query = QSqlQuery(db) |
| @@ -582,7 +585,7 @@ if perf_db_export_calls: · 286 unmodified lines | ||
| 582 | 585 | call_file = open_output_file("call_table.bin") |
| 583 | 586 | |
| 584 | 587 | def trace_begin(): |
| 585 | − print(datetime.datetime.today(), "Writing to intermediate files...") | |
| 588 | + printdate("Writing to intermediate files...") | |
| 586 | 589 | # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs |
| 587 | 590 | evsel_table(0, "unknown") |
| 588 | 591 | machine_table(0, 0, "unknown") |
| @@ -598,7 +601,7 @@ def trace_begin(): · 9 unmodified lines | ||
| 598 | 601 | unhandled_count = 0 |
| 599 | 602 | |
| 600 | 603 | def trace_end(): |
| 601 | − print(datetime.datetime.today(), "Copying to database...") | |
| 604 | + printdate("Copying to database...") | |
| 602 | 605 | copy_output_file(evsel_file, "selected_events") |
| 603 | 606 | copy_output_file(machine_file, "machines") |
| 604 | 607 | copy_output_file(thread_file, "threads") |
| @@ -613,7 +616,7 @@ def trace_end(): · 8 unmodified lines | ||
| 613 | 616 | if perf_db_export_calls: |
| 614 | 617 | copy_output_file(call_file, "calls") |
| 615 | 618 | |
| 616 | − print(datetime.datetime.today(), "Removing intermediate files...") | |
| 619 | + printdate("Removing intermediate files...") | |
| 617 | 620 | remove_output_file(evsel_file) |
| 618 | 621 | remove_output_file(machine_file) |
| 619 | 622 | remove_output_file(thread_file) |
| @@ -628,7 +631,7 @@ def trace_end(): · 8 unmodified lines | ||
| 628 | 631 | if perf_db_export_calls: |
| 629 | 632 | remove_output_file(call_file) |
| 630 | 633 | os.rmdir(output_dir_name) |
| 631 | − print(datetime.datetime.today(), "Adding primary keys") | |
| 634 | + printdate("Adding primary keys") | |
| 632 | 635 | do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)') |
| 633 | 636 | do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)') |
| 634 | 637 | do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)') |
| @@ -643,7 +646,7 @@ def trace_end(): · 8 unmodified lines | ||
| 643 | 646 | if perf_db_export_calls: |
| 644 | 647 | do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)') |
| 645 | 648 | |
| 646 | − print(datetime.datetime.today(), "Adding foreign keys") | |
| 649 | + printdate("Adding foreign keys") | |
| 647 | 650 | do_query(query, 'ALTER TABLE threads ' |
| 648 | 651 | 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),' |
| 649 | 652 | 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)') |
| @@ -679,8 +682,8 @@ def trace_end(): · 29 unmodified lines | ||
| 679 | 682 | do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)') |
| 680 | 683 | |
| 681 | 684 | if (unhandled_count): |
| 682 | − print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events") | |
| 683 | − print(datetime.datetime.today(), "Done") | |
| 685 | + printdate("Warning: ", unhandled_count, " unhandled events") | |
| 686 | + printdate("Done") | |
| 684 | 687 | |
| 685 | 688 | def trace_unhandled(event_name, context, event_fields_dict): |
| 686 | 689 | global unhandled_count |
| @@ -65,6 +65,9 @@ perf_db_export_callchains = False · 64 unmodified lines | ||
| 65 | 65 | def printerr(*args, **keyword_args): |
| 66 | 66 | print(*args, file=sys.stderr, **keyword_args) |
| 67 | 67 | |
| 68 | +def printdate(*args, **kw_args): | |
| 69 | + print(datetime.datetime.today(), *args, sep=' ', **kw_args) | |
| 70 | + | |
| 68 | 71 | def usage(): |
| 69 | 72 | printerr("Usage is: export-to-sqlite.py <database name> [<columns>] [<calls>] [<callchains>]"); |
| 70 | 73 | printerr("where: columns 'all' or 'branches'"); |
| @@ -105,7 +108,7 @@ def do_query_(q): · 34 unmodified lines | ||
| 105 | 108 | return |
| 106 | 109 | raise Exception("Query failed: " + q.lastError().text()) |
| 107 | 110 | |
| 108 | −print(datetime.datetime.today(), "Creating database ...") | |
| 111 | +printdate("Creating database ...") | |
| 109 | 112 | |
| 110 | 113 | db_exists = False |
| 111 | 114 | try: |
| @@ -383,7 +386,7 @@ if perf_db_export_calls: · 271 unmodified lines | ||
| 383 | 386 | call_query.prepare("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") |
| 384 | 387 | |
| 385 | 388 | def trace_begin(): |
| 386 | − print(datetime.datetime.today(), "Writing records...") | |
| 389 | + printdate("Writing records...") | |
| 387 | 390 | do_query(query, 'BEGIN TRANSACTION') |
| 388 | 391 | # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs |
| 389 | 392 | evsel_table(0, "unknown") |
| @@ -402,14 +405,14 @@ unhandled_count = 0 · 12 unmodified lines | ||
| 402 | 405 | def trace_end(): |
| 403 | 406 | do_query(query, 'END TRANSACTION') |
| 404 | 407 | |
| 405 | − print(datetime.datetime.today(), "Adding indexes") | |
| 408 | + printdate("Adding indexes") | |
| 406 | 409 | if perf_db_export_calls: |
| 407 | 410 | do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)') |
| 408 | 411 | do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)') |
| 409 | 412 | |
| 410 | 413 | if (unhandled_count): |
| 411 | − print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events") | |
| 412 | − print(datetime.datetime.today(), "Done") | |
| 414 | + printdate("Warning: ", unhandled_count, " unhandled events") | |
| 415 | + printdate("Done") | |
| 413 | 416 | |
| 414 | 417 | def trace_unhandled(event_name, context, event_fields_dict): |
| 415 | 418 | global unhandled_count |