1#!/usr/bin/env python 2# 3# bitehist.py Block I/O size histogram. 4# For Linux, uses BCC, eBPF. See .c file. 5# 6# USAGE: bitesize 7# 8# Ctrl-C will print the partially gathered histogram then exit. 9# 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 14# 19-Mar-2019 Brendan Gregg Switched to use tracepoints. 15 16from bcc import BPF 17from time import sleep 18 19bpf_text = """ 20#include <uapi/linux/ptrace.h> 21#include <linux/blkdev.h> 22 23struct proc_key_t { 24 char name[TASK_COMM_LEN]; 25 u64 slot; 26}; 27 28BPF_HISTOGRAM(dist, struct proc_key_t); 29 30TRACEPOINT_PROBE(block, block_rq_issue) 31{ 32 struct proc_key_t key = {.slot = bpf_log2l(args->bytes / 1024)}; 33 bpf_probe_read_kernel(&key.name, sizeof(key.name), args->comm); 34 dist.atomic_increment(key); 35 return 0; 36} 37""" 38 39# load BPF program 40b = BPF(text=bpf_text) 41 42print("Tracing block I/O... Hit Ctrl-C to end.") 43 44# trace until Ctrl-C 45dist = b.get_table("dist") 46 47try: 48 sleep(99999999) 49except KeyboardInterrupt: 50 dist.print_log2_hist("Kbytes", "Process Name", 51 section_print_fn=bytes.decode) 52