Skip to content

Commit 877cc63

Browse files
jeremyclineacmel
authored andcommitted
perf tools: Generate a Python script compatible with Python 2 and 3
When generating a Python script with "perf script -g python", produce one that is compatible with Python 2 and 3. The difference between the two generated scripts is: --- python2-perf-script.py 2018-05-08 15:35:00.865889705 -0400 +++ python3-perf-script.py 2018-05-08 15:34:49.019789564 -0400 @@ -7,6 +7,8 @@ # be retrieved using Python functions of the form common_*(context). # See the perf-script-python Documentation for the list of available functions. +from __future__ import print_function + import os import sys @@ -18,10 +20,10 @@ def trace_begin(): - print "in trace_begin" + print("in trace_begin") def trace_end(): - print "in trace_end" + print("in trace_end") def raw_syscalls__sys_enter(event_name, context, common_cpu, common_secs, common_nsecs, common_pid, common_comm, @@ -29,26 +31,26 @@ print_header(event_name, common_cpu, common_secs, common_nsecs, common_pid, common_comm) - print "id=%d, args=%s" % \ - (id, args) + print("id=%d, args=%s" % \ + (id, args)) - print 'Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}' + print('Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}') for node in common_callchain: if 'sym' in node: - print "\t[%x] %s" % (node['ip'], node['sym']['name']) + print("\t[%x] %s" % (node['ip'], node['sym']['name'])) else: - print " [%x]" % (node['ip']) + print(" [%x]" % (node['ip'])) - print "\n" + print() def trace_unhandled(event_name, context, event_fields_dict, perf_sample_dict): - print get_dict_as_string(event_fields_dict) - print 'Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}' + print(get_dict_as_string(event_fields_dict)) + print('Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}') def print_header(event_name, cpu, secs, nsecs, pid, comm): - print "%-20s %5u %05u.%09u %8u %-20s " % \ - (event_name, cpu, secs, nsecs, pid, comm), + print("%-20s %5u %05u.%09u %8u %-20s " % \ + (event_name, cpu, secs, nsecs, pid, comm), end="") def get_dict_as_string(a_dict, delimiter=' '): return delimiter.join(['%s=%s'%(k,str(v))for k,v in sorted(a_dict.items())]) Signed-off-by: Jeremy Cline <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Herton Krzesinski <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/0100016341a7278a-d178c724-2b0f-49ca-be93-80a7d51aaa0d-000000@email.amazonses.com Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 092150a commit 877cc63

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

tools/perf/util/scripting-engines/trace-event-python.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ static int python_generate_script(struct pevent *pevent, const char *outfile)
16271627
fprintf(ofp, "# See the perf-script-python Documentation for the list "
16281628
"of available functions.\n\n");
16291629

1630+
fprintf(ofp, "from __future__ import print_function\n\n");
16301631
fprintf(ofp, "import os\n");
16311632
fprintf(ofp, "import sys\n\n");
16321633

@@ -1636,10 +1637,10 @@ static int python_generate_script(struct pevent *pevent, const char *outfile)
16361637
fprintf(ofp, "from Core import *\n\n\n");
16371638

16381639
fprintf(ofp, "def trace_begin():\n");
1639-
fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1640+
fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
16401641

16411642
fprintf(ofp, "def trace_end():\n");
1642-
fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1643+
fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
16431644

16441645
while ((event = trace_find_next_event(pevent, event))) {
16451646
fprintf(ofp, "def %s__%s(", event->system, event->name);
@@ -1675,7 +1676,7 @@ static int python_generate_script(struct pevent *pevent, const char *outfile)
16751676
"common_secs, common_nsecs,\n\t\t\t"
16761677
"common_pid, common_comm)\n\n");
16771678

1678-
fprintf(ofp, "\t\tprint \"");
1679+
fprintf(ofp, "\t\tprint(\"");
16791680

16801681
not_first = 0;
16811682
count = 0;
@@ -1736,31 +1737,31 @@ static int python_generate_script(struct pevent *pevent, const char *outfile)
17361737
fprintf(ofp, "%s", f->name);
17371738
}
17381739

1739-
fprintf(ofp, ")\n\n");
1740+
fprintf(ofp, "))\n\n");
17401741

1741-
fprintf(ofp, "\t\tprint 'Sample: {'+"
1742-
"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1742+
fprintf(ofp, "\t\tprint('Sample: {'+"
1743+
"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
17431744

17441745
fprintf(ofp, "\t\tfor node in common_callchain:");
17451746
fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1746-
fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1747+
fprintf(ofp, "\n\t\t\t\tprint(\"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name']))");
17471748
fprintf(ofp, "\n\t\t\telse:");
1748-
fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1749-
fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1749+
fprintf(ofp, "\n\t\t\t\tprint(\"\t[%%x]\" %% (node['ip']))\n\n");
1750+
fprintf(ofp, "\t\tprint()\n\n");
17501751

17511752
}
17521753

17531754
fprintf(ofp, "def trace_unhandled(event_name, context, "
17541755
"event_fields_dict, perf_sample_dict):\n");
17551756

1756-
fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n");
1757-
fprintf(ofp, "\t\tprint 'Sample: {'+"
1758-
"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
1757+
fprintf(ofp, "\t\tprint(get_dict_as_string(event_fields_dict))\n");
1758+
fprintf(ofp, "\t\tprint('Sample: {'+"
1759+
"get_dict_as_string(perf_sample_dict['sample'], ', ')+'}')\n\n");
17591760

17601761
fprintf(ofp, "def print_header("
17611762
"event_name, cpu, secs, nsecs, pid, comm):\n"
1762-
"\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1763-
"(event_name, cpu, secs, nsecs, pid, comm),\n\n");
1763+
"\tprint(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1764+
"(event_name, cpu, secs, nsecs, pid, comm), end=\"\")\n\n");
17641765

17651766
fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
17661767
"\treturn delimiter.join"

0 commit comments

Comments
 (0)