Skip to contentSkip to conversation

perf scripts python: Add Python 3 support to check-perf-trace.py

PATCH Superseded Seeteena Thoufeek 5 participants 34 messages Jan 17, 2019 – Jan 21, 2019 perf events

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to check-perf-trace.py

Support both Python 2 and Python 3 in check-perf-trace.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix indentation issue, replace spaces with tab

---
 tools/perf/scripts/python/check-perf-trace.py | 33 ++++++++++++++-------------
 1 file changed, 17 insertions(+), 16 deletions(-)
tools/perf/scripts/python/check-perf-trace.py +17 -16
index 334599c..91fc499 100644
@@ -7,6 +7,7 @@
 # events, etc.  Basically, if this script runs successfully and
 # displays expected results, Python scripting support should be ok.
 
+from __future__ import print_function
 import os
 import sys
 
@@ -19,7 +20,7 @@ from perf_trace_context import *
 unhandled = autodict()
 
 def trace_begin():
-	print "trace_begin"
+	print("trace_begin")
 	pass
 
 def trace_end():
@@ -31,10 +32,10 @@ def irq__softirq_entry(event_name, context, common_cpu,
 		print_header(event_name, common_cpu, common_secs, common_nsecs,
 			common_pid, common_comm)
 
-                print_uncommon(context)
+		print_uncommon(context)
 
-		print "vec=%s\n" % \
-		(symbol_str("irq__softirq_entry", "vec", vec)),
+		print("vec=%s\n" % \
+		(symbol_str("irq__softirq_entry", "vec", vec))),
 
 def kmem__kmalloc(event_name, context, common_cpu,
 	common_secs, common_nsecs, common_pid, common_comm,
@@ -43,13 +44,13 @@ def kmem__kmalloc(event_name, context, common_cpu,
 		print_header(event_name, common_cpu, common_secs, common_nsecs,
 			common_pid, common_comm)
 
-                print_uncommon(context)
+		print_uncommon(context)
 
-		print "call_site=%u, ptr=%u, bytes_req=%u, " \
+		print("call_site=%u, ptr=%u, bytes_req=%u, " \
 		"bytes_alloc=%u, gfp_flags=%s\n" % \
 		(call_site, ptr, bytes_req, bytes_alloc,
 
-		flag_str("kmem__kmalloc", "gfp_flags", gfp_flags)),
+		flag_str("kmem__kmalloc", "gfp_flags", gfp_flags))),
 
 def trace_unhandled(event_name, context, event_fields_dict):
     try:
@@ -58,25 +59,25 @@ def trace_unhandled(event_name, context, event_fields_dict):
         unhandled[event_name] = 1
 
 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)),
 
 # print trace fields not included in handler args
 def print_uncommon(context):
-    print "common_preempt_count=%d, common_flags=%s, common_lock_depth=%d, " \
+    print("common_preempt_count=%d, common_flags=%s, common_lock_depth=%d, " \
         % (common_pc(context), trace_flag_str(common_flags(context)), \
-               common_lock_depth(context))
+               common_lock_depth(context)))
 
 def print_unhandled():
     keys = unhandled.keys()
     if not keys:
         return
 
-    print "\nunhandled events:\n\n",
+    print("\nunhandled events:\n\n"),
 
-    print "%-40s  %10s\n" % ("event", "count"),
-    print "%-40s  %10s\n" % ("----------------------------------------", \
-                                 "-----------"),
+    print("%-40s  %10s\n" % ("event", "count")),
+    print("%-40s  %10s\n" % ("----------------------------------------", \
+                                 "-----------")),
 
     for event_name in keys:
-	print "%-40s  %10d\n" % (event_name, unhandled[event_name])
+        print("%-40s  %10d\n" % (event_name, unhandled[event_name]))
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to event_analyzing_sample.py

Support both Python 2 and Python 3 in event_analyzing_sample.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fixes indentation issue, replace spaces with tab. The "has_key()" method is deprecated in favor of the "in" operator. So incorporate those changes here.

---
 .../perf/scripts/python/event_analyzing_sample.py  | 57 +++++++++++-----------
 1 file changed, 29 insertions(+), 28 deletions(-)
tools/perf/scripts/python/event_analyzing_sample.py +29 -28
index 4e843b9..4ae239d 100644
@@ -14,6 +14,7 @@
 # generic events with the help of sqlite, and the 2nd one "show_pebs_ll" is
 # for a x86 HW PMU event: PEBS with load latency data.
 #
+from __future__ import print_function
 
 import os
 import sys
@@ -37,20 +38,20 @@ con = sqlite3.connect("/dev/shm/perf.db")
 con.isolation_level = None
 
 def trace_begin():
-	print "In trace_begin:\n"
+	print("In trace_begin:\n")
 
         #
         # Will create several tables at the start, pebs_ll is for PEBS data with
         # load latency info, while gen_events is for general event.
         #
