blob: 10c6b1eb185452e7aa7dbd25daed84597c4f09d7 [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 = {};
Naveen N. Rao4afa96a2016-05-03 14:54:21 +053030 key.ip = PT_REGS_IP(ctx);
Javier Honduvilla Coto64bf9652018-08-01 06:50:19 +020031 counts.increment(key);
Brendan Greggb90bbab2016-02-15 15:55:08 -080032 return 0;
33}
34""")
Brendan Gregg4cbdef72015-08-29 14:53:22 +100035b.attach_kprobe(event_re="^vfs_.*", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070036
37# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070038print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070039
40# output
41try:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080042 sleep(99999999)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070043except KeyboardInterrupt:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080044 pass
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070045
Brendan Gregg4cbdef72015-08-29 14:53:22 +100046print("\n%-16s %-26s %8s" % ("ADDR", "FUNC", "COUNT"))
Brendan Gregg057b0782015-08-20 12:40:37 -070047counts = b.get_table("counts")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070048for k, v in sorted(counts.items(), key=lambda counts: counts[1].value):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 print("%-16x %-26s %8d" % (k.ip, b.ksym(k.ip), v.value))