Last active
February 8, 2021 23:14
-
-
Save aspsk/cdd53cba9ca55d3c3186981fe71dbda7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
# | |
# Very simple bcc tool written as an example. It counts how many | |
# times mutex_lock/mutex_unlock were called per second. Code is | |
# kinda based on the vfsstat example by Brendan Gregg, but the actual | |
# intersection is almost zero, and this one is smp-safe. | |
# | |
from bcc import BPF | |
from ctypes import c_int | |
from time import sleep, strftime | |
from sys import argv | |
b = BPF(text=""" | |
BPF_PERCPU_ARRAY(mutex_stats, u64, 2); | |
static inline void inc(int key) | |
{ | |
u64 *x = mutex_stats.lookup(&key); | |
if (x) | |
*x += 1; | |
} | |
void do_lock(struct pt_regs *ctx) { inc(0); } | |
void do_unlock(struct pt_regs *ctx) { inc(1); } | |
""") | |
b.attach_kprobe(event="mutex_lock", fn_name="do_lock") | |
b.attach_kprobe(event="mutex_unlock", fn_name="do_unlock") | |
print("%-8s%10s%10s" % ("TIME", "LOCKED", "UNLOCKED")) | |
while 2 * 2 == 4: | |
try: | |
sleep(1) | |
except KeyboardInterrupt: | |
exit() | |
print("%-8s%10d%10d" % ( | |
strftime("%H:%M:%S"), | |
b["mutex_stats"].sum(0).value, | |
b["mutex_stats"].sum(1).value)) | |
b["mutex_stats"].clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment