blob: 69e36335ba71a248b2a16fbe494e1c20ea1aac0e [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
mcaleavya6959dcf2016-02-06 12:00:34 +00002#
3# bitehist.py Block I/O size histogram.
4# For Linux, uses BCC, eBPF. See .c file.
5#
6# USAGE: bitesize
mcaleavya6959dcf2016-02-06 12:00:34 +00007#
Brendan Gregg6497a842016-02-06 17:08:38 -08008# Ctrl-C will print the partially gathered histogram then exit.
mcaleavya6959dcf2016-02-06 12:00:34 +00009#
10# Copyright (c) 2016 Allan McAleavy
11# Licensed under the Apache License, Version 2.0 (the "License")
12#
13# 05-Feb-2016 Allan McAleavy ran pep8 against file
Brendan Greggc7736ee2019-03-20 09:01:57 -070014# 19-Mar-2019 Brendan Gregg Switched to use tracepoints.
mcaleavya6959dcf2016-02-06 12:00:34 +000015
16from bcc import BPF
17from time import sleep
18
19bpf_text = """
20#include <uapi/linux/ptrace.h>
21#include <linux/blkdev.h>
22
Brendan Greggf6e92c92016-02-06 17:11:59 -080023struct proc_key_t {
mcaleavya6959dcf2016-02-06 12:00:34 +000024 char name[TASK_COMM_LEN];
25 u64 slot;
26};
27
mcaleavya6959dcf2016-02-06 12:00:34 +000028BPF_HISTOGRAM(dist, struct proc_key_t);
mcaleavya6959dcf2016-02-06 12:00:34 +000029
Brendan Gregg6b05e612019-03-20 21:13:59 -070030TRACEPOINT_PROBE(block, block_rq_issue)
mcaleavya6959dcf2016-02-06 12:00:34 +000031{
Brendan Greggc7736ee2019-03-20 09:01:57 -070032 struct proc_key_t key = {.slot = bpf_log2l(args->bytes / 1024)};
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -050033 bpf_probe_read_kernel(&key.name, sizeof(key.name), args->comm);
zcy80242fb2021-07-02 00:12:32 +080034 dist.atomic_increment(key);
Brendan Gregg6497a842016-02-06 17:08:38 -080035 return 0;
mcaleavya6959dcf2016-02-06 12:00:34 +000036}
37"""
38
39# load BPF program
40b = BPF(text=bpf_text)
mcaleavya6959dcf2016-02-06 12:00:34 +000041
Brendan Greggc7736ee2019-03-20 09:01:57 -070042print("Tracing block I/O... Hit Ctrl-C to end.")
mcaleavya6959dcf2016-02-06 12:00:34 +000043
44# trace until Ctrl-C
45dist = b.get_table("dist")
46
47try:
48 sleep(99999999)
49except KeyboardInterrupt:
Rafael F78948e42017-03-26 14:54:25 +020050 dist.print_log2_hist("Kbytes", "Process Name",
51 section_print_fn=bytes.decode)