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
251251 def printerr(*args, **kw_args):
252252 print(*args, file=sys.stderr, **kw_args)
253253
254+def printdate(*args, **kw_args):
255+ print(datetime.datetime.today(), *args, sep=' ', **kw_args)
256+
254257 def usage():
255258 printerr("Usage is: export-to-postgresql.py <database name> [<columns>] [<calls>] [<callchains>]")
256259 printerr("where: columns 'all' or 'branches'")
@@ -289,7 +292,7 @@ def do_query(q, s): · 32 unmodified lines
289292 return
290293 raise Exception("Query failed: " + q.lastError().text())
291294
292−print(datetime.datetime.today(), "Creating database...")
295+printdate("Creating database...")
293296
294297 db = QSqlDatabase.addDatabase('QPSQL')
295298 query = QSqlQuery(db)
@@ -582,7 +585,7 @@ if perf_db_export_calls: · 286 unmodified lines
582585 call_file = open_output_file("call_table.bin")
583586
584587 def trace_begin():
585− print(datetime.datetime.today(), "Writing to intermediate files...")
588+ printdate("Writing to intermediate files...")
586589 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
587590 evsel_table(0, "unknown")
588591 machine_table(0, 0, "unknown")
@@ -598,7 +601,7 @@ def trace_begin(): · 9 unmodified lines
598601 unhandled_count = 0
599602
600603 def trace_end():
601− print(datetime.datetime.today(), "Copying to database...")
604+ printdate("Copying to database...")
602605 copy_output_file(evsel_file, "selected_events")
603606 copy_output_file(machine_file, "machines")
604607 copy_output_file(thread_file, "threads")
@@ -613,7 +616,7 @@ def trace_end(): · 8 unmodified lines
613616 if perf_db_export_calls:
614617 copy_output_file(call_file, "calls")
615618
616− print(datetime.datetime.today(), "Removing intermediate files...")
619+ printdate("Removing intermediate files...")
617620 remove_output_file(evsel_file)
618621 remove_output_file(machine_file)
619622 remove_output_file(thread_file)
@@ -628,7 +631,7 @@ def trace_end(): · 8 unmodified lines
628631 if perf_db_export_calls:
629632 remove_output_file(call_file)
630633 os.rmdir(output_dir_name)
631− print(datetime.datetime.today(), "Adding primary keys")
634+ printdate("Adding primary keys")
632635 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
633636 do_query(query, 'ALTER TABLE machines ADD PRIMARY KEY (id)')
634637 do_query(query, 'ALTER TABLE threads ADD PRIMARY KEY (id)')
@@ -643,7 +646,7 @@ def trace_end(): · 8 unmodified lines
643646 if perf_db_export_calls:
644647 do_query(query, 'ALTER TABLE calls ADD PRIMARY KEY (id)')
645648
646− print(datetime.datetime.today(), "Adding foreign keys")
649+ printdate("Adding foreign keys")
647650 do_query(query, 'ALTER TABLE threads '
648651 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
649652 'ADD CONSTRAINT processfk FOREIGN KEY (process_id) REFERENCES threads (id)')
@@ -679,8 +682,8 @@ def trace_end(): · 29 unmodified lines
679682 do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)')
680683
681684 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")
684687
685688 def trace_unhandled(event_name, context, event_fields_dict):
686689 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
6565 def printerr(*args, **keyword_args):
6666 print(*args, file=sys.stderr, **keyword_args)
6767
68+def printdate(*args, **kw_args):
69+ print(datetime.datetime.today(), *args, sep=' ', **kw_args)
70+
6871 def usage():
6972 printerr("Usage is: export-to-sqlite.py <database name> [<columns>] [<calls>] [<callchains>]");
7073 printerr("where: columns 'all' or 'branches'");
@@ -105,7 +108,7 @@ def do_query_(q): · 34 unmodified lines
105108 return
106109 raise Exception("Query failed: " + q.lastError().text())
107110
108−print(datetime.datetime.today(), "Creating database ...")
111+printdate("Creating database ...")
109112
110113 db_exists = False
111114 try:
@@ -383,7 +386,7 @@ if perf_db_export_calls: · 271 unmodified lines
383386 call_query.prepare("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
384387
385388 def trace_begin():
386− print(datetime.datetime.today(), "Writing records...")
389+ printdate("Writing records...")
387390 do_query(query, 'BEGIN TRANSACTION')
388391 # id == 0 means unknown. It is easier to create records for them than replace the zeroes with NULLs
389392 evsel_table(0, "unknown")
@@ -402,14 +405,14 @@ unhandled_count = 0 · 12 unmodified lines
402405 def trace_end():
403406 do_query(query, 'END TRANSACTION')
404407
405− print(datetime.datetime.today(), "Adding indexes")
408+ printdate("Adding indexes")
406409 if perf_db_export_calls:
407410 do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
408411 do_query(query, 'CREATE INDEX pid_idx ON calls (parent_id)')
409412
410413 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")
413416
414417 def trace_unhandled(event_name, context, event_fields_dict):
415418 global unhandled_count