merge disksnoop example
authorBrendan Gregg <brendan.d.gregg@gmail.com>
Sun, 24 Jul 2016 20:37:20 +0000 (13:37 -0700)
committerBrendan Gregg <brendan.d.gregg@gmail.com>
Sun, 24 Jul 2016 20:37:20 +0000 (13:37 -0700)
README.md
examples/tracing/disksnoop.c [deleted file]
examples/tracing/disksnoop.py

index ded4d9e..543efb1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -60,7 +60,7 @@ pair of .c and .py files, and some are directories of files.
 Examples:
 
 - examples/tracing/[bitehist.py](examples/tracing/bitehist.py): Block I/O size histogram. [Examples](examples/tracing/bitehist_example.txt).
-- examples/tracing/[disksnoop.py](examples/tracing/disksnoop.py) examples/tracing/[disksnoop.c](examples/tracing/disksnoop.c): Trace block device I/O latency. [Examples](examples/tracing/disksnoop_example.txt).
+- examples/tracing/[disksnoop.py](examples/tracing/disksnoop.py): Trace block device I/O latency. [Examples](examples/tracing/disksnoop_example.txt).
 - examples/[hello_world.py](examples/hello_world.py): Prints "Hello, World!" for new processes.
 - examples/tracing/[tcpv4connect.py](examples/tracing/tcpv4connect.py): Trace TCP IPv4 active connections. [Examples](examples/tracing/tcpv4connect_example.txt).
 - examples/tracing/[trace_fields.py](examples/tracing/trace_fields.py): Simple example of printing fields from traced events.
diff --git a/examples/tracing/disksnoop.c b/examples/tracing/disksnoop.c
deleted file mode 100644 (file)
index 8c29549..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * disksnoop.c Trace block device I/O: basic version of iosnoop.
- *             For Linux, uses BCC, eBPF. See .py file.
- *
- * Copyright (c) 2015 Brendan Gregg.
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * 11-Aug-2015 Brendan Gregg   Created this.
- */
-
-#include <uapi/linux/ptrace.h>
-#include <linux/blkdev.h>
-
-BPF_HASH(start, struct request *);
-
-void trace_start(struct pt_regs *ctx, struct request *req) {
-       // stash start timestamp by request ptr
-       u64 ts = bpf_ktime_get_ns();
-
-       start.update(&req, &ts);
-}
-
-void trace_completion(struct pt_regs *ctx, struct request *req) {
-       u64 *tsp, delta;
-
-       tsp = start.lookup(&req);
-       if (tsp != 0) {
-               delta = bpf_ktime_get_ns() - *tsp;
-               bpf_trace_printk("%d %x %d\n", req->__data_len,
-                   req->cmd_flags, delta / 1000);
-               start.delete(&req);
-       }
-}
index 206b618..ed3dd81 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/python
 #
 # disksnoop.py Trace block device I/O: basic version of iosnoop.
-#              For Linux, uses BCC, eBPF. See .c file.
+#              For Linux, uses BCC, eBPF. Embedded C.
 #
 # Written as a basic example of tracing latency.
 #
@@ -16,7 +16,32 @@ from bcc import BPF
 REQ_WRITE = 1          # from include/linux/blk_types.h
 
 # load BPF program
-b = BPF(src_file="disksnoop.c")
+b = BPF(text="""
+#include <uapi/linux/ptrace.h>
+#include <linux/blkdev.h>
+
+BPF_HASH(start, struct request *);
+
+void trace_start(struct pt_regs *ctx, struct request *req) {
+       // stash start timestamp by request ptr
+       u64 ts = bpf_ktime_get_ns();
+
+       start.update(&req, &ts);
+}
+
+void trace_completion(struct pt_regs *ctx, struct request *req) {
+       u64 *tsp, delta;
+
+       tsp = start.lookup(&req);
+       if (tsp != 0) {
+               delta = bpf_ktime_get_ns() - *tsp;
+               bpf_trace_printk("%d %x %d\\n", req->__data_len,
+                   req->cmd_flags, delta / 1000);
+               start.delete(&req);
+       }
+}
+""")
+
 b.attach_kprobe(event="blk_start_request", fn_name="trace_start")
 b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_start")
 b.attach_kprobe(event="blk_account_io_completion", fn_name="trace_completion")