1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/env python 2*387f9dfdSAndroid Build Coastguard Worker# 3*387f9dfdSAndroid Build Coastguard Worker# bitehist.py Block I/O size histogram. 4*387f9dfdSAndroid Build Coastguard Worker# For Linux, uses BCC, eBPF. See .c file. 5*387f9dfdSAndroid Build Coastguard Worker# 6*387f9dfdSAndroid Build Coastguard Worker# USAGE: bitesize 7*387f9dfdSAndroid Build Coastguard Worker# 8*387f9dfdSAndroid Build Coastguard Worker# Ctrl-C will print the partially gathered histogram then exit. 9*387f9dfdSAndroid Build Coastguard Worker# 10*387f9dfdSAndroid Build Coastguard Worker# Copyright (c) 2016 Allan McAleavy 11*387f9dfdSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License") 12*387f9dfdSAndroid Build Coastguard Worker# 13*387f9dfdSAndroid Build Coastguard Worker# 05-Feb-2016 Allan McAleavy ran pep8 against file 14*387f9dfdSAndroid Build Coastguard Worker# 19-Mar-2019 Brendan Gregg Switched to use tracepoints. 15*387f9dfdSAndroid Build Coastguard Worker 16*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF 17*387f9dfdSAndroid Build Coastguard Workerfrom time import sleep 18*387f9dfdSAndroid Build Coastguard Worker 19*387f9dfdSAndroid Build Coastguard Workerbpf_text = """ 20*387f9dfdSAndroid Build Coastguard Worker#include <uapi/linux/ptrace.h> 21*387f9dfdSAndroid Build Coastguard Worker#include <linux/blkdev.h> 22*387f9dfdSAndroid Build Coastguard Worker 23*387f9dfdSAndroid Build Coastguard Workerstruct proc_key_t { 24*387f9dfdSAndroid Build Coastguard Worker char name[TASK_COMM_LEN]; 25*387f9dfdSAndroid Build Coastguard Worker u64 slot; 26*387f9dfdSAndroid Build Coastguard Worker}; 27*387f9dfdSAndroid Build Coastguard Worker 28*387f9dfdSAndroid Build Coastguard WorkerBPF_HISTOGRAM(dist, struct proc_key_t); 29*387f9dfdSAndroid Build Coastguard Worker 30*387f9dfdSAndroid Build Coastguard WorkerTRACEPOINT_PROBE(block, block_rq_issue) 31*387f9dfdSAndroid Build Coastguard Worker{ 32*387f9dfdSAndroid Build Coastguard Worker struct proc_key_t key = {.slot = bpf_log2l(args->bytes / 1024)}; 33*387f9dfdSAndroid Build Coastguard Worker bpf_probe_read_kernel(&key.name, sizeof(key.name), args->comm); 34*387f9dfdSAndroid Build Coastguard Worker dist.atomic_increment(key); 35*387f9dfdSAndroid Build Coastguard Worker return 0; 36*387f9dfdSAndroid Build Coastguard Worker} 37*387f9dfdSAndroid Build Coastguard Worker""" 38*387f9dfdSAndroid Build Coastguard Worker 39*387f9dfdSAndroid Build Coastguard Worker# load BPF program 40*387f9dfdSAndroid Build Coastguard Workerb = BPF(text=bpf_text) 41*387f9dfdSAndroid Build Coastguard Worker 42*387f9dfdSAndroid Build Coastguard Workerprint("Tracing block I/O... Hit Ctrl-C to end.") 43*387f9dfdSAndroid Build Coastguard Worker 44*387f9dfdSAndroid Build Coastguard Worker# trace until Ctrl-C 45*387f9dfdSAndroid Build Coastguard Workerdist = b.get_table("dist") 46*387f9dfdSAndroid Build Coastguard Worker 47*387f9dfdSAndroid Build Coastguard Workertry: 48*387f9dfdSAndroid Build Coastguard Worker sleep(99999999) 49*387f9dfdSAndroid Build Coastguard Workerexcept KeyboardInterrupt: 50*387f9dfdSAndroid Build Coastguard Worker dist.print_log2_hist("Kbytes", "Process Name", 51*387f9dfdSAndroid Build Coastguard Worker section_print_fn=bytes.decode) 52