Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/20190710085810.1650-22-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Adrian Hunter and committed by
Arnaldo Carvalho de Melo
56789f3d 37c1f991

+51
+51
tools/perf/scripts/python/export-to-postgresql.py
··· 482 482 'last_cstate integer,' 483 483 'wake_reason integer)') 484 484 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 + 485 496 do_query(query, 'CREATE VIEW machines_view AS ' 486 497 'SELECT ' 487 498 'id,' ··· 706 695 ' INNER JOIN selected_events ON selected_events.id = samples.evsel_id' 707 696 ' ORDER BY samples.id') 708 697 698 + do_query(query, 'CREATE VIEW context_switches_view AS ' 699 + 'SELECT ' 700 + 'context_switches.id,' 701 + 'context_switches.machine_id,' 702 + 'context_switches.time,' 703 + 'context_switches.cpu,' 704 + 'th_out.pid AS pid_out,' 705 + 'th_out.tid AS tid_out,' 706 + 'comm_out.comm AS comm_out,' 707 + 'th_in.pid AS pid_in,' 708 + 'th_in.tid AS tid_in,' 709 + 'comm_in.comm AS comm_in,' 710 + 'CASE WHEN context_switches.flags = 0 THEN \'in\'' 711 + ' WHEN context_switches.flags = 1 THEN \'out\'' 712 + ' WHEN context_switches.flags = 3 THEN \'out preempt\'' 713 + ' ELSE CAST ( context_switches.flags AS VARCHAR(11) )' 714 + 'END AS flags' 715 + ' FROM context_switches' 716 + ' INNER JOIN threads AS th_out ON th_out.id = context_switches.thread_out_id' 717 + ' INNER JOIN threads AS th_in ON th_in.id = context_switches.thread_in_id' 718 + ' INNER JOIN comms AS comm_out ON comm_out.id = context_switches.comm_out_id' 719 + ' INNER JOIN comms AS comm_in ON comm_in.id = context_switches.comm_in_id') 720 + 709 721 file_header = struct.pack("!11sii", b"PGCOPY\n\377\r\n\0", 0, 0) 710 722 file_trailer = b"\377\377" 711 723 ··· 793 759 pwre_file = open_output_file("pwre_table.bin") 794 760 exstop_file = open_output_file("exstop_table.bin") 795 761 pwrx_file = open_output_file("pwrx_table.bin") 762 + context_switches_file = open_output_file("context_switches_table.bin") 796 763 797 764 def trace_begin(): 798 765 printdate("Writing to intermediate files...") ··· 842 807 copy_output_file(pwre_file, "pwre") 843 808 copy_output_file(exstop_file, "exstop") 844 809 copy_output_file(pwrx_file, "pwrx") 810 + copy_output_file(context_switches_file, "context_switches") 845 811 846 812 printdate("Removing intermediate files...") 847 813 remove_output_file(evsel_file) ··· 864 828 remove_output_file(pwre_file) 865 829 remove_output_file(exstop_file) 866 830 remove_output_file(pwrx_file) 831 + remove_output_file(context_switches_file) 867 832 os.rmdir(output_dir_name) 868 833 printdate("Adding primary keys") 869 834 do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)') ··· 886 849 do_query(query, 'ALTER TABLE pwre ADD PRIMARY KEY (id)') 887 850 do_query(query, 'ALTER TABLE exstop ADD PRIMARY KEY (id)') 888 851 do_query(query, 'ALTER TABLE pwrx ADD PRIMARY KEY (id)') 852 + do_query(query, 'ALTER TABLE context_switches ADD PRIMARY KEY (id)') 889 853 890 854 printdate("Adding foreign keys") 891 855 do_query(query, 'ALTER TABLE threads ' ··· 938 900 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)') 939 901 do_query(query, 'ALTER TABLE pwrx ' 940 902 'ADD CONSTRAINT idfk FOREIGN KEY (id) REFERENCES samples (id)') 903 + do_query(query, 'ALTER TABLE context_switches ' 904 + 'ADD CONSTRAINT machinefk FOREIGN KEY (machine_id) REFERENCES machines (id),' 905 + 'ADD CONSTRAINT toutfk FOREIGN KEY (thread_out_id) REFERENCES threads (id),' 906 + 'ADD CONSTRAINT tinfk FOREIGN KEY (thread_in_id) REFERENCES threads (id),' 907 + 'ADD CONSTRAINT coutfk FOREIGN KEY (comm_out_id) REFERENCES comms (id),' 908 + 'ADD CONSTRAINT cinfk FOREIGN KEY (comm_in_id) REFERENCES comms (id)') 941 909 942 910 printdate("Dropping unused tables") 943 911 if is_table_empty("ptwrite"): ··· 956 912 drop("pwrx") 957 913 if is_table_empty("cbr"): 958 914 drop("cbr") 915 + if is_table_empty("context_switches"): 916 + drop("context_switches") 959 917 960 918 if (unhandled_count): 961 919 printdate("Warning: ", unhandled_count, " unhandled events") ··· 1104 1058 pwrx(id, raw_buf) 1105 1059 elif config == 5: 1106 1060 cbr(id, raw_buf) 1061 + 1062 + def context_switch_table(id, machine_id, time, cpu, thread_out_id, comm_out_id, thread_in_id, comm_in_id, flags, *x): 1063 + fmt = "!hiqiqiqiiiqiqiqiqii" 1064 + 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) 1065 + context_switches_file.write(value)