Commit 20e23428 authored by Brenden Blanco's avatar Brenden Blanco

Merge pull request #153 from brendangregg/master

more examples of b[] syntax, fix func arg passing
parents d3c27a7d 9421054e
...@@ -40,7 +40,6 @@ if len(argv) > 1: ...@@ -40,7 +40,6 @@ if len(argv) > 1:
# load BPF program # load BPF program
b = BPF(src_file = "bitehist.c") b = BPF(src_file = "bitehist.c")
b.attach_kprobe(event="blk_start_request", fn_name="do_request") b.attach_kprobe(event="blk_start_request", fn_name="do_request")
dist = b.get_table("dist")
dist_max = 64 dist_max = 64
# header # header
...@@ -63,7 +62,7 @@ def stars(val, val_max, width): ...@@ -63,7 +62,7 @@ def stars(val, val_max, width):
text = text[:-1] + "+" text = text[:-1] + "+"
return text return text
def print_log2_hist(d, val_type): def print_log2_hist(dist, val_type):
idx_max = -1 idx_max = -1
val_max = 0 val_max = 0
for i in range(1, dist_max + 1): for i in range(1, dist_max + 1):
...@@ -104,6 +103,6 @@ while (1): ...@@ -104,6 +103,6 @@ while (1):
pass; do_exit = 1 pass; do_exit = 1
print print
print_log2_hist(dist, "kbytes") print_log2_hist(b["dist"], "kbytes")
if do_exit: if do_exit:
exit() exit()
...@@ -63,12 +63,12 @@ def stars(val, val_max, width): ...@@ -63,12 +63,12 @@ def stars(val, val_max, width):
text = text[:-1] + "+" text = text[:-1] + "+"
return text return text
def print_log2_hist(d, val_type): def print_log2_hist(dist, val_type):
idx_max = -1 idx_max = -1
val_max = 0 val_max = 0
for i in range(1, dist_max + 1): for i in range(1, dist_max + 1):
try: try:
val = b["dist"][c_int(i)].value - last[i] val = dist[c_int(i)].value - last[i]
if (val > 0): if (val > 0):
idx_max = i idx_max = i
if (val > val_max): if (val > val_max):
...@@ -83,10 +83,10 @@ def print_log2_hist(d, val_type): ...@@ -83,10 +83,10 @@ def print_log2_hist(d, val_type):
if (low == high): if (low == high):
low -= 1 low -= 1
try: try:
val = b["dist"][c_int(i)].value - last[i] val = dist[c_int(i)].value - last[i]
print("%8d -> %-8d : %-8d |%-*s|" % (low, high, val, print("%8d -> %-8d : %-8d |%-*s|" % (low, high, val,
stars_max, stars(val, val_max, stars_max))) stars_max, stars(val, val_max, stars_max)))
last[i] = b["dist"][c_int(i)].value last[i] = dist[c_int(i)].value
except: except:
break break
......
...@@ -19,10 +19,9 @@ from time import sleep, strftime ...@@ -19,10 +19,9 @@ from time import sleep, strftime
# load BPF program # load BPF program
b = BPF(src_file = "pidpersec.c") b = BPF(src_file = "pidpersec.c")
b.attach_kprobe(event="sched_fork", fn_name="do_count") b.attach_kprobe(event="sched_fork", fn_name="do_count")
stats = b.get_table("stats")
# stat indexes # stat indexes
S_COUNT = 1 S_COUNT = c_int(1)
# header # header
print("Tracing... Ctrl-C to end.") print("Tracing... Ctrl-C to end.")
...@@ -36,5 +35,5 @@ while (1): ...@@ -36,5 +35,5 @@ while (1):
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))) (b["stats"][S_COUNT].value - last)))
last = stats[c_int(S_COUNT)].value last = b["stats"][S_COUNT].value
...@@ -57,7 +57,6 @@ b.attach_kprobe(event="vfs_write", fn_name="do_count") ...@@ -57,7 +57,6 @@ b.attach_kprobe(event="vfs_write", fn_name="do_count")
b.attach_kprobe(event="vfs_fsync", fn_name="do_count") b.attach_kprobe(event="vfs_fsync", fn_name="do_count")
b.attach_kprobe(event="vfs_open", fn_name="do_count") b.attach_kprobe(event="vfs_open", fn_name="do_count")
b.attach_kprobe(event="vfs_create", fn_name="do_count") b.attach_kprobe(event="vfs_create", fn_name="do_count")
counts = b.get_table("counts")
# header # header
print("Tracing... Ctrl-C to end.") print("Tracing... Ctrl-C to end.")
...@@ -69,5 +68,6 @@ except KeyboardInterrupt: ...@@ -69,5 +68,6 @@ except KeyboardInterrupt:
pass pass
print("\n%-16s %-12s %8s" % ("ADDR", "FUNC", "COUNT")) print("\n%-16s %-12s %8s" % ("ADDR", "FUNC", "COUNT"))
counts = b.get_table("counts")
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))
...@@ -42,7 +42,6 @@ b.attach_kprobe(event="vfs_write", fn_name="do_write") ...@@ -42,7 +42,6 @@ b.attach_kprobe(event="vfs_write", fn_name="do_write")
b.attach_kprobe(event="vfs_fsync", fn_name="do_fsync") b.attach_kprobe(event="vfs_fsync", fn_name="do_fsync")
b.attach_kprobe(event="vfs_open", fn_name="do_open") b.attach_kprobe(event="vfs_open", fn_name="do_open")
b.attach_kprobe(event="vfs_create", fn_name="do_create") b.attach_kprobe(event="vfs_create", fn_name="do_create")
stats = b.get_table("stats")
# stat column labels and indexes # stat column labels and indexes
stat_types = { stat_types = {
...@@ -79,9 +78,9 @@ while (1): ...@@ -79,9 +78,9 @@ while (1):
for stype in stat_types.keys(): for stype in stat_types.keys():
idx = stat_types[stype] idx = stat_types[stype]
try: try:
delta = stats[c_int(idx)].value - last[idx] delta = b["stats"][c_int(idx)].value - last[idx]
print(" %8d" % (delta / interval), end="") print(" %8d" % (delta / interval), end="")
last[idx] = stats[c_int(idx)].value last[idx] = b["stats"][c_int(idx)].value
except: except:
print(" %8d" % 0, end="") print(" %8d" % 0, end="")
print("") print("")
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