History

perf script python: Add printdate function to SQL exporters

Introduce a printdate function to eliminate the repetitive use of datetime.datetime.today() in the SQL exporting scripts.

Tony Jones committed 7 years ago

Commit 49f93bb1 parent ebf6c5c2 files · +19 −13Browse files

Showing the changes to tools/perf/scripts/python (2 of 2 files). Show all

Unified · Split

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