From: Brendan Gregg Date: Sun, 24 Jul 2016 20:37:20 +0000 (-0700) Subject: merge disksnoop example X-Git-Tag: v0.2.0~39^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9894e3e03525ba45f6bca67ccf1e46efc45f73a7;p=platform%2Fupstream%2Fbcc.git merge disksnoop example --- diff --git a/README.md b/README.md index ded4d9e..543efb1 100644 --- 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 index 8c29549..0000000 --- a/examples/tracing/disksnoop.c +++ /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 -#include - -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); - } -} diff --git a/examples/tracing/disksnoop.py b/examples/tracing/disksnoop.py index 206b618..ed3dd81 100755 --- a/examples/tracing/disksnoop.py +++ b/examples/tracing/disksnoop.py @@ -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 +#include + +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")