1#!/usr/bin/env python 2# @lint-avoid-python-3-compatibility-imports 3# 4# vfscount Count VFS calls ("vfs_*"). 5# For Linux, uses BCC, eBPF. See .c file. 6# 7# Written as a basic example of counting functions. 8# 9# Copyright (c) 2015 Brendan Gregg. 10# Licensed under the Apache License, Version 2.0 (the "License") 11# 12# 14-Aug-2015 Brendan Gregg Created this. 13 14from __future__ import print_function 15from bcc import BPF 16from time import sleep 17from sys import argv 18def usage(): 19 print("USAGE: %s [time]" % argv[0]) 20 exit() 21 22interval = 99999999 23if len(argv) > 1: 24 try: 25 interval = int(argv[1]) 26 if interval == 0: 27 raise 28 except: # also catches -h, --help 29 usage() 30# load BPF program 31b = BPF(text=""" 32#include <uapi/linux/ptrace.h> 33 34struct key_t { 35 u64 ip; 36}; 37 38BPF_HASH(counts, struct key_t, u64, 256); 39 40int do_count(struct pt_regs *ctx) { 41 struct key_t key = {}; 42 key.ip = PT_REGS_IP(ctx); 43 counts.atomic_increment(key); 44 return 0; 45} 46""") 47b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count") 48 49# header 50print("Tracing... Ctrl-C to end.") 51 52# output 53try: 54 sleep(interval) 55except KeyboardInterrupt: 56 pass 57 58print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT")) 59counts = b.get_table("counts") 60for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): 61 print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value)) 62