72cf811149829ed368256c2b6eb61efb82fadaed
[platform/kernel/linux-starfive.git] / tools / perf / util / bpf_lock_contention.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/debug.h"
3 #include "util/evlist.h"
4 #include "util/machine.h"
5 #include "util/map.h"
6 #include "util/symbol.h"
7 #include "util/target.h"
8 #include "util/thread.h"
9 #include "util/thread_map.h"
10 #include "util/lock-contention.h"
11 #include <linux/zalloc.h>
12 #include <linux/string.h>
13 #include <bpf/bpf.h>
14
15 #include "bpf_skel/lock_contention.skel.h"
16 #include "bpf_skel/lock_data.h"
17
18 static struct lock_contention_bpf *skel;
19
20 int lock_contention_prepare(struct lock_contention *con)
21 {
22         int i, fd;
23         int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1;
24         struct evlist *evlist = con->evlist;
25         struct target *target = con->target;
26
27         skel = lock_contention_bpf__open();
28         if (!skel) {
29                 pr_err("Failed to open lock-contention BPF skeleton\n");
30                 return -1;
31         }
32
33         bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
34         bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
35         bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
36
37         if (con->aggr_mode == LOCK_AGGR_TASK)
38                 bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries);
39         else
40                 bpf_map__set_max_entries(skel->maps.task_data, 1);
41
42         if (con->save_callstack)
43                 bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
44         else
45                 bpf_map__set_max_entries(skel->maps.stacks, 1);
46
47         if (target__has_cpu(target))
48                 ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
49         if (target__has_task(target))
50                 ntasks = perf_thread_map__nr(evlist->core.threads);
51         if (con->filters->nr_types)
52                 ntypes = con->filters->nr_types;
53
54         /* resolve lock name filters to addr */
55         if (con->filters->nr_syms) {
56                 struct symbol *sym;
57                 struct map *kmap;
58                 unsigned long *addrs;
59
60                 for (i = 0; i < con->filters->nr_syms; i++) {
61                         sym = machine__find_kernel_symbol_by_name(con->machine,
62                                                                   con->filters->syms[i],
63                                                                   &kmap);
64                         if (sym == NULL) {
65                                 pr_warning("ignore unknown symbol: %s\n",
66                                            con->filters->syms[i]);
67                                 continue;
68                         }
69
70                         addrs = realloc(con->filters->addrs,
71                                         (con->filters->nr_addrs + 1) * sizeof(*addrs));
72                         if (addrs == NULL) {
73                                 pr_warning("memory allocation failure\n");
74                                 continue;
75                         }
76
77                         addrs[con->filters->nr_addrs++] = kmap->unmap_ip(kmap, sym->start);
78                         con->filters->addrs = addrs;
79                 }
80                 naddrs = con->filters->nr_addrs;
81         }
82
83         bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
84         bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
85         bpf_map__set_max_entries(skel->maps.type_filter, ntypes);
86         bpf_map__set_max_entries(skel->maps.addr_filter, naddrs);
87
88         if (lock_contention_bpf__load(skel) < 0) {
89                 pr_err("Failed to load lock-contention BPF skeleton\n");
90                 return -1;
91         }
92
93         if (target__has_cpu(target)) {
94                 u32 cpu;
95                 u8 val = 1;
96
97                 skel->bss->has_cpu = 1;
98                 fd = bpf_map__fd(skel->maps.cpu_filter);
99
100                 for (i = 0; i < ncpus; i++) {
101                         cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
102                         bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
103                 }
104         }
105
106         if (target__has_task(target)) {
107                 u32 pid;
108                 u8 val = 1;
109
110                 skel->bss->has_task = 1;
111                 fd = bpf_map__fd(skel->maps.task_filter);
112
113                 for (i = 0; i < ntasks; i++) {
114                         pid = perf_thread_map__pid(evlist->core.threads, i);
115                         bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
116                 }
117         }
118
119         if (target__none(target) && evlist->workload.pid > 0) {
120                 u32 pid = evlist->workload.pid;
121                 u8 val = 1;
122
123                 skel->bss->has_task = 1;
124                 fd = bpf_map__fd(skel->maps.task_filter);
125                 bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
126         }
127
128         if (con->filters->nr_types) {
129                 u8 val = 1;
130
131                 skel->bss->has_type = 1;
132                 fd = bpf_map__fd(skel->maps.type_filter);
133
134                 for (i = 0; i < con->filters->nr_types; i++)
135                         bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY);
136         }
137
138         if (con->filters->nr_addrs) {
139                 u8 val = 1;
140
141                 skel->bss->has_addr = 1;
142                 fd = bpf_map__fd(skel->maps.addr_filter);
143
144                 for (i = 0; i < con->filters->nr_addrs; i++)
145                         bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY);
146         }
147
148         /* these don't work well if in the rodata section */
149         skel->bss->stack_skip = con->stack_skip;
150         skel->bss->aggr_mode = con->aggr_mode;
151         skel->bss->needs_callstack = con->save_callstack;
152
153         lock_contention_bpf__attach(skel);
154         return 0;
155 }
156
157 int lock_contention_start(void)
158 {
159         skel->bss->enabled = 1;
160         return 0;
161 }
162
163 int lock_contention_stop(void)
164 {
165         skel->bss->enabled = 0;
166         return 0;
167 }
168
169 static const char *lock_contention_get_name(struct lock_contention *con,
170                                             struct contention_key *key,
171                                             u64 *stack_trace)
172 {
173         int idx = 0;
174         u64 addr;
175         const char *name = "";
176         static char name_buf[KSYM_NAME_LEN];
177         struct symbol *sym;
178         struct map *kmap;
179         struct machine *machine = con->machine;
180
181         if (con->aggr_mode == LOCK_AGGR_TASK) {
182                 struct contention_task_data task;
183                 int pid = key->pid;
184                 int task_fd = bpf_map__fd(skel->maps.task_data);
185
186                 /* do not update idle comm which contains CPU number */
187                 if (pid) {
188                         struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid);
189
190                         if (t == NULL)
191                                 return name;
192                         if (!bpf_map_lookup_elem(task_fd, &pid, &task) &&
193                             thread__set_comm(t, task.comm, /*timestamp=*/0))
194                                 name = task.comm;
195                 }
196                 return name;
197         }
198
199         if (con->aggr_mode == LOCK_AGGR_ADDR) {
200                 sym = machine__find_kernel_symbol(machine, key->lock_addr, &kmap);
201                 if (sym)
202                         name = sym->name;
203                 return name;
204         }
205
206         /* LOCK_AGGR_CALLER: skip lock internal functions */
207         while (machine__is_lock_function(machine, stack_trace[idx]) &&
208                idx < con->max_stack - 1)
209                 idx++;
210
211         addr = stack_trace[idx];
212         sym = machine__find_kernel_symbol(machine, addr, &kmap);
213
214         if (sym) {
215                 unsigned long offset;
216
217                 offset = kmap->map_ip(kmap, addr) - sym->start;
218
219                 if (offset == 0)
220                         return sym->name;
221
222                 snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset);
223         } else {
224                 snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr);
225         }
226
227         return name_buf;
228 }
229
230 int lock_contention_read(struct lock_contention *con)
231 {
232         int fd, stack, err = 0;
233         struct contention_key *prev_key, key;
234         struct contention_data data = {};
235         struct lock_stat *st = NULL;
236         struct machine *machine = con->machine;
237         u64 *stack_trace;
238         size_t stack_size = con->max_stack * sizeof(*stack_trace);
239
240         fd = bpf_map__fd(skel->maps.lock_stat);
241         stack = bpf_map__fd(skel->maps.stacks);
242
243         con->lost = skel->bss->lost;
244
245         stack_trace = zalloc(stack_size);
246         if (stack_trace == NULL)
247                 return -1;
248
249         if (con->aggr_mode == LOCK_AGGR_TASK) {
250                 struct thread *idle = __machine__findnew_thread(machine,
251                                                                 /*pid=*/0,
252                                                                 /*tid=*/0);
253                 thread__set_comm(idle, "swapper", /*timestamp=*/0);
254         }
255
256         /* make sure it loads the kernel map */
257         map__load(maps__first(machine->kmaps));
258
259         prev_key = NULL;
260         while (!bpf_map_get_next_key(fd, prev_key, &key)) {
261                 s64 ls_key;
262                 const char *name;
263
264                 /* to handle errors in the loop body */
265                 err = -1;
266
267                 bpf_map_lookup_elem(fd, &key, &data);
268                 if (con->save_callstack) {
269                         bpf_map_lookup_elem(stack, &key.stack_id, stack_trace);
270
271                         if (!match_callstack_filter(machine, stack_trace))
272                                 goto next;
273                 }
274
275                 switch (con->aggr_mode) {
276                 case LOCK_AGGR_CALLER:
277                         ls_key = key.stack_id;
278                         break;
279                 case LOCK_AGGR_TASK:
280                         ls_key = key.pid;
281                         break;
282                 case LOCK_AGGR_ADDR:
283                         ls_key = key.lock_addr;
284                         break;
285                 default:
286                         goto next;
287                 }
288
289                 st = lock_stat_find(ls_key);
290                 if (st != NULL) {
291                         st->wait_time_total += data.total_time;
292                         if (st->wait_time_max < data.max_time)
293                                 st->wait_time_max = data.max_time;
294                         if (st->wait_time_min > data.min_time)
295                                 st->wait_time_min = data.min_time;
296
297                         st->nr_contended += data.count;
298                         if (st->nr_contended)
299                                 st->avg_wait_time = st->wait_time_total / st->nr_contended;
300                         goto next;
301                 }
302
303                 name = lock_contention_get_name(con, &key, stack_trace);
304                 st = lock_stat_findnew(ls_key, name, data.flags);
305                 if (st == NULL)
306                         break;
307
308                 st->nr_contended = data.count;
309                 st->wait_time_total = data.total_time;
310                 st->wait_time_max = data.max_time;
311                 st->wait_time_min = data.min_time;
312
313                 if (data.count)
314                         st->avg_wait_time = data.total_time / data.count;
315
316                 if (con->save_callstack) {
317                         st->callstack = memdup(stack_trace, stack_size);
318                         if (st->callstack == NULL)
319                                 break;
320                 }
321
322 next:
323                 prev_key = &key;
324
325                 /* we're fine now, reset the error */
326                 err = 0;
327         }
328
329         free(stack_trace);
330
331         return err;
332 }
333
334 int lock_contention_finish(void)
335 {
336         if (skel) {
337                 skel->bss->enabled = 0;
338                 lock_contention_bpf__destroy(skel);
339         }
340
341         return 0;
342 }