1407b36f6SNamhyung Kim // SPDX-License-Identifier: GPL-2.0 2d0c502e4SNamhyung Kim #include "util/cgroup.h" 3407b36f6SNamhyung Kim #include "util/debug.h" 46fda2405SNamhyung Kim #include "util/evlist.h" 5407b36f6SNamhyung Kim #include "util/machine.h" 6407b36f6SNamhyung Kim #include "util/map.h" 7407b36f6SNamhyung Kim #include "util/symbol.h" 86fda2405SNamhyung Kim #include "util/target.h" 9eca949b2SNamhyung Kim #include "util/thread.h" 106fda2405SNamhyung Kim #include "util/thread_map.h" 11407b36f6SNamhyung Kim #include "util/lock-contention.h" 12407b36f6SNamhyung Kim #include <linux/zalloc.h> 13a6eaf966SNamhyung Kim #include <linux/string.h> 14407b36f6SNamhyung Kim #include <bpf/bpf.h> 15*29b8e94dSYang Jihong #include <inttypes.h> 16407b36f6SNamhyung Kim 17407b36f6SNamhyung Kim #include "bpf_skel/lock_contention.skel.h" 18fd507d3eSNamhyung Kim #include "bpf_skel/lock_data.h" 19407b36f6SNamhyung Kim 20407b36f6SNamhyung Kim static struct lock_contention_bpf *skel; 21407b36f6SNamhyung Kim 22447ec4e5SNamhyung Kim int lock_contention_prepare(struct lock_contention *con) 23407b36f6SNamhyung Kim { 246fda2405SNamhyung Kim int i, fd; 254fd06bd2SNamhyung Kim int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1, ncgrps = 1; 26447ec4e5SNamhyung Kim struct evlist *evlist = con->evlist; 27447ec4e5SNamhyung Kim struct target *target = con->target; 286fda2405SNamhyung Kim 29407b36f6SNamhyung Kim skel = lock_contention_bpf__open(); 30407b36f6SNamhyung Kim if (!skel) { 31407b36f6SNamhyung Kim pr_err("Failed to open lock-contention BPF skeleton\n"); 32407b36f6SNamhyung Kim return -1; 33407b36f6SNamhyung Kim } 34407b36f6SNamhyung Kim 3596532a83SNamhyung Kim bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64)); 36ceb13bfcSNamhyung Kim bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries); 37c66a36afSNamhyung Kim bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries); 38ceb13bfcSNamhyung Kim 39ebab2916SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_TASK) 40eca949b2SNamhyung Kim bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries); 41ebab2916SNamhyung Kim else 42eca949b2SNamhyung Kim bpf_map__set_max_entries(skel->maps.task_data, 1); 43ebab2916SNamhyung Kim 44ebab2916SNamhyung Kim if (con->save_callstack) 45eca949b2SNamhyung Kim bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries); 46ebab2916SNamhyung Kim else 47ebab2916SNamhyung Kim bpf_map__set_max_entries(skel->maps.stacks, 1); 48eca949b2SNamhyung Kim 496fda2405SNamhyung Kim if (target__has_cpu(target)) 506fda2405SNamhyung Kim ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus); 516fda2405SNamhyung Kim if (target__has_task(target)) 526fda2405SNamhyung Kim ntasks = perf_thread_map__nr(evlist->core.threads); 53529772c4SNamhyung Kim if (con->filters->nr_types) 54529772c4SNamhyung Kim ntypes = con->filters->nr_types; 554fd06bd2SNamhyung Kim if (con->filters->nr_cgrps) 564fd06bd2SNamhyung Kim ncgrps = con->filters->nr_cgrps; 576fda2405SNamhyung Kim 585e3febe7SNamhyung Kim /* resolve lock name filters to addr */ 595e3febe7SNamhyung Kim if (con->filters->nr_syms) { 605e3febe7SNamhyung Kim struct symbol *sym; 615e3febe7SNamhyung Kim struct map *kmap; 625e3febe7SNamhyung Kim unsigned long *addrs; 635e3febe7SNamhyung Kim 645e3febe7SNamhyung Kim for (i = 0; i < con->filters->nr_syms; i++) { 655e3febe7SNamhyung Kim sym = machine__find_kernel_symbol_by_name(con->machine, 665e3febe7SNamhyung Kim con->filters->syms[i], 675e3febe7SNamhyung Kim &kmap); 685e3febe7SNamhyung Kim if (sym == NULL) { 695e3febe7SNamhyung Kim pr_warning("ignore unknown symbol: %s\n", 705e3febe7SNamhyung Kim con->filters->syms[i]); 715e3febe7SNamhyung Kim continue; 725e3febe7SNamhyung Kim } 735e3febe7SNamhyung Kim 745e3febe7SNamhyung Kim addrs = realloc(con->filters->addrs, 755e3febe7SNamhyung Kim (con->filters->nr_addrs + 1) * sizeof(*addrs)); 765e3febe7SNamhyung Kim if (addrs == NULL) { 775e3febe7SNamhyung Kim pr_warning("memory allocation failure\n"); 785e3febe7SNamhyung Kim continue; 795e3febe7SNamhyung Kim } 805e3febe7SNamhyung Kim 8178a1f7cdSIan Rogers addrs[con->filters->nr_addrs++] = map__unmap_ip(kmap, sym->start); 825e3febe7SNamhyung Kim con->filters->addrs = addrs; 835e3febe7SNamhyung Kim } 845e3febe7SNamhyung Kim naddrs = con->filters->nr_addrs; 855e3febe7SNamhyung Kim } 865e3febe7SNamhyung Kim 876fda2405SNamhyung Kim bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus); 886fda2405SNamhyung Kim bpf_map__set_max_entries(skel->maps.task_filter, ntasks); 89529772c4SNamhyung Kim bpf_map__set_max_entries(skel->maps.type_filter, ntypes); 905e3febe7SNamhyung Kim bpf_map__set_max_entries(skel->maps.addr_filter, naddrs); 914fd06bd2SNamhyung Kim bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps); 926fda2405SNamhyung Kim 93407b36f6SNamhyung Kim if (lock_contention_bpf__load(skel) < 0) { 94407b36f6SNamhyung Kim pr_err("Failed to load lock-contention BPF skeleton\n"); 95407b36f6SNamhyung Kim return -1; 96407b36f6SNamhyung Kim } 97407b36f6SNamhyung Kim 986fda2405SNamhyung Kim if (target__has_cpu(target)) { 996fda2405SNamhyung Kim u32 cpu; 1006fda2405SNamhyung Kim u8 val = 1; 1016fda2405SNamhyung Kim 1026fda2405SNamhyung Kim skel->bss->has_cpu = 1; 1036fda2405SNamhyung Kim fd = bpf_map__fd(skel->maps.cpu_filter); 1046fda2405SNamhyung Kim 1056fda2405SNamhyung Kim for (i = 0; i < ncpus; i++) { 1066fda2405SNamhyung Kim cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu; 1076fda2405SNamhyung Kim bpf_map_update_elem(fd, &cpu, &val, BPF_ANY); 1086fda2405SNamhyung Kim } 1096fda2405SNamhyung Kim } 1106fda2405SNamhyung Kim 1116fda2405SNamhyung Kim if (target__has_task(target)) { 1126fda2405SNamhyung Kim u32 pid; 1136fda2405SNamhyung Kim u8 val = 1; 1146fda2405SNamhyung Kim 1156fda2405SNamhyung Kim skel->bss->has_task = 1; 1166fda2405SNamhyung Kim fd = bpf_map__fd(skel->maps.task_filter); 1176fda2405SNamhyung Kim 1186fda2405SNamhyung Kim for (i = 0; i < ntasks; i++) { 1196fda2405SNamhyung Kim pid = perf_thread_map__pid(evlist->core.threads, i); 1206fda2405SNamhyung Kim bpf_map_update_elem(fd, &pid, &val, BPF_ANY); 1216fda2405SNamhyung Kim } 1226fda2405SNamhyung Kim } 1236fda2405SNamhyung Kim 1246fda2405SNamhyung Kim if (target__none(target) && evlist->workload.pid > 0) { 1256fda2405SNamhyung Kim u32 pid = evlist->workload.pid; 1266fda2405SNamhyung Kim u8 val = 1; 1276fda2405SNamhyung Kim 1286fda2405SNamhyung Kim skel->bss->has_task = 1; 1296fda2405SNamhyung Kim fd = bpf_map__fd(skel->maps.task_filter); 1306fda2405SNamhyung Kim bpf_map_update_elem(fd, &pid, &val, BPF_ANY); 1316fda2405SNamhyung Kim } 1326fda2405SNamhyung Kim 133529772c4SNamhyung Kim if (con->filters->nr_types) { 134529772c4SNamhyung Kim u8 val = 1; 135529772c4SNamhyung Kim 136529772c4SNamhyung Kim skel->bss->has_type = 1; 137529772c4SNamhyung Kim fd = bpf_map__fd(skel->maps.type_filter); 138529772c4SNamhyung Kim 139529772c4SNamhyung Kim for (i = 0; i < con->filters->nr_types; i++) 140529772c4SNamhyung Kim bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY); 141529772c4SNamhyung Kim } 142529772c4SNamhyung Kim 1435e3febe7SNamhyung Kim if (con->filters->nr_addrs) { 1445e3febe7SNamhyung Kim u8 val = 1; 1455e3febe7SNamhyung Kim 1465e3febe7SNamhyung Kim skel->bss->has_addr = 1; 1475e3febe7SNamhyung Kim fd = bpf_map__fd(skel->maps.addr_filter); 1485e3febe7SNamhyung Kim 1495e3febe7SNamhyung Kim for (i = 0; i < con->filters->nr_addrs; i++) 1505e3febe7SNamhyung Kim bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY); 1515e3febe7SNamhyung Kim } 1525e3febe7SNamhyung Kim 1534fd06bd2SNamhyung Kim if (con->filters->nr_cgrps) { 1544fd06bd2SNamhyung Kim u8 val = 1; 1554fd06bd2SNamhyung Kim 1564fd06bd2SNamhyung Kim skel->bss->has_cgroup = 1; 1574fd06bd2SNamhyung Kim fd = bpf_map__fd(skel->maps.cgroup_filter); 1584fd06bd2SNamhyung Kim 1594fd06bd2SNamhyung Kim for (i = 0; i < con->filters->nr_cgrps; i++) 1604fd06bd2SNamhyung Kim bpf_map_update_elem(fd, &con->filters->cgrps[i], &val, BPF_ANY); 1614fd06bd2SNamhyung Kim } 1624fd06bd2SNamhyung Kim 163eca949b2SNamhyung Kim /* these don't work well if in the rodata section */ 164c1da8dd5SNamhyung Kim skel->bss->stack_skip = con->stack_skip; 165eca949b2SNamhyung Kim skel->bss->aggr_mode = con->aggr_mode; 166ebab2916SNamhyung Kim skel->bss->needs_callstack = con->save_callstack; 1673477f079SNamhyung Kim skel->bss->lock_owner = con->owner; 168c1da8dd5SNamhyung Kim 1694d1792d0SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_CGROUP) { 1704d1792d0SNamhyung Kim if (cgroup_is_v2("perf_event")) 1714d1792d0SNamhyung Kim skel->bss->use_cgroup_v2 = 1; 1724d1792d0SNamhyung Kim 173d0c502e4SNamhyung Kim read_all_cgroups(&con->cgroups); 174d0c502e4SNamhyung Kim } 175d0c502e4SNamhyung Kim 176d24c0144SNamhyung Kim bpf_program__set_autoload(skel->progs.collect_lock_syms, false); 177d24c0144SNamhyung Kim 178407b36f6SNamhyung Kim lock_contention_bpf__attach(skel); 179407b36f6SNamhyung Kim return 0; 180407b36f6SNamhyung Kim } 181407b36f6SNamhyung Kim 182407b36f6SNamhyung Kim int lock_contention_start(void) 183407b36f6SNamhyung Kim { 184407b36f6SNamhyung Kim skel->bss->enabled = 1; 185407b36f6SNamhyung Kim return 0; 186407b36f6SNamhyung Kim } 187407b36f6SNamhyung Kim 188407b36f6SNamhyung Kim int lock_contention_stop(void) 189407b36f6SNamhyung Kim { 190407b36f6SNamhyung Kim skel->bss->enabled = 0; 191407b36f6SNamhyung Kim return 0; 192407b36f6SNamhyung Kim } 193407b36f6SNamhyung Kim 194492fef21SNamhyung Kim static const char *lock_contention_get_name(struct lock_contention *con, 195492fef21SNamhyung Kim struct contention_key *key, 1961811e827SNamhyung Kim u64 *stack_trace, u32 flags) 197492fef21SNamhyung Kim { 198492fef21SNamhyung Kim int idx = 0; 199492fef21SNamhyung Kim u64 addr; 200492fef21SNamhyung Kim const char *name = ""; 201492fef21SNamhyung Kim static char name_buf[KSYM_NAME_LEN]; 202492fef21SNamhyung Kim struct symbol *sym; 203492fef21SNamhyung Kim struct map *kmap; 204492fef21SNamhyung Kim struct machine *machine = con->machine; 205492fef21SNamhyung Kim 206492fef21SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_TASK) { 207492fef21SNamhyung Kim struct contention_task_data task; 208ebab2916SNamhyung Kim int pid = key->pid; 209492fef21SNamhyung Kim int task_fd = bpf_map__fd(skel->maps.task_data); 210492fef21SNamhyung Kim 211492fef21SNamhyung Kim /* do not update idle comm which contains CPU number */ 212492fef21SNamhyung Kim if (pid) { 213492fef21SNamhyung Kim struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid); 214492fef21SNamhyung Kim 215492fef21SNamhyung Kim if (t == NULL) 216492fef21SNamhyung Kim return name; 217492fef21SNamhyung Kim if (!bpf_map_lookup_elem(task_fd, &pid, &task) && 218492fef21SNamhyung Kim thread__set_comm(t, task.comm, /*timestamp=*/0)) 219492fef21SNamhyung Kim name = task.comm; 220492fef21SNamhyung Kim } 221492fef21SNamhyung Kim return name; 222492fef21SNamhyung Kim } 223492fef21SNamhyung Kim 224492fef21SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_ADDR) { 225d24c0144SNamhyung Kim int lock_fd = bpf_map__fd(skel->maps.lock_syms); 226d24c0144SNamhyung Kim 227d24c0144SNamhyung Kim /* per-process locks set upper bits of the flags */ 2281811e827SNamhyung Kim if (flags & LCD_F_MMAP_LOCK) 2291811e827SNamhyung Kim return "mmap_lock"; 2301811e827SNamhyung Kim if (flags & LCD_F_SIGHAND_LOCK) 2311811e827SNamhyung Kim return "siglock"; 232d24c0144SNamhyung Kim 233d24c0144SNamhyung Kim /* global locks with symbols */ 2344d1792d0SNamhyung Kim sym = machine__find_kernel_symbol(machine, key->lock_addr_or_cgroup, &kmap); 235492fef21SNamhyung Kim if (sym) 236d24c0144SNamhyung Kim return sym->name; 237d24c0144SNamhyung Kim 238d24c0144SNamhyung Kim /* try semi-global locks collected separately */ 2394d1792d0SNamhyung Kim if (!bpf_map_lookup_elem(lock_fd, &key->lock_addr_or_cgroup, &flags)) { 240d24c0144SNamhyung Kim if (flags == LOCK_CLASS_RQLOCK) 241d24c0144SNamhyung Kim return "rq_lock"; 242d24c0144SNamhyung Kim } 243d24c0144SNamhyung Kim 244d24c0144SNamhyung Kim return ""; 245492fef21SNamhyung Kim } 246492fef21SNamhyung Kim 2474d1792d0SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_CGROUP) { 2484d1792d0SNamhyung Kim u64 cgrp_id = key->lock_addr_or_cgroup; 249d0c502e4SNamhyung Kim struct cgroup *cgrp = __cgroup__find(&con->cgroups, cgrp_id); 250d0c502e4SNamhyung Kim 251d0c502e4SNamhyung Kim if (cgrp) 252d0c502e4SNamhyung Kim return cgrp->name; 253d0c502e4SNamhyung Kim 254*29b8e94dSYang Jihong snprintf(name_buf, sizeof(name_buf), "cgroup:%" PRIu64 "", cgrp_id); 255d0c502e4SNamhyung Kim return name_buf; 256d0c502e4SNamhyung Kim } 257d0c502e4SNamhyung Kim 258492fef21SNamhyung Kim /* LOCK_AGGR_CALLER: skip lock internal functions */ 259492fef21SNamhyung Kim while (machine__is_lock_function(machine, stack_trace[idx]) && 260492fef21SNamhyung Kim idx < con->max_stack - 1) 261492fef21SNamhyung Kim idx++; 262492fef21SNamhyung Kim 263492fef21SNamhyung Kim addr = stack_trace[idx]; 264492fef21SNamhyung Kim sym = machine__find_kernel_symbol(machine, addr, &kmap); 265492fef21SNamhyung Kim 266492fef21SNamhyung Kim if (sym) { 267492fef21SNamhyung Kim unsigned long offset; 268492fef21SNamhyung Kim 26978a1f7cdSIan Rogers offset = map__map_ip(kmap, addr) - sym->start; 270492fef21SNamhyung Kim 271492fef21SNamhyung Kim if (offset == 0) 272492fef21SNamhyung Kim return sym->name; 273492fef21SNamhyung Kim 274492fef21SNamhyung Kim snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset); 275492fef21SNamhyung Kim } else { 276492fef21SNamhyung Kim snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr); 277492fef21SNamhyung Kim } 278492fef21SNamhyung Kim 279492fef21SNamhyung Kim return name_buf; 280492fef21SNamhyung Kim } 281492fef21SNamhyung Kim 282447ec4e5SNamhyung Kim int lock_contention_read(struct lock_contention *con) 283407b36f6SNamhyung Kim { 284492fef21SNamhyung Kim int fd, stack, err = 0; 2855d8c0f0eSNamhyung Kim struct contention_key *prev_key, key = {}; 286fd507d3eSNamhyung Kim struct contention_data data = {}; 2879e9c5f3cSNamhyung Kim struct lock_stat *st = NULL; 288447ec4e5SNamhyung Kim struct machine *machine = con->machine; 2899e9c5f3cSNamhyung Kim u64 *stack_trace; 2909e9c5f3cSNamhyung Kim size_t stack_size = con->max_stack * sizeof(*stack_trace); 291407b36f6SNamhyung Kim 292407b36f6SNamhyung Kim fd = bpf_map__fd(skel->maps.lock_stat); 293407b36f6SNamhyung Kim stack = bpf_map__fd(skel->maps.stacks); 294407b36f6SNamhyung Kim 29584c3a2bbSNamhyung Kim con->fails.task = skel->bss->task_fail; 29684c3a2bbSNamhyung Kim con->fails.stack = skel->bss->stack_fail; 29784c3a2bbSNamhyung Kim con->fails.time = skel->bss->time_fail; 298954cdac7SNamhyung Kim con->fails.data = skel->bss->data_fail; 2996d499a6bSNamhyung Kim 3009e9c5f3cSNamhyung Kim stack_trace = zalloc(stack_size); 3019e9c5f3cSNamhyung Kim if (stack_trace == NULL) 3029e9c5f3cSNamhyung Kim return -1; 3039e9c5f3cSNamhyung Kim 304eca949b2SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_TASK) { 305eca949b2SNamhyung Kim struct thread *idle = __machine__findnew_thread(machine, 306eca949b2SNamhyung Kim /*pid=*/0, 307eca949b2SNamhyung Kim /*tid=*/0); 308eca949b2SNamhyung Kim thread__set_comm(idle, "swapper", /*timestamp=*/0); 309eca949b2SNamhyung Kim } 310eca949b2SNamhyung Kim 311d24c0144SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_ADDR) { 312d24c0144SNamhyung Kim DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, 313d24c0144SNamhyung Kim .flags = BPF_F_TEST_RUN_ON_CPU, 314d24c0144SNamhyung Kim ); 315d24c0144SNamhyung Kim int prog_fd = bpf_program__fd(skel->progs.collect_lock_syms); 316d24c0144SNamhyung Kim 317d24c0144SNamhyung Kim bpf_prog_test_run_opts(prog_fd, &opts); 318d24c0144SNamhyung Kim } 319d24c0144SNamhyung Kim 320688d2e8dSNamhyung Kim /* make sure it loads the kernel map */ 321ff583dc4SIan Rogers map__load(maps__first(machine->kmaps)->map); 322688d2e8dSNamhyung Kim 323fd507d3eSNamhyung Kim prev_key = NULL; 324fd507d3eSNamhyung Kim while (!bpf_map_get_next_key(fd, prev_key, &key)) { 325ebab2916SNamhyung Kim s64 ls_key; 32616cad1d3SNamhyung Kim const char *name; 327407b36f6SNamhyung Kim 3289e9c5f3cSNamhyung Kim /* to handle errors in the loop body */ 3299e9c5f3cSNamhyung Kim err = -1; 3309e9c5f3cSNamhyung Kim 331407b36f6SNamhyung Kim bpf_map_lookup_elem(fd, &key, &data); 33216cad1d3SNamhyung Kim if (con->save_callstack) { 333ebab2916SNamhyung Kim bpf_map_lookup_elem(stack, &key.stack_id, stack_trace); 334ebab2916SNamhyung Kim 335aae7e453SNamhyung Kim if (!match_callstack_filter(machine, stack_trace)) { 336aae7e453SNamhyung Kim con->nr_filtered += data.count; 337ebab2916SNamhyung Kim goto next; 33816cad1d3SNamhyung Kim } 339aae7e453SNamhyung Kim } 34016cad1d3SNamhyung Kim 341ebab2916SNamhyung Kim switch (con->aggr_mode) { 342ebab2916SNamhyung Kim case LOCK_AGGR_CALLER: 343ebab2916SNamhyung Kim ls_key = key.stack_id; 344ebab2916SNamhyung Kim break; 345ebab2916SNamhyung Kim case LOCK_AGGR_TASK: 346ebab2916SNamhyung Kim ls_key = key.pid; 347ebab2916SNamhyung Kim break; 348ebab2916SNamhyung Kim case LOCK_AGGR_ADDR: 3494d1792d0SNamhyung Kim case LOCK_AGGR_CGROUP: 3504d1792d0SNamhyung Kim ls_key = key.lock_addr_or_cgroup; 351ebab2916SNamhyung Kim break; 352ebab2916SNamhyung Kim default: 353ebab2916SNamhyung Kim goto next; 354ebab2916SNamhyung Kim } 355ebab2916SNamhyung Kim 356ebab2916SNamhyung Kim st = lock_stat_find(ls_key); 35716cad1d3SNamhyung Kim if (st != NULL) { 35816cad1d3SNamhyung Kim st->wait_time_total += data.total_time; 35916cad1d3SNamhyung Kim if (st->wait_time_max < data.max_time) 36016cad1d3SNamhyung Kim st->wait_time_max = data.max_time; 36116cad1d3SNamhyung Kim if (st->wait_time_min > data.min_time) 36216cad1d3SNamhyung Kim st->wait_time_min = data.min_time; 36316cad1d3SNamhyung Kim 36416cad1d3SNamhyung Kim st->nr_contended += data.count; 36516cad1d3SNamhyung Kim if (st->nr_contended) 36616cad1d3SNamhyung Kim st->avg_wait_time = st->wait_time_total / st->nr_contended; 36716cad1d3SNamhyung Kim goto next; 36816cad1d3SNamhyung Kim } 36916cad1d3SNamhyung Kim 3701811e827SNamhyung Kim name = lock_contention_get_name(con, &key, stack_trace, data.flags); 371ebab2916SNamhyung Kim st = lock_stat_findnew(ls_key, name, data.flags); 372407b36f6SNamhyung Kim if (st == NULL) 3739e9c5f3cSNamhyung Kim break; 374407b36f6SNamhyung Kim 375407b36f6SNamhyung Kim st->nr_contended = data.count; 376407b36f6SNamhyung Kim st->wait_time_total = data.total_time; 377407b36f6SNamhyung Kim st->wait_time_max = data.max_time; 378407b36f6SNamhyung Kim st->wait_time_min = data.min_time; 379407b36f6SNamhyung Kim 380407b36f6SNamhyung Kim if (data.count) 381407b36f6SNamhyung Kim st->avg_wait_time = data.total_time / data.count; 382407b36f6SNamhyung Kim 3830fba2265SNamhyung Kim if (con->aggr_mode == LOCK_AGGR_CALLER && verbose > 0) { 3849e9c5f3cSNamhyung Kim st->callstack = memdup(stack_trace, stack_size); 3859e9c5f3cSNamhyung Kim if (st->callstack == NULL) 3869e9c5f3cSNamhyung Kim break; 387a6eaf966SNamhyung Kim } 388492fef21SNamhyung Kim 38916cad1d3SNamhyung Kim next: 390fd507d3eSNamhyung Kim prev_key = &key; 3919e9c5f3cSNamhyung Kim 39216cad1d3SNamhyung Kim /* we're fine now, reset the error */ 3939e9c5f3cSNamhyung Kim err = 0; 394407b36f6SNamhyung Kim } 395407b36f6SNamhyung Kim 3969e9c5f3cSNamhyung Kim free(stack_trace); 3979e9c5f3cSNamhyung Kim 3989e9c5f3cSNamhyung Kim return err; 399407b36f6SNamhyung Kim } 400407b36f6SNamhyung Kim 401d0c502e4SNamhyung Kim int lock_contention_finish(struct lock_contention *con) 402407b36f6SNamhyung Kim { 403407b36f6SNamhyung Kim if (skel) { 404407b36f6SNamhyung Kim skel->bss->enabled = 0; 405407b36f6SNamhyung Kim lock_contention_bpf__destroy(skel); 406407b36f6SNamhyung Kim } 407407b36f6SNamhyung Kim 408d0c502e4SNamhyung Kim while (!RB_EMPTY_ROOT(&con->cgroups)) { 409d0c502e4SNamhyung Kim struct rb_node *node = rb_first(&con->cgroups); 410d0c502e4SNamhyung Kim struct cgroup *cgrp = rb_entry(node, struct cgroup, node); 411d0c502e4SNamhyung Kim 412d0c502e4SNamhyung Kim rb_erase(node, &con->cgroups); 413d0c502e4SNamhyung Kim cgroup__put(cgrp); 414d0c502e4SNamhyung Kim } 415d0c502e4SNamhyung Kim 416407b36f6SNamhyung Kim return 0; 417407b36f6SNamhyung Kim } 418