History

perf scripts python: export-to-postgresql.py: Export switch events

Export switch events to a new table 'context_switches' and create a view 'context_switches_view'. The table and view will show automatically in the exported-sql-viewer.py script. If the table ends up empty, then it and the view are dropped.

Adrian Hunter committed 7 years ago

Commit 56789f31 parent 37c1f991 file · +51 −0Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/export-to-postgresql.py+51−0
@@ -482,6 +482,17 @@ do_query(query, 'CREATE TABLE pwrx (' · 481 unmodified lines
482482 'last_cstate integer,'
483483 'wake_reason integer)')
484484
485+do_query(query, 'CREATE TABLE context_switches ('
486+ 'id bigint NOT NULL,'
487+ 'machine_id bigint,'
488+ 'time bigint,'
489+ 'cpu integer,'
490+ 'thread_out_id bigint,'
491+ 'comm_out_id bigint,'
492+ 'thread_in_id bigint,'
493+ 'comm_in_id bigint,'
494+ 'flags integer)')
495+
485496 do_query(query, 'CREATE VIEW machines_view AS '
486497 'SELECT '
487498 'id,'
@@ -695,6 +706,29 @@ do_query(query, 'CREATE VIEW power_events_view AS ' · 207 unmodified lines
695706 ' INNER JOIN selected_events ON selected_events.id = samples.evsel_id'
696707 ' ORDER BY samples.id')
697708
709+do_query(query, 'CREATE VIEW context_switches_view AS '
710+ 'SELECT '
711+ 'context_switches.id,'
712+ 'context_switches.machine_id,'
713+ 'context_switches.time,'
714+ 'context_switches.cpu,'
715+ 'th_out.pid AS pid_out,'
716+ 'th_out.tid AS tid_out,'
717+ 'comm_out.comm AS comm_out,'
718+ 'th_in.pid AS pid_in,'
719+ 'th_in.tid AS tid_in,'
720+ 'comm_in.comm AS comm_in,'
721+ 'CASE WHEN context_switches.flags = 0 THEN \'in\''
722+ ' WHEN context_switches.flags = 1 THEN \'out\''
723+ ' WHEN context_switches.flags = 3 THEN \'out preempt\''
724+ ' ELSE CAST ( context_switches.flags AS VARCHAR(11) )'
725+ 'END AS flags'
726+ ' FROM context_switches'
727+ ' INNER JOIN threads AS th_out ON th_out.id = context_switches.thread_out_id'
728+ ' INNER JOIN threads AS th_in ON th_in.id = context_switches.thread_in_id'
729+ ' INNER JOIN comms AS comm_out ON comm_out.id = context_switches.comm_out_id'
730+ ' INNER JOIN comms AS comm_in ON comm_in.id = context_switches.comm_in_id')
731+
698732 file_header = struct.pack("!11sii", b"PGCOPY\n\377\r\n\0", 0, 0)
699733 file_trailer = b"\377\377"
700734
@@ -759,6 +793,7 @@ mwait_file = open_output_file("mwait_table.bin") · 58 unmodified lines
759793 pwre_file = open_output_file("pwre_table.bin")
760794 exstop_file = open_output_file("exstop_table.bin")
761795 pwrx_file = open_output_file("pwrx_table.bin")
796+context_switches_file = open_output_file("context_switches_table.bin")
762797
763798 def trace_begin():
764799 printdate("Writing to intermediate files...")
@@ -807,6 +842,7 @@ def trace_end(): · 42 unmodified lines
807842 copy_output_file(pwre_file, "pwre")
808843 copy_output_file(exstop_file, "exstop")
809844 copy_output_file(pwrx_file, "pwrx")
845+ copy_output_file(context_switches_file, "context_switches")
810846
811847 printdate("Removing intermediate files...")
812848 remove_output_file(evsel_file)
@@ -828,6 +864,7 @@ def trace_end(): · 15 unmodified lines
828864 remove_output_file(pwre_file)
829865 remove_output_file(exstop_file)
830866 remove_output_file(pwrx_file)
867+ remove_output_file(context_switches_file)
831868 os.rmdir(output_dir_name)
832869 printdate("Adding primary keys")
833870 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
@@ -849,6 +886,7 @@ def trace_end(): · 15 unmodified lines
849886 do_query(query, 'ALTER TABLE pwre ADD PRIMARY KEY (id)')
850887 do_query(query, 'ALTER TABLE exstop ADD PRIMARY KEY (id)')
851888 do_query(query, 'ALTER TABLE pwrx ADD PRIMARY KEY (id)')
889+ do_query(query, 'ALTER TABLE context_switches ADD PRIMARY KEY (id)')
852890
853891 printdate("Adding foreign keys")
854892 do_query(query, 'ALTER TABLE threads '
@@ -900,6 +938,12 @@ def trace_end(): · 45 unmodified lines
900938 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
901939 do_query(query, 'ALTER TABLE pwrx '
902940 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)')
941+ do_query(query, 'ALTER TABLE context_switches '
942+ 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),'
943+ 'ADD CONSTRAINT toutfk FOREIGN KEY (thread_out_id) REFERENCES threads (id),'
944+ 'ADD CONSTRAINT tinfk FOREIGN KEY (thread_in_id) REFERENCES threads (id),'
945+ 'ADD CONSTRAINT coutfk FOREIGN KEY (comm_out_id) REFERENCES comms (id),'
946+ 'ADD CONSTRAINT cinfk FOREIGN KEY (comm_in_id) REFERENCES comms (id)')
903947
904948 printdate("Dropping unused tables")
905949 if is_table_empty("ptwrite"):
@@ -912,6 +956,8 @@ def trace_end(): · 6 unmodified lines
912956 drop("pwrx")
913957 if is_table_empty("cbr"):
914958 drop("cbr")
959+ if is_table_empty("context_switches"):
960+ drop("context_switches")
915961
916962 if (unhandled_count):
917963 printdate("Warning: ", unhandled_count, " unhandled events")
@@ -1058,3 +1104,8 @@ def synth_data(id, config, raw_buf, *x): · 140 unmodified lines
10581104 pwrx(id, raw_buf)
10591105 elif config == 5:
10601106 cbr(id, raw_buf)
1107+
1108+def context_switch_table(id, machine_id, time, cpu, thread_out_id, comm_out_id, thread_in_id, comm_in_id, flags, *x):
1109+ fmt = "!hiqiqiqiiiqiqiqiqii"
1110+ value = struct.pack(fmt, 9, 8, id, 8, machine_id, 8, time, 4, cpu, 8, thread_out_id, 8, comm_out_id, 8, thread_in_id, 8, comm_in_id, 4, flags)
1111+ context_switches_file.write(value)