-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathsoftirq-latency-net-rx.bpf.c
166 lines (124 loc) · 4.47 KB
/
softirq-latency-net-rx.bpf.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include "maps.bpf.h"
// Loosely based on https://github.com/xdp-project/xdp-project/blob/master/areas/latency/softirq_net_latency.bt
// 30 buckets for latency, max range is 0.5s .. 1.0s
#define MAX_LATENCY_SLOT 31
#define check_net_rx(vec_nr) \
if (vec_nr != NET_RX_SOFTIRQ) { \
return 0; \
}
/* This provide easy way to disable measuring 'runtime'.
* This avoids hooking 'softirq_exit' as it can be expensive and for NET_RX
* this isn't the right hook as runtime is affected by NAPI packet bulking.
*/
#define CONFIG_MEASURE_RUNTIME 1
// We only use one index in the array for this
u32 net_rx_idx = 0;
struct softirq_latency_key_t {
u32 bucket;
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, u64);
} softirq_raised_total SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, u64);
} softirq_serviced_total SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, u64);
} softirq_raise_timestamp SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, MAX_LATENCY_SLOT + 2);
__type(key, u32);
__type(value, u64);
} softirq_wait_seconds SEC(".maps");
#ifdef CONFIG_MEASURE_RUNTIME
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, u64);
} softirq_entry_timestamp SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, MAX_LATENCY_SLOT + 2);
__type(key, u32);
__type(value, u64);
} softirq_runtime_seconds SEC(".maps");
#endif
SEC("tp_btf/softirq_raise")
int BPF_PROG(softirq_raise, unsigned int vec_nr)
{
u64 *existing_ts_ptr, *raised_total_ptr, ts;
check_net_rx(vec_nr);
ts = bpf_ktime_get_ns();
read_array_ptr(&softirq_raised_total, &net_rx_idx, raised_total_ptr);
*raised_total_ptr += 1;
read_array_ptr(&softirq_raise_timestamp, &net_rx_idx, existing_ts_ptr);
// Set the timestamp only if it is not set, so that we always measure the oldest non-entered raise
if (!*existing_ts_ptr) {
*existing_ts_ptr = ts;
}
return 0;
}
SEC("tp_btf/softirq_entry")
int BPF_PROG(softirq_entry, unsigned int vec_nr)
{
u64 delta_ns, *raise_ts_ptr, *serviced_total_ptr, ts;
struct softirq_latency_key_t key = {};
check_net_rx(vec_nr);
ts = bpf_ktime_get_ns();
read_array_ptr(&softirq_serviced_total, &net_rx_idx, serviced_total_ptr);
*serviced_total_ptr += 1;
read_array_ptr(&softirq_raise_timestamp, &net_rx_idx, raise_ts_ptr);
// Interrupt was re-rased after ts was obtained, resulting in negative duration
if (*raise_ts_ptr > ts) {
return 0;
}
// Interrupt entry started with no corresponding raise, resulting in large duration
if (!*raise_ts_ptr) {
return 0;
}
delta_ns = ts - *raise_ts_ptr;
increment_exp2_histogram_nosync(&softirq_wait_seconds, key, delta_ns, MAX_LATENCY_SLOT);
// Allow raise timestamp to be set again
*raise_ts_ptr = 0;
#ifdef CONFIG_MEASURE_RUNTIME
u64 *existing_entry_ts_ptr;
read_array_ptr(&softirq_entry_timestamp, &net_rx_idx, existing_entry_ts_ptr);
// There is some time from function start to here, so overall service time includes it
*existing_entry_ts_ptr = ts;
#endif
return 0;
}
#ifdef CONFIG_MEASURE_RUNTIME
SEC("tp_btf/softirq_exit")
int BPF_PROG(softirq_exit, unsigned int vec_nr)
{
u64 delta_ns, *entry_ts_ptr, ts;
struct softirq_latency_key_t key = {};
check_net_rx(vec_nr);
ts = bpf_ktime_get_ns();
read_array_ptr(&softirq_entry_timestamp, &net_rx_idx, entry_ts_ptr);
// Interrupt exited with no corresponding entry, resulting in large duration
if (!*entry_ts_ptr) {
return 0;
}
delta_ns = ts - *entry_ts_ptr;
increment_exp2_histogram_nosync(&softirq_runtime_seconds, key, delta_ns, MAX_LATENCY_SLOT);
// Reset entry ts to prevent skipped entries to be counted at exit
*entry_ts_ptr = 0;
return 0;
}
#endif
char LICENSE[] SEC("license") = "GPL";