blob: e58ce68589a083af360e4819f9a9607de71689fd [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# vfscount Count VFS calls ("vfs_*").
5# For Linux, uses BCC, eBPF. See .c file.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07006#
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#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080012# 14-Aug-2015 Brendan Gregg Created this.
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070013
Brendan Gregg762b1b42015-08-18 16:49:48 -070014from __future__ import print_function
Brenden Blancoc35989d2015-09-02 18:04:07 -070015from bcc import BPF
Brendan Greggd21af642015-09-04 17:42:51 -070016from time import sleep
Wjiea2e71a92019-05-11 00:19:22 +080017from sys import argv
18def usage():
19 print("USAGE: %s [time]" % argv[0])
20 exit()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070021
Wjiea2e71a92019-05-11 00:19:22 +080022interval = 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()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070030# load BPF program
Brendan Greggb90bbab2016-02-15 15:55:08 -080031b = BPF(text="""
32#include <uapi/linux/ptrace.h>
33
34struct key_t {
35 u64 ip;
36};
37
Teng Qin7a3e5bc2017-03-29 13:39:17 -070038BPF_HASH(counts, struct key_t, u64, 256);
Brendan Greggb90bbab2016-02-15 15:55:08 -080039
Nan Xiao92d86bb2017-08-22 09:59:26 +080040int do_count(struct pt_regs *ctx) {
Brendan Greggb90bbab2016-02-15 15:55:08 -080041 struct key_t key = {};
Naveen N. Rao4afa96a2016-05-03 14:54:21 +053042 key.ip = PT_REGS_IP(ctx);
zcy80242fb2021-07-02 00:12:32 +080043 counts.atomic_increment(key);
Brendan Greggb90bbab2016-02-15 15:55:08 -080044 return 0;
45}
46""")
Brendan Gregg4cbdef72015-08-29 14:53:22 +100047b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070048
49# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070050print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070051
52# output
53try:
Wjiea2e71a92019-05-11 00:19:22 +080054 sleep(interval)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070055except KeyboardInterrupt:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080056 pass
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070057
Brendan Gregg4cbdef72015-08-29 14:53:22 +100058print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT"))
Brendan Gregg057b0782015-08-20 12:40:37 -070059counts = b.get_table("counts")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070060for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080061 print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value))