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 17 18# load BPF program 19b = BPF(text=""" 20#include <uapi/linux/ptrace.h> 21 22struct key_t { 23 u64 ip; 24}; 25 26BPF_HASH(counts, struct key_t, u64, 256); 27 28int do_count(struct pt_regs *ctx) { 29 struct key_t key = {}; 30 key.ip = PT_REGS_IP(ctx); 31 counts.increment(key); 32 return 0; 33} 34""") 35b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count") 36 37# header 38print("Tracing... Ctrl-C to end.") 39 40# output 41try: 42 sleep(99999999) 43except KeyboardInterrupt: 44 pass 45 46print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT")) 47counts = b.get_table("counts") 48for k, v in sorted(counts.items(), key=lambda counts: counts[1].value): 49 print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value)) 50