blob: 1dee5dc059371e876572ee667f565ee41d0358ec [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
3# bitehist.py Block I/O size histogram.
Brendan Gregg310ab532016-07-24 13:34:40 -07004# For Linux, uses BCC, eBPF. Embedded C.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07005#
6# Written as a basic example of using a histogram to show a distribution.
7#
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07008# 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 Blancoc35989d2015-09-02 18:04:07 -070016from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070017from time import sleep
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070018
19# load BPF program
Brendan Gregg310ab532016-07-24 13:34:40 -070020b = BPF(text="""
21#include <uapi/linux/ptrace.h>
22#include <linux/blkdev.h>
23
24BPF_HISTOGRAM(dist);
25
26int 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 Gregg48fbc3e2015-08-18 14:56:14 -070032
33# header
34print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070035
Brendan Gregg0823f562015-09-25 11:07:35 -070036# trace until Ctrl-C
Brendan Greggf32a67c2015-09-07 14:42:12 -070037try:
38 sleep(99999999)
39except KeyboardInterrupt:
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070040 print
Brendan Greggf32a67c2015-09-07 14:42:12 -070041
Brendan Gregg0823f562015-09-25 11:07:35 -070042# output
Brendan Gregg665c5b02015-09-21 11:55:52 -070043b["dist"].print_log2_hist("kbytes")