forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncsnoop.py
executable file
·128 lines (108 loc) · 3 KB
/
syncsnoop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
# @lint-avoid-python-3-compatibility-imports
#
# syncsnoop Trace sync() syscall.
# For Linux, uses BCC, eBPF. Embedded C.
#
# Written as a basic example of BCC trace & reformat. See
# examples/hello_world.py for a BCC trace with default output example.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 13-Aug-2015 Brendan Gregg Created this.
# 19-Feb-2016 Allan McAleavy migrated to BPF_PERF_OUTPUT
# 17-Jul-2024 Rong Tao Support more sync syscalls.
from __future__ import print_function
from bcc import BPF
from bcc.utils import printb
import sys
# load BPF program
b = BPF(text="""
#include <linux/sched.h>
enum sync_syscalls {
SYS_T_MIN,
SYS_SYNC,
SYS_FSYNC,
SYS_FDATASYNC,
SYS_MSYNC,
SYS_SYNC_FILE_RANGE,
SYS_SYNCFS,
SYS_T_MAX,
};
struct data_t {
char comm[TASK_COMM_LEN];
u32 sys;
u64 ts;
};
BPF_PERF_OUTPUT(events);
static void __syscall(void *ctx, enum sync_syscalls sys) {
struct data_t data = {};
bpf_get_current_comm(data.comm, sizeof(data.comm));
data.ts = bpf_ktime_get_ns() / 1000;
data.sys = sys;
events.perf_submit(ctx, &data, sizeof(data));
};
void syscall__sync(void *ctx) {
return __syscall(ctx, SYS_SYNC);
}
void syscall__fsync(void *ctx) {
return __syscall(ctx, SYS_FSYNC);
}
void syscall__fdatasync(void *ctx) {
return __syscall(ctx, SYS_FDATASYNC);
}
void syscall__msync(void *ctx) {
return __syscall(ctx, SYS_MSYNC);
}
void syscall__sync_file_range(void *ctx) {
return __syscall(ctx, SYS_SYNC_FILE_RANGE);
}
void syscall__syncfs(void *ctx) {
return __syscall(ctx, SYS_SYNCFS);
}
""")
class EventType(object):
SYS_SYNC = 1,
SYS_FSYNC = 2,
SYS_FDATASYNC = 3,
SYS_MSYNC = 4,
SYS_SYNC_FILE_RANGE = 5,
SYS_SYNCFS = 6,
sys_names = (
"N/A",
"sync",
"fsync",
"fdatasync",
"msync",
"sync_file_range",
"syncfs",
"N/A",
)
b.attach_kprobe(event=b.get_syscall_fnname("sync"),
fn_name="syscall__sync")
b.attach_kprobe(event=b.get_syscall_fnname("fsync"),
fn_name="syscall__fsync")
b.attach_kprobe(event=b.get_syscall_fnname("fdatasync"),
fn_name="syscall__fdatasync")
b.attach_kprobe(event=b.get_syscall_fnname("msync"),
fn_name="syscall__msync")
b.attach_kprobe(event=b.get_syscall_fnname("sync_file_range"),
fn_name="syscall__sync_file_range")
b.attach_kprobe(event=b.get_syscall_fnname("syncfs"),
fn_name="syscall__syncfs")
# header
print("%-18s %-16s %-16s" % ("TIME(s)", "COMM", "CALL"))
# process event
def print_event(cpu, data, size):
event = b["events"].event(data)
printb(b"%-18.9f %-16s" % (float(event.ts) / 1000000, event.comm), nl="")
print(" %-16s" % sys_names[event.sys])
sys.stdout.flush()
# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()