File: task_storage.py

package info (click to toggle)
bpfcc 0.26.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 20,568 kB
  • sloc: ansic: 488,515; python: 38,439; cpp: 25,333; sh: 780; makefile: 262
file content (41 lines) | stat: -rwxr-xr-x 928 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python3

from bcc import BPF

source = r"""
BPF_TASK_STORAGE(task_storage_map, __u64);

KFUNC_PROBE(inet_listen)
{
	__u64 ts = bpf_ktime_get_ns();

	/* save timestamp to local storage on function entry */
	task_storage_map.task_storage_get(bpf_get_current_task_btf(), &ts, BPF_LOCAL_STORAGE_GET_F_CREATE);

	bpf_trace_printk("inet_listen entry: store timestamp %lld", ts);
	return 0;
}

KRETFUNC_PROBE(inet_listen)
{
	__u64 *ts;

	/* retrieve timestamp stored at local storage on function exit */
	ts = task_storage_map.task_storage_get(bpf_get_current_task_btf(), 0, 0);
	if (!ts)
		return 0;

	/* delete timestamp from local storage */
	task_storage_map.task_storage_delete(bpf_get_current_task_btf());

	/* calculate latency */
	bpf_trace_printk("inet_listen exit: cost %lldus", (bpf_ktime_get_ns() - *ts) / 1000);
	return 0;
}
"""

b = BPF(text=source)
try:
    b.trace_print()
except KeyboardInterrupt:
    pass