blob: c8c7f7a6500b1fd2f98059f35992363903dfce6b [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
Javier Honduvilla Cotofe966bb2018-05-10 20:45:45 +020016from __future__ import print_function
Brenden Blancoc35989d2015-09-02 18:04:07 -070017from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070018from time import sleep
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070019
20# load BPF program
Brendan Gregg310ab532016-07-24 13:34:40 -070021b = BPF(text="""
22#include <uapi/linux/ptrace.h>
23#include <linux/blkdev.h>
24
25BPF_HISTOGRAM(dist);
26
27int kprobe__blk_account_io_completion(struct pt_regs *ctx, struct request *req)
28{
29 dist.increment(bpf_log2l(req->__data_len / 1024));
30 return 0;
31}
32""")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070033
34# header
35print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070036
Brendan Gregg0823f562015-09-25 11:07:35 -070037# trace until Ctrl-C
Brendan Greggf32a67c2015-09-07 14:42:12 -070038try:
39 sleep(99999999)
40except KeyboardInterrupt:
Javier Honduvilla Cotofe966bb2018-05-10 20:45:45 +020041 print()
Brendan Greggf32a67c2015-09-07 14:42:12 -070042
Brendan Gregg0823f562015-09-25 11:07:35 -070043# output
Brendan Gregg665c5b02015-09-21 11:55:52 -070044b["dist"].print_log2_hist("kbytes")