-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.py
More file actions
executable file
·40 lines (30 loc) · 851 Bytes
/
trace.py
File metadata and controls
executable file
·40 lines (30 loc) · 851 Bytes
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
#!/usr/bin/python3
from bcc import BPF
import contextlib
import struct
b = BPF(src_file="probes.c")
class Stats:
def __init__(self):
self.sum = 0
self.count = 0
self.min = float("+inf")
self.max = float("-inf")
def add(self, value):
self.sum += value
self.count += 1
if value < self.min:
self.min = value
if value > self.max:
self.max = value
def handle_event(self, cpu, data, size):
event = b["events"].event(data)
self.add(event.latency / 1000)
@property
def avg(self):
return self.sum / self.count
stats = Stats()
b["events"].open_perf_buffer(stats.handle_event)
with contextlib.suppress(KeyboardInterrupt):
while True:
b.kprobe_poll()
print(f"{stats.min:.3f} {stats.avg:.3f} {stats.max:.3f}")