Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 2 | # |
| 3 | # bitehist.py Block I/O size histogram. |
Brendan Gregg | 310ab53 | 2016-07-24 13:34:40 -0700 | [diff] [blame] | 4 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 5 | # |
| 6 | # Written as a basic example of using a histogram to show a distribution. |
| 7 | # |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 8 | # The default interval is 5 seconds. A Ctrl-C will print the partially |
| 9 | # gathered histogram then exit. |
| 10 | # |
| 11 | # Copyright (c) 2015 Brendan Gregg. |
| 12 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 13 | # |
| 14 | # 15-Aug-2015 Brendan Gregg Created this. |
| 15 | |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 16 | from bcc import BPF |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 17 | from time import sleep |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 18 | |
| 19 | # load BPF program |
Brendan Gregg | 310ab53 | 2016-07-24 13:34:40 -0700 | [diff] [blame] | 20 | b = BPF(text=""" |
| 21 | #include <uapi/linux/ptrace.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | |
| 24 | BPF_HISTOGRAM(dist); |
| 25 | |
| 26 | int kprobe__blk_account_io_completion(struct pt_regs *ctx, struct request *req) |
| 27 | { |
| 28 | dist.increment(bpf_log2l(req->__data_len / 1024)); |
| 29 | return 0; |
| 30 | } |
| 31 | """) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 32 | |
| 33 | # header |
| 34 | print("Tracing... Hit Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 35 | |
Brendan Gregg | 0823f56 | 2015-09-25 11:07:35 -0700 | [diff] [blame] | 36 | # trace until Ctrl-C |
Brendan Gregg | f32a67c | 2015-09-07 14:42:12 -0700 | [diff] [blame] | 37 | try: |
| 38 | sleep(99999999) |
| 39 | except KeyboardInterrupt: |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 40 | print |
Brendan Gregg | f32a67c | 2015-09-07 14:42:12 -0700 | [diff] [blame] | 41 | |
Brendan Gregg | 0823f56 | 2015-09-25 11:07:35 -0700 | [diff] [blame] | 42 | # output |
Brendan Gregg | 665c5b0 | 2015-09-21 11:55:52 -0700 | [diff] [blame] | 43 | b["dist"].print_log2_hist("kbytes") |