python 2 & 3 compatibility
authorBrendan Gregg <brendan.d.gregg@gmail.com>
Tue, 18 Aug 2015 23:49:48 +0000 (16:49 -0700)
committerBrendan Gregg <brendan.d.gregg@gmail.com>
Tue, 18 Aug 2015 23:49:48 +0000 (16:49 -0700)
examples/disksnoop.py
tools/pidpersec
tools/syncsnoop
tools/vfscount

index ee660f7..f7b1806 100755 (executable)
@@ -10,6 +10,7 @@
 #
 # 11-Aug-2015  Brendan Gregg   Created this.
 
+from __future__ import print_function
 from bpf import BPF
 import sys
 
@@ -21,13 +22,13 @@ BPF.attach_kprobe(b.load_func("do_request", BPF.KPROBE), "blk_start_request")
 BPF.attach_kprobe(b.load_func("do_completion", BPF.KPROBE), "blk_update_request")
 
 # header
-print "%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)")
+print("%-18s %-2s %-7s %8s" % ("TIME(s)", "T", "BYTES", "LAT(ms)"))
 
 # open trace pipe
 try:
        trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
 except:
-       print >> sys.stderr, "ERROR: opening trace_pipe"
+       print("ERROR: opening trace_pipe", file=sys.stderr)
        exit(1)
 
 # format output
@@ -49,4 +50,4 @@ while 1:
                type_s = "R"
        ms = float(int(us_s, 10)) / 1000
 
-       print "%-18s %-2s %-7s %8.2f" % (time_s, type_s, bytes_s, ms)
+       print("%-18s %-2s %-7s %8.2f" % (time_s, type_s, bytes_s, ms))
index b4a7ffb..6a3ca8c 100755 (executable)
@@ -25,7 +25,7 @@ stats = b.get_table("stats")
 S_COUNT = 1
 
 # header
-print "Tracing... Ctrl-C to end."
+print("Tracing... Ctrl-C to end.")
 
 # output
 last = 0
@@ -35,6 +35,6 @@ while (1):
        except KeyboardInterrupt:
                pass; exit()
 
-       print "%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
-           (stats[c_int(S_COUNT)].value - last))
+       print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
+           (stats[c_int(S_COUNT)].value - last)))
        last = stats[c_int(S_COUNT)].value
index 25f1e8c..5490bcc 100755 (executable)
@@ -11,6 +11,7 @@
 #
 # 13-Aug-2015  Brendan Gregg   Created this.
 
+from __future__ import print_function
 from bpf import BPF
 import sys
 
@@ -24,13 +25,13 @@ int do_sync(void *ctx) {
 BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync")
 
 # header
-print "%-18s %s" % ("TIME(s)", "CALL")
+print("%-18s %s" % ("TIME(s)", "CALL"))
 
 # open trace pipe
 try:
        trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
 except:
-       print >> sys.stderr, "ERROR: opening trace_pipe"
+       print("ERROR: opening trace_pipe", file=sys.stderr)
        exit(1)
 
 # format output
@@ -43,4 +44,4 @@ while 1:
        prolog, time_s, colon, word = line.rsplit(" ", 3)
        time_s = time_s[:-1]    # strip trailing ":"
 
-       print "%-18s %s" % (time_s, word)
+       print("%-18s %s" % (time_s, word))
index 5fe3f9b..2f5002e 100755 (executable)
@@ -10,6 +10,7 @@
 #
 # 14-Aug-2015  Brendan Gregg   Created this.
 
+from __future__ import print_function
 from bpf import BPF
 from ctypes import c_ushort, c_int, c_ulonglong
 from time import sleep, strftime
@@ -23,7 +24,7 @@ def load_kallsyms():
        try:
                syms = open(symfile, "r")
        except:
-               print >> stderr, "ERROR: reading " + symfile
+               print("ERROR: reading " + symfile, file=sys.stderr)
                exit()
        line = syms.readline()
        for line in iter(syms):
@@ -39,7 +40,7 @@ def ksym(addr):
        start = -1
        end = len(ksym_addrs)
        while end != start + 1:
-               mid = (start + end) / 2
+               mid = int((start + end) / 2)
                if addr < ksym_addrs[mid]:
                        end = mid
                else:
@@ -60,7 +61,7 @@ BPF.attach_kprobe(fn, "vfs_create")
 counts = b.get_table("counts")
 
 # header
-print "Tracing... Ctrl-C to end."
+print("Tracing... Ctrl-C to end.")
 
 # output
 try:
@@ -68,6 +69,6 @@ try:
 except KeyboardInterrupt:
        pass
 
-print "\n%-16s %-12s %8s" % ("ADDR", "FUNC", "COUNT")
+print("\n%-16s %-12s %8s" % ("ADDR", "FUNC", "COUNT"))
 for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
-       print "%-16x %-12s %8d" % (k.ip, ksym(k.ip), v.value)
+       print("%-16x %-12s %8d" % (k.ip, ksym(k.ip), v.value))