Last active
June 8, 2021 02:23
-
-
Save pyhundan/b594589b9af5f6f859807b0665700d37 to your computer and use it in GitHub Desktop.
sync_count.py
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
from __future__ import print_function | |
from sys import flags, maxsize | |
from bcc import BPF, PerfHWConfig | |
b= BPF (text=""" | |
//create a map with default | |
BPF_HASH(last); | |
//BPF_HASH(map); | |
int do_trace(struct pt_regs *ctx){ | |
u64 ts,*tsp,delta,key=0; | |
u64 count,*index=0; | |
u64 key1=1; | |
//find key from map,return a pointer | |
index=last.lookup(&key1); | |
tsp= last.lookup(&key); | |
if(tsp!=NULL) | |
{ | |
//Returns the time as nanoseconds. | |
delta=bpf_ktime_get_ns()- *tsp; | |
// output if time is less than 1 second | |
if(delta<1000000000){ | |
bpf_trace_printk("%d\\n",count,delta/1000000); | |
} | |
last.delete(&key); | |
} | |
//add a counter to trace | |
if(index==0) | |
{ | |
count=1; | |
} | |
else{ | |
count=++*index; | |
last.delete(&key1); | |
} | |
last.update(&key1,&count); | |
ts=bpf_ktime_get_ns(); | |
last.update(&key,&ts); | |
return 0; | |
} | |
""") | |
b.attach_kprobe(event=b.get_syscall_fnname("sync"),fn_name="do_trace") | |
print("tracing for sync") | |
start=0 | |
# while 1: | |
# (task,pid,cpu,flags,ts,ms)=b.trace_fields() | |
# if start==0: | |
# start=ts | |
# ts=ts-start | |
# print("At time %.2f s: multiple syncs detected, last %s ms ago " % (ts, ms)) | |
while 1: | |
try: | |
(task,pid,cpu,flags,ts,msg)=b.trace_fields() | |
[count,ms]=msg.split(",") | |
if start==0: | |
start=ts | |
ts=ts-start | |
print("At time %.2f s: %smultiple syncs detected, last %s ms ago " % (ts,count,ms)) | |
except KeyboardInterrupt: | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment