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, err = 0;
112 	s32 prev_key, key;
113 	struct lock_contention_data data = {};
114 	struct lock_stat *st = NULL;
115 	struct machine *machine = con->machine;
116 	u64 *stack_trace;
117 	size_t stack_size = con->max_stack * sizeof(*stack_trace);
118 
119 	fd = bpf_map__fd(skel->maps.lock_stat);
120 	stack = bpf_map__fd(skel->maps.stacks);
121 
122 	con->lost = skel->bss->lost;
123 
124 	stack_trace = zalloc(stack_size);
125 	if (stack_trace == NULL)
126 		return -1;
127 
128 	prev_key = 0;
129 	while (!bpf_map_get_next_key(fd, &prev_key, &key)) {
130 		struct map *kmap;
131 		struct symbol *sym;
132 		int idx = 0;
133 
134 		/* to handle errors in the loop body */
135 		err = -1;
136 
137 		bpf_map_lookup_elem(fd, &key, &data);
138 		st = zalloc(sizeof(*st));
139 		if (st == NULL)
140 			break;
141 
142 		st->nr_contended = data.count;
143 		st->wait_time_total = data.total_time;
144 		st->wait_time_max = data.max_time;
145 		st->wait_time_min = data.min_time;
146 
147 		if (data.count)
148 			st->avg_wait_time = data.total_time / data.count;
149 
150 		st->flags = data.flags;
151 
152 		bpf_map_lookup_elem(stack, &key, stack_trace);
153 
154 		/* skip lock internal functions */
155 		while (is_lock_function(machine, stack_trace[idx]) &&
156 		       idx < con->max_stack - 1)
157 			idx++;
158 
159 		st->addr = stack_trace[idx];
160 		sym = machine__find_kernel_symbol(machine, st->addr, &kmap);
161 
162 		if (sym) {
163 			unsigned long offset;
164 			int ret = 0;
165 
166 			offset = kmap->map_ip(kmap, st->addr) - sym->start;
167 
168 			if (offset)
169 				ret = asprintf(&st->name, "%s+%#lx", sym->name, offset);
170 			else
171 				st->name = strdup(sym->name);
172 
173 			if (ret < 0 || st->name == NULL)
174 				break;
175 		} else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) {
176 			break;
177 		}
178 
179 		if (verbose) {
180 			st->callstack = memdup(stack_trace, stack_size);
181 			if (st->callstack == NULL)
182 				break;
183 		}
184 
185 		hlist_add_head(&st->hash_entry, con->result);
186 		prev_key = key;
187 
188 		/* we're fine now, reset the values */
189 		st = NULL;
190 		err = 0;
191 	}
192 
193 	free(stack_trace);
194 	if (st) {
195 		free(st->name);
196 		free(st);
197 	}
198 
199 	return err;
200 }
201 
202 int lock_contention_finish(void)
203 {
204 	if (skel) {
205 		skel->bss->enabled = 0;
206 		lock_contention_bpf__destroy(skel);
207 	}
208 
209 	return 0;
210 }
211