blob: 4d7c7958b64ff1cb01ef7d9e6de1d7ed44fd05ac [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/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#
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +08006# Written as a basic example of using histograms to show a distribution.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07007#
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +08008# A Ctrl-C will print the gathered histogram then exit.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07009#
10# Copyright (c) 2015 Brendan Gregg.
11# Licensed under the Apache License, Version 2.0 (the "License")
12#
13# 15-Aug-2015 Brendan Gregg Created this.
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080014# 03-Feb-2019 Xiaozhou Liu added linear histogram.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070015
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);
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080026BPF_HISTOGRAM(dist_linear);
Brendan Gregg310ab532016-07-24 13:34:40 -070027
28int kprobe__blk_account_io_completion(struct pt_regs *ctx, struct request *req)
29{
30 dist.increment(bpf_log2l(req->__data_len / 1024));
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080031 dist_linear.increment(req->__data_len / 1024);
Brendan Gregg310ab532016-07-24 13:34:40 -070032 return 0;
33}
34""")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070035
36# header
37print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070038
Brendan Gregg0823f562015-09-25 11:07:35 -070039# trace until Ctrl-C
Brendan Greggf32a67c2015-09-07 14:42:12 -070040try:
41 sleep(99999999)
42except KeyboardInterrupt:
Javier Honduvilla Cotofe966bb2018-05-10 20:45:45 +020043 print()
Brendan Greggf32a67c2015-09-07 14:42:12 -070044
Brendan Gregg0823f562015-09-25 11:07:35 -070045# output
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080046print("log2 histogram")
47print("~~~~~~~~~~~~~~")
Brendan Gregg665c5b02015-09-21 11:55:52 -070048b["dist"].print_log2_hist("kbytes")
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080049
50print("\nlinear histogram")
51print("~~~~~~~~~~~~~~~~")
52b["dist_linear"].print_linear_hist("kbytes")