Skip to content

Instantly share code, notes, and snippets.

@CyrusF
Last active July 4, 2023 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyrusF/7f830944f4d8ac237880ea0bb27438ef to your computer and use it in GitHub Desktop.
Save CyrusF/7f830944f4d8ac237880ea0bb27438ef to your computer and use it in GitHub Desktop.
sync_perf_output.py
#!/usr/bin/python
# 2019 by Cyrus
from __future__ import print_function
from bcc import BPF
from bcc.utils import printb
# load BPF program
b = BPF(text="""
#include <uapi/linux/ptrace.h>
struct data_t {
u32 pid;
u64 delta;
u64 time;
};
BPF_HASH(last);
BPF_PERF_OUTPUT(result);
int do_trace(struct pt_regs *ctx) {
u64 ts, *tsp, delta, key = 0;
struct data_t data = {};
// attempt to read stored timestamp
tsp = last.lookup(&key);
if (tsp != 0) {
delta = bpf_ktime_get_ns() - *tsp;
if (delta < 1000000000) {
// output if time is less than 1 second
data.pid = bpf_get_current_pid_tgid();
data.delta = delta / 1000000;
data.time = bpf_ktime_get_ns();
result.perf_submit(ctx, &data, sizeof(data));
}
last.delete(&key);
}
// update stored timestamp
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 quick sync's... Ctrl-C to end")
# format output
start = 0
def print_event(cpu, data, size):
global start
event = b["result"].event(data)
if start == 0:
start = int(event.time)
start_time = (int(event.time) - start) / 1000000;
print(b"[PID:%6s] At time %d ms: multiple syncs detected, last %s ms ago" % (event.pid, start_time, event.delta))
b["result"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
@freelw
Copy link

freelw commented Dec 30, 2022

print(b"[PID:%6s] At time %d ms: multiple syncs detected, last %s ms ago" % (event.pid, start_time, event.delta))
TypeError: %b requires a bytes-like object, or an object that implements bytes, not 'int'

@CyrusF
Copy link
Author

CyrusF commented Dec 30, 2022

Hmmmm… That is an error but it seems no %b in the code? Maybe you type %d as %b by mistake?

@jinjiaKarl
Copy link

event.pid and event.delta are integers, but you use the %s. Maybe it is a problem.

@lvan188
Copy link

lvan188 commented Jul 4, 2023

printb(b"[PID:%6d] At time %.2f ms: multiple syncs detected, last %.2f ms ago" % (event.pid, start_time, event.delta))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment