-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.py
51 lines (37 loc) · 1.49 KB
/
trace.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
#!/bin/python3
from bcc import BPF
import signal
import sys
class ReqData:
__slots__ = ['timestamp','count', 'flag', 'complete']
def __init__(self, other):
for name in self.__slots__:
self.__setattr__(name, other.__getattribute__(name))
def print_cvs(type, data, file):
print(','.join(type.__slots__), file=file)
for event in data:
data = map(lambda name: str(event.__getattribute__(name)), type.__slots__)
print(','.join(data), file=file)
if __name__ == '__main__':
prefix = sys.argv[1]
with open('trace.c') as file:
program = file.read()
bpf = BPF(text=program)
bpf.attach_kprobe(event='__page_cache_alloc', fn_name='trace_req_start')
bpf.attach_kretprobe(event='__page_cache_alloc', fn_name='trace_req_done')
# bpf.attach_kprobe(event='__page_cache_alloc_readahead', fn_name='trace_req_start')
# bpf.attach_kretprobe(event='__page_cache_alloc_readahead', fn_name='trace_req_done')
write = []
def handler(sig=None, frame=None):
print_cvs(ReqData, write, open(prefix + 'req.txt', 'w'))
exit()
signal.signal(signal.SIGINT, handler)
def handle_write(cpu, omg_data, size):
event = bpf['req_output'].event(omg_data)
write.append(ReqData(event))
bpf['req_output'].open_ring_buffer(handle_write)
try:
while 1:
bpf.ring_buffer_poll()
except KeyboardInterrupt:
handler()