perf tools: Generate a Python script compatible with Python 2 and 3
authorJeremy Cline <jeremy@jcline.org>
Tue, 8 May 2018 21:27:43 +0000 (21:27 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 11 Jul 2018 13:01:50 +0000 (10:01 -0300)
commit877cc639686b68c7de179a485544f4761e376b30
treeda8347f4ddf5a66806affae8ffd2f88660ea42fb
parent092150a25cb7bd6a79aa00bb1ad131063f58073d
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 <jeremy@jcline.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Herton Krzesinski <herton@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/0100016341a7278a-d178c724-2b0f-49ca-be93-80a7d51aaa0d-000000@email.amazonses.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/scripting-engines/trace-event-python.c