-        con.execute("""
+	con.execute("""
                 create table if not exists gen_events (
                         name text,
                         symbol text,
                         comm text,
                         dso text
                 );""")
-        con.execute("""
+	con.execute("""
                 create table if not exists pebs_ll (
                         name text,
                         symbol text,
@@ -76,12 +77,12 @@ def process_event(param_dict):
         name       = param_dict["ev_name"]
 
         # Symbol and dso info are not always resolved
-        if (param_dict.has_key("dso")):
+        if ('dso' in param_dict):
                 dso = param_dict["dso"]
         else:
                 dso = "Unknown_dso"
 
-        if (param_dict.has_key("symbol")):
+        if ('symbol' in param_dict):
                 symbol = param_dict["symbol"]
         else:
                 symbol = "Unknown_symbol"
@@ -102,11 +103,11 @@ def insert_db(event):
                                 event.ip, event.status, event.dse, event.dla, event.lat))
 
 def trace_end():
-	print "In trace_end:\n"
+	print("In trace_end:\n")
         # We show the basic info for the 2 type of event classes
-        show_general_events()
-        show_pebs_ll()
-        con.close()
+	show_general_events()
+	show_pebs_ll()
+	con.close()
 
 #
 # As the event number may be very big, so we can't use linear way
@@ -123,29 +124,29 @@ def show_general_events():
         # Check the total record number in the table
         count = con.execute("select count(*) from gen_events")
         for t in count:
-                print "There is %d records in gen_events table" % t[0]
+                print("There is %d records in gen_events table" % t[0])
                 if t[0] == 0:
                         return
 
-        print "Statistics about the general events grouped by thread/symbol/dso: \n"
+        print("Statistics about the general events grouped by thread/symbol/dso: \n")
 
          # Group by thread
         commq = con.execute("select comm, count(comm) from gen_events group by comm order by -count(comm)")
-        print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42)
+        print("\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42))
         for row in commq:
-             print "%16s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%16s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
         # Group by symbol
-        print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58)
+        print("\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58))
         symbolq = con.execute("select symbol, count(symbol) from gen_events group by symbol order by -count(symbol)")
         for row in symbolq:
-             print "%32s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%32s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
         # Group by dso
-        print "\n%40s %8s %16s\n%s" % ("dso", "number", "histogram", "="*74)
+        print("\n%40s %8s %16s\n%s" % ("dso", "number", "histogram", "="*74))
         dsoq = con.execute("select dso, count(dso) from gen_events group by dso order by -count(dso)")
         for row in dsoq:
-             print "%40s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%40s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
 #
 # This function just shows the basic info, and we could do more with the
@@ -156,35 +157,35 @@ def show_pebs_ll():
 
         count = con.execute("select count(*) from pebs_ll")
         for t in count:
-                print "There is %d records in pebs_ll table" % t[0]
+                print("There is %d records in pebs_ll table" % t[0])
                 if t[0] == 0:
                         return
 
-        print "Statistics about the PEBS Load Latency events grouped by thread/symbol/dse/latency: \n"
+        print("Statistics about the PEBS Load Latency events grouped by thread/symbol/dse/latency: \n")
 
         # Group by thread
         commq = con.execute("select comm, count(comm) from pebs_ll group by comm order by -count(comm)")
-        print "\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42)
+        print("\n%16s %8s %16s\n%s" % ("comm", "number", "histogram", "="*42))
         for row in commq:
-             print "%16s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%16s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
         # Group by symbol
-        print "\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58)
+        print("\n%32s %8s %16s\n%s" % ("symbol", "number", "histogram", "="*58))
         symbolq = con.execute("select symbol, count(symbol) from pebs_ll group by symbol order by -count(symbol)")
         for row in symbolq:
-             print "%32s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%32s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
         # Group by dse
         dseq = con.execute("select dse, count(dse) from pebs_ll group by dse order by -count(dse)")
-        print "\n%32s %8s %16s\n%s" % ("dse", "number", "histogram", "="*58)
+        print("\n%32s %8s %16s\n%s" % ("dse", "number", "histogram", "="*58))
         for row in dseq:
-             print "%32s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%32s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
         # Group by latency
         latq = con.execute("select lat, count(lat) from pebs_ll group by lat order by lat")
-        print "\n%32s %8s %16s\n%s" % ("latency", "number", "histogram", "="*58)
+        print("\n%32s %8s %16s\n%s" % ("latency", "number", "histogram", "="*58))
         for row in latq:
-             print "%32s %8d     %s" % (row[0], row[1], num2sym(row[1]))
+             print("%32s %8d     %s" % (row[0], row[1], num2sym(row[1])))
 
 def trace_unhandled(event_name, context, event_fields_dict):
-		print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])
+		print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to exported-sql-viewer.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in exported-sql-viewer.py. ``print`` is now a function rather than a statement. This should have no functional change.

Add support of _pickle module in Python3

---
 tools/perf/scripts/python/exported-sql-viewer.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
tools/perf/scripts/python/exported-sql-viewer.py +13 -9
index f278ce5..a4aef58 100755
@@ -1,4 +1,3 @@
-#!/usr/bin/python2
 # SPDX-License-Identifier: GPL-2.0
 # exported-sql-viewer.py: view data from sql database
 # Copyright (c) 2014-2018, Intel Corporation.
@@ -87,12 +86,12 @@
 #                                                                              7fab593ea94f 48 8b 05 1a 15 22 00                            movq  0x22151a(%rip), %rax
 #                                                                              7fab593ea956 48 89 15 3b 13 22 00                            movq  %rdx, 0x22133b(%rip)
 # 8107675243232  2    ls       22011  22011  hardware interrupt     No         7fab593ea956 _dl_start+0x26 (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
+from __future__ import print_function
 
 import sys
 import weakref
 import threading
 import string
-import cPickle
 import re
 import os
 from PySide.QtCore import *
@@ -102,6 +101,11 @@ from decimal import *
 from ctypes import *
 from multiprocessing import Process, Array, Value, Event
 
+if sys.version_info[0] < 3:
+    import cPickle
+else:
+    import _pickle as cPickle
+
 # Data formatting helpers
 
 def tohex(ip):
@@ -1560,7 +1564,7 @@ class SQLTableDialogDataItem():
 					return str(lower_id)
 
 	def ConvertRelativeTime(self, val):
-		print "val ", val
+		print("val ", val)
 		mult = 1
 		suffix = val[-2:]
 		if suffix == "ms":
@@ -1582,29 +1586,29 @@ class SQLTableDialogDataItem():
 		return str(val)
 
 	def ConvertTimeRange(self, vrange):
-		print "vrange ", vrange
+		print("vrange ", vrange)
 		if vrange[0] == "":
 			vrange[0] = str(self.first_time)
 		if vrange[1] == "":
 			vrange[1] = str(self.last_time)
 		vrange[0] = self.ConvertRelativeTime(vrange[0])
 		vrange[1] = self.ConvertRelativeTime(vrange[1])
-		print "vrange2 ", vrange
+		print("vrange2 ", vrange)
 		if not self.IsNumber(vrange[0]) or not self.IsNumber(vrange[1]):
 			return False
-		print "ok1"
+		print("ok1")
 		beg_range = max(int(vrange[0]), self.first_time)
 		end_range = min(int(vrange[1]), self.last_time)
 		if beg_range > self.last_time or end_range < self.first_time:
 			return False
-		print "ok2"
+		print("ok2")
 		vrange[0] = self.BinarySearchTime(0, self.last_id, beg_range, True)
 		vrange[1] = self.BinarySearchTime(1, self.last_id + 1, end_range, False)
-		print "vrange3 ", vrange
+		print("vrange3 ", vrange)
 		return True
 
 	def AddTimeRange(self, value, ranges):
-		print "value ", value
+		print("value ", value)
 		n = value.count("-")
 		if n == 1:
 			pass
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to exported-sql-viewer.py

Review

Quoted 4 lines · Seeteena Thoufeek
(quoted diff) +4 -0
+if sys.version_info[0] < 3:
+    import cPickle
+else:
+    import _pickle as cPickle

Do you really need this?

pickle is already in Python2.

Also, did you test these changes on Python3?

I think you'll find you also need the following hunk otherwise you're running into Unicode diffs on Py3.

@@ -2590,8 +2590,8 @@ def Main():

is_sqlite3 = False
try:

-               f = open(dbname)
-               if f.read(15) == "SQLite format 3":
+               f = open(dbname, "rb")
+               if f.read(15) == b'SQLite format 3':

is_sqlite3 = True
f.close()
except:

Plus you need to handle the conversion of "print >> sys.stderr"

Attached is my version (against tip):

tools/perf/scripts/python/exported-sql-viewer.py +11 -11
@@ -91,7 +91,7 @@ import sys
 import weakref
 import threading
 import string
-import cPickle
+import pickle
 import re
 import os
 from PySide.QtCore import *
@@ -1559,7 +1559,7 @@ class SQLTableDialogDataItem():
 					return str(lower_id)
 
 	def ConvertRelativeTime(self, val):
-		print "val ", val
+		print("val ", val)
 		mult = 1
 		suffix = val[-2:]
 		if suffix == "ms":
@@ -1581,29 +1581,29 @@ class SQLTableDialogDataItem():
 		return str(val)
 
 	def ConvertTimeRange(self, vrange):
-		print "vrange ", vrange
+		print("vrange ", vrange)
 		if vrange[0] == "":
 			vrange[0] = str(self.first_time)
 		if vrange[1] == "":
 			vrange[1] = str(self.last_time)
 		vrange[0] = self.ConvertRelativeTime(vrange[0])
 		vrange[1] = self.ConvertRelativeTime(vrange[1])
-		print "vrange2 ", vrange
+		print("vrange2 ", vrange)
 		if not self.IsNumber(vrange[0]) or not self.IsNumber(vrange[1]):
 			return False
-		print "ok1"
+		print("ok1")
 		beg_range = max(int(vrange[0]), self.first_time)
 		end_range = min(int(vrange[1]), self.last_time)
 		if beg_range > self.last_time or end_range < self.first_time:
 			return False
-		print "ok2"
+		print("ok2")
 		vrange[0] = self.BinarySearchTime(0, self.last_id, beg_range, True)
 		vrange[1] = self.BinarySearchTime(1, self.last_id + 1, end_range, False)
-		print "vrange3 ", vrange
+		print("vrange3 ", vrange)
 		return True
 
 	def AddTimeRange(self, value, ranges):
-		print "value ", value
+		print("value ", value)
 		n = value.count("-")
 		if n == 1:
 			pass
@@ -2577,7 +2577,7 @@ class DBRef():
 
 def Main():
 	if (len(sys.argv) < 2):
-		print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}"
+		sys.stderr.write("Usage is: exported-sql-viewer.py {<database name> | --help-only}\n");
 		raise Exception("Too few arguments")
 
 	dbname = sys.argv[1]
@@ -2590,8 +2590,8 @@ def Main():
 
 	is_sqlite3 = False
 	try:
-		f = open(dbname)
-		if f.read(15) == "SQLite format 3":
+		f = open(dbname, "rb")
+		if f.read(15) == b'SQLite format 3':
 			is_sqlite3 = True
 		f.close()
 	except:
#4 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to exported-sql-viewer.py

Quoted 10 lines · On Fri, 18 Jan 2019 16:45:04 -0800 Tony Jones <[email protected]> wrote:

Review

Quoted 4 lines · Seeteena Thoufeek
(quoted diff) +4 -0
+if sys.version_info[0] < 3:
+    import cPickle
+else:
+    import _pickle as cPickle  

Do you really need this?

pickle is already in Python2.

Did you mean in Python3? I would agree that using it is better than importing the semi-hidden _pickle module.

That said, I'll echo the questions about testing. Pickle works in Python3, but it is fraught with all kinds of bytes/str and encoding issues; I've found it sufficiently fragile in practice that I really just try to avoid it. How have you verified that this script works under both versions of Python?

Thanks,

jon

#5 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to exported-sql-viewer.py

Quoted 12 lines · Jonathan Corbet
Quoted 9 lines · On Fri, 18 Jan 2019 16:45:04 -0800 Tony Jones <[email protected]> wrote:

Review

Quoted 4 lines · Seeteena Thoufeek
(quoted diff) +4 -0
+if sys.version_info[0] < 3:
+    import cPickle
+else:
+    import _pickle as cPickle  

Do you really need this?

Quoted 4 lines
Quoted 1 line

pickle is already in Python2.

Did you mean in Python3? I would agree that using it is better than importing the semi-hidden _pickle module.

No. I meant Python2 :)

pickle in Python2 is the python implementation
cPickle in Python2 is the C implementation.

Read: https://docs.python.org/3.1/whatsnew/3.0.html#library-changes
A common pattern in Python 2.x is to have one version of a module implemented in pure Python, with an optional accelerated version implemented as a C extension; for example, pickle and cPickle. This places the burden of importing the accelerated version and falling back on the pure Python version on each user of these modules. In Python 3.0, the accelerated versions are considered implementation details of the pure Python versions. Users should always import the standard version, which attempts to import the accelerated version and falls back to the pure Python version.

I my patchset "import pickle" was sufficient for Python2 and Python3 The question I suppose is whether this script, for Python2, needs the accelerated C implementation. I decided it didn't.

Tony

author
#6 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to exported-sql-viewer.py

In reply to Tony Jones
Quoted 26 lines · Tony Jones

Review

Quoted 4 lines · Seeteena Thoufeek
(quoted diff) +4 -0
+if sys.version_info[0] < 3:
+    import cPickle
+else:
+    import _pickle as cPickle

Do you really need this?

pickle is already in Python2.

Also, did you test these changes on Python3?

I think you'll find you also need the following hunk otherwise you're running into Unicode diffs on Py3.

tools/perf/scripts/python/exported-sql-viewer.py +2 -2
@@ -2590,8 +2590,8 @@ def Main():
 
         is_sqlite3 = False
         try:
-               f = open(dbname)
-               if f.read(15) == "SQLite format 3":
+               f = open(dbname, "rb")
+               if f.read(15) == b'SQLite format 3':
                         is_sqlite3 = True
                 f.close()
         except:
Quoted 1 line

Plus you need to handle the conversion of "print >> sys.stderr"

Thanks Tony. added changes to handle the conversion of "print >> sys.stderr" in export-to-postgresql.py and export-to-sqlite.py file as well.  revised patch posted.

Quoted 84 lines

Attached is my version (against tip):

tools/perf/scripts/python/exported-sql-viewer.py +11 -11
@@ -91,7 +91,7 @@ import sys
  import weakref
  import threading
  import string
-import cPickle
+import pickle
  import re
  import os
  from PySide.QtCore import *
@@ -1559,7 +1559,7 @@ class SQLTableDialogDataItem():
  					return str(lower_id)
 
  	def ConvertRelativeTime(self, val):
-		print "val ", val
+		print("val ", val)
  		mult = 1
  		suffix = val[-2:]
  		if suffix == "ms":
@@ -1581,29 +1581,29 @@ class SQLTableDialogDataItem():
  		return str(val)
 
  	def ConvertTimeRange(self, vrange):
-		print "vrange ", vrange
+		print("vrange ", vrange)
  		if vrange[0] == "":
  			vrange[0] = str(self.first_time)
  		if vrange[1] == "":
  			vrange[1] = str(self.last_time)
  		vrange[0] = self.ConvertRelativeTime(vrange[0])
  		vrange[1] = self.ConvertRelativeTime(vrange[1])
-		print "vrange2 ", vrange
+		print("vrange2 ", vrange)
  		if not self.IsNumber(vrange[0]) or not self.IsNumber(vrange[1]):
  			return False
-		print "ok1"
+		print("ok1")
  		beg_range = max(int(vrange[0]), self.first_time)
  		end_range = min(int(vrange[1]), self.last_time)
  		if beg_range > self.last_time or end_range < self.first_time:
  			return False
-		print "ok2"
+		print("ok2")
  		vrange[0] = self.BinarySearchTime(0, self.last_id, beg_range, True)
  		vrange[1] = self.BinarySearchTime(1, self.last_id + 1, end_range, False)
-		print "vrange3 ", vrange
+		print("vrange3 ", vrange)
  		return True
 
  	def AddTimeRange(self, value, ranges):
-		print "value ", value
+		print("value ", value)
  		n = value.count("-")
  		if n == 1:
  			pass
@@ -2577,7 +2577,7 @@ class DBRef():
 
  def Main():
  	if (len(sys.argv) < 2):
-		print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}"
+		sys.stderr.write("Usage is: exported-sql-viewer.py {<database name> | --help-only}\n");
  		raise Exception("Too few arguments")
 
  	dbname = sys.argv[1]
@@ -2590,8 +2590,8 @@ def Main():
 
  	is_sqlite3 = False
  	try:
-		f = open(dbname)
-		if f.read(15) == "SQLite format 3":
+		f = open(dbname, "rb")
+		if f.read(15) == b'SQLite format 3':
  			is_sqlite3 = True
  		f.close()
  	except:
#7 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to export-to-sqlite.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in export-to-sqlite.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/export-to-sqlite.py | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
tools/perf/scripts/python/export-to-sqlite.py +6 -5
index 245caf2..66be899 100644
@@ -9,6 +9,7 @@
 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 # more details.
+from __future__ import print_function
 
 import os
 import sys
@@ -100,7 +101,7 @@ def do_query_(q):
 		return
 	raise Exception("Query failed: " + q.lastError().text())
 
-print datetime.datetime.today(), "Creating database..."
+print(datetime.datetime.today(), "Creating database...")
 
 db_exists = False
 try:
@@ -376,7 +377,7 @@ if perf_db_export_calls:
 	call_query.prepare("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
 
 def trace_begin():
-	print datetime.datetime.today(), "Writing records..."
+	print(datetime.datetime.today(), "Writing records...")
 	do_query(query, 'BEGIN TRANSACTION')
 	# id == 0 means unknown.  It is easier to create records for them than replace the zeroes with NULLs
 	evsel_table(0, "unknown")
@@ -394,13 +395,13 @@ unhandled_count = 0
 def trace_end():
 	do_query(query, 'END TRANSACTION')
 
-	print datetime.datetime.today(), "Adding indexes"
+	print(datetime.datetime.today(), "Adding indexes")
 	if perf_db_export_calls:
 		do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
 
 	if (unhandled_count):
-		print datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events"
-	print datetime.datetime.today(), "Done"
+		print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events")
+	print(datetime.datetime.today(), "Done")
 
 def trace_unhandled(event_name, context, event_fields_dict):
 	global unhandled_count
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to export-to-sqlite.py

Quoted 3 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in export-to-sqlite.py. ``print`` is now a function rather than a statement. This should have no functional change.

I don't see any changes handling the following:

$ git annotate tools/perf/scripts/python/export-to-sqlite.py | grep ">> sys"
564b9527d1ccf	(Adrian Hunter	2017-08-03 11:31:28 +0300	64)	print >> sys.stderr, "Usage is: export-to-sqlite.py <database name> [<columns>] [<calls>] [<callchains>]"
564b9527d1ccf	(Adrian Hunter	2017-08-03 11:31:28 +0300	65)	print >> sys.stderr, "where:	columns		'all' or 'branches'"
564b9527d1ccf	(Adrian Hunter	2017-08-03 11:31:28 +0300	66)	print >> sys.stderr, "		calls		'calls' => create calls and call_paths table"
564b9527d1ccf	(Adrian Hunter	2017-08-03 11:31:28 +0300	67)	print >> sys.stderr, "		callchains	'callchains' => create call_paths table"
$ echo 'import sys ; print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}"' | python2
Usage is: exported-sql-viewer.py {<database name> | --help-only}
$ echo 'import sys ; print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}"' | python3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?

They are best handled via conversion to sys.stderr.write() since sys is already imported.

Tony

#9 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to futex-contention.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in futex-contention.py. ``print`` is now a function rather than a statement. This should have no functional change.

The "has_key()" method is deprecated in favor of the "in" operator. So incorporate those changes here.

Please enter the commit message for your changes. Lines starting

---
 tools/perf/scripts/python/futex-contention.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/perf/scripts/python/futex-contention.py +5 -4
index 0f5cf43..794a9f0 100644
@@ -9,6 +9,7 @@
 # to perf python scripting.
 #
 # Measures futex contention
+from __future__ import print_function
 
 import os, sys
 sys.path.append(os.environ['PERF_EXEC_PATH'] + '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
@@ -33,18 +34,18 @@ def syscalls__sys_enter_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
 
 def syscalls__sys_exit_futex(event, ctxt, cpu, s, ns, tid, comm, callchain,
 			     nr, ret):
-	if thread_blocktime.has_key(tid):
+	if (tid in thread_blocktime):
 		elapsed = nsecs(s, ns) - thread_blocktime[tid]
 		add_stats(lock_waits, (tid, thread_thislock[tid]), elapsed)
 		del thread_blocktime[tid]
 		del thread_thislock[tid]
 
 def trace_begin():
-	print "Press control+C to stop and show the summary"
+	print("Press control+C to stop and show the summary")
 
 def trace_end():
 	for (tid, lock) in lock_waits:
 		min, max, avg, count = lock_waits[tid, lock]
-		print "%s[%d] lock %x contended %d times, %d avg ns" % \
-		      (process_names[tid], tid, lock, count, avg)
+		print("%s[%d] lock %x contended %d times, %d avg ns" % \
+		      (process_names[tid], tid, lock, count, avg))
 
Signature · 1 line
1.8.3.1
#10 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to intel-pt-events.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in intel-pt-events.py.``print`` is now a function rather than a statement. This should have no functional change.

Fixes indentation issue, replace spaces with tab.

---
 tools/perf/scripts/python/intel-pt-events.py | 57 ++++++++++++++--------------
 1 file changed, 29 insertions(+), 28 deletions(-)
tools/perf/scripts/python/intel-pt-events.py +29 -28
index b19172d..de153f7 100644
@@ -9,6 +9,7 @@
 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 # more details.
+from __future__ import print_function
 
 import os
 import sys
@@ -22,34 +23,34 @@ sys.path.append(os.environ['PERF_EXEC_PATH'] + \
 #from Core import *
 
 def trace_begin():
-	print "Intel PT Power Events and PTWRITE"
+	print("Intel PT Power Events and PTWRITE")
 
 def trace_end():
-	print "End"
+	print("End")
 
 def trace_unhandled(event_name, context, event_fields_dict):
-		print ' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())])
+		print(' '.join(['%s=%s'%(k,str(v))for k,v in sorted(event_fields_dict.items())]))
 
 def print_ptwrite(raw_buf):
 	data = struct.unpack_from("<IQ", raw_buf)
 	flags = data[0]
 	payload = data[1]
 	exact_ip = flags & 1
-	print "IP: %u payload: %#x" % (exact_ip, payload),
+	print("IP: %u payload: %#x" % (exact_ip, payload)),
 
 def print_cbr(raw_buf):
 	data = struct.unpack_from("<BBBBII", raw_buf)
 	cbr = data[0]
 	f = (data[4] + 500) / 1000
 	p = ((cbr * 1000 / data[2]) + 5) / 10
-	print "%3u  freq: %4u MHz  (%3u%%)" % (cbr, f, p),
+	print("%3u  freq: %4u MHz  (%3u%%)" % (cbr, f, p)),
 
 def print_mwait(raw_buf):
 	data = struct.unpack_from("<IQ", raw_buf)
 	payload = data[1]
 	hints = payload & 0xff
 	extensions = (payload >> 32) & 0x3
-	print "hints: %#x extensions: %#x" % (hints, extensions),
+	print("hints: %#x extensions: %#x" % (hints, extensions)),
 
 def print_pwre(raw_buf):
 	data = struct.unpack_from("<IQ", raw_buf)
@@ -57,13 +58,13 @@ def print_pwre(raw_buf):
 	hw = (payload >> 7) & 1
 	cstate = (payload >> 12) & 0xf
 	subcstate = (payload >> 8) & 0xf
-	print "hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate),
+	print("hw: %u cstate: %u sub-cstate: %u" % (hw, cstate, subcstate)),
 
 def print_exstop(raw_buf):
 	data = struct.unpack_from("<I", raw_buf)
 	flags = data[0]
 	exact_ip = flags & 1
-	print "IP: %u" % (exact_ip),
+	print("IP: %u" % (exact_ip)),
 
 def print_pwrx(raw_buf):
 	data = struct.unpack_from("<IQ", raw_buf)
@@ -71,37 +72,37 @@ def print_pwrx(raw_buf):
 	deepest_cstate = payload & 0xf
 	last_cstate = (payload >> 4) & 0xf
 	wake_reason = (payload >> 8) & 0xf
-	print "deepest cstate: %u last cstate: %u wake reason: %#x" % (deepest_cstate, last_cstate, wake_reason),
+	print("deepest cstate: %u last cstate: %u wake reason: %#x" % (deepest_cstate, last_cstate, wake_reason)),
 
 def print_common_start(comm, sample, name):
 	ts = sample["time"]
 	cpu = sample["cpu"]
 	pid = sample["pid"]
 	tid = sample["tid"]
-	print "%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name),
+	print("%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name)),
 
 def print_common_ip(sample, symbol, dso):
 	ip = sample["ip"]
-	print "%16x %s (%s)" % (ip, symbol, dso)
+	print("%16x %s (%s)" % (ip, symbol, dso))
 
 def process_event(param_dict):
-        event_attr = param_dict["attr"]
-        sample     = param_dict["sample"]
-        raw_buf    = param_dict["raw_buf"]
-        comm       = param_dict["comm"]
-        name       = param_dict["ev_name"]
-
-        # Symbol and dso info are not always resolved
-        if (param_dict.has_key("dso")):
-                dso = param_dict["dso"]
-        else:
-                dso = "[unknown]"
-
-        if (param_dict.has_key("symbol")):
-                symbol = param_dict["symbol"]
-        else:
-                symbol = "[unknown]"
-
+	event_attr = param_dict["attr"]
+	sample     = param_dict["sample"]
+	raw_buf    = param_dict["raw_buf"]
+	comm       = param_dict["comm"]
+	name       = param_dict["ev_name"]
+
+	# Symbol and dso info are not always resolved
+	if ('dso' in param_dict):
+		dso = param_dict["dso"]
+	else:
+		dso = "[unknown]"
+
+	if ('symbol' in param_dict):
+		symbol = param_dict["symbol"]
+	else:
+		symbol = "[unknown]"
+
 	if name == "ptwrite":
 		print_common_start(comm, sample, name)
 		print_ptwrite(raw_buf)
Signature · 1 line
1.8.3.1
#11 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to intel-pt-events.py

Quoted 5 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in intel-pt-events.py.``print`` is now a function rather than a statement. This should have no functional change.

Fixes indentation issue, replace spaces with tab.

Again, trailing comma use is incorrect.

tools/perf/scripts/python/intel-pt-events.py def print_pwrx(raw_buf): · line 82

Quoted 2 lines
tools/perf/scripts/python/intel-pt-events.py +1 -1
-	print "%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name),
+	print("%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name)),

print ("%16s %5u/%-5u [%03u] %9u.%09u %7s:" % (comm, pid, tid, cpu, ts / 1000000000, ts %1000000000, name), end='')

and so on for rest of changes.

Tony

#12 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to mem-phys-addr.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in mem-phys-addr.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix lambda syntax error.

---
 tools/perf/scripts/python/mem-phys-addr.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
tools/perf/scripts/python/mem-phys-addr.py +7 -6
index ebee2c5..e30fc9d 100644
@@ -3,6 +3,7 @@
 #
 # Copyright (c) 2018, Intel Corporation.
 
+from __future__ import print_function
 from __future__ import division
 import os
 import sys
@@ -38,14 +39,14 @@ def parse_iomem():
 			pmem.append(long(m[1], 16))
 
 def print_memory_type():
-	print "Event: %s" % (event_name)
-	print "%-40s  %10s  %10s\n" % ("Memory type", "count", "percentage"),
-	print "%-40s  %10s  %10s\n" % ("----------------------------------------", \
-					"-----------", "-----------"),
+	print("Event: %s" % (event_name))
+	print("%-40s  %10s  %10s\n" % ("Memory type", "count", "percentage")),
+	print("%-40s  %10s  %10s\n" % ("----------------------------------------", \
+					"-----------", "-----------")),
 	total = sum(load_mem_type_cnt.values())
 	for mem_type, count in sorted(load_mem_type_cnt.most_common(), \
-					key = lambda(k, v): (v, k), reverse = True):
-		print "%-40s  %10d  %10.1f%%\n" % (mem_type, count, 100 * count / total),
+					key = lambda k_v: (k_v[1], k_v[0]), reverse = True):
+		print("%-40s  %10d  %10.1f%%\n" % (mem_type, count, 100 * count / total)),
 
 def trace_begin():
 	parse_iomem()
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to powerpc-hcalls.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in powerpc-hcalls.py. ``print`` is now a function rather than a statement. This should have no functional change.

The "has_key()" method is deprecated in favor of the "in" operator. So incorporate those changes here.

---
 tools/perf/scripts/python/powerpc-hcalls.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
tools/perf/scripts/python/powerpc-hcalls.py +8 -7
index 00e0e74..19ff684 100644
@@ -3,6 +3,7 @@
 # Copyright (C) 2018 Ravi Bangoria, IBM Corporation
 #
 # Hypervisor call statisics
+from __future__ import print_function
 
 import os
 import sys
@@ -149,7 +150,7 @@ hcall_table = {
 }
 
 def hcall_table_lookup(opcode):
-	if (hcall_table.has_key(opcode)):
+	if (opcode in hcall_table):
 		return hcall_table[opcode]
 	else:
 		return opcode
@@ -157,8 +158,8 @@ def hcall_table_lookup(opcode):
 print_ptrn = '%-28s%10s%10s%10s%10s'
 
 def trace_end():
-	print print_ptrn % ('hcall', 'count', 'min(ns)', 'max(ns)', 'avg(ns)')
-	print '-' * 68
+	print(print_ptrn % ('hcall', 'count', 'min(ns)', 'max(ns)', 'avg(ns)'))
+	print('-' * 68)
 	for opcode in output:
 		h_name = hcall_table_lookup(opcode)
 		time = output[opcode]['time']
@@ -166,14 +167,14 @@ def trace_end():
 		min_t = output[opcode]['min']
 		max_t = output[opcode]['max']
 
-		print print_ptrn % (h_name, cnt, min_t, max_t, time/cnt)
+		print(print_ptrn % (h_name, cnt, min_t, max_t, time/cnt))
 
 def powerpc__hcall_exit(name, context, cpu, sec, nsec, pid, comm, callchain,
 			opcode, retval):
-	if (d_enter.has_key(cpu) and d_enter[cpu].has_key(opcode)):
+	if ((cpu in d_enter) and (opcode in d_enter[cpu])):
 		diff = nsecs(sec, nsec) - d_enter[cpu][opcode]
 
-		if (output.has_key(opcode)):
+		if (opcode in output):
 			output[opcode]['time'] += diff
 			output[opcode]['cnt'] += 1
 			if (output[opcode]['min'] > diff):
@@ -194,7 +195,7 @@ def powerpc__hcall_exit(name, context, cpu, sec, nsec, pid, comm, callchain,
 
 def powerpc__hcall_entry(event_name, context, cpu, sec, nsec, pid, comm,
 			 callchain, opcode):
-		if (d_enter.has_key(cpu)):
+		if (cpu in d_enter):
 			d_enter[cpu][opcode] = nsecs(sec, nsec)
 		else:
 			d_enter[cpu] = {opcode: nsecs(sec, nsec)}
Signature · 1 line
1.8.3.1
#14 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to sctop.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in sctop.py.``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/sctop.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
tools/perf/scripts/python/sctop.py +8 -7
index 61621b9..d059a2a 100644
@@ -7,6 +7,7 @@
 # [comm] are displayed. If an [interval] arg is specified, the display
 # will be refreshed every [interval] seconds.  The default interval is
 # 3 seconds.
+from __future__ import print_function
 
 import os, sys, thread, time
 
@@ -62,18 +63,18 @@ def print_syscall_totals(interval):
 	while 1:
 		clear_term()
 		if for_comm is not None:
-			print "\nsyscall events for %s:\n\n" % (for_comm),
+			print("\nsyscall events for %s:\n\n" % (for_comm)),
 		else:
-			print "\nsyscall events:\n\n",
+			print("\nsyscall events:\n\n"),
 
-		print "%-40s  %10s\n" % ("event", "count"),
-		print "%-40s  %10s\n" % ("----------------------------------------", \
-						 "----------"),
+		print("%-40s  %10s\n" % ("event", "count")),
+		print("%-40s  %10s\n" % ("----------------------------------------", \
+						 "----------")),
 
-		for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
+		for id, val in sorted(syscalls.iteritems(), key = lambda k_v: (k_v[1], k_v[0]), \
 					      reverse = True):
 			try:
-				print "%-40s  %10d\n" % (syscall_name(id), val),
+				print("%-40s  %10d\n" % (syscall_name(id), val)),
 			except TypeError:
 				pass
 		syscalls.clear()
Signature · 1 line
1.8.3.1
#15 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to sctop.py

Quoted 1 line · Seeteena Thoufeek

+from __future__ import print_function

Again, you don't need this.

tools/perf/scripts/python/sctop.py def print_syscall_totals(interval): · line 66

Quoted 2 lines
tools/perf/scripts/python/sctop.py +1 -1
-			print "\nsyscall events for %s:\n\n" % (for_comm),
+			print("\nsyscall events for %s:\n\n" % (for_comm)),

same comments regarding trailing comma usage

$ git annotate tools/perf/scripts/python/sctop.py | grep thread
2e7d1e3fb8043	(Arnaldo Carvalho de Melo	2010-10-25 18:39:20 -0200	11)import os, sys, thread, time
47902f3611b39	(Tom Zanussi	2010-04-01 23:59:23 -0500	42)	thread.start_new_thread(print_syscall_totals, (interval,))

The low level threading api has changed in the most recent Python versions. I think you'll find you also need:

@@ -8,7 +8,12 @@

 # will be refreshed every [interval] seconds.  The default interval is
 # 3 seconds.

-import os, sys, thread, time
+import os, sys, time
+
+try:
+ import thread
+except ImportError:
+ import _thread as thread

Tony

#16 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to stackcollapse.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in stackcollapse.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/stackcollapse.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/scripts/python/stackcollapse.py +2 -1
index 1697b5e..f77bc0d 100755
@@ -18,6 +18,7 @@
 #
 # Written by Paolo Bonzini <[email protected]>
 # Based on Brendan Gregg's stackcollapse-perf.pl script.
+from __future__ import print_function
 
 import os
 import sys
@@ -123,4 +124,4 @@ def trace_end():
     list = lines.keys()
     list.sort()
     for stack in list:
-        print "%s %d" % (stack, lines[stack])
+        print("%s %d" % (stack, lines[stack]))
Signature · 1 line
1.8.3.1
#17 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to stackcollapse.py

Quoted 14 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in stackcollapse.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/stackcollapse.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/scripts/python/stackcollapse.py +0 -0
index 1697b5e..f77bc0d 100755

tools/perf/scripts/python/stackcollapse.py line 21

Quoted 4 lines
tools/perf/scripts/python/stackcollapse.py +1 -0
@@ -18,6 +18,7 @@
 #
 # Written by Paolo Bonzini <[email protected]>
 # Based on Brendan Gregg's stackcollapse-perf.pl script.
+from __future__ import print_function

Again, not necessary.

tools/perf/scripts/python/stackcollapse.py def trace_end(): · line 127

Quoted 9 lines
tools/perf/scripts/python/stackcollapse.py +1 -1
 import os
 import sys
@@ -123,4 +124,4 @@ def trace_end():
     list = lines.keys()
     list.sort()
     for stack in list:
-        print "%s %d" % (stack, lines[stack])
+        print("%s %d" % (stack, lines[stack]))

Did you test any of these changes with Python3?

If you run 'ldd /usr/bin/perf | grep python' when you've built with PYTHON=python3, what do you see?

$ ldd /usr/bin/perf | grep python
	libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007fea66701000)

If you do, you'll run into this error:

$ /usr/bin/perf script -s scripts/python/stackcollapse.py -i /tmp/perf.data
Traceback (most recent call last):
  File "scripts/python/stackcollapse.py", line 124, in trace_end
    list.sort()
AttributeError: 'dict_keys' object has no attribute 'sort'
Fatal Python error: problem in Python trace event handler

You need the following change in addition:

tools/perf/scripts/python/stackcollapse.py +2 -3
@@ -120,7 +120,6 @@ def process_event(param_dict):
     lines[stack_string] = lines[stack_string] + 1
 
 def trace_end():
-    list = lines.keys()
-    list.sort()
+    list = sorted(lines)
     for stack in list:
-        print "%s %d" % (stack, lines[stack])
+        print ("%s %d" % (stack, lines[stack]))

As I said in a different post, I'd not yet posted my series as I was still trying to fix the failure of the "import perf" testcase (aka tools/perf/python) and (apologies) I'd missed your V1 but maybe I should post them as they've been thoroughly integrated into perf and tested with python2 and python3.

The full patchset is in SLE15-SP1 beta2 which I know IBM has access to. A backport is also in Factory.

Tony

#18 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to syscall-counts-by-pid.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in syscall-counts-by-pid.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix lambda syntax error.

---
 tools/perf/scripts/python/syscall-counts-by-pid.py | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tools/perf/scripts/python/syscall-counts-by-pid.py +10 -9
index daf314c..3146094 100644
@@ -4,6 +4,7 @@
 #
 # Displays system-wide system call totals, broken down by syscall.
 # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+from __future__ import print_function
 
 import os, sys
 
@@ -31,7 +32,7 @@ if len(sys.argv) > 1:
 syscalls = autodict()
 
 def trace_begin():
-	print "Press control+C to stop and show the summary"
+	print("Press control+C to stop and show the summary")
 
 def trace_end():
 	print_syscall_totals()
@@ -55,20 +56,20 @@ def syscalls__sys_enter(event_name, context, common_cpu,
 
 def print_syscall_totals():
     if for_comm is not None:
-	    print "\nsyscall events for %s:\n\n" % (for_comm),
+	    print("\nsyscall events for %s:\n\n" % (for_comm)),
     else:
-	    print "\nsyscall events by comm/pid:\n\n",
+	    print("\nsyscall events by comm/pid:\n\n"),
 
-    print "%-40s  %10s\n" % ("comm [pid]/syscalls", "count"),
-    print "%-40s  %10s\n" % ("----------------------------------------", \
-                                 "----------"),
+    print("%-40s  %10s\n" % ("comm [pid]/syscalls", "count")),
+    print("%-40s  %10s\n" % ("----------------------------------------", \
+                                 "----------")),
 
     comm_keys = syscalls.keys()
     for comm in comm_keys:
 	    pid_keys = syscalls[comm].keys()
 	    for pid in pid_keys:
-		    print "\n%s [%d]\n" % (comm, pid),
+		    print("\n%s [%d]\n" % (comm, pid)),
 		    id_keys = syscalls[comm][pid].keys()
 		    for id, val in sorted(syscalls[comm][pid].iteritems(), \
-				  key = lambda(k, v): (v, k),  reverse = True):
-			    print "  %-38s  %10d\n" % (syscall_name(id), val),
+				  key = lambda k_v: (k_v[1], k_v[0]),  reverse = True):
+			    print("  %-38s  %10d\n" % (syscall_name(id), val)),
Signature · 1 line
1.8.3.1
#19 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to syscall-counts-by-pid.py

tools/perf/scripts/python/syscall-counts-by-pid.py def syscalls__sys_enter(event_name, context, common_cpu, · line 65

Quoted 12 lines · Seeteena Thoufeek
tools/perf/scripts/python/syscall-counts-by-pid.py +5 -5
-	    print "\nsyscall events for %s:\n\n" % (for_comm),
+	    print("\nsyscall events for %s:\n\n" % (for_comm)),
     else:
-	    print "\nsyscall events by comm/pid:\n\n",
+	    print("\nsyscall events by comm/pid:\n\n"),
 
-    print "%-40s  %10s\n" % ("comm [pid]/syscalls", "count"),
-    print "%-40s  %10s\n" % ("----------------------------------------", \
-                                 "----------"),
+    print("%-40s  %10s\n" % ("comm [pid]/syscalls", "count")),
+    print("%-40s  %10s\n" % ("----------------------------------------", \
+                                 "----------")),

Is the 'print (x),' [trailing comma] syntax valid for function syntax?

Print "x", in Py2 means suppress the trailing newline.

You need to actually run the scripts (old, new PYTHON=python2, new PYTHON=python3) and compare the output.

This:
print "%-40s %10s\n" % ("comm [pid]/syscalls", "count"),

can be reworked as:
print ("%-40s %10s" % ("comm [pid]/syscalls", "count"))

See: https://build.opensuse.org/package/view_file/devel:tools/perf/port-failed-syscalls-by-pid-script-to-python3.patch?expand=1

#20 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to syscall-counts.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in syscall-counts.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix lambda syntax error

---
 tools/perf/scripts/python/syscall-counts.py | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tools/perf/scripts/python/syscall-counts.py +9 -8
index e66a773..6ca67bd 100644
@@ -4,6 +4,7 @@
 #
 # Displays system-wide system call totals, broken down by syscall.
 # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+from __future__ import print_function
 
 import os
 import sys
@@ -28,7 +29,7 @@ if len(sys.argv) > 1:
 syscalls = autodict()
 
 def trace_begin():
-	print "Press control+C to stop and show the summary"
+	print("Press control+C to stop and show the summary")
 
 def trace_end():
 	print_syscall_totals()
@@ -51,14 +52,14 @@ def syscalls__sys_enter(event_name, context, common_cpu,
 
 def print_syscall_totals():
     if for_comm is not None:
-	    print "\nsyscall events for %s:\n\n" % (for_comm),
+	    print("\nsyscall events for %s:\n\n" % (for_comm)),
     else:
-	    print "\nsyscall events:\n\n",
+	    print("\nsyscall events:\n\n"),
 
-    print "%-40s  %10s\n" % ("event", "count"),
-    print "%-40s  %10s\n" % ("----------------------------------------", \
-                                 "-----------"),
+    print("%-40s  %10s\n" % ("event", "count")),
+    print("%-40s  %10s\n" % ("----------------------------------------", \
+                                 "-----------")),
 
-    for id, val in sorted(syscalls.iteritems(), key = lambda(k, v): (v, k), \
+    for id, val in sorted(syscalls.iteritems(), key = lambda k_v: (k_v[1], k_v[0]), \
 				  reverse = True):
-	    print "%-40s  %10d\n" % (syscall_name(id), val),
+	    print("%-40s  %10d\n" % (syscall_name(id), val)),
Signature · 1 line
1.8.3.1
#21 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to export-to-postgresql.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in export-to-postgresql.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/export-to-postgresql.py | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
tools/perf/scripts/python/export-to-postgresql.py +9 -8
index 0564dd7..82e10a6 100644
@@ -9,6 +9,7 @@
 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 # more details.
+from __future__ import print_function
 
 import os
 import sys
@@ -273,7 +274,7 @@ def do_query(q, s):
 		return
 	raise Exception("Query failed: " + q.lastError().text())
 
-print datetime.datetime.today(), "Creating database..."
+print(datetime.datetime.today(), "Creating database...")
 
 db = QSqlDatabase.addDatabase('QPSQL')
 query = QSqlQuery(db)
@@ -564,7 +565,7 @@ if perf_db_export_calls:
 	call_file		= open_output_file("call_table.bin")
 
 def trace_begin():
-	print datetime.datetime.today(), "Writing to intermediate files..."
+	print(datetime.datetime.today(), "Writing to intermediate files...")
 	# id == 0 means unknown.  It is easier to create records for them than replace the zeroes with NULLs
 	evsel_table(0, "unknown")
 	machine_table(0, 0, "unknown")
@@ -579,7 +580,7 @@ def trace_begin():
 unhandled_count = 0
 
 def trace_end():
-	print datetime.datetime.today(), "Copying to database..."
+	print(datetime.datetime.today(), "Copying to database...")
 	copy_output_file(evsel_file,		"selected_events")
 	copy_output_file(machine_file,		"machines")
 	copy_output_file(thread_file,		"threads")
@@ -594,7 +595,7 @@ def trace_end():
 	if perf_db_export_calls:
 		copy_output_file(call_file,		"calls")
 
-	print datetime.datetime.today(), "Removing intermediate files..."
+	print(datetime.datetime.today(), "Removing intermediate files...")
 	remove_output_file(evsel_file)
 	remove_output_file(machine_file)
 	remove_output_file(thread_file)
@@ -609,7 +610,7 @@ def trace_end():
 	if perf_db_export_calls:
 		remove_output_file(call_file)
 	os.rmdir(output_dir_name)
-	print datetime.datetime.today(), "Adding primary keys"
+	print(datetime.datetime.today(), "Adding primary keys")
 	do_query(query, 'ALTER TABLE selected_events ADD PRIMARY KEY (id)')
 	do_query(query, 'ALTER TABLE machines        ADD PRIMARY KEY (id)')
 	do_query(query, 'ALTER TABLE threads         ADD PRIMARY KEY (id)')
@@ -624,7 +625,7 @@ def trace_end():
 	if perf_db_export_calls:
 		do_query(query, 'ALTER TABLE calls           ADD PRIMARY KEY (id)')
 
-	print datetime.datetime.today(), "Adding foreign keys"
+	print(datetime.datetime.today(), "Adding foreign keys")
 	do_query(query, 'ALTER TABLE threads '
 					'ADD CONSTRAINT machinefk  FOREIGN KEY (machine_id)   REFERENCES machines   (id),'
 					'ADD CONSTRAINT processfk  FOREIGN KEY (process_id)   REFERENCES threads    (id)')
@@ -659,8 +660,8 @@ def trace_end():
 		do_query(query, 'CREATE INDEX pcpid_idx ON calls (parent_call_path_id)')
 
 	if (unhandled_count):
-		print datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events"
-	print datetime.datetime.today(), "Done"
+		print(datetime.datetime.today(), "Warning: ", unhandled_count, " unhandled events")
+	print(datetime.datetime.today(), "Done")
 
 def trace_unhandled(event_name, context, event_fields_dict):
 	global unhandled_count
Signature · 1 line
1.8.3.1
#22 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to stat-cpi.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in stat-cpi.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/stat-cpi.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/scripts/python/stat-cpi.py +1 -1
index 8410672..1d8e8b7 100644
@@ -59,7 +59,7 @@ def stat__interval(time):
             if ins != 0:
                 cpi = cyc/float(ins)
 
-            print "%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins)
+            print("%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins))
 
 def trace_end():
     pass
Signature · 1 line
1.8.3.1
#23 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to net_dropmonitor.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in net_dropmonitor.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/net_dropmonitor.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/perf/scripts/python/net_dropmonitor.py +5 -4
index a150164..88fe4a2 100755
@@ -1,5 +1,6 @@
 # Monitor the system for dropped packets and proudce a report of drop locations and counts
 # SPDX-License-Identifier: GPL-2.0
+from __future__ import print_function
 
 import os
 import sys
@@ -50,19 +51,19 @@ def get_sym(sloc):
 		return (None, 0)
 
 def print_drop_table():
-	print "%25s %25s %25s" % ("LOCATION", "OFFSET", "COUNT")
+	print("%25s %25s %25s" % ("LOCATION", "OFFSET", "COUNT"))
 	for i in drop_log.keys():
 		(sym, off) = get_sym(i)
 		if sym == None:
 			sym = i
-		print "%25s %25s %25s" % (sym, off, drop_log[i])
+		print("%25s %25s %25s" % (sym, off, drop_log[i]))
 
 
 def trace_begin():
-	print "Starting trace (Ctrl-C to dump results)"
+	print("Starting trace (Ctrl-C to dump results)")
 
 def trace_end():
-	print "Gathering kallsyms data"
+	print("Gathering kallsyms data")
 	get_kallsyms_table()
 	print_drop_table()
 
Signature · 1 line
1.8.3.1
#24 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to netdev-times.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in netdev-times.py. ``print`` is now a function rather than a statement. This should have no functional change.

---
 tools/perf/scripts/python/netdev-times.py | 77 ++++++++++++++++---------------
 1 file changed, 39 insertions(+), 38 deletions(-)
tools/perf/scripts/python/netdev-times.py +39 -38
index 9b2050f..c25965c 100644
@@ -7,6 +7,7 @@
 # rx: show only rx chart
 # dev=: show only thing related to specified device
 # debug: work with debug mode. It shows buffer status.
+from __future__ import print_function
 
 import os
 import sys
@@ -61,12 +62,12 @@ def diff_msec(src, dst):
 def print_transmit(hunk):
 	if dev != 0 and hunk['dev'].find(dev) < 0:
 		return
-	print "%7s %5d %6d.%06dsec %12.3fmsec      %12.3fmsec" % \
+	print("%7s %5d %6d.%06dsec %12.3fmsec      %12.3fmsec" % \
 		(hunk['dev'], hunk['len'],
 		nsecs_secs(hunk['queue_t']),
 		nsecs_nsecs(hunk['queue_t'])/1000,
 		diff_msec(hunk['queue_t'], hunk['xmit_t']),
-		diff_msec(hunk['xmit_t'], hunk['free_t']))
+		diff_msec(hunk['xmit_t'], hunk['free_t'])))
 
 # Format for displaying rx packet processing
 PF_IRQ_ENTRY= "  irq_entry(+%.3fmsec irq=%d:%s)"
@@ -98,55 +99,55 @@ def print_receive(hunk):
 	if show_hunk == 0:
 		return
 
-	print "%d.%06dsec cpu=%d" % \
-		(nsecs_secs(base_t), nsecs_nsecs(base_t)/1000, cpu)
+	print("%d.%06dsec cpu=%d" % \
+		(nsecs_secs(base_t), nsecs_nsecs(base_t)/1000, cpu))
 	for i in range(len(irq_list)):
-		print PF_IRQ_ENTRY % \
+		print(PF_IRQ_ENTRY % \
 			(diff_msec(base_t, irq_list[i]['irq_ent_t']),
-			irq_list[i]['irq'], irq_list[i]['name'])
-		print PF_JOINT
+			irq_list[i]['irq'], irq_list[i]['name']))
+		print(PF_JOINT)
 		irq_event_list = irq_list[i]['event_list']
 		for j in range(len(irq_event_list)):
 			irq_event = irq_event_list[j]
 			if irq_event['event'] == 'netif_rx':
-				print PF_NET_RX % \
+				print(PF_NET_RX % \
 					(diff_msec(base_t, irq_event['time']),
-					irq_event['skbaddr'])
-				print PF_JOINT
-	print PF_SOFT_ENTRY % \
-		diff_msec(base_t, hunk['sirq_ent_t'])
-	print PF_JOINT
+					irq_event['skbaddr']))
+				print(PF_JOINT)
+	print(PF_SOFT_ENTRY % \
+		diff_msec(base_t, hunk['sirq_ent_t']))
+	print(PF_JOINT)
 	event_list = hunk['event_list']
 	for i in range(len(event_list)):
 		event = event_list[i]
 		if event['event_name'] == 'napi_poll':
-			print PF_NAPI_POLL % \
-			    (diff_msec(base_t, event['event_t']), event['dev'])
+			print(PF_NAPI_POLL % \
+			    (diff_msec(base_t, event['event_t']), event['dev']))
 			if i == len(event_list) - 1:
-				print ""
+				print("")
 			else:
-				print PF_JOINT
+				print(PF_JOINT)
 		else:
-			print PF_NET_RECV % \
+			print(PF_NET_RECV % \
 			    (diff_msec(base_t, event['event_t']), event['skbaddr'],
-				event['len'])
+				event['len']))
 			if 'comm' in event.keys():
-				print PF_WJOINT
-				print PF_CPY_DGRAM % \
+				print(PF_WJOINT)
+				print(PF_CPY_DGRAM % \
 					(diff_msec(base_t, event['comm_t']),
-					event['pid'], event['comm'])
+					event['pid'], event['comm']))
 			elif 'handle' in event.keys():
-				print PF_WJOINT
+				print(PF_WJOINT)
 				if event['handle'] == "kfree_skb":
-					print PF_KFREE_SKB % \
+					print(PF_KFREE_SKB % \
 						(diff_msec(base_t,
 						event['comm_t']),
-						event['location'])
+						event['location']))
 				elif event['handle'] == "consume_skb":
-					print PF_CONS_SKB % \
+					print(PF_CONS_SKB % \
 						diff_msec(base_t,
-							event['comm_t'])
-			print PF_JOINT
+							event['comm_t']))
+			print(PF_JOINT)
 
 def trace_begin():
 	global show_tx
@@ -210,19 +211,19 @@ def trace_end():
 			print_receive(receive_hunk_list[i])
 	# display transmit hunks
 	if show_tx:
-		print "   dev    len      Qdisc        " \
-			"       netdevice             free"
+		print("   dev    len      Qdisc        " \
+			"       netdevice             free")
 		for i in range(len(tx_free_list)):
 			print_transmit(tx_free_list[i])
 	if debug:
-		print "debug buffer status"
-		print "----------------------------"
-		print "xmit Qdisc:remain:%d overflow:%d" % \
-			(len(tx_queue_list), of_count_tx_queue_list)
-		print "xmit netdevice:remain:%d overflow:%d" % \
-			(len(tx_xmit_list), of_count_tx_xmit_list)
-		print "receive:remain:%d overflow:%d" % \
-			(len(rx_skb_list), of_count_rx_skb_list)
+		print("debug buffer status")
+		print("----------------------------")
+		print("xmit Qdisc:remain:%d overflow:%d" % \
+			(len(tx_queue_list), of_count_tx_queue_list))
+		print("xmit netdevice:remain:%d overflow:%d" % \
+			(len(tx_xmit_list), of_count_tx_xmit_list))
+		print("receive:remain:%d overflow:%d" % \
+			(len(rx_skb_list), of_count_rx_skb_list))
 
 # called from perf, when it finds a correspoinding event
 def irq__softirq_entry(name, context, cpu, sec, nsec, pid, comm, callchain, vec):
Signature · 1 line
1.8.3.1
#25 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to netdev-times.py

On 1/17/19 1:45 AM, Seeteena Thoufeek wrote:> Support both Python 2 and Python 3 in netdev-times.py. ``print``

Quoted 2 lines

is now a function rather than a statement. This should have no functional change.

Same feedback as all the other patches applies.

In addition:

$ git annotate tools/perf/scripts/python/netdev-times.py | grep "all_event_list.sort"
359d5106a2ff4	(Koki Sanagi	2010-08-23 18:47:09 +0900	175)	all_event_list.sort(lambda a,b :cmp(a[EINFO_IDX_TIME],

I didn't think the above was valid with Python3.

In my version (https://build.opensuse.org/package/view_file/devel:tools/perf/port-failed-syscalls-by-pid-script-to-python3.patch?expand=1)
I reworked it as:

from functools import cmp_to_key all_event_list.sort(key=cmp_to_key(lambda a,b :a[EINFO_IDX_TIME] < b[EINFO_IDX_TIME]))

Tony

#26 lore raw

PATCH v2 1/1 perf scripts python: Add Python 3 support to failed-syscalls-by-pid.py

In reply to Seeteena Thoufeek

Support both Python 2 and Python 3 in failed-syscalls-by-pid.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fixes lambda syntax error.

Please enter the commit message for your changes. Lines starting

---
 tools/perf/scripts/python/failed-syscalls-by-pid.py | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
tools/perf/scripts/python/failed-syscalls-by-pid.py +11 -10
index cafeff3..a174755 100644
@@ -4,6 +4,7 @@
 #
 # Displays system-wide failed system call totals, broken down by pid.
 # If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+from __future__ import print_function
 
 import os
 import sys
@@ -32,7 +33,7 @@ if len(sys.argv) > 1:
 syscalls = autodict()
 
 def trace_begin():
-	print "Press control+C to stop and show the summary"
+	print("Press control+C to stop and show the summary")
 
 def trace_end():
 	print_error_totals()
@@ -57,22 +58,22 @@ def syscalls__sys_exit(event_name, context, common_cpu,
 
 def print_error_totals():
     if for_comm is not None:
-	    print "\nsyscall errors for %s:\n\n" % (for_comm),
+	    print("\nsyscall errors for %s:\n\n" % (for_comm)),
     else:
-	    print "\nsyscall errors:\n\n",
+	    print("\nsyscall errors:\n\n"),
 
-    print "%-30s  %10s\n" % ("comm [pid]", "count"),
-    print "%-30s  %10s\n" % ("------------------------------", \
-                                 "----------"),
+    print("%-30s  %10s\n" % ("comm [pid]", "count")),
+    print("%-30s  %10s\n" % ("------------------------------", \
+                                 "----------")),
 
     comm_keys = syscalls.keys()
     for comm in comm_keys:
 	    pid_keys = syscalls[comm].keys()
 	    for pid in pid_keys:
-		    print "\n%s [%d]\n" % (comm, pid),
+		    print("\n%s [%d]\n" % (comm, pid)),
 		    id_keys = syscalls[comm][pid].keys()
 		    for id in id_keys:
-			    print "  syscall: %-16s\n" % syscall_name(id),
+			    print("  syscall: %-16s\n" % syscall_name(id)),
 			    ret_keys = syscalls[comm][pid][id].keys()
-			    for ret, val in sorted(syscalls[comm][pid][id].iteritems(), key = lambda(k, v): (v, k),  reverse = True):
-				    print "    err = %-20s  %10d\n" % (strerror(ret), val),
+			    for ret, val in sorted(syscalls[comm][pid][id].iteritems(), key = lambda k_v: (k_v[1], k_v[0]),  reverse = True):
+				    print("    err = %-20s  %10d\n" % (strerror(ret), val)),
Signature · 1 line
1.8.3.1
#27 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to failed-syscalls-by-pid.py

Quoted 1 line · Seeteena Thoufeek

+from __future__ import print_function

You don't need this unless you're actually requiring functionality that only exists in v3. For example, you need it to handle the suppress newline functionality such as "end=".

Quoted 4 lines

def print_error_totals():
if for_comm is not None:

tools/perf/scripts/python/failed-syscalls-by-pid.py +1 -1
-	    print "\nsyscall errors for %s:\n\n" % (for_comm),
+	    print("\nsyscall errors for %s:\n\n" % (for_comm)),

tools/perf/scripts/python/failed-syscalls-by-pid.py def syscalls__sys_exit(event_name, context, common_cpu, · line 67

Quoted 10 lines
tools/perf/scripts/python/failed-syscalls-by-pid.py +4 -4
     else:
-	    print "\nsyscall errors:\n\n",
+	    print("\nsyscall errors:\n\n"),
 
-    print "%-30s  %10s\n" % ("comm [pid]", "count"),
-    print "%-30s  %10s\n" % ("------------------------------", \
-                                 "----------"),
+    print("%-30s  %10s\n" % ("comm [pid]", "count")),
+    print("%-30s  %10s\n" % ("------------------------------", \
+                                 "----------")),

Same comments as before regarding trailing comma for function.

See: https://build.opensuse.org/package/view_file/devel:tools/perf/port-failed-syscalls-by-pid-script-to-python3.patch?expand=1

#28 lore raw

Re: [PATCH v2] perf scripts python: Add Python 3 support to failed-syscalls-by-pid.py

Quoted 6 lines · Tony Jones
Quoted 1 line · Seeteena Thoufeek

+from __future__ import print_function

You don't need this unless you're actually requiring functionality that only exists in v3. For example, you need it to handle the suppress newline functionality such as "end=".

Also, it brings up a question I had. What python versions are expected to be supported with PYTHON=python2?

https://python-future.org/imports.html So importing from future (for end='' support) implies >= 2.6.

The base release of 2.6 was October 2008 so maybe this is fine. Otherwise it may be preferable to use sys.stdout.write which has more consistent semantics (see tools/perf/scripts/python/compaction-times.py)

Tony

#29 lore raw
In reply to Seeteena Thoufeek
Quoted 8 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in check-perf-trace.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix indentation issue, replace spaces with tab

hum, could you please add some info about testing those changes? (or even some global into 0/.. patch)

this is working for me on python2:

	[root@krava perf]# perf script rec check-perf-trace
	^C
	[root@krava perf]# perf script   -s scripts/python/check-perf-trace.py
	trace_begin

	unhandled events:


	event                                          count

	----------------------------------------  -----------

	raw_syscalls__sys_enter                      3509879

but fails for python3:

[root@ibm-x3650m4-01-vm-04 perf]# perf script rec check-perf-trace ^C[ perf record: Woken up 0 times to write data ]
Warning:
1 out of order events recorded.
[ perf record: Captured and wrote 43.132 MB perf.data (490171 samples) ]

[root@ibm-x3650m4-01-vm-04 perf]# perf script -s scripts/python/check-perf-trace.py
Traceback (most recent call last):
File "scripts/python/check-perf-trace.py", line 18, in <module>
from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context' Error running python script scripts/python/check-perf-trace.py

I did not test with rpm, just did 'make install' for perf

thanks,
jirka

#30 lore raw

Hi Seeteena, Jiri,

Quoted 38 lines · Jiri Olsa
Quoted 8 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in check-perf-trace.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix indentation issue, replace spaces with tab

hum, could you please add some info about testing those changes? (or even some global into 0/.. patch)

this is working for me on python2:

	[root@krava perf]# perf script rec check-perf-trace
	^C
	[root@krava perf]# perf script   -s scripts/python/check-perf-trace.py
	trace_begin

	unhandled events:


	event                                          count

	----------------------------------------  -----------

	raw_syscalls__sys_enter                      3509879

but fails for python3:

[root@ibm-x3650m4-01-vm-04 perf]# perf script rec check-perf-trace ^C[ perf record: Woken up 0 times to write data ]
Warning:
1 out of order events recorded.
[ perf record: Captured and wrote 43.132 MB perf.data (490171 samples) ]

Quoted 6 lines

[root@ibm-x3650m4-01-vm-04 perf]# perf script -s scripts/python/check-perf-trace.py
Traceback (most recent call last):
File "scripts/python/check-perf-trace.py", line 18, in <module>
from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context' Error running python script scripts/python/check-perf-trace.py

Yes I'm seeing the same error with check-perf-trace.py.

I think the issue happens only when we use 'perf script -s path/to/script.py' instead of 'perf script report script_name':

Ex:

# PERF_EXEC_PATH=pwd ./perf.python3 script record stackcollapse
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.263 MB perf.data (497 samples) ]

# PERF_EXEC_PATH=pwd ./perf.python3 script report stackcollapse

    migration/21 1
    migration/41 1
    perf 9
    rngd 1
    sshd 1
    swapper 482
    xfsaild/sda5 2

# PERF_EXEC_PATH=pwd ./perf.python3 script -s scripts/python/stackcollapse.py
Traceback (most recent call last):
File "scripts/python/stackcollapse.py", line 31, in <module>
from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context'
Error running python script scripts/python/stackcollapse.py

So, it seems, this is not the script issue. Something is wrong when we build perf with python3?

Here is my test summary on Powerpc Fedora 29:

                              Python2           Python3
 check-perf-trace.py          ok                FAIL (No module named 'perf_trace_context')
 event_analyzing_sample.py    ok                ok
 export-to-sqlite.py          ok                ok
 exported-sql-viewer.py       ok                FAIL (details at the end)
 export-to-postgresql.py      FAIL              FAIL
 failed-syscalls-by-pid.py    ok                ok
 futex-contention.py          ok                ok
 intel-pt-events.py           NOT TESTED        NOT TESTED
 mem-phys-addr.py             NOT TESTED        NOT TESTED
 net_dropmonitor.py           ok                ok
 netdev-times.py              ok                ok
 powerpc-hcalls.py            ok                ok
 sctop.py                     ok                ok
 stackcollapse.py             ok                ok
 stat-cpi.py                  FAIL (Seg fault)  FAIL (Seg fault)
 syscall-counts-by-pid.py     ok                ok
 syscall-counts.py            ok                ok

exported-sql-viewer.py FAILURE:
===============================
# PERF_EXEC_PATH=pwd ./perf.python3 script -s ./scripts/python/exported-sql-viewer.py my_test_db
Traceback (most recent call last):
File "./scripts/python/exported-sql-viewer.py", line 2614, in <module>
Main()
File "./scripts/python/exported-sql-viewer.py", line 2601, in Main
db, dbname = dbref.Open("main")
File "./scripts/python/exported-sql-viewer.py", line 2573, in Open
raise Exception("Failed to open database " + dbname + " error: " + db.lastError().text())
Exception: Failed to open database my_test_db error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
QPSQL: Unable to connect
Error running python script ./scripts/python/exported-sql-viewer.py

Thanks,
Ravi

#31 lore raw
In reply to Jiri Olsa
Quoted 45 lines · Jiri Olsa
Quoted 8 lines · Seeteena Thoufeek

Support both Python 2 and Python 3 in check-perf-trace.py. ``print`` is now a function rather than a statement. This should have no functional change.

Fix indentation issue, replace spaces with tab

hum, could you please add some info about testing those changes? (or even some global into 0/.. patch)

this is working for me on python2:

	[root@krava perf]# perf script rec check-perf-trace
	^C
	[root@krava perf]# perf script   -s scripts/python/check-perf-trace.py
	trace_begin

	unhandled events:


	event                                          count

	----------------------------------------  -----------

	raw_syscalls__sys_enter                      3509879

but fails for python3:

[root@ibm-x3650m4-01-vm-04 perf]# perf script rec check-perf-trace ^C[ perf record: Woken up 0 times to write data ]
Warning:
1 out of order events recorded.
[ perf record: Captured and wrote 43.132 MB perf.data (490171 samples) ]

[root@ibm-x3650m4-01-vm-04 perf]# perf script -s scripts/python/check-perf-trace.py
Traceback (most recent call last):
File "scripts/python/check-perf-trace.py", line 18, in <module>
from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context' Error running python script scripts/python/check-perf-trace.py

Quoted 5 lines

I did not test with rpm, just did 'make install' for perf

thanks,
jirka

I'd been simultaneously working on a patch set to fix up Python3.

It's actually already in our Factory and SLE15-SP1 releases as we had a deadline to kill Python2 usage for internal rpms.

I was going to post once I'd fixed the last remaining issue ('import perf' is still failing [test #18]).

I guess "you snooze you lose" :-)

https://build.opensuse.org/package/view_file/devel:tools/perf/perf.changes?expand=1

Anyhow, the fix for the above is: 'add-trace_context-extension-module-to-sys-modules.patch' from above.

Attached below. Verified with PYTHON=python2 and PYTHON=python3

Tony

---------------------------------

In Python3, the result of PyModule_Create (called from scripts/python/Perf-Trace-Util/Context.c) is not automatically added to sys.modules. See: https://bugs.python.org/issue4592

Below is the observed behavior without the fix.

# ldd /usr/bin/perf | grep -i python
	libpython3.6m.so.1.0 => /usr/lib64/libpython3.6m.so.1.0 (0x00007f8e1dfb2000)
# perf record -a -e raw_syscalls:sys_enter sleep 5
[ perf record: Woken up 0 times to write data ]
[ perf record: Captured and wrote 187.177 MB perf.data (1581501 samples) ]
# perf script -g python | cat
generated Python script: perf-script.py
# perf script -s ./perf-script.py
Traceback (most recent call last):
  File "./perf-script.py", line 18, in <module>
    from perf_trace_context import *
ModuleNotFoundError: No module named 'perf_trace_context'
Error running python script ./perf-script.py
---
 tools/perf/util/scripting-engines/trace-event-python.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/scripting-engines/trace-event-python.c +4 -2
@@ -1494,6 +1494,7 @@ static void _free_command_line(wchar_t *
 static int python_start_script(const char *script, int argc, const char **argv)
 {
 	struct tables *tables = &tables_global;
+	PyMODINIT_FUNC (*initfunc)(void);
 #if PY_MAJOR_VERSION < 3
 	const char **command_line;
 #else
@@ -1504,24 +1505,25 @@ static int python_start_script(const cha
 	FILE *fp;
 
 #if PY_MAJOR_VERSION < 3
+	initfunc = initperf_trace_context;
 	command_line = malloc((argc + 1) * sizeof(const char *));
 	command_line[0] = script;
 	for (i = 1; i < argc + 1; i++)
 		command_line[i] = argv[i - 1];
 #else
+	initfunc = PyInit_perf_trace_context;
 	command_line = malloc((argc + 1) * sizeof(wchar_t *));
 	command_line[0] = Py_DecodeLocale(script, NULL);
 	for (i = 1; i < argc + 1; i++)
 		command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
 #endif
 
+	PyImport_AppendInittab("perf_trace_context", initfunc);
 	Py_Initialize();
 
 #if PY_MAJOR_VERSION < 3
-	initperf_trace_context();
 	PySys_SetArgv(argc + 1, (char **)command_line);
 #else
-	PyInit_perf_trace_context();
 	PySys_SetArgv(argc + 1, command_line);
 #endif
 
#32 lore raw
Quoted 9 lines · Tony Jones

I'd been simultaneously working on a patch set to fix up Python3.

It's actually already in our Factory and SLE15-SP1 releases as we had a deadline to kill Python2 usage for internal rpms.

I was going to post once I'd fixed the last remaining issue ('import perf' is still failing [test #18]).

I guess "you snooze you lose" :-)

https://build.opensuse.org/package/view_file/devel:tools/perf/perf.changes?expand=1

Seeteena, I'm than happy to forward my patches via email. Alternatively, as I said in another post, the full series is in SLE15-SP1/beta2 (should apply to tip) which IBM has access to and they've been backported to v4.19 (Factory) at https://build.opensuse.org/package/show/openSUSE:Factory/perf. Everything has been tested against python2 and python3.

Also, in this series there are patches to:
- port tests/attr.py to Python3
- remove shebangs from the .py scripts and change to mode 644 as IMO it makes no sense to explicitly have #!/usr/bin/python since per pep-0394 this refers to version2 and the system may only have python3 installed
- remove the shebang from setup.py since it's explicitly invoked via call to ${PYTHON_WORD}

Also I found in testing that the following fix is also needed. I'm not 100% sure on it and was going to revisit before posting but _PyUnicode_FromStringAndSize() is definitely unsafe to use on attr.

----

Fixes 66dfdff03d196e51322c6a85c0d8db8bb2bdd655

With Python3. PyUnicode_FromStringAndSize is unsafe to call on attr and will return NULL. Use _PyBytes_FromStringAndSize (as with raw_buf).

$ perf script -s perf-script.py -i perf.data
in trace_begin
Segmentation fault (core dumped)
---
 tools/perf/util/scripting-engines/trace-event-python.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/scripting-engines/trace-event-python.c +1 -2
@@ -733,8 +733,7 @@ static PyObject *get_perf_sample_dict(st
                Py_FatalError("couldn't create Python dictionary");
 
        pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
-       pydict_set_item_string_decref(dict, "attr", _PyUnicode_FromStringAndSize(
-                       (const char *)&evsel->attr, sizeof(evsel->attr)));
+       pydict_set_item_string_decref(dict, "attr", _PyBytes_FromStringAndSize((const char *)&evsel->attr, sizeof(evsel->attr)));
 
        pydict_set_item_string_decref(dict_sample, "pid",
                        _PyLong_FromLong(sample->pid));
#33 lore raw
In reply to Seeteena Thoufeek

tools/perf/scripts/python/check-perf-trace.py def irq__softirq_entry(event_name, context, common_cpu, · line 38

Quoted 4 lines · Seeteena Thoufeek
tools/perf/scripts/python/check-perf-trace.py +2 -2
-		print "vec=%s\n" % \
-		(symbol_str("irq__softirq_entry", "vec", vec)),
+		print("vec=%s\n" % \
+		(symbol_str("irq__softirq_entry", "vec", vec))),

Again, check the trailing comma usage:

$ echo 'print "abc", ; print "def"' | python2
abc def
$ echo 'print ("abc"), ; print ("def")' | python2
abc def
$ echo 'print ("abc"), ; print ("def")' | python3
abc
def