Skip to content

Commit beda0e7

Browse files
tonyj-suseacmel
authored andcommitted
perf script python: Add Python3 support to exported-sql-viewer.py
Support both Python2 and Python3 in the exported-sql-viewer.py script. The use of 'from __future__' implies the minimum supported Python2 version is now v2.6 Signed-off-by: Tony Jones <tonyj@suse.de> Acked-by: Adrian Hunter <adrian.hunter@intel.com> Link: http://lkml.kernel.org/r/20190309000518.2438-2-tonyj@suse.de Signed-off-by: Seeteena Thoufeek <s1seetee@linux.vnet.ibm.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 75065a8 commit beda0e7

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

‎tools/perf/scripts/python/exported-sql-viewer.py‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,20 @@
8888
# 7fab593ea956 48 89 15 3b 13 22 00 movq %rdx, 0x22133b(%rip)
8989
# 8107675243232 2 ls 22011 22011 hardware interrupt No 7fab593ea956 _dl_start+0x26 (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
9090

91+
from __future__ import print_function
92+
9193
import sys
9294
import weakref
9395
import threading
9496
import string
95-
import cPickle
97+
try:
98+
# Python2
99+
import cPickle as pickle
100+
# size of pickled integer big enough for record size
101+
glb_nsz = 8
102+
except ImportError:
103+
import pickle
104+
glb_nsz = 16
96105
import re
97106
import os
98107
from PySide.QtCore import *
@@ -102,6 +111,15 @@
102111
from ctypes import *
103112
from multiprocessing import Process, Array, Value, Event
104113

114+
# xrange is range in Python3
115+
try:
116+
xrange
117+
except NameError:
118+
xrange = range
119+
120+
def printerr(*args, **keyword_args):
121+
print(*args, file=sys.stderr, **keyword_args)
122+
105123
# Data formatting helpers
106124

107125
def tohex(ip):
@@ -1004,10 +1022,6 @@ def FindDone(self, thread, callback, row):
10041022

10051023
glb_chunk_sz = 10000
10061024

1007-
# size of pickled integer big enough for record size
1008-
1009-
glb_nsz = 8
1010-
10111025
# Background process for SQL data fetcher
10121026

10131027
class SQLFetcherProcess():
@@ -1066,7 +1080,7 @@ def HasSpace(self, sz):
10661080
return True
10671081
if space >= glb_nsz:
10681082
# Use 0 (or space < glb_nsz) to mean there is no more at the top of the buffer
1069-
nd = cPickle.dumps(0, cPickle.HIGHEST_PROTOCOL)
1083+
nd = pickle.dumps(0, pickle.HIGHEST_PROTOCOL)
10701084
self.buffer[self.local_head : self.local_head + len(nd)] = nd
10711085
self.local_head = 0
10721086
if self.local_tail - self.local_head > sz:
@@ -1084,9 +1098,9 @@ def WaitForSpace(self, sz):
10841098
self.wait_event.wait()
10851099

10861100
def AddToBuffer(self, obj):
1087-
d = cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL)
1101+
d = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
10881102
n = len(d)
1089-
nd = cPickle.dumps(n, cPickle.HIGHEST_PROTOCOL)
1103+
nd = pickle.dumps(n, pickle.HIGHEST_PROTOCOL)
10901104
sz = n + glb_nsz
10911105
self.WaitForSpace(sz)
10921106
pos = self.local_head
@@ -1198,12 +1212,12 @@ def RemoveFromBuffer(self):
11981212
pos = self.local_tail
11991213
if len(self.buffer) - pos < glb_nsz:
12001214
pos = 0
1201-
n = cPickle.loads(self.buffer[pos : pos + glb_nsz])
1215+
n = pickle.loads(self.buffer[pos : pos + glb_nsz])
12021216
if n == 0:
12031217
pos = 0
1204-
n = cPickle.loads(self.buffer[0 : glb_nsz])
1218+
n = pickle.loads(self.buffer[0 : glb_nsz])
12051219
pos += glb_nsz
1206-
obj = cPickle.loads(self.buffer[pos : pos + n])
1220+
obj = pickle.loads(self.buffer[pos : pos + n])
12071221
self.local_tail = pos + n
12081222
return obj
12091223

@@ -2973,7 +2987,7 @@ def Open(self, connection_name):
29732987

29742988
def Main():
29752989
if (len(sys.argv) < 2):
2976-
print >> sys.stderr, "Usage is: exported-sql-viewer.py {<database name> | --help-only}"
2990+
printerr("Usage is: exported-sql-viewer.py {<database name> | --help-only}");
29772991
raise Exception("Too few arguments")
29782992

29792993
dbname = sys.argv[1]
@@ -2986,8 +3000,8 @@ def Main():
29863000

29873001
is_sqlite3 = False
29883002
try:
2989-
f = open(dbname)
2990-
if f.read(15) == "SQLite format 3":
3003+
f = open(dbname, "rb")
3004+
if f.read(15) == b'SQLite format 3':
29913005
is_sqlite3 = True
29923006
f.close()
29933007
except:

0 commit comments

Comments
 (0)