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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
// Copyright (c) 2022, Huawei
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#define KWORK_COUNT 100
#define MAX_KWORKNAME 128
/*
* This should be in sync with "util/kwork.h"
*/
enum kwork_class_type {
KWORK_CLASS_IRQ,
KWORK_CLASS_SOFTIRQ,
KWORK_CLASS_WORKQUEUE,
KWORK_CLASS_MAX,
};
struct work_key {
__u32 type;
__u32 cpu;
__u64 id;
};
struct report_data {
__u64 nr;
__u64 total_time;
__u64 max_time;
__u64 max_time_start;
__u64 max_time_end;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(struct work_key));
__uint(value_size, MAX_KWORKNAME);
__uint(max_entries, KWORK_COUNT);
} perf_kwork_names SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(struct work_key));
__uint(value_size, sizeof(__u64));
__uint(max_entries, KWORK_COUNT);
} perf_kwork_time SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(struct work_key));
__uint(value_size, sizeof(struct report_data));
__uint(max_entries, KWORK_COUNT);
} perf_kwork_report SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(key_size, sizeof(__u32));
__uint(value_size, sizeof(__u8));
__uint(max_entries, 1);
} perf_kwork_cpu_filter SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(key_size, sizeof(__u32));
__uint(value_size, MAX_KWORKNAME);
__uint(max_entries, 1);
} perf_kwork_name_filter SEC(".maps");
int enabled = 0;
int has_cpu_filter = 0;
int has_name_filter = 0;
static __always_inline int local_strncmp(const char *s1,
unsigned int sz, const char *s2)
{
int ret = 0;
unsigned int i;
for (i = 0; i < sz; i++) {
ret = (unsigned char)s1[i] - (unsigned char)s2[i];
if (ret || !s1[i] || !s2[i])
break;
}
return ret;
}
static __always_inline int trace_event_match(struct work_key *key, char *name)
{
__u8 *cpu_val;
char *name_val;
__u32 zero = 0;
__u32 cpu = bpf_get_smp_processor_id();
if (!enabled)
return 0;
if (has_cpu_filter) {
cpu_val = bpf_map_lookup_elem(&perf_kwork_cpu_filter, &cpu);
if (!cpu_val)
return 0;
}
if (has_name_filter && (name != NULL)) {
name_val = bpf_map_lookup_elem(&perf_kwork_name_filter, &zero);
if (name_val &&
(local_strncmp(name_val, MAX_KWORKNAME, name) != 0)) {
return 0;
}
}
return 1;
}
static __always_inline void do_update_time(void *map, struct work_key *key,
__u64 time_start, __u64 time_end)
{
struct report_data zero, *data;
__s64 delta = time_end - time_start;
if (delta < 0)
return;
data = bpf_map_lookup_elem(map, key);
if (!data) {
__builtin_memset(&zero, 0, sizeof(zero));
bpf_map_update_elem(map, key, &zero, BPF_NOEXIST);
data = bpf_map_lookup_elem(map, key);
if (!data)
return;
}
if ((delta > data->max_time) ||
(data->max_time == 0)) {
data->max_time = delta;
data->max_time_start = time_start;
data->max_time_end = time_end;
}
data->total_time += delta;
data->nr++;
}
static __always_inline void do_update_timestart(void *map, struct work_key *key)
{
__u64 ts = bpf_ktime_get_ns();
bpf_map_update_elem(map, key, &ts, BPF_ANY);
}
static __always_inline void do_update_timeend(void *report_map, void *time_map,
struct work_key *key)
{
__u64 *time = bpf_map_lookup_elem(time_map, key);
if (time) {
bpf_map_delete_elem(time_map, key);
do_update_time(report_map, key, *time, bpf_ktime_get_ns());
}
}
static __always_inline void do_update_name(void *map,
struct work_key *key, char *name)
{
if (!bpf_map_lookup_elem(map, key))
bpf_map_update_elem(map, key, name, BPF_ANY);
}
static __always_inline int update_timestart_and_name(void *time_map,
void *names_map,
struct work_key *key,
char *name)
{
if (!trace_event_match(key, name))
return 0;
do_update_timestart(time_map, key);
do_update_name(names_map, key, name);
return 0;
}
static __always_inline int update_timeend(void *report_map,
void *time_map, struct work_key *key)
{
if (!trace_event_match(key, NULL))
return 0;
do_update_timeend(report_map, time_map, key);
return 0;
}
SEC("tracepoint/irq/irq_handler_entry")
int report_irq_handler_entry(struct trace_event_raw_irq_handler_entry *ctx)
{
char name[MAX_KWORKNAME];
struct work_key key = {
.type = KWORK_CLASS_IRQ,
.cpu = bpf_get_smp_processor_id(),
.id = (__u64)ctx->irq,
};
void *name_addr = (void *)ctx + (ctx->__data_loc_name & 0xffff);
bpf_probe_read_kernel_str(name, sizeof(name), name_addr);
return update_timestart_and_name(&perf_kwork_time,
&perf_kwork_names, &key, name);
}
SEC("tracepoint/irq/irq_handler_exit")
int report_irq_handler_exit(struct trace_event_raw_irq_handler_exit *ctx)
{
struct work_key key = {
.type = KWORK_CLASS_IRQ,
.cpu = bpf_get_smp_processor_id(),
.id = (__u64)ctx->irq,
};
return update_timeend(&perf_kwork_report, &perf_kwork_time, &key);
}
char LICENSE[] SEC("license") = "Dual BSD/GPL";
|