bpf_lock_contention.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "util/cgroup.h"
  3. #include "util/debug.h"
  4. #include "util/evlist.h"
  5. #include "util/hashmap.h"
  6. #include "util/machine.h"
  7. #include "util/map.h"
  8. #include "util/symbol.h"
  9. #include "util/target.h"
  10. #include "util/thread.h"
  11. #include "util/thread_map.h"
  12. #include "util/lock-contention.h"
  13. #include <linux/zalloc.h>
  14. #include <linux/string.h>
  15. #include <api/fs/fs.h>
  16. #include <bpf/bpf.h>
  17. #include <bpf/btf.h>
  18. #include <inttypes.h>
  19. #include "bpf_skel/lock_contention.skel.h"
  20. #include "bpf_skel/lock_data.h"
  21. static struct lock_contention_bpf *skel;
  22. static bool has_slab_iter;
  23. static struct hashmap slab_hash;
  24. static size_t slab_cache_hash(long key, void *ctx __maybe_unused)
  25. {
  26. return key;
  27. }
  28. static bool slab_cache_equal(long key1, long key2, void *ctx __maybe_unused)
  29. {
  30. return key1 == key2;
  31. }
  32. static void check_slab_cache_iter(struct lock_contention *con)
  33. {
  34. s32 ret;
  35. hashmap__init(&slab_hash, slab_cache_hash, slab_cache_equal, /*ctx=*/NULL);
  36. con->btf = btf__load_vmlinux_btf();
  37. if (con->btf == NULL) {
  38. pr_debug("BTF loading failed: %m\n");
  39. return;
  40. }
  41. ret = btf__find_by_name_kind(con->btf, "bpf_iter__kmem_cache", BTF_KIND_STRUCT);
  42. if (ret < 0) {
  43. bpf_program__set_autoload(skel->progs.slab_cache_iter, false);
  44. pr_debug("slab cache iterator is not available: %d\n", ret);
  45. return;
  46. }
  47. has_slab_iter = true;
  48. bpf_map__set_max_entries(skel->maps.slab_caches, con->map_nr_entries);
  49. }
  50. static void run_slab_cache_iter(void)
  51. {
  52. int fd;
  53. char buf[256];
  54. long key, *prev_key;
  55. if (!has_slab_iter)
  56. return;
  57. fd = bpf_iter_create(bpf_link__fd(skel->links.slab_cache_iter));
  58. if (fd < 0) {
  59. pr_debug("cannot create slab cache iter: %d\n", fd);
  60. return;
  61. }
  62. /* This will run the bpf program */
  63. while (read(fd, buf, sizeof(buf)) > 0)
  64. continue;
  65. close(fd);
  66. /* Read the slab cache map and build a hash with IDs */
  67. fd = bpf_map__fd(skel->maps.slab_caches);
  68. prev_key = NULL;
  69. while (!bpf_map_get_next_key(fd, prev_key, &key)) {
  70. struct slab_cache_data *data;
  71. data = malloc(sizeof(*data));
  72. if (data == NULL)
  73. break;
  74. if (bpf_map_lookup_elem(fd, &key, data) < 0)
  75. break;
  76. hashmap__add(&slab_hash, data->id, data);
  77. prev_key = &key;
  78. }
  79. }
  80. static void exit_slab_cache_iter(void)
  81. {
  82. struct hashmap_entry *cur;
  83. unsigned bkt;
  84. hashmap__for_each_entry(&slab_hash, cur, bkt)
  85. free(cur->pvalue);
  86. hashmap__clear(&slab_hash);
  87. }
  88. static void init_numa_data(struct lock_contention *con)
  89. {
  90. struct symbol *sym;
  91. struct map *kmap;
  92. char *buf = NULL, *p;
  93. size_t len;
  94. long last = -1;
  95. int ret;
  96. if (!con->btf)
  97. return;
  98. /*
  99. * 'struct zone' is embedded in 'struct pglist_data' as an array.
  100. * As we may not have full information of the struct zone in the
  101. * (fake) vmlinux.h, let's get the actual size from BTF.
  102. */
  103. ret = btf__find_by_name_kind(con->btf, "zone", BTF_KIND_STRUCT);
  104. if (ret < 0) {
  105. pr_debug("cannot get type of struct zone: %d\n", ret);
  106. return;
  107. }
  108. ret = btf__resolve_size(con->btf, ret);
  109. if (ret < 0) {
  110. pr_debug("cannot get size of struct zone: %d\n", ret);
  111. return;
  112. }
  113. skel->rodata->sizeof_zone = ret;
  114. /* UMA system doesn't have 'node_data[]' - just use contig_page_data. */
  115. sym = machine__find_kernel_symbol_by_name(con->machine,
  116. "contig_page_data",
  117. &kmap);
  118. if (sym) {
  119. skel->rodata->contig_page_data_addr = map__unmap_ip(kmap, sym->start);
  120. map__put(kmap);
  121. return;
  122. }
  123. /*
  124. * The 'node_data' is an array of pointers to struct pglist_data.
  125. * It needs to follow the pointer for each node in BPF to get the
  126. * address of struct pglist_data and its zones.
  127. */
  128. sym = machine__find_kernel_symbol_by_name(con->machine,
  129. "node_data",
  130. &kmap);
  131. if (sym == NULL)
  132. return;
  133. skel->rodata->node_data_addr = map__unmap_ip(kmap, sym->start);
  134. map__put(kmap);
  135. /* get the number of online nodes using the last node number + 1 */
  136. ret = sysfs__read_str("devices/system/node/online", &buf, &len);
  137. if (ret < 0) {
  138. pr_debug("failed to read online node: %d\n", ret);
  139. return;
  140. }
  141. p = buf;
  142. while (p && *p) {
  143. last = strtol(p, &p, 0);
  144. if (p && (*p == ',' || *p == '-' || *p == '\n'))
  145. p++;
  146. }
  147. skel->rodata->nr_nodes = last + 1;
  148. free(buf);
  149. }
  150. int lock_contention_prepare(struct lock_contention *con)
  151. {
  152. int i, fd;
  153. int ncpus = 1, ntasks = 1, ntypes = 1, naddrs = 1, ncgrps = 1, nslabs = 1;
  154. struct evlist *evlist = con->evlist;
  155. struct target *target = con->target;
  156. /* make sure it loads the kernel map before lookup */
  157. map__load(machine__kernel_map(con->machine));
  158. skel = lock_contention_bpf__open();
  159. if (!skel) {
  160. pr_err("Failed to open lock-contention BPF skeleton\n");
  161. return -1;
  162. }
  163. bpf_map__set_value_size(skel->maps.stacks, con->max_stack * sizeof(u64));
  164. bpf_map__set_max_entries(skel->maps.lock_stat, con->map_nr_entries);
  165. bpf_map__set_max_entries(skel->maps.tstamp, con->map_nr_entries);
  166. if (con->aggr_mode == LOCK_AGGR_TASK)
  167. bpf_map__set_max_entries(skel->maps.task_data, con->map_nr_entries);
  168. else
  169. bpf_map__set_max_entries(skel->maps.task_data, 1);
  170. if (con->save_callstack) {
  171. bpf_map__set_max_entries(skel->maps.stacks, con->map_nr_entries);
  172. if (con->owner) {
  173. bpf_map__set_value_size(skel->maps.stack_buf, con->max_stack * sizeof(u64));
  174. bpf_map__set_key_size(skel->maps.owner_stacks,
  175. con->max_stack * sizeof(u64));
  176. bpf_map__set_max_entries(skel->maps.owner_stacks, con->map_nr_entries);
  177. bpf_map__set_max_entries(skel->maps.owner_data, con->map_nr_entries);
  178. bpf_map__set_max_entries(skel->maps.owner_stat, con->map_nr_entries);
  179. skel->rodata->max_stack = con->max_stack;
  180. }
  181. } else {
  182. bpf_map__set_max_entries(skel->maps.stacks, 1);
  183. }
  184. if (target__has_cpu(target)) {
  185. skel->rodata->has_cpu = 1;
  186. ncpus = perf_cpu_map__nr(evlist->core.user_requested_cpus);
  187. }
  188. if (target__has_task(target)) {
  189. skel->rodata->has_task = 1;
  190. ntasks = perf_thread_map__nr(evlist->core.threads);
  191. }
  192. if (con->filters->nr_types) {
  193. skel->rodata->has_type = 1;
  194. ntypes = con->filters->nr_types;
  195. }
  196. if (con->filters->nr_cgrps) {
  197. skel->rodata->has_cgroup = 1;
  198. ncgrps = con->filters->nr_cgrps;
  199. }
  200. /* resolve lock name filters to addr */
  201. if (con->filters->nr_syms) {
  202. struct symbol *sym;
  203. struct map *kmap;
  204. unsigned long *addrs;
  205. for (i = 0; i < con->filters->nr_syms; i++) {
  206. sym = machine__find_kernel_symbol_by_name(con->machine,
  207. con->filters->syms[i],
  208. &kmap);
  209. if (sym == NULL) {
  210. pr_warning("ignore unknown symbol: %s\n",
  211. con->filters->syms[i]);
  212. continue;
  213. }
  214. addrs = realloc(con->filters->addrs,
  215. (con->filters->nr_addrs + 1) * sizeof(*addrs));
  216. if (addrs == NULL) {
  217. pr_warning("memory allocation failure\n");
  218. continue;
  219. }
  220. addrs[con->filters->nr_addrs++] = map__unmap_ip(kmap, sym->start);
  221. con->filters->addrs = addrs;
  222. }
  223. naddrs = con->filters->nr_addrs;
  224. skel->rodata->has_addr = 1;
  225. }
  226. /* resolve lock name in delays */
  227. if (con->nr_delays) {
  228. struct symbol *sym;
  229. struct map *kmap;
  230. for (i = 0; i < con->nr_delays; i++) {
  231. sym = machine__find_kernel_symbol_by_name(con->machine,
  232. con->delays[i].sym,
  233. &kmap);
  234. if (sym == NULL) {
  235. pr_warning("ignore unknown symbol: %s\n",
  236. con->delays[i].sym);
  237. continue;
  238. }
  239. con->delays[i].addr = map__unmap_ip(kmap, sym->start);
  240. }
  241. skel->rodata->lock_delay = 1;
  242. bpf_map__set_max_entries(skel->maps.lock_delays, con->nr_delays);
  243. }
  244. bpf_map__set_max_entries(skel->maps.cpu_filter, ncpus);
  245. bpf_map__set_max_entries(skel->maps.task_filter, ntasks);
  246. bpf_map__set_max_entries(skel->maps.type_filter, ntypes);
  247. bpf_map__set_max_entries(skel->maps.addr_filter, naddrs);
  248. bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps);
  249. skel->rodata->stack_skip = con->stack_skip;
  250. skel->rodata->aggr_mode = con->aggr_mode;
  251. skel->rodata->needs_callstack = con->save_callstack;
  252. skel->rodata->lock_owner = con->owner;
  253. if (con->aggr_mode == LOCK_AGGR_CGROUP || con->filters->nr_cgrps) {
  254. if (cgroup_is_v2("perf_event"))
  255. skel->rodata->use_cgroup_v2 = 1;
  256. }
  257. check_slab_cache_iter(con);
  258. if (con->filters->nr_slabs && has_slab_iter) {
  259. skel->rodata->has_slab = 1;
  260. nslabs = con->filters->nr_slabs;
  261. }
  262. bpf_map__set_max_entries(skel->maps.slab_filter, nslabs);
  263. init_numa_data(con);
  264. if (lock_contention_bpf__load(skel) < 0) {
  265. pr_err("Failed to load lock-contention BPF skeleton\n");
  266. return -1;
  267. }
  268. if (target__has_cpu(target)) {
  269. u32 cpu;
  270. u8 val = 1;
  271. fd = bpf_map__fd(skel->maps.cpu_filter);
  272. for (i = 0; i < ncpus; i++) {
  273. cpu = perf_cpu_map__cpu(evlist->core.user_requested_cpus, i).cpu;
  274. bpf_map_update_elem(fd, &cpu, &val, BPF_ANY);
  275. }
  276. }
  277. if (target__has_task(target)) {
  278. u32 pid;
  279. u8 val = 1;
  280. fd = bpf_map__fd(skel->maps.task_filter);
  281. for (i = 0; i < ntasks; i++) {
  282. pid = perf_thread_map__pid(evlist->core.threads, i);
  283. bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
  284. }
  285. }
  286. if (target__none(target) && evlist->workload.pid > 0) {
  287. u32 pid = evlist->workload.pid;
  288. u8 val = 1;
  289. fd = bpf_map__fd(skel->maps.task_filter);
  290. bpf_map_update_elem(fd, &pid, &val, BPF_ANY);
  291. }
  292. if (con->filters->nr_types) {
  293. u8 val = 1;
  294. fd = bpf_map__fd(skel->maps.type_filter);
  295. for (i = 0; i < con->filters->nr_types; i++)
  296. bpf_map_update_elem(fd, &con->filters->types[i], &val, BPF_ANY);
  297. }
  298. if (con->filters->nr_addrs) {
  299. u8 val = 1;
  300. fd = bpf_map__fd(skel->maps.addr_filter);
  301. for (i = 0; i < con->filters->nr_addrs; i++)
  302. bpf_map_update_elem(fd, &con->filters->addrs[i], &val, BPF_ANY);
  303. }
  304. if (con->filters->nr_cgrps) {
  305. u8 val = 1;
  306. fd = bpf_map__fd(skel->maps.cgroup_filter);
  307. for (i = 0; i < con->filters->nr_cgrps; i++)
  308. bpf_map_update_elem(fd, &con->filters->cgrps[i], &val, BPF_ANY);
  309. }
  310. if (con->nr_delays) {
  311. fd = bpf_map__fd(skel->maps.lock_delays);
  312. for (i = 0; i < con->nr_delays; i++)
  313. bpf_map_update_elem(fd, &con->delays[i].addr, &con->delays[i].time, BPF_ANY);
  314. }
  315. if (con->aggr_mode == LOCK_AGGR_CGROUP)
  316. read_all_cgroups(&con->cgroups);
  317. bpf_program__set_autoload(skel->progs.collect_lock_syms, false);
  318. lock_contention_bpf__attach(skel);
  319. /* run the slab iterator after attaching */
  320. run_slab_cache_iter();
  321. if (con->filters->nr_slabs) {
  322. u8 val = 1;
  323. int cache_fd;
  324. long key, *prev_key;
  325. fd = bpf_map__fd(skel->maps.slab_filter);
  326. /* Read the slab cache map and build a hash with its address */
  327. cache_fd = bpf_map__fd(skel->maps.slab_caches);
  328. prev_key = NULL;
  329. while (!bpf_map_get_next_key(cache_fd, prev_key, &key)) {
  330. struct slab_cache_data data;
  331. if (bpf_map_lookup_elem(cache_fd, &key, &data) < 0)
  332. break;
  333. for (i = 0; i < con->filters->nr_slabs; i++) {
  334. if (!strcmp(con->filters->slabs[i], data.name)) {
  335. bpf_map_update_elem(fd, &key, &val, BPF_ANY);
  336. break;
  337. }
  338. }
  339. prev_key = &key;
  340. }
  341. }
  342. return 0;
  343. }
  344. /*
  345. * Run the BPF program directly using BPF_PROG_TEST_RUN to update the end
  346. * timestamp in ktime so that it can calculate delta easily.
  347. */
  348. static void mark_end_timestamp(void)
  349. {
  350. DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
  351. .flags = BPF_F_TEST_RUN_ON_CPU,
  352. );
  353. int prog_fd = bpf_program__fd(skel->progs.end_timestamp);
  354. bpf_prog_test_run_opts(prog_fd, &opts);
  355. }
  356. static void update_lock_stat(int map_fd, int pid, u64 end_ts,
  357. enum lock_aggr_mode aggr_mode,
  358. struct tstamp_data *ts_data)
  359. {
  360. u64 delta;
  361. struct contention_key stat_key = {};
  362. struct contention_data stat_data;
  363. if (ts_data->timestamp >= end_ts)
  364. return;
  365. delta = end_ts - ts_data->timestamp;
  366. switch (aggr_mode) {
  367. case LOCK_AGGR_CALLER:
  368. stat_key.stack_id = ts_data->stack_id;
  369. break;
  370. case LOCK_AGGR_TASK:
  371. stat_key.pid = pid;
  372. break;
  373. case LOCK_AGGR_ADDR:
  374. stat_key.lock_addr_or_cgroup = ts_data->lock;
  375. break;
  376. case LOCK_AGGR_CGROUP:
  377. /* TODO */
  378. return;
  379. default:
  380. return;
  381. }
  382. if (bpf_map_lookup_elem(map_fd, &stat_key, &stat_data) < 0)
  383. return;
  384. stat_data.total_time += delta;
  385. stat_data.count++;
  386. if (delta > stat_data.max_time)
  387. stat_data.max_time = delta;
  388. if (delta < stat_data.min_time)
  389. stat_data.min_time = delta;
  390. bpf_map_update_elem(map_fd, &stat_key, &stat_data, BPF_EXIST);
  391. }
  392. /*
  393. * Account entries in the tstamp map (which didn't see the corresponding
  394. * lock:contention_end tracepoint) using end_ts.
  395. */
  396. static void account_end_timestamp(struct lock_contention *con)
  397. {
  398. int ts_fd, stat_fd;
  399. int *prev_key, key;
  400. u64 end_ts = skel->bss->end_ts;
  401. int total_cpus;
  402. enum lock_aggr_mode aggr_mode = con->aggr_mode;
  403. struct tstamp_data ts_data, *cpu_data;
  404. /* Iterate per-task tstamp map (key = TID) */
  405. ts_fd = bpf_map__fd(skel->maps.tstamp);
  406. stat_fd = bpf_map__fd(skel->maps.lock_stat);
  407. prev_key = NULL;
  408. while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) {
  409. if (bpf_map_lookup_elem(ts_fd, &key, &ts_data) == 0) {
  410. int pid = key;
  411. if (aggr_mode == LOCK_AGGR_TASK && con->owner)
  412. pid = ts_data.flags;
  413. update_lock_stat(stat_fd, pid, end_ts, aggr_mode,
  414. &ts_data);
  415. }
  416. prev_key = &key;
  417. }
  418. /* Now it'll check per-cpu tstamp map which doesn't have TID. */
  419. if (aggr_mode == LOCK_AGGR_TASK || aggr_mode == LOCK_AGGR_CGROUP)
  420. return;
  421. total_cpus = cpu__max_cpu().cpu;
  422. ts_fd = bpf_map__fd(skel->maps.tstamp_cpu);
  423. cpu_data = calloc(total_cpus, sizeof(*cpu_data));
  424. if (cpu_data == NULL)
  425. return;
  426. prev_key = NULL;
  427. while (!bpf_map_get_next_key(ts_fd, prev_key, &key)) {
  428. if (bpf_map_lookup_elem(ts_fd, &key, cpu_data) < 0)
  429. goto next;
  430. for (int i = 0; i < total_cpus; i++) {
  431. if (cpu_data[i].lock == 0)
  432. continue;
  433. update_lock_stat(stat_fd, -1, end_ts, aggr_mode,
  434. &cpu_data[i]);
  435. }
  436. next:
  437. prev_key = &key;
  438. }
  439. free(cpu_data);
  440. }
  441. int lock_contention_start(void)
  442. {
  443. skel->bss->enabled = 1;
  444. return 0;
  445. }
  446. int lock_contention_stop(void)
  447. {
  448. skel->bss->enabled = 0;
  449. mark_end_timestamp();
  450. return 0;
  451. }
  452. static const char *lock_contention_get_name(struct lock_contention *con,
  453. struct contention_key *key,
  454. u64 *stack_trace, u32 flags)
  455. {
  456. int idx = 0;
  457. u64 addr;
  458. static char name_buf[KSYM_NAME_LEN];
  459. struct symbol *sym;
  460. struct map *kmap;
  461. struct machine *machine = con->machine;
  462. if (con->aggr_mode == LOCK_AGGR_TASK) {
  463. struct contention_task_data task;
  464. int pid = key->pid;
  465. int task_fd = bpf_map__fd(skel->maps.task_data);
  466. /* do not update idle comm which contains CPU number */
  467. if (pid) {
  468. struct thread *t = machine__findnew_thread(machine, /*pid=*/-1, pid);
  469. if (t != NULL &&
  470. !bpf_map_lookup_elem(task_fd, &pid, &task) &&
  471. thread__set_comm(t, task.comm, /*timestamp=*/0)) {
  472. snprintf(name_buf, sizeof(name_buf), "%s", task.comm);
  473. return name_buf;
  474. }
  475. }
  476. return "";
  477. }
  478. if (con->aggr_mode == LOCK_AGGR_ADDR) {
  479. int lock_fd = bpf_map__fd(skel->maps.lock_syms);
  480. struct slab_cache_data *slab_data;
  481. /* per-process locks set upper bits of the flags */
  482. if (flags & LCD_F_MMAP_LOCK)
  483. return "mmap_lock";
  484. if (flags & LCD_F_SIGHAND_LOCK)
  485. return "siglock";
  486. /* global locks with symbols */
  487. sym = machine__find_kernel_symbol(machine, key->lock_addr_or_cgroup, &kmap);
  488. if (sym)
  489. return sym->name;
  490. /* try semi-global locks collected separately */
  491. if (!bpf_map_lookup_elem(lock_fd, &key->lock_addr_or_cgroup, &flags)) {
  492. if (flags == LOCK_CLASS_RQLOCK)
  493. return "rq_lock";
  494. }
  495. if (!bpf_map_lookup_elem(lock_fd, &key->lock_addr_or_cgroup, &flags)) {
  496. if (flags == LOCK_CLASS_ZONE_LOCK)
  497. return "zone_lock";
  498. }
  499. /* look slab_hash for dynamic locks in a slab object */
  500. if (hashmap__find(&slab_hash, flags & LCB_F_SLAB_ID_MASK, &slab_data)) {
  501. snprintf(name_buf, sizeof(name_buf), "&%s", slab_data->name);
  502. return name_buf;
  503. }
  504. return "";
  505. }
  506. if (con->aggr_mode == LOCK_AGGR_CGROUP) {
  507. u64 cgrp_id = key->lock_addr_or_cgroup;
  508. struct cgroup *cgrp = __cgroup__find(&con->cgroups, cgrp_id);
  509. if (cgrp)
  510. return cgrp->name;
  511. snprintf(name_buf, sizeof(name_buf), "cgroup:%" PRIu64 "", cgrp_id);
  512. return name_buf;
  513. }
  514. /* LOCK_AGGR_CALLER: skip lock internal functions */
  515. while (machine__is_lock_function(machine, stack_trace[idx]) &&
  516. idx < con->max_stack - 1)
  517. idx++;
  518. addr = stack_trace[idx];
  519. sym = machine__find_kernel_symbol(machine, addr, &kmap);
  520. if (sym) {
  521. unsigned long offset;
  522. offset = map__map_ip(kmap, addr) - sym->start;
  523. if (offset == 0)
  524. return sym->name;
  525. snprintf(name_buf, sizeof(name_buf), "%s+%#lx", sym->name, offset);
  526. } else {
  527. snprintf(name_buf, sizeof(name_buf), "%#lx", (unsigned long)addr);
  528. }
  529. return name_buf;
  530. }
  531. struct lock_stat *pop_owner_stack_trace(struct lock_contention *con)
  532. {
  533. int stacks_fd, stat_fd;
  534. u64 *stack_trace = NULL;
  535. s32 stack_id;
  536. struct contention_key ckey = {};
  537. struct contention_data cdata = {};
  538. size_t stack_size = con->max_stack * sizeof(*stack_trace);
  539. struct lock_stat *st = NULL;
  540. stacks_fd = bpf_map__fd(skel->maps.owner_stacks);
  541. stat_fd = bpf_map__fd(skel->maps.owner_stat);
  542. if (!stacks_fd || !stat_fd)
  543. goto out_err;
  544. stack_trace = zalloc(stack_size);
  545. if (stack_trace == NULL)
  546. goto out_err;
  547. if (bpf_map_get_next_key(stacks_fd, NULL, stack_trace))
  548. goto out_err;
  549. bpf_map_lookup_elem(stacks_fd, stack_trace, &stack_id);
  550. ckey.stack_id = stack_id;
  551. bpf_map_lookup_elem(stat_fd, &ckey, &cdata);
  552. st = zalloc(sizeof(struct lock_stat));
  553. if (!st)
  554. goto out_err;
  555. st->name = strdup(stack_trace[0] ? lock_contention_get_name(con, NULL, stack_trace, 0) :
  556. "unknown");
  557. if (!st->name)
  558. goto out_err;
  559. st->flags = cdata.flags;
  560. st->nr_contended = cdata.count;
  561. st->wait_time_total = cdata.total_time;
  562. st->wait_time_max = cdata.max_time;
  563. st->wait_time_min = cdata.min_time;
  564. st->callstack = stack_trace;
  565. if (cdata.count)
  566. st->avg_wait_time = cdata.total_time / cdata.count;
  567. bpf_map_delete_elem(stacks_fd, stack_trace);
  568. bpf_map_delete_elem(stat_fd, &ckey);
  569. return st;
  570. out_err:
  571. free(stack_trace);
  572. free(st);
  573. return NULL;
  574. }
  575. int lock_contention_read(struct lock_contention *con)
  576. {
  577. int fd, stack, err = 0;
  578. struct contention_key *prev_key, key = {};
  579. struct contention_data data = {};
  580. struct lock_stat *st = NULL;
  581. struct machine *machine = con->machine;
  582. u64 *stack_trace;
  583. size_t stack_size = con->max_stack * sizeof(*stack_trace);
  584. fd = bpf_map__fd(skel->maps.lock_stat);
  585. stack = bpf_map__fd(skel->maps.stacks);
  586. con->fails.task = skel->bss->task_fail;
  587. con->fails.stack = skel->bss->stack_fail;
  588. con->fails.time = skel->bss->time_fail;
  589. con->fails.data = skel->bss->data_fail;
  590. stack_trace = zalloc(stack_size);
  591. if (stack_trace == NULL)
  592. return -1;
  593. account_end_timestamp(con);
  594. if (con->aggr_mode == LOCK_AGGR_TASK) {
  595. struct thread *idle = machine__findnew_thread(machine,
  596. /*pid=*/0,
  597. /*tid=*/0);
  598. thread__set_comm(idle, "swapper", /*timestamp=*/0);
  599. }
  600. if (con->aggr_mode == LOCK_AGGR_ADDR) {
  601. DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts,
  602. .flags = BPF_F_TEST_RUN_ON_CPU,
  603. );
  604. int prog_fd = bpf_program__fd(skel->progs.collect_lock_syms);
  605. bpf_prog_test_run_opts(prog_fd, &opts);
  606. }
  607. prev_key = NULL;
  608. while (!bpf_map_get_next_key(fd, prev_key, &key)) {
  609. s64 ls_key;
  610. const char *name;
  611. /* to handle errors in the loop body */
  612. err = -1;
  613. bpf_map_lookup_elem(fd, &key, &data);
  614. if (con->save_callstack) {
  615. bpf_map_lookup_elem(stack, &key.stack_id, stack_trace);
  616. if (!match_callstack_filter(machine, stack_trace, con->max_stack)) {
  617. con->nr_filtered += data.count;
  618. goto next;
  619. }
  620. }
  621. switch (con->aggr_mode) {
  622. case LOCK_AGGR_CALLER:
  623. ls_key = key.stack_id;
  624. break;
  625. case LOCK_AGGR_TASK:
  626. ls_key = key.pid;
  627. break;
  628. case LOCK_AGGR_ADDR:
  629. case LOCK_AGGR_CGROUP:
  630. ls_key = key.lock_addr_or_cgroup;
  631. break;
  632. default:
  633. goto next;
  634. }
  635. st = lock_stat_find(ls_key);
  636. if (st != NULL) {
  637. st->wait_time_total += data.total_time;
  638. if (st->wait_time_max < data.max_time)
  639. st->wait_time_max = data.max_time;
  640. if (st->wait_time_min > data.min_time)
  641. st->wait_time_min = data.min_time;
  642. st->nr_contended += data.count;
  643. if (st->nr_contended)
  644. st->avg_wait_time = st->wait_time_total / st->nr_contended;
  645. goto next;
  646. }
  647. name = lock_contention_get_name(con, &key, stack_trace, data.flags);
  648. st = lock_stat_findnew(ls_key, name, data.flags);
  649. if (st == NULL)
  650. break;
  651. st->nr_contended = data.count;
  652. st->wait_time_total = data.total_time;
  653. st->wait_time_max = data.max_time;
  654. st->wait_time_min = data.min_time;
  655. if (data.count)
  656. st->avg_wait_time = data.total_time / data.count;
  657. if (con->aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
  658. st->callstack = memdup(stack_trace, stack_size);
  659. if (st->callstack == NULL)
  660. break;
  661. }
  662. next:
  663. prev_key = &key;
  664. /* we're fine now, reset the error */
  665. err = 0;
  666. }
  667. free(stack_trace);
  668. return err;
  669. }
  670. int lock_contention_finish(struct lock_contention *con)
  671. {
  672. if (skel) {
  673. skel->bss->enabled = 0;
  674. lock_contention_bpf__destroy(skel);
  675. }
  676. while (!RB_EMPTY_ROOT(&con->cgroups)) {
  677. struct rb_node *node = rb_first(&con->cgroups);
  678. struct cgroup *cgrp = rb_entry(node, struct cgroup, node);
  679. rb_erase(node, &con->cgroups);
  680. cgroup__put(cgrp);
  681. }
  682. exit_slab_cache_iter();
  683. btf__free(con->btf);
  684. return 0;
  685. }