blob: 4641ea3a35af63e964ec7a0ea8e1f047cf6bf0fa [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/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
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070017
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070018# load BPF program
Brendan Greggb90bbab2016-02-15 15:55:08 -080019b = BPF(text="""
20#include <uapi/linux/ptrace.h>
21
22struct key_t {
23 u64 ip;
24};
25
Teng Qin7a3e5bc2017-03-29 13:39:17 -070026BPF_HASH(counts, struct key_t, u64, 256);
Brendan Greggb90bbab2016-02-15 15:55:08 -080027
Nan Xiao92d86bb2017-08-22 09:59:26 +080028int do_count(struct pt_regs *ctx) {
Brendan Greggb90bbab2016-02-15 15:55:08 -080029 struct key_t key = {};
30 u64 zero = 0, *val;
Naveen N. Rao4afa96a2016-05-03 14:54:21 +053031 key.ip = PT_REGS_IP(ctx);
Brendan Greggb90bbab2016-02-15 15:55:08 -080032 val = counts.lookup_or_init(&key, &zero);
33 (*val)++;
34 return 0;
35}
36""")
Brendan Gregg4cbdef72015-08-29 14:53:22 +100037b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070038
39# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070040print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070041
42# output
43try:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080044 sleep(99999999)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070045except KeyboardInterrupt:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080046 pass
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070047
Brendan Gregg4cbdef72015-08-29 14:53:22 +100048print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT"))
Brendan Gregg057b0782015-08-20 12:40:37 -070049counts = b.get_table("counts")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070050for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value))