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 	bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
43 
44 	if (target__has_cpu(target))
45 		ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
46 	if (target__has_task(target))
47 		ntasks = perf_thread_map__nr(evlist->core.threads);
48 
49 	bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
50 	bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
51 
52 	if (lock_contention_bpf__load(skel) < 0) {
53 		pr_err("Failed to load lock-contention BPF skeleton\n");
54 		return -1;
55 	}
56 
57 	if (target__has_cpu(target)) {
58 		u32 cpu;
59 		u8 val = 1;
60 
61 		skel->bss->has_cpu = 1;
62 		fd = bpf_map__fd(skel->maps.cpu_filter);
63 
64 		for (i = 0; i < ncpus; i++) {
65 			cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
66 			bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
67 		}
68 	}
69 
70 	if (target__has_task(target)) {
71 		u32 pid;
72 		u8 val = 1;
73 
74 		skel->bss->has_task = 1;
75 		fd = bpf_map__fd(skel->maps.task_filter);
76 
77 		for (i = 0; i < ntasks; i++) {
78 			pid = perf_thread_map__pid(evlist->core.threads, i);
79 			bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
80 		}
81 	}
82 
83 	if (target__none(target) && evlist->workload.pid > 0) {
84 		u32 pid = evlist->workload.pid;
85 		u8 val = 1;
86 
87 		skel->bss->has_task = 1;
88 		fd = bpf_map__fd(skel->maps.task_filter);
89 		bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
90 	}
91 
92 	skel->bss->stack_skip = con->stack_skip;
93 
94 	lock_contention_bpf__attach(skel);
95 	return 0;
96 }
97 
98 int lock_contention_start(void)
99 {
100 	skel->bss->enabled = 1;
101 	return 0;
102 }
103 
104 int lock_contention_stop(void)
105 {
106 	skel->bss->enabled = 0;
107 	return 0;
108 }
109 
110 int lock_contention_read(struct lock_contention *con)
111 {
112 	int fd, stack, err = 0;
113 	s32 prev_key, key;
114 	struct lock_contention_data data = {};
115 	struct lock_stat *st = NULL;
116 	struct machine *machine = con->machine;
117 	u64 *stack_trace;
118 	size_t stack_size = con->max_stack * sizeof(*stack_trace);
119 
120 	fd = bpf_map__fd(skel->maps.lock_stat);
121 	stack = bpf_map__fd(skel->maps.stacks);
122 
123 	con->lost = skel->bss->lost;
124 
125 	stack_trace = zalloc(stack_size);
126 	if (stack_trace == NULL)
127 		return -1;
128 
129 	prev_key = 0;
130 	while (!bpf_map_get_next_key(fd, &prev_key, &key)) {
131 		struct map *kmap;
132 		struct symbol *sym;
133 		int idx = 0;
134 
135 		/* to handle errors in the loop body */
136 		err = -1;
137 
138 		bpf_map_lookup_elem(fd, &key, &data);
139 		st = zalloc(sizeof(*st));
140 		if (st == NULL)
141 			break;
142 
143 		st->nr_contended = data.count;
144 		st->wait_time_total = data.total_time;
145 		st->wait_time_max = data.max_time;
146 		st->wait_time_min = data.min_time;
147 
148 		if (data.count)
149 			st->avg_wait_time = data.total_time / data.count;
150 
151 		st->flags = data.flags;
152 
153 		bpf_map_lookup_elem(stack, &key, stack_trace);
154 
155 		/* skip lock internal functions */
156 		while (is_lock_function(machine, stack_trace[idx]) &&
157 		       idx < con->max_stack - 1)
158 			idx++;
159 
160 		st->addr = stack_trace[idx];
161 		sym = machine__find_kernel_symbol(machine, st->addr, &kmap);
162 
163 		if (sym) {
164 			unsigned long offset;
165 			int ret = 0;
166 
167 			offset = kmap->map_ip(kmap, st->addr) - sym->start;
168 
169 			if (offset)
170 				ret = asprintf(&st->name, "%s+%#lx", sym->name, offset);
171 			else
172 				st->name = strdup(sym->name);
173 
174 			if (ret < 0 || st->name == NULL)
175 				break;
176 		} else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) {
177 			break;
178 		}
179 
180 		if (verbose) {
181 			st->callstack = memdup(stack_trace, stack_size);
182 			if (st->callstack == NULL)
183 				break;
184 		}
185 
186 		hlist_add_head(&st->hash_entry, con->result);
187 		prev_key = key;
188 
189 		/* we're fine now, reset the values */
190 		st = NULL;
191 		err = 0;
192 	}
193 
194 	free(stack_trace);
195 	if (st) {
196 		free(st->name);
197 		free(st);
198 	}
199 
200 	return err;
201 }
202 
203 int lock_contention_finish(void)
204 {
205 	if (skel) {
206 		skel->bss->enabled = 0;
207 		lock_contention_bpf__destroy(skel);
208 	}
209 
210 	return 0;
211 }
212