06466da792e41913d5435b372db6b0be486508a5
[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_map.h"
9 #include "util/lock-contention.h"
10 #include <linux/zalloc.h>
11 #include <linux/string.h>
12 #include <bpf/bpf.h>
13
14 #include "bpf_skel/lock_contention.skel.h"
15
16 static struct lock_contention_bpf *skel;
17
18 struct lock_contention_data {
19         u64 total_time;
20         u64 min_time;
21         u64 max_time;
22         u32 count;
23         u32 flags;
24 };
25
26 int lock_contention_prepare(struct lock_contention *con)
27 {
28         int i, fd;
29         int ncpus = 1, ntasks = 1;
30         struct evlist *evlist = con->evlist;
31         struct target *target = con->target;
32
33         skel = lock_contention_bpf__open();
34         if (!skel) {
35                 pr_err("Failed to open lock-contention BPF skeleton\n");
36                 return -1;
37         }
38
39         bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
40         bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
41         bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
42
43         if (target__has_cpu(target))
44                 ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
45         if (target__has_task(target))
46                 ntasks = perf_thread_map__nr(evlist->core.threads);
47
48         bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
49         bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
50
51         if (lock_contention_bpf__load(skel) < 0) {
52                 pr_err("Failed to load lock-contention BPF skeleton\n");
53                 return -1;
54         }
55
56         if (target__has_cpu(target)) {
57                 u32 cpu;
58                 u8 val = 1;
59
60                 skel->bss->has_cpu = 1;
61                 fd = bpf_map__fd(skel->maps.cpu_filter);
62
63                 for (i = 0; i < ncpus; i++) {
64                         cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
65                         bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
66                 }
67         }
68
69         if (target__has_task(target)) {
70                 u32 pid;
71                 u8 val = 1;
72
73                 skel->bss->has_task = 1;
74                 fd = bpf_map__fd(skel->maps.task_filter);
75
76                 for (i = 0; i < ntasks; i++) {
77                         pid = perf_thread_map__pid(evlist->core.threads, i);
78                         bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
79                 }
80         }
81
82         if (target__none(target) && evlist->workload.pid > 0) {
83                 u32 pid = evlist->workload.pid;
84                 u8 val = 1;
85
86                 skel->bss->has_task = 1;
87                 fd = bpf_map__fd(skel->maps.task_filter);
88                 bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
89         }
90
91         skel->bss->stack_skip = con->stack_skip;
92
93         lock_contention_bpf__attach(skel);
94         return 0;
95 }
96
97 int lock_contention_start(void)
98 {
99         skel->bss->enabled = 1;
100         return 0;
101 }
102
103 int lock_contention_stop(void)
104 {
105         skel->bss->enabled = 0;
106         return 0;
107 }
108
109 int lock_contention_read(struct lock_contention *con)
110 {
111         int fd, stack;
112         s32 prev_key, key;
113         struct lock_contention_data data = {};
114         struct lock_stat *st;
115         struct machine *machine = con->machine;
116         u64 stack_trace[con->max_stack];
117
118         fd = bpf_map__fd(skel->maps.lock_stat);
119         stack = bpf_map__fd(skel->maps.stacks);
120
121         con->lost = skel->bss->lost;
122
123         prev_key = 0;
124         while (!bpf_map_get_next_key(fd, &prev_key, &key)) {
125                 struct map *kmap;
126                 struct symbol *sym;
127                 int idx = 0;
128
129                 bpf_map_lookup_elem(fd, &key, &data);
130                 st = zalloc(sizeof(*st));
131                 if (st == NULL)
132                         return -1;
133
134                 st->nr_contended = data.count;
135                 st->wait_time_total = data.total_time;
136                 st->wait_time_max = data.max_time;
137                 st->wait_time_min = data.min_time;
138
139                 if (data.count)
140                         st->avg_wait_time = data.total_time / data.count;
141
142                 st->flags = data.flags;
143
144                 bpf_map_lookup_elem(stack, &key, stack_trace);
145
146                 /* skip lock internal functions */
147                 while (is_lock_function(machine, stack_trace[idx]) &&
148                        idx < con->max_stack - 1)
149                         idx++;
150
151                 st->addr = stack_trace[idx];
152                 sym = machine__find_kernel_symbol(machine, st->addr, &kmap);
153
154                 if (sym) {
155                         unsigned long offset;
156                         int ret = 0;
157
158                         offset = kmap->map_ip(kmap, st->addr) - sym->start;
159
160                         if (offset)
161                                 ret = asprintf(&st->name, "%s+%#lx", sym->name, offset);
162                         else
163                                 st->name = strdup(sym->name);
164
165                         if (ret < 0 || st->name == NULL)
166                                 return -1;
167                 } else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) {
168                         free(st);
169                         return -1;
170                 }
171
172                 if (verbose) {
173                         st->callstack = memdup(stack_trace, sizeof(stack_trace));
174                         if (st->callstack == NULL) {
175                                 free(st);
176                                 return -1;
177                         }
178                 }
179
180                 hlist_add_head(&st->hash_entry, con->result);
181                 prev_key = key;
182         }
183
184         return 0;
185 }
186
187 int lock_contention_finish(void)
188 {
189         if (skel) {
190                 skel->bss->enabled = 0;
191                 lock_contention_bpf__destroy(skel);
192         }
193
194         return 0;
195 }