~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/bpf_lock_contention.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include "util/cgroup.h"
  3 #include "util/debug.h"
  4 #include "util/evlist.h"
  5 #include "util/machine.h"
  6 #include "util/map.h"
  7 #include "util/symbol.h"
  8 #include "util/target.h"
  9 #include "util/thread.h"
 10 #include "util/thread_map.h"
 11 #include "util/lock-contention.h"
 12 #include <linux/zalloc.h>
 13 #include <linux/string.h>
 14 #include <bpf/bpf.h>
 15 #include <inttypes.h>
 16 
 17 #include "bpf_skel/lock_contention.skel.h"
 18 #include "bpf_skel/lock_data.h"
 19 
 20 static struct lock_contention_bpf *skel;
 21 
 22 int lock_contention_prepare(struct lock_contention *con)
 23 {
 24         int i, fd;
 25         int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1, ncgrps = 1;
 26         struct evlist *evlist = con->evlist;
 27         struct target *target = con->target;
 28 
 29         skel = lock_contention_bpf__open();
 30         if (!skel) {
 31                 pr_err("Failed to open lock-contention BPF skeleton\n");
 32                 return -1;
 33         }
 34 
 35         bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
 36         bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
 37         bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
 38 
 39         if (con->aggr_mode == LOCK_AGGR_TASK)
 40                 bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries);
 41         else
 42                 bpf_map__set_max_entries(skel->maps.task_data, 1);
 43 
 44         if (con->save_callstack)
 45                 bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
 46         else
 47                 bpf_map__set_max_entries(skel->maps.stacks, 1);
 48 
 49         if (target__has_cpu(target))
 50                 ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
 51         if (target__has_task(target))
 52                 ntasks = perf_thread_map__nr(evlist->core.threads);
 53         if (con->filters->nr_types)
 54                 ntypes = con->filters->nr_types;
 55         if (con->filters->nr_cgrps)
 56                 ncgrps = con->filters->nr_cgrps;
 57 
 58         /* resolve lock name filters to addr */
 59         if (con->filters->nr_syms) {
 60                 struct symbol *sym;
 61                 struct map *kmap;
 62                 unsigned long *addrs;
 63 
 64                 for (i = 0; i < con->filters->nr_syms; i++) {
 65                         sym = machine__find_kernel_symbol_by_name(con->machine,
 66                                                                   con->filters->syms[i],
 67                                                                   &kmap);
 68                         if (sym == NULL) {
 69                                 pr_warning("ignore unknown symbol: %s\n",
 70                                            con->filters->syms[i]);
 71                                 continue;
 72                         }
 73 
 74                         addrs = realloc(con->filters->addrs,
 75                                         (con->filters->nr_addrs + 1) * sizeof(*addrs));
 76                         if (addrs == NULL) {
 77                                 pr_warning("memory allocation failure\n");
 78                                 continue;
 79                         }
 80 
 81                         addrs[con->filters->nr_addrs++] = map__unmap_ip(kmap, sym->start);
 82                         con->filters->addrs = addrs;
 83                 }
 84                 naddrs = con->filters->nr_addrs;
 85         }
 86 
 87         bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
 88         bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
 89         bpf_map__set_max_entries(skel->maps.type_filter, ntypes);
 90         bpf_map__set_max_entries(skel->maps.addr_filter, naddrs);
 91         bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps);
 92 
 93         if (lock_contention_bpf__load(skel) < 0) {
 94                 pr_err("Failed to load lock-contention BPF skeleton\n");
 95                 return -1;
 96         }
 97 
 98         if (target__has_cpu(target)) {
 99                 u32 cpu;
100                 u8 val = 1;
101 
102                 skel->bss->has_cpu = 1;
103                 fd = bpf_map__fd(skel->maps.cpu_filter);
104 
105                 for (i = 0; i < ncpus; i++) {
106                         cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
107                         bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
108                 }
109         }
110 
111         if (target__has_task(target)) {
112                 u32 pid;
113                 u8 val = 1;
114 
115                 skel->bss->has_task = 1;
116                 fd = bpf_map__fd(skel->maps.task_filter);
117 
118                 for (i = 0; i < ntasks; i++) {
119                         pid = perf_thread_map__pid(evlist->core.threads, i);
120                         bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
121                 }
122         }
123 
124         if (target__none(target) && evlist->workload.pid > 0) {
125                 u32 pid = evlist->workload.pid;
126                 u8 val = 1;
127 
128                 skel->bss->has_task = 1;
129                 fd = bpf_map__fd(skel->maps.task_filter);
130                 bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
131         }
132 
133         if (con->filters->nr_types) {
134                 u8 val = 1;
135 
136                 skel->bss->has_type = 1;
137                 fd = bpf_map__fd(skel->maps.type_filter);
138 
139                 for (i = 0; i < con->filters->nr_types; i++)
140                         bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY);
141         }
142 
143         if (con->filters->nr_addrs) {
144                 u8 val = 1;
145 
146                 skel->bss->has_addr = 1;
147                 fd = bpf_map__fd(skel->maps.addr_filter);
148 
149                 for (i = 0; i < con->filters->nr_addrs; i++)
150                         bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY);
151         }
152 
153         if (con->filters->nr_cgrps) {
154                 u8 val = 1;
155 
156                 skel->bss->has_cgroup = 1;
157                 fd = bpf_map__fd(skel->maps.cgroup_filter);
158 
159                 for (i = 0; i < con->filters->nr_cgrps; i++)
160                         bpf_map_update_elem(fd, &con->filters->cgrps[i], &val, BPF_ANY);
161         }
162 
163         /* these don't work well if in the rodata section */
164         skel->bss->stack_skip = con->stack_skip;
165         skel->bss->aggr_mode = con->aggr_mode;
166         skel->bss->needs_callstack = con->save_callstack;
167         skel->bss->lock_owner = con->owner;
168 
169         if (con->aggr_mode == LOCK_AGGR_CGROUP) {
170                 if (cgroup_is_v2("perf_event"))
171                         skel->bss->use_cgroup_v2 = 1;
172 
173                 read_all_cgroups(&con->cgroups);
174         }
175 
176         bpf_program__set_autoload(skel->progs.collect_lock_syms, false);
177 
178         lock_contention_bpf__attach(skel);
179         return 0;
180 }
181 
182 /*
183  * Run the BPF program directly using BPF_PROG_TEST_RUN to update the end
184  * timestamp in ktime so that it can calculate delta easily.
185  */
186 static void mark_end_timestamp(void)
187 {
188         DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
189                 .flags = BPF_F_TEST_RUN_ON_CPU,
190         );
191         int prog_fd = bpf_program__fd(skel->progs.end_timestamp);
192 
193         bpf_prog_test_run_opts(prog_fd, &opts);
194 }
195 
196 static void update_lock_stat(int map_fd, int pid, u64 end_ts,
197                              enum lock_aggr_mode aggr_mode,
198                              struct tstamp_data *ts_data)
199 {
200         u64 delta;
201         struct contention_key stat_key = {};
202         struct contention_data stat_data;
203 
204         if (ts_data->timestamp >= end_ts)
205                 return;
206 
207         delta = end_ts - ts_data->timestamp;
208 
209         switch (aggr_mode) {
210         case LOCK_AGGR_CALLER:
211                 stat_key.stack_id = ts_data->stack_id;
212                 break;
213         case LOCK_AGGR_TASK:
214                 stat_key.pid = pid;
215                 break;
216         case LOCK_AGGR_ADDR:
217                 stat_key.lock_addr_or_cgroup = ts_data->lock;
218                 break;
219         case LOCK_AGGR_CGROUP:
220                 /* TODO */
221                 return;
222         default:
223                 return;
224         }
225 
226         if (bpf_map_lookup_elem(map_fd, &stat_key, &stat_data) < 0)
227                 return;
228 
229         stat_data.total_time += delta;
230         stat_data.count++;
231 
232         if (delta > stat_data.max_time)
233                 stat_data.max_time = delta;
234         if (delta < stat_data.min_time)
235                 stat_data.min_time = delta;
236 
237         bpf_map_update_elem(map_fd, &stat_key, &stat_data, BPF_EXIST);
238 }
239 
240 /*
241  * Account entries in the tstamp map (which didn't see the corresponding
242  * lock:contention_end tracepoint) using end_ts.
243  */
244 static void account_end_timestamp(struct lock_contention *con)
245 {
246         int ts_fd, stat_fd;
247         int *prev_key, key;
248         u64 end_ts = skel->bss->end_ts;
249         int total_cpus;
250         enum lock_aggr_mode aggr_mode = con->aggr_mode;
251         struct tstamp_data ts_data, *cpu_data;
252 
253         /* Iterate per-task tstamp map (key = TID) */
254         ts_fd = bpf_map__fd(skel->maps.tstamp);
255         stat_fd = bpf_map__fd(skel->maps.lock_stat);
256 
257         prev_key = NULL;
258         while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) {
259                 if (bpf_map_lookup_elem(ts_fd, &key, &ts_data) == 0) {
260                         int pid = key;
261 
262                         if (aggr_mode == LOCK_AGGR_TASK && con->owner)
263                                 pid = ts_data.flags;
264 
265                         update_lock_stat(stat_fd, pid, end_ts, aggr_mode,
266                                          &ts_data);
267                 }
268 
269                 prev_key = &key;
270         }
271 
272         /* Now it'll check per-cpu tstamp map which doesn't have TID. */
273         if (aggr_mode == LOCK_AGGR_TASK || aggr_mode == LOCK_AGGR_CGROUP)
274                 return;
275 
276         total_cpus = cpu__max_cpu().cpu;
277         ts_fd = bpf_map__fd(skel->maps.tstamp_cpu);
278 
279         cpu_data = calloc(total_cpus, sizeof(*cpu_data));
280         if (cpu_data == NULL)
281                 return;
282 
283         prev_key = NULL;
284         while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) {
285                 if (bpf_map_lookup_elem(ts_fd, &key, cpu_data) < 0)
286                         goto next;
287 
288                 for (int i = 0; i < total_cpus; i++) {
289                         if (cpu_data[i].lock == 0)
290                                 continue;
291 
292                         update_lock_stat(stat_fd, -1, end_ts, aggr_mode,
293                                          &cpu_data[i]);
294                 }
295 
296 next:
297                 prev_key = &key;
298         }
299         free(cpu_data);
300 }
301 
302 int lock_contention_start(void)
303 {
304         skel->bss->enabled = 1;
305         return 0;
306 }
307 
308 int lock_contention_stop(void)
309 {
310         skel->bss->enabled = 0;
311         mark_end_timestamp();
312         return 0;
313 }
314 
315 static const char *lock_contention_get_name(struct lock_contention *con,
316                                             struct contention_key *key,
317                                             u64 *stack_trace, u32 flags)
318 {
319         int idx = 0;
320         u64 addr;
321         const char *name = "";
322         static char name_buf[KSYM_NAME_LEN];
323         struct symbol *sym;
324         struct map *kmap;
325         struct machine *machine = con->machine;
326 
327         if (con->aggr_mode == LOCK_AGGR_TASK) {
328                 struct contention_task_data task;
329                 int pid = key->pid;
330                 int task_fd = bpf_map__fd(skel->maps.task_data);
331 
332                 /* do not update idle comm which contains CPU number */
333                 if (pid) {
334                         struct thread *t = machine__findnew_thread(machine, /*pid=*/-1, pid);
335 
336                         if (t == NULL)
337                                 return name;
338                         if (!bpf_map_lookup_elem(task_fd, &pid, &task) &&
339                             thread__set_comm(t, task.comm, /*timestamp=*/0))
340                                 name = task.comm;
341                 }
342                 return name;
343         }
344 
345         if (con->aggr_mode == LOCK_AGGR_ADDR) {
346                 int lock_fd = bpf_map__fd(skel->maps.lock_syms);
347 
348                 /* per-process locks set upper bits of the flags */
349                 if (flags & LCD_F_MMAP_LOCK)
350                         return "mmap_lock";
351                 if (flags & LCD_F_SIGHAND_LOCK)
352                         return "siglock";
353 
354                 /* global locks with symbols */
355                 sym = machine__find_kernel_symbol(machine, key->lock_addr_or_cgroup, &kmap);
356                 if (sym)
357                         return sym->name;
358 
359                 /* try semi-global locks collected separately */
360                 if (!bpf_map_lookup_elem(lock_fd, &key->lock_addr_or_cgroup, &flags)) {
361                         if (flags == LOCK_CLASS_RQLOCK)
362                                 return "rq_lock";
363                 }
364 
365                 return "";
366         }
367 
368         if (con->aggr_mode == LOCK_AGGR_CGROUP) {
369                 u64 cgrp_id = key->lock_addr_or_cgroup;
370                 struct cgroup *cgrp = __cgroup__find(&con->cgroups, cgrp_id);
371 
372                 if (cgrp)
373                         return cgrp->name;
374 
375                 snprintf(name_buf, sizeof(name_buf), "cgroup:%" PRIu64 "", cgrp_id);
376                 return name_buf;
377         }
378 
379         /* LOCK_AGGR_CALLER: skip lock internal functions */
380         while (machine__is_lock_function(machine, stack_trace[idx]) &&
381                idx < con->max_stack - 1)
382                 idx++;
383 
384         addr = stack_trace[idx];
385         sym = machine__find_kernel_symbol(machine, addr, &kmap);
386 
387         if (sym) {
388                 unsigned long offset;
389 
390                 offset = map__map_ip(kmap, addr) - sym->start;
391 
392                 if (offset == 0)
393                         return sym->name;
394 
395                 snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset);
396         } else {
397                 snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr);
398         }
399 
400         return name_buf;
401 }
402 
403 int lock_contention_read(struct lock_contention *con)
404 {
405         int fd, stack, err = 0;
406         struct contention_key *prev_key, key = {};
407         struct contention_data data = {};
408         struct lock_stat *st = NULL;
409         struct machine *machine = con->machine;
410         u64 *stack_trace;
411         size_t stack_size = con->max_stack * sizeof(*stack_trace);
412 
413         fd = bpf_map__fd(skel->maps.lock_stat);
414         stack = bpf_map__fd(skel->maps.stacks);
415 
416         con->fails.task = skel->bss->task_fail;
417         con->fails.stack = skel->bss->stack_fail;
418         con->fails.time = skel->bss->time_fail;
419         con->fails.data = skel->bss->data_fail;
420 
421         stack_trace = zalloc(stack_size);
422         if (stack_trace == NULL)
423                 return -1;
424 
425         account_end_timestamp(con);
426 
427         if (con->aggr_mode == LOCK_AGGR_TASK) {
428                 struct thread *idle = machine__findnew_thread(machine,
429                                                                 /*pid=*/0,
430                                                                 /*tid=*/0);
431                 thread__set_comm(idle, "swapper", /*timestamp=*/0);
432         }
433 
434         if (con->aggr_mode == LOCK_AGGR_ADDR) {
435                 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
436                         .flags = BPF_F_TEST_RUN_ON_CPU,
437                 );
438                 int prog_fd = bpf_program__fd(skel->progs.collect_lock_syms);
439 
440                 bpf_prog_test_run_opts(prog_fd, &opts);
441         }
442 
443         /* make sure it loads the kernel map */
444         maps__load_first(machine->kmaps);
445 
446         prev_key = NULL;
447         while (!bpf_map_get_next_key(fd, prev_key, &key)) {
448                 s64 ls_key;
449                 const char *name;
450 
451                 /* to handle errors in the loop body */
452                 err = -1;
453 
454                 bpf_map_lookup_elem(fd, &key, &data);
455                 if (con->save_callstack) {
456                         bpf_map_lookup_elem(stack, &key.stack_id, stack_trace);
457 
458                         if (!match_callstack_filter(machine, stack_trace)) {
459                                 con->nr_filtered += data.count;
460                                 goto next;
461                         }
462                 }
463 
464                 switch (con->aggr_mode) {
465                 case LOCK_AGGR_CALLER:
466                         ls_key = key.stack_id;
467                         break;
468                 case LOCK_AGGR_TASK:
469                         ls_key = key.pid;
470                         break;
471                 case LOCK_AGGR_ADDR:
472                 case LOCK_AGGR_CGROUP:
473                         ls_key = key.lock_addr_or_cgroup;
474                         break;
475                 default:
476                         goto next;
477                 }
478 
479                 st = lock_stat_find(ls_key);
480                 if (st != NULL) {
481                         st->wait_time_total += data.total_time;
482                         if (st->wait_time_max < data.max_time)
483                                 st->wait_time_max = data.max_time;
484                         if (st->wait_time_min > data.min_time)
485                                 st->wait_time_min = data.min_time;
486 
487                         st->nr_contended += data.count;
488                         if (st->nr_contended)
489                                 st->avg_wait_time = st->wait_time_total / st->nr_contended;
490                         goto next;
491                 }
492 
493                 name = lock_contention_get_name(con, &key, stack_trace, data.flags);
494                 st = lock_stat_findnew(ls_key, name, data.flags);
495                 if (st == NULL)
496                         break;
497 
498                 st->nr_contended = data.count;
499                 st->wait_time_total = data.total_time;
500                 st->wait_time_max = data.max_time;
501                 st->wait_time_min = data.min_time;
502 
503                 if (data.count)
504                         st->avg_wait_time = data.total_time / data.count;
505 
506                 if (con->aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
507                         st->callstack = memdup(stack_trace, stack_size);
508                         if (st->callstack == NULL)
509                                 break;
510                 }
511 
512 next:
513                 prev_key = &key;
514 
515                 /* we're fine now, reset the error */
516                 err = 0;
517         }
518 
519         free(stack_trace);
520 
521         return err;
522 }
523 
524 int lock_contention_finish(struct lock_contention *con)
525 {
526         if (skel) {
527                 skel->bss->enabled = 0;
528                 lock_contention_bpf__destroy(skel);
529         }
530 
531         while (!RB_EMPTY_ROOT(&con->cgroups)) {
532                 struct rb_node *node = rb_first(&con->cgroups);
533                 struct cgroup *cgrp = rb_entry(node, struct cgroup, node);
534 
535                 rb_erase(node, &con->cgroups);
536                 cgroup__put(cgrp);
537         }
538 
539         return 0;
540 }
541 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php