Commit 762b1b45 authored by Brendan Gregg's avatar Brendan Gregg

python 2 & 3 compatibility

parent 177e07eb
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
# #
# 11-Aug-2015 Brendan Gregg Created this. # 11-Aug-2015 Brendan Gregg Created this.
from __future__ import print_function
from bpf import BPF from bpf import BPF
import sys import sys
...@@ -21,13 +22,13 @@ BPF.attach_kprobe(b.load_func("do_request", BPF.KPROBE), "blk_start_request") ...@@ -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") BPF.attach_kprobe(b.load_func("do_completion", BPF.KPROBE), "blk_update_request")
# header # 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 # open trace pipe
try: try:
trace = open("/sys/kernel/debug/tracing/trace_pipe", "r") trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
except: except:
print >> sys.stderr, "ERROR: opening trace_pipe" print("ERROR: opening trace_pipe", file=sys.stderr)
exit(1) exit(1)
# format output # format output
...@@ -49,4 +50,4 @@ while 1: ...@@ -49,4 +50,4 @@ while 1:
type_s = "R" type_s = "R"
ms = float(int(us_s, 10)) / 1000 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))
...@@ -25,7 +25,7 @@ stats = b.get_table("stats") ...@@ -25,7 +25,7 @@ stats = b.get_table("stats")
S_COUNT = 1 S_COUNT = 1
# header # header
print "Tracing... Ctrl-C to end." print("Tracing... Ctrl-C to end.")
# output # output
last = 0 last = 0
...@@ -35,6 +35,6 @@ while (1): ...@@ -35,6 +35,6 @@ while (1):
except KeyboardInterrupt: except KeyboardInterrupt:
pass; exit() pass; exit()
print "%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
(stats[c_int(S_COUNT)].value - last)) (stats[c_int(S_COUNT)].value - last)))
last = stats[c_int(S_COUNT)].value last = stats[c_int(S_COUNT)].value
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
# #
# 13-Aug-2015 Brendan Gregg Created this. # 13-Aug-2015 Brendan Gregg Created this.
from __future__ import print_function
from bpf import BPF from bpf import BPF
import sys import sys
...@@ -24,13 +25,13 @@ int do_sync(void *ctx) { ...@@ -24,13 +25,13 @@ int do_sync(void *ctx) {
BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync") BPF.attach_kprobe(b.load_func("do_sync", BPF.KPROBE), "sys_sync")
# header # header
print "%-18s %s" % ("TIME(s)", "CALL") print("%-18s %s" % ("TIME(s)", "CALL"))
# open trace pipe # open trace pipe
try: try:
trace = open("/sys/kernel/debug/tracing/trace_pipe", "r") trace = open("/sys/kernel/debug/tracing/trace_pipe", "r")
except: except:
print >> sys.stderr, "ERROR: opening trace_pipe" print("ERROR: opening trace_pipe", file=sys.stderr)
exit(1) exit(1)
# format output # format output
...@@ -43,4 +44,4 @@ while 1: ...@@ -43,4 +44,4 @@ while 1:
prolog, time_s, colon, word = line.rsplit(" ", 3) prolog, time_s, colon, word = line.rsplit(" ", 3)
time_s = time_s[:-1] # strip trailing ":" time_s = time_s[:-1] # strip trailing ":"
print "%-18s %s" % (time_s, word) print("%-18s %s" % (time_s, word))
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
# #
# 14-Aug-2015 Brendan Gregg Created this. # 14-Aug-2015 Brendan Gregg Created this.
from __future__ import print_function
from bpf import BPF from bpf import BPF
from ctypes import c_ushort, c_int, c_ulonglong from ctypes import c_ushort, c_int, c_ulonglong
from time import sleep, strftime from time import sleep, strftime
...@@ -23,7 +24,7 @@ def load_kallsyms(): ...@@ -23,7 +24,7 @@ def load_kallsyms():
try: try:
syms = open(symfile, "r") syms = open(symfile, "r")
except: except:
print >> stderr, "ERROR: reading " + symfile print("ERROR: reading " + symfile, file=sys.stderr)
exit() exit()
line = syms.readline() line = syms.readline()
for line in iter(syms): for line in iter(syms):
...@@ -39,7 +40,7 @@ def ksym(addr): ...@@ -39,7 +40,7 @@ def ksym(addr):
start = -1 start = -1
end = len(ksym_addrs) end = len(ksym_addrs)
while end != start + 1: while end != start + 1:
mid = (start + end) / 2 mid = int((start + end) / 2)
if addr < ksym_addrs[mid]: if addr < ksym_addrs[mid]:
end = mid end = mid
else: else:
...@@ -60,7 +61,7 @@ BPF.attach_kprobe(fn, "vfs_create") ...@@ -60,7 +61,7 @@ BPF.attach_kprobe(fn, "vfs_create")
counts = b.get_table("counts") counts = b.get_table("counts")
# header # header
print "Tracing... Ctrl-C to end." print("Tracing... Ctrl-C to end.")
# output # output
try: try:
...@@ -68,6 +69,6 @@ try: ...@@ -68,6 +69,6 @@ try:
except KeyboardInterrupt: except KeyboardInterrupt:
pass 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): 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))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment