Last active
July 4, 2023 08:32
-
-
Save CyrusF/7f830944f4d8ac237880ea0bb27438ef to your computer and use it in GitHub Desktop.
sync_perf_output.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
#!/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() |
Hmmmm… That is an error but it seems no %b in the code? Maybe you type %d as %b by mistake?
event.pid and event.delta are integers, but you use the %s. Maybe it is a problem.
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
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'