blob: 81e84594b6a8f9e8bf915d5aa9210c6be3b07c81 [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>
xingfeng2510109453e2022-03-04 16:57:51 +080023#include <linux/blk-mq.h>
Brendan Gregg310ab532016-07-24 13:34:40 -070024
25BPF_HISTOGRAM(dist);
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080026BPF_HISTOGRAM(dist_linear);
Brendan Gregg310ab532016-07-24 13:34:40 -070027
xingfeng2510109453e2022-03-04 16:57:51 +080028int trace_req_done(struct pt_regs *ctx, struct request *req)
Brendan Gregg310ab532016-07-24 13:34:40 -070029{
xingfeng2510109453e2022-03-04 16:57:51 +080030 dist.increment(bpf_log2l(req->__data_len / 1024));
31 dist_linear.increment(req->__data_len / 1024);
32 return 0;
Brendan Gregg310ab532016-07-24 13:34:40 -070033}
34""")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070035
xingfeng2510109453e2022-03-04 16:57:51 +080036if BPF.get_kprobe_functions(b'__blk_account_io_done'):
37 b.attach_kprobe(event="__blk_account_io_done", fn_name="trace_req_done")
38else:
39 b.attach_kprobe(event="blk_account_io_done", fn_name="trace_req_done")
40
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070041# header
42print("Tracing... Hit Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070043
Brendan Gregg0823f562015-09-25 11:07:35 -070044# trace until Ctrl-C
Brendan Greggf32a67c2015-09-07 14:42:12 -070045try:
xingfeng2510109453e2022-03-04 16:57:51 +080046 sleep(99999999)
Brendan Greggf32a67c2015-09-07 14:42:12 -070047except KeyboardInterrupt:
xingfeng2510109453e2022-03-04 16:57:51 +080048 print()
Brendan Greggf32a67c2015-09-07 14:42:12 -070049
Brendan Gregg0823f562015-09-25 11:07:35 -070050# output
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080051print("log2 histogram")
52print("~~~~~~~~~~~~~~")
Brendan Gregg665c5b02015-09-21 11:55:52 -070053b["dist"].print_log2_hist("kbytes")
Xiaozhou Liu10c8bd32019-02-03 15:50:21 +080054
55print("\nlinear histogram")
56print("~~~~~~~~~~~~~~~~")
57b["dist_linear"].print_linear_hist("kbytes")