Skip to content

Commit

Permalink
continue adding --ebpf option to the python tools/ scripts
Browse files Browse the repository at this point in the history
Several python tools allow their eBPF code to be printed to
stdout for debugging.  There are other projects that would
like to share these program definitions however, instead of
duplicating code.  We previously agreed on an --ebpf option
and we now continue adding it to more tools.

Signed-off-by: Nathan Scott <[email protected]>
  • Loading branch information
natoscott committed Feb 2, 2018
1 parent 3f39bc1 commit cf0792f
Show file tree
Hide file tree
Showing 47 changed files with 249 additions and 42 deletions.
6 changes: 5 additions & 1 deletion tools/btrfsdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
pid = args.pid
countdown = int(args.count)
Expand Down Expand Up @@ -183,8 +185,10 @@
bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
else:
bpf_text = bpf_text.replace('FILTER_PID', '0')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# load BPF program
b = BPF(text=bpf_text)
Expand Down
6 changes: 5 additions & 1 deletion tools/btrfsslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
help="trace this PID only")
parser.add_argument("min_ms", nargs="?", default='10',
help="minimum I/O duration to trace, in ms (default 10)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
min_ms = int(args.min_ms)
pid = args.pid
Expand Down Expand Up @@ -280,8 +282,10 @@
bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
else:
bpf_text = bpf_text.replace('FILTER_PID', '0')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# kernel->user event data: struct data_t
DNAME_INLINE_LEN = 32 # linux/dcache.h
Expand Down
6 changes: 5 additions & 1 deletion tools/cpudist.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
countdown = int(args.count)
debug = 0
Expand Down Expand Up @@ -149,8 +151,10 @@
bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
bpf_text = bpf_text.replace('STORE',
'dist.increment(bpf_log2l(delta));')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

b = BPF(text=bpf_text)
b.attach_kprobe(event="finish_task_switch", fn_name="sched_switch")
Expand Down
6 changes: 5 additions & 1 deletion tools/cpuunclaimed.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
countdown = int(args.count)
frequency = 99
Expand Down Expand Up @@ -155,8 +157,10 @@
"""

# code substitutions
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# initialize BPF & perf_events
b = BPF(text=bpf_text)
Expand Down
6 changes: 5 additions & 1 deletion tools/dbslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
dest="path", metavar="PATH", help="path to binary")
parser.add_argument("-m", "--threshold", type=int, default=1,
help="trace queries slower than this threshold (ms)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

threshold_ns = args.threshold * 1000000
Expand Down Expand Up @@ -196,8 +198,10 @@

bpf = BPF(text=program, usdt_contexts=usdts)

if args.verbose:
if args.verbose or args.ebpf:
print(program)
if args.ebpf:
exit()

class Data(ct.Structure):
_fields_ = [
Expand Down
6 changes: 6 additions & 0 deletions tools/dcsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
epilog=examples)
parser.add_argument("-a", "--all", action="store_true",
help="trace all lookups (default is fails only)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

# define BPF program
Expand Down Expand Up @@ -132,6 +134,10 @@ class Data(ct.Structure):
("filename", ct.c_char * MAX_FILE_LEN),
]

if args.ebpf:
print(bpf_text)
exit()

# initialize BPF
b = BPF(text=bpf_text)
if args.all:
Expand Down
9 changes: 8 additions & 1 deletion tools/execsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
help="only print commands where arg contains this line (regex)")
parser.add_argument("--max-args", default="20",
help="maximum number of arguments parsed and displayed, defaults to 20")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

# define BPF program
Expand Down Expand Up @@ -128,8 +130,13 @@
}
"""

bpf_text.replace("MAXARG", args.max_args)
if args.ebpf:
print(bpf_text)
exit()

# initialize BPF
b = BPF(text=bpf_text.replace("MAXARG", args.max_args))
b = BPF(text=bpf_text)

# header
if args.timestamp:
Expand Down
6 changes: 5 additions & 1 deletion tools/ext4dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
pid = args.pid
countdown = int(args.count)
Expand Down Expand Up @@ -163,8 +165,10 @@
bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
else:
bpf_text = bpf_text.replace('FILTER_PID', '0')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# load BPF program
b = BPF(text=bpf_text)
Expand Down
6 changes: 5 additions & 1 deletion tools/ext4slower.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
help="trace this PID only")
parser.add_argument("min_ms", nargs="?", default='10',
help="minimum I/O duration to trace, in ms (default 10)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
min_ms = int(args.min_ms)
pid = args.pid
Expand Down Expand Up @@ -274,8 +276,10 @@
bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % pid)
else:
bpf_text = bpf_text.replace('FILTER_PID', '0')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# kernel->user event data: struct data_t
DNAME_INLINE_LEN = 32 # linux/dcache.h
Expand Down
6 changes: 5 additions & 1 deletion tools/filelife.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
epilog=examples)
parser.add_argument("-p", "--pid",
help="trace this PID only")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
debug = 0

