2022-07-30 04:07:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "util/debug.h"
|
2022-07-30 04:07:56 +08:00
|
|
|
#include "util/evlist.h"
|
2022-07-30 04:07:55 +08:00
|
|
|
#include "util/machine.h"
|
|
|
|
#include "util/map.h"
|
|
|
|
#include "util/symbol.h"
|
2022-07-30 04:07:56 +08:00
|
|
|
#include "util/target.h"
|
2022-12-10 03:07:25 +08:00
|
|
|
#include "util/thread.h"
|
2022-07-30 04:07:56 +08:00
|
|
|
#include "util/thread_map.h"
|
2022-07-30 04:07:55 +08:00
|
|
|
#include "util/lock-contention.h"
|
|
|
|
#include <linux/zalloc.h>
|
2022-09-12 13:53:12 +08:00
|
|
|
#include <linux/string.h>
|
2022-07-30 04:07:55 +08:00
|
|
|
#include <bpf/bpf.h>
|
|
|
|
|
|
|
|
#include "bpf_skel/lock_contention.skel.h"
|
2022-12-10 03:07:24 +08:00
|
|
|
#include "bpf_skel/lock_data.h"
|
2022-07-30 04:07:55 +08:00
|
|
|
|
|
|
|
static struct lock_contention_bpf *skel;
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
int lock_contention_prepare(struct lock_contention *con)
|
2022-07-30 04:07:55 +08:00
|
|
|
{
|
2022-07-30 04:07:56 +08:00
|
|
|
int i, fd;
|
2022-12-20 04:17:31 +08:00
|
|
|
int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1;
|
2022-08-03 03:10:02 +08:00
|
|
|
struct evlist *evlist = con->evlist;
|
|
|
|
struct target *target = con->target;
|
2022-07-30 04:07:56 +08:00
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
skel = lock_contention_bpf__open();
|
|
|
|
if (!skel) {
|
|
|
|
pr_err("Failed to open lock-contention BPF skeleton\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-09-12 13:53:13 +08:00
|
|
|
bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
|
2022-08-03 03:10:03 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
|
2022-11-19 03:01:09 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
|
2022-08-03 03:10:03 +08:00
|
|
|
|
2023-02-03 10:13:24 +08:00
|
|
|
if (con->aggr_mode == LOCK_AGGR_TASK)
|
2022-12-10 03:07:25 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries);
|
2023-02-03 10:13:24 +08:00
|
|
|
else
|
2022-12-10 03:07:25 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.task_data, 1);
|
2023-02-03 10:13:24 +08:00
|
|
|
|
|
|
|
if (con->save_callstack)
|
2022-12-10 03:07:25 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
|
2023-02-03 10:13:24 +08:00
|
|
|
else
|
|
|
|
bpf_map__set_max_entries(skel->maps.stacks, 1);
|
2022-12-10 03:07:25 +08:00
|
|
|
|
2022-07-30 04:07:56 +08:00
|
|
|
if (target__has_cpu(target))
|
|
|
|
ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
|
|
|
|
if (target__has_task(target))
|
|
|
|
ntasks = perf_thread_map__nr(evlist->core.threads);
|
2022-12-20 04:17:29 +08:00
|
|
|
if (con->filters->nr_types)
|
|
|
|
ntypes = con->filters->nr_types;
|
2022-07-30 04:07:56 +08:00
|
|
|
|
2022-12-20 04:17:31 +08:00
|
|
|
/* resolve lock name filters to addr */
|
|
|
|
if (con->filters->nr_syms) {
|
|
|
|
struct symbol *sym;
|
|
|
|
struct map *kmap;
|
|
|
|
unsigned long *addrs;
|
|
|
|
|
|
|
|
for (i = 0; i < con->filters->nr_syms; i++) {
|
|
|
|
sym = machine__find_kernel_symbol_by_name(con->machine,
|
|
|
|
con->filters->syms[i],
|
|
|
|
&kmap);
|
|
|
|
if (sym == NULL) {
|
|
|
|
pr_warning("ignore unknown symbol: %s\n",
|
|
|
|
con->filters->syms[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs = realloc(con->filters->addrs,
|
|
|
|
(con->filters->nr_addrs + 1) * sizeof(*addrs));
|
|
|
|
if (addrs == NULL) {
|
|
|
|
pr_warning("memory allocation failure\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs[con->filters->nr_addrs++] = kmap->unmap_ip(kmap, sym->start);
|
|
|
|
con->filters->addrs = addrs;
|
|
|
|
}
|
|
|
|
naddrs = con->filters->nr_addrs;
|
|
|
|
}
|
|
|
|
|
2022-07-30 04:07:56 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
|
|
|
|
bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
|
2022-12-20 04:17:29 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.type_filter, ntypes);
|
2022-12-20 04:17:31 +08:00
|
|
|
bpf_map__set_max_entries(skel->maps.addr_filter, naddrs);
|
2022-07-30 04:07:56 +08:00
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
if (lock_contention_bpf__load(skel) < 0) {
|
|
|
|
pr_err("Failed to load lock-contention BPF skeleton\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-07-30 04:07:56 +08:00
|
|
|
if (target__has_cpu(target)) {
|
|
|
|
u32 cpu;
|
|
|
|
u8 val = 1;
|
|
|
|
|
|
|
|
skel->bss->has_cpu = 1;
|
|
|
|
fd = bpf_map__fd(skel->maps.cpu_filter);
|
|
|
|
|
|
|
|
for (i = 0; i < ncpus; i++) {
|
|
|
|
cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
|
|
|
|
bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target__has_task(target)) {
|
|
|
|
u32 pid;
|
|
|
|
u8 val = 1;
|
|
|
|
|
|
|
|
skel->bss->has_task = 1;
|
|
|
|
fd = bpf_map__fd(skel->maps.task_filter);
|
|
|
|
|
|
|
|
for (i = 0; i < ntasks; i++) {
|
|
|
|
pid = perf_thread_map__pid(evlist->core.threads, i);
|
|
|
|
bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target__none(target) && evlist->workload.pid > 0) {
|
|
|
|
u32 pid = evlist->workload.pid;
|
|
|
|
u8 val = 1;
|
|
|
|
|
|
|
|
skel->bss->has_task = 1;
|
|
|
|
fd = bpf_map__fd(skel->maps.task_filter);
|
|
|
|
bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
|
|
|
|
}
|
|
|
|
|
2022-12-20 04:17:29 +08:00
|
|
|
if (con->filters->nr_types) {
|
|
|
|
u8 val = 1;
|
|
|
|
|
|
|
|
skel->bss->has_type = 1;
|
|
|
|
fd = bpf_map__fd(skel->maps.type_filter);
|
|
|
|
|
|
|
|
for (i = 0; i < con->filters->nr_types; i++)
|
|
|
|
bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY);
|
|
|
|
}
|
|
|
|
|
2022-12-20 04:17:31 +08:00
|
|
|
if (con->filters->nr_addrs) {
|
|
|
|
u8 val = 1;
|
|
|
|
|
|
|
|
skel->bss->has_addr = 1;
|
|
|
|
fd = bpf_map__fd(skel->maps.addr_filter);
|
|
|
|
|
|
|
|
for (i = 0; i < con->filters->nr_addrs; i++)
|
|
|
|
bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY);
|
|
|
|
}
|
|
|
|
|
2022-12-10 03:07:25 +08:00
|
|
|
/* these don't work well if in the rodata section */
|
2022-09-12 13:53:14 +08:00
|
|
|
skel->bss->stack_skip = con->stack_skip;
|
2022-12-10 03:07:25 +08:00
|
|
|
skel->bss->aggr_mode = con->aggr_mode;
|
2023-02-03 10:13:24 +08:00
|
|
|
skel->bss->needs_callstack = con->save_callstack;
|
2023-02-07 08:24:02 +08:00
|
|
|
skel->bss->lock_owner = con->owner;
|
2022-09-12 13:53:14 +08:00
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
lock_contention_bpf__attach(skel);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lock_contention_start(void)
|
|
|
|
{
|
|
|
|
skel->bss->enabled = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lock_contention_stop(void)
|
|
|
|
{
|
|
|
|
skel->bss->enabled = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:13:22 +08:00
|
|
|
static const char *lock_contention_get_name(struct lock_contention *con,
|
|
|
|
struct contention_key *key,
|
2023-03-14 04:48:23 +08:00
|
|
|
u64 *stack_trace, u32 flags)
|
2023-02-03 10:13:22 +08:00
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
u64 addr;
|
|
|
|
const char *name = "";
|
|
|
|
static char name_buf[KSYM_NAME_LEN];
|
|
|
|
struct symbol *sym;
|
|
|
|
struct map *kmap;
|
|
|
|
struct machine *machine = con->machine;
|
|
|
|
|
|
|
|
if (con->aggr_mode == LOCK_AGGR_TASK) {
|
|
|
|
struct contention_task_data task;
|
2023-02-03 10:13:24 +08:00
|
|
|
int pid = key->pid;
|
2023-02-03 10:13:22 +08:00
|
|
|
int task_fd = bpf_map__fd(skel->maps.task_data);
|
|
|
|
|
|
|
|
/* do not update idle comm which contains CPU number */
|
|
|
|
if (pid) {
|
|
|
|
struct thread *t = __machine__findnew_thread(machine, /*pid=*/-1, pid);
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return name;
|
|
|
|
if (!bpf_map_lookup_elem(task_fd, &pid, &task) &&
|
|
|
|
thread__set_comm(t, task.comm, /*timestamp=*/0))
|
|
|
|
name = task.comm;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->aggr_mode == LOCK_AGGR_ADDR) {
|
2023-03-14 04:48:23 +08:00
|
|
|
if (flags & LCD_F_MMAP_LOCK)
|
|
|
|
return "mmap_lock";
|
|
|
|
if (flags & LCD_F_SIGHAND_LOCK)
|
|
|
|
return "siglock";
|
2023-02-03 10:13:24 +08:00
|
|
|
sym = machine__find_kernel_symbol(machine, key->lock_addr, &kmap);
|
2023-02-03 10:13:22 +08:00
|
|
|
if (sym)
|
|
|
|
name = sym->name;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* LOCK_AGGR_CALLER: skip lock internal functions */
|
|
|
|
while (machine__is_lock_function(machine, stack_trace[idx]) &&
|
|
|
|
idx < con->max_stack - 1)
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
addr = stack_trace[idx];
|
|
|
|
sym = machine__find_kernel_symbol(machine, addr, &kmap);
|
|
|
|
|
|
|
|
if (sym) {
|
|
|
|
unsigned long offset;
|
|
|
|
|
|
|
|
offset = kmap->map_ip(kmap, addr) - sym->start;
|
|
|
|
|
|
|
|
if (offset == 0)
|
|
|
|
return sym->name;
|
|
|
|
|
|
|
|
snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset);
|
|
|
|
} else {
|
|
|
|
snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return name_buf;
|
|
|
|
}
|
|
|
|
|
2022-08-03 03:10:02 +08:00
|
|
|
int lock_contention_read(struct lock_contention *con)
|
2022-07-30 04:07:55 +08:00
|
|
|
{
|
2023-02-03 10:13:22 +08:00
|
|
|
int fd, stack, err = 0;
|
2022-12-10 03:07:24 +08:00
|
|
|
struct contention_key *prev_key, key;
|
|
|
|
struct contention_data data = {};
|
2022-10-29 02:01:27 +08:00
|
|
|
struct lock_stat *st = NULL;
|
2022-08-03 03:10:02 +08:00
|
|
|
struct machine *machine = con->machine;
|
2022-10-29 02:01:27 +08:00
|
|
|
u64 *stack_trace;
|
|
|
|
size_t stack_size = con->max_stack * sizeof(*stack_trace);
|
2022-07-30 04:07:55 +08:00
|
|
|
|
|
|
|
fd = bpf_map__fd(skel->maps.lock_stat);
|
|
|
|
stack = bpf_map__fd(skel->maps.stacks);
|
|
|
|
|
2022-08-03 03:10:04 +08:00
|
|
|
con->lost = skel->bss->lost;
|
|
|
|
|
2022-10-29 02:01:27 +08:00
|
|
|
stack_trace = zalloc(stack_size);
|
|
|
|
if (stack_trace == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2022-12-10 03:07:25 +08:00
|
|
|
if (con->aggr_mode == LOCK_AGGR_TASK) {
|
|
|
|
struct thread *idle = __machine__findnew_thread(machine,
|
|
|
|
/*pid=*/0,
|
|
|
|
/*tid=*/0);
|
|
|
|
thread__set_comm(idle, "swapper", /*timestamp=*/0);
|
|
|
|
}
|
|
|
|
|
2022-12-10 03:07:26 +08:00
|
|
|
/* make sure it loads the kernel map */
|
|
|
|
map__load(maps__first(machine->kmaps));
|
|
|
|
|
2022-12-10 03:07:24 +08:00
|
|
|
prev_key = NULL;
|
|
|
|
while (!bpf_map_get_next_key(fd, prev_key, &key)) {
|
2023-02-03 10:13:24 +08:00
|
|
|
s64 ls_key;
|
2023-02-03 10:13:23 +08:00
|
|
|
const char *name;
|
2022-07-30 04:07:55 +08:00
|
|
|
|
2022-10-29 02:01:27 +08:00
|
|
|
/* to handle errors in the loop body */
|
|
|
|
err = -1;
|
|
|
|
|
2022-07-30 04:07:55 +08:00
|
|
|
bpf_map_lookup_elem(fd, &key, &data);
|
2023-02-03 10:13:23 +08:00
|
|
|
if (con->save_callstack) {
|
2023-02-03 10:13:24 +08:00
|
|
|
bpf_map_lookup_elem(stack, &key.stack_id, stack_trace);
|
|
|
|
|
|
|
|
if (!match_callstack_filter(machine, stack_trace))
|
|
|
|
goto next;
|
2023-02-03 10:13:23 +08:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:13:24 +08:00
|
|
|
switch (con->aggr_mode) {
|
|
|
|
case LOCK_AGGR_CALLER:
|
|
|
|
ls_key = key.stack_id;
|
|
|
|
break;
|
|
|
|
case LOCK_AGGR_TASK:
|
|
|
|
ls_key = key.pid;
|
|
|
|
break;
|
|
|
|
case LOCK_AGGR_ADDR:
|
|
|
|
ls_key = key.lock_addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
st = lock_stat_find(ls_key);
|
2023-02-03 10:13:23 +08:00
|
|
|
if (st != NULL) {
|
|
|
|
st->wait_time_total += data.total_time;
|
|
|
|
if (st->wait_time_max < data.max_time)
|
|
|
|
st->wait_time_max = data.max_time;
|
|
|
|
if (st->wait_time_min > data.min_time)
|
|
|
|
st->wait_time_min = data.min_time;
|
|
|
|
|
|
|
|
st->nr_contended += data.count;
|
|
|
|
if (st->nr_contended)
|
|
|
|
st->avg_wait_time = st->wait_time_total / st->nr_contended;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2023-03-14 04:48:23 +08:00
|
|
|
name = lock_contention_get_name(con, &key, stack_trace, data.flags);
|
2023-02-03 10:13:24 +08:00
|
|
|
st = lock_stat_findnew(ls_key, name, data.flags);
|
2022-07-30 04:07:55 +08:00
|
|
|
if (st == NULL)
|
2022-10-29 02:01:27 +08:00
|
|
|
break;
|
2022-07-30 04:07:55 +08:00
|
|
|
|
|
|
|
st->nr_contended = data.count;
|
|
|
|
st->wait_time_total = data.total_time;
|
|
|
|
st->wait_time_max = data.max_time;
|
|
|
|
st->wait_time_min = data.min_time;
|
|
|
|
|
|
|
|
if (data.count)
|
|
|
|
st->avg_wait_time = data.total_time / data.count;
|
|
|
|
|
2023-01-26 08:09:36 +08:00
|
|
|
if (con->save_callstack) {
|
2022-10-29 02:01:27 +08:00
|
|
|
st->callstack = memdup(stack_trace, stack_size);
|
|
|
|
if (st->callstack == NULL)
|
|
|
|
break;
|
2022-09-12 13:53:12 +08:00
|
|
|
}
|
2023-02-03 10:13:22 +08:00
|
|
|
|
2023-02-03 10:13:23 +08:00
|
|
|
next:
|
2022-12-10 03:07:24 +08:00
|
|
|
prev_key = &key;
|
2022-10-29 02:01:27 +08:00
|
|
|
|
2023-02-03 10:13:23 +08:00
|
|
|
/* we're fine now, reset the error */
|
2022-10-29 02:01:27 +08:00
|
|
|
err = 0;
|
2022-07-30 04:07:55 +08:00
|
|
|
}
|
|
|
|
|
2022-10-29 02:01:27 +08:00
|
|
|
free(stack_trace);
|
|
|
|
|
|
|
|
return err;
|
2022-07-30 04:07:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int lock_contention_finish(void)
|
|
|
|
{
|
|
|
|
if (skel) {
|
|
|
|
skel->bss->enabled = 0;
|
|
|
|
lock_contention_bpf__destroy(skel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|