Expand Down Expand Up @@ -114,8 +116,10 @@ class Data(ct.Structure):
'if (pid != %s) { return 0; }' % args.pid)
else:
bpf_text = bpf_text.replace('FILTER', '')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# initialize BPF
b = BPF(text=bpf_text)
Expand Down
8 changes: 6 additions & 2 deletions tools/fileslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
help="include non-regular file types (sockets, FIFOs, etc)")
parser.add_argument("min_ms", nargs="?", default='10',
help="minimum I/O duration to trace, in ms (default 10)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
min_ms = int(args.min_ms)
tgid = args.tgid
Expand Down Expand Up @@ -185,11 +187,13 @@
else:
bpf_text = bpf_text.replace('TYPE_FILTER', '!S_ISREG(mode)')

if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# initialize BPF
b = BPF(text=bpf_text,)
b = BPF(text=bpf_text)

# I'd rather trace these via new_sync_read/new_sync_write (which used to be
# do_sync_read/do_sync_write), but those became static. So trace these from
Expand Down
6 changes: 5 additions & 1 deletion tools/filetop.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
interval = int(args.interval)
countdown = int(args.count)
Expand Down Expand Up @@ -149,8 +151,10 @@ def signal_ignore(signal, frame):
else:
bpf_text = bpf_text.replace('TYPE_FILTER', '!S_ISREG(mode)')

if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# initialize BPF
b = BPF(text=bpf_text)
Expand Down
6 changes: 5 additions & 1 deletion tools/funclatency.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
help="print the BPF program (for debugging purposes)")
parser.add_argument("pattern",
help="search expression for functions")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

def bail(error):
Expand Down Expand Up @@ -184,8 +186,10 @@ def bail(error):
bpf_text = bpf_text.replace('ENTRYSTORE', '')
bpf_text = bpf_text.replace('STORE',
'dist.increment(bpf_log2l(delta));')
if args.verbose:
if args.verbose or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# signal handler
def signal_ignore(signal, frame):
Expand Down
6 changes: 5 additions & 1 deletion tools/funcslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
help="print the BPF program for debugging purposes")
parser.add_argument(metavar="function", nargs="+", dest="functions",
help="function(s) to trace")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)

args = parser.parse_args()
# fractions are allowed, but rounded to an integer nanosecond
Expand Down Expand Up @@ -165,8 +167,10 @@
}
""" % (i, i)

if args.verbose:
if args.verbose or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

b = BPF(text=bpf_text)

Expand Down
6 changes: 6 additions & 0 deletions tools/gethostlatency.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
epilog=examples)
parser.add_argument("-p", "--pid", help="trace this PID only", type=int,
default=-1)
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

# load BPF program
Expand Down Expand Up @@ -94,6 +96,10 @@
return 0;
}
"""
if args.ebpf:
print(bpf_text)
exit()

b = BPF(text=bpf_text)
b.attach_uprobe(name="c", sym="getaddrinfo", fn_name="do_entry", pid=args.pid)
b.attach_uprobe(name="c", sym="gethostbyname", fn_name="do_entry",
Expand Down
6 changes: 5 additions & 1 deletion tools/hardirqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
help="output interval, in seconds")
parser.add_argument("outputs", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
countdown = int(args.outputs)
if args.count and (args.dist or args.nanoseconds):
Expand Down Expand Up @@ -136,8 +138,10 @@
'bpf_probe_read(&key.name, sizeof(key.name), name);' +
'u64 zero = 0, *vp = dist.lookup_or_init(&key, &zero);' +
'(*vp) += delta;')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# load BPF program
b = BPF(text=bpf_text)
Expand Down
6 changes: 5 additions & 1 deletion tools/killsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
help="only show failed kill syscalls")
parser.add_argument("-p", "--pid",
help="trace this PID only")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
debug = 0

Expand Down Expand Up @@ -102,8 +104,10 @@
'if (pid != %s) { return 0; }' % args.pid)
else:
bpf_text = bpf_text.replace('FILTER', '')
if debug:
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()

# initialize BPF
b = BPF(text=bpf_text)
Expand Down
11 changes: 9 additions & 2 deletions tools/llcstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
help="Sample one in this many number of cache reference / miss events")
parser.add_argument(
"duration", nargs="?", default=10, help="Duration, in seconds, to run")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()

# load BPF program
b = BPF(text="""
bpf_text="""
#include <linux/ptrace.h>
#include <uapi/linux/bpf_perf_event.h>
Expand Down Expand Up @@ -71,8 +73,13 @@
return 0;
}
""")
"""

if args.ebpf:
print(bpf_text)
exit()

b = BPF(text=bpf_text)
b.attach_perf_event(
ev_type=PerfType.HARDWARE, ev_config=PerfHWConfig.CACHE_MISSES,
fn_name="on_cache_miss", sample_period=args.sample_period)
Expand Down
Loading

0 comments on commit cf0792f

Please sign in to comment.