1 // SPDX-License-Identifier: GPL-2.0
7 #include "util/evlist.h" // for struct evsel_str_handler
8 #include "util/evsel.h"
9 #include "util/symbol.h"
10 #include "util/thread.h"
11 #include "util/header.h"
12 #include "util/target.h"
13 #include "util/cgroup.h"
14 #include "util/callchain.h"
15 #include "util/lock-contention.h"
16 #include "util/bpf_skel/lock_data.h"
18 #include <subcmd/pager.h>
19 #include <subcmd/parse-options.h>
20 #include "util/trace-event.h"
21 #include "util/tracepoint.h"
23 #include "util/debug.h"
24 #include "util/session.h"
25 #include "util/tool.h"
26 #include "util/data.h"
27 #include "util/string2.h"
29 #include "util/util.h"
32 #include <sys/types.h>
33 #include <sys/prctl.h>
34 #include <semaphore.h>
39 #include <linux/list.h>
40 #include <linux/hash.h>
41 #include <linux/kernel.h>
42 #include <linux/zalloc.h>
43 #include <linux/err.h>
44 #include <linux/stringify.h>
46 static struct perf_session *session;
47 static struct target target;
49 /* based on kernel/lockdep.c */
50 #define LOCKHASH_BITS 12
51 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
53 static struct hlist_head *lockhash_table;
55 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
56 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
58 static struct rb_root thread_stats;
60 static bool combine_locks;
61 static bool show_thread_stats;
62 static bool show_lock_addrs;
63 static bool show_lock_owner;
64 static bool show_lock_cgroups;
66 static unsigned long bpf_map_entries = MAX_ENTRIES;
67 static int max_stack_depth = CONTENTION_STACK_DEPTH;
68 static int stack_skip = CONTENTION_STACK_SKIP;
69 static int print_nr_entries = INT_MAX / 2;
70 static LIST_HEAD(callstack_filters);
71 static const char *output_name = NULL;
72 static FILE *lock_output;
74 struct callstack_filter {
75 struct list_head list;
79 static struct lock_filter filters;
81 static enum lock_aggr_mode aggr_mode = LOCK_AGGR_ADDR;
83 static bool needs_callstack(void)
85 return !list_empty(&callstack_filters);
88 static struct thread_stat *thread_stat_find(u32 tid)
91 struct thread_stat *st;
93 node = thread_stats.rb_node;
95 st = container_of(node, struct thread_stat, rb);
98 else if (tid < st->tid)
101 node = node->rb_right;
107 static void thread_stat_insert(struct thread_stat *new)
109 struct rb_node **rb = &thread_stats.rb_node;
110 struct rb_node *parent = NULL;
111 struct thread_stat *p;
114 p = container_of(*rb, struct thread_stat, rb);
117 if (new->tid < p->tid)
118 rb = &(*rb)->rb_left;
119 else if (new->tid > p->tid)
120 rb = &(*rb)->rb_right;
122 BUG_ON("inserting invalid thread_stat\n");
125 rb_link_node(&new->rb, parent, rb);
126 rb_insert_color(&new->rb, &thread_stats);
129 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
131 struct thread_stat *st;
133 st = thread_stat_find(tid);
137 st = zalloc(sizeof(struct thread_stat));
139 pr_err("memory allocation failed\n");
144 INIT_LIST_HEAD(&st->seq_list);
146 thread_stat_insert(st);
151 static struct thread_stat *thread_stat_findnew_first(u32 tid);
152 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
153 thread_stat_findnew_first;
155 static struct thread_stat *thread_stat_findnew_first(u32 tid)
157 struct thread_stat *st;
159 st = zalloc(sizeof(struct thread_stat));
161 pr_err("memory allocation failed\n");
165 INIT_LIST_HEAD(&st->seq_list);
167 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
168 rb_insert_color(&st->rb, &thread_stats);
170 thread_stat_findnew = thread_stat_findnew_after_first;
174 /* build simple key function one is bigger than two */
175 #define SINGLE_KEY(member) \
176 static int lock_stat_key_ ## member(struct lock_stat *one, \
177 struct lock_stat *two) \
179 return one->member > two->member; \
182 SINGLE_KEY(nr_acquired)
183 SINGLE_KEY(nr_contended)
184 SINGLE_KEY(avg_wait_time)
185 SINGLE_KEY(wait_time_total)
186 SINGLE_KEY(wait_time_max)
188 static int lock_stat_key_wait_time_min(struct lock_stat *one,
189 struct lock_stat *two)
191 u64 s1 = one->wait_time_min;
192 u64 s2 = two->wait_time_min;
193 if (s1 == ULLONG_MAX)
195 if (s2 == ULLONG_MAX)
202 * name: the value for specify by user
203 * this should be simpler than raw name of member
204 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
207 /* header: the string printed on the header line */
209 /* len: the printing width of the field */
211 /* key: a pointer to function to compare two lock stats for sorting */
212 int (*key)(struct lock_stat*, struct lock_stat*);
213 /* print: a pointer to function to print a given lock stats */
214 void (*print)(struct lock_key*, struct lock_stat*);
215 /* list: list entry to link this */
216 struct list_head list;
219 static void lock_stat_key_print_time(unsigned long long nsec, int len)
221 static const struct {
225 { 1e9 * 3600, "h " },
235 fprintf(lock_output, "%llu", nsec);
239 for (int i = 0; table[i].unit; i++) {
240 if (nsec < table[i].base)
243 fprintf(lock_output, "%*.2f %s", len - 3, nsec / table[i].base, table[i].unit);
247 fprintf(lock_output, "%*llu %s", len - 3, nsec, "ns");
250 #define PRINT_KEY(member) \
251 static void lock_stat_key_print_ ## member(struct lock_key *key, \
252 struct lock_stat *ls) \
254 fprintf(lock_output, "%*llu", key->len, (unsigned long long)ls->member);\
257 #define PRINT_TIME(member) \
258 static void lock_stat_key_print_ ## member(struct lock_key *key, \
259 struct lock_stat *ls) \
261 lock_stat_key_print_time((unsigned long long)ls->member, key->len); \
264 PRINT_KEY(nr_acquired)
265 PRINT_KEY(nr_contended)
266 PRINT_TIME(avg_wait_time)
267 PRINT_TIME(wait_time_total)
268 PRINT_TIME(wait_time_max)
270 static void lock_stat_key_print_wait_time_min(struct lock_key *key,
271 struct lock_stat *ls)
273 u64 wait_time = ls->wait_time_min;
275 if (wait_time == ULLONG_MAX)
278 lock_stat_key_print_time(wait_time, key->len);
282 static const char *sort_key = "acquired";
284 static int (*compare)(struct lock_stat *, struct lock_stat *);
286 static struct rb_root sorted; /* place to store intermediate data */
287 static struct rb_root result; /* place to store sorted data */
289 static LIST_HEAD(lock_keys);
290 static const char *output_fields;
292 #define DEF_KEY_LOCK(name, header, fn_suffix, len) \
293 { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
294 static struct lock_key report_keys[] = {
295 DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
296 DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
297 DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
298 DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
299 DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
300 DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
302 /* extra comparisons much complicated should be here */
306 static struct lock_key contention_keys[] = {
307 DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
308 DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
309 DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
310 DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
311 DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
313 /* extra comparisons much complicated should be here */
317 static int select_key(bool contention)
320 struct lock_key *keys = report_keys;
323 keys = contention_keys;
325 for (i = 0; keys[i].name; i++) {
326 if (!strcmp(keys[i].name, sort_key)) {
327 compare = keys[i].key;
329 /* selected key should be in the output fields */
330 if (list_empty(&keys[i].list))
331 list_add_tail(&keys[i].list, &lock_keys);
337 pr_err("Unknown compare key: %s\n", sort_key);
341 static int add_output_field(bool contention, char *name)
344 struct lock_key *keys = report_keys;
347 keys = contention_keys;
349 for (i = 0; keys[i].name; i++) {
350 if (strcmp(keys[i].name, name))
353 /* prevent double link */
354 if (list_empty(&keys[i].list))
355 list_add_tail(&keys[i].list, &lock_keys);
360 pr_err("Unknown output field: %s\n", name);
364 static int setup_output_field(bool contention, const char *str)
366 char *tok, *tmp, *orig;
368 struct lock_key *keys = report_keys;
371 keys = contention_keys;
373 /* no output field given: use all of them */
375 for (i = 0; keys[i].name; i++)
376 list_add_tail(&keys[i].list, &lock_keys);
380 for (i = 0; keys[i].name; i++)
381 INIT_LIST_HEAD(&keys[i].list);
383 orig = tmp = strdup(str);
387 while ((tok = strsep(&tmp, ",")) != NULL){
388 ret = add_output_field(contention, tok);
397 static void combine_lock_stats(struct lock_stat *st)
399 struct rb_node **rb = &sorted.rb_node;
400 struct rb_node *parent = NULL;
405 p = container_of(*rb, struct lock_stat, rb);
408 if (st->name && p->name)
409 ret = strcmp(st->name, p->name);
411 ret = !!st->name - !!p->name;
414 p->nr_acquired += st->nr_acquired;
415 p->nr_contended += st->nr_contended;
416 p->wait_time_total += st->wait_time_total;
419 p->avg_wait_time = p->wait_time_total / p->nr_contended;
421 if (p->wait_time_min > st->wait_time_min)
422 p->wait_time_min = st->wait_time_min;
423 if (p->wait_time_max < st->wait_time_max)
424 p->wait_time_max = st->wait_time_max;
426 p->broken |= st->broken;
432 rb = &(*rb)->rb_left;
434 rb = &(*rb)->rb_right;
437 rb_link_node(&st->rb, parent, rb);
438 rb_insert_color(&st->rb, &sorted);
441 static void insert_to_result(struct lock_stat *st,
442 int (*bigger)(struct lock_stat *, struct lock_stat *))
444 struct rb_node **rb = &result.rb_node;
445 struct rb_node *parent = NULL;
448 if (combine_locks && st->combined)
452 p = container_of(*rb, struct lock_stat, rb);
456 rb = &(*rb)->rb_left;
458 rb = &(*rb)->rb_right;
461 rb_link_node(&st->rb, parent, rb);
462 rb_insert_color(&st->rb, &result);
465 /* returns left most element of result, and erase it */
466 static struct lock_stat *pop_from_result(void)
468 struct rb_node *node = result.rb_node;
473 while (node->rb_left)
474 node = node->rb_left;
476 rb_erase(node, &result);
477 return container_of(node, struct lock_stat, rb);
480 struct lock_stat *lock_stat_find(u64 addr)
482 struct hlist_head *entry = lockhashentry(addr);
483 struct lock_stat *ret;
485 hlist_for_each_entry(ret, entry, hash_entry) {
486 if (ret->addr == addr)
492 struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags)
494 struct hlist_head *entry = lockhashentry(addr);
495 struct lock_stat *ret, *new;
497 hlist_for_each_entry(ret, entry, hash_entry) {
498 if (ret->addr == addr)
502 new = zalloc(sizeof(struct lock_stat));
507 new->name = strdup(name);
514 new->wait_time_min = ULLONG_MAX;
516 hlist_add_head(&new->hash_entry, entry);
520 pr_err("memory allocation failed\n");
524 bool match_callstack_filter(struct machine *machine, u64 *callstack)
529 const char *arch = perf_env__arch(machine->env);
531 if (list_empty(&callstack_filters))
534 for (int i = 0; i < max_stack_depth; i++) {
535 struct callstack_filter *filter;
538 * In powerpc, the callchain saved by kernel always includes
539 * first three entries as the NIP (next instruction pointer),
540 * LR (link register), and the contents of LR save area in the
541 * second stack frame. In certain scenarios its possible to have
542 * invalid kernel instruction addresses in either LR or the second
543 * stack frame's LR. In that case, kernel will store that address as
546 * The below check will continue to look into callstack,
547 * incase first or second callstack index entry has 0
548 * address for powerpc.
550 if (!callstack || (!callstack[i] && (strcmp(arch, "powerpc") ||
551 (i != 1 && i != 2))))
555 sym = machine__find_kernel_symbol(machine, ip, &kmap);
559 list_for_each_entry(filter, &callstack_filters, list) {
560 if (strstr(sym->name, filter->name))
567 struct trace_lock_handler {
568 /* it's used on CONFIG_LOCKDEP */
569 int (*acquire_event)(struct evsel *evsel,
570 struct perf_sample *sample);
572 /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
573 int (*acquired_event)(struct evsel *evsel,
574 struct perf_sample *sample);
576 /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
577 int (*contended_event)(struct evsel *evsel,
578 struct perf_sample *sample);
580 /* it's used on CONFIG_LOCKDEP */
581 int (*release_event)(struct evsel *evsel,
582 struct perf_sample *sample);
584 /* it's used when CONFIG_LOCKDEP is off */
585 int (*contention_begin_event)(struct evsel *evsel,
586 struct perf_sample *sample);
588 /* it's used when CONFIG_LOCKDEP is off */
589 int (*contention_end_event)(struct evsel *evsel,
590 struct perf_sample *sample);
593 static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
595 struct lock_seq_stat *seq;
597 list_for_each_entry(seq, &ts->seq_list, list) {
598 if (seq->addr == addr)
602 seq = zalloc(sizeof(struct lock_seq_stat));
604 pr_err("memory allocation failed\n");
607 seq->state = SEQ_STATE_UNINITIALIZED;
610 list_add(&seq->list, &ts->seq_list);
622 static int bad_hist[BROKEN_MAX];
629 static int get_key_by_aggr_mode_simple(u64 *key, u64 addr, u32 tid)
638 case LOCK_AGGR_CALLER:
639 case LOCK_AGGR_CGROUP:
641 pr_err("Invalid aggregation mode: %d\n", aggr_mode);
647 static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample);
649 static int get_key_by_aggr_mode(u64 *key, u64 addr, struct evsel *evsel,
650 struct perf_sample *sample)
652 if (aggr_mode == LOCK_AGGR_CALLER) {
653 *key = callchain_id(evsel, sample);
656 return get_key_by_aggr_mode_simple(key, addr, sample->tid);
659 static int report_lock_acquire_event(struct evsel *evsel,
660 struct perf_sample *sample)
662 struct lock_stat *ls;
663 struct thread_stat *ts;
664 struct lock_seq_stat *seq;
665 const char *name = evsel__strval(evsel, sample, "name");
666 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
667 int flag = evsel__intval(evsel, sample, "flags");
671 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
675 ls = lock_stat_findnew(key, name, 0);
679 ts = thread_stat_findnew(sample->tid);
683 seq = get_seq(ts, addr);
687 switch (seq->state) {
688 case SEQ_STATE_UNINITIALIZED:
689 case SEQ_STATE_RELEASED:
691 seq->state = SEQ_STATE_ACQUIRING;
695 if (flag & READ_LOCK)
697 seq->state = SEQ_STATE_READ_ACQUIRED;
702 case SEQ_STATE_READ_ACQUIRED:
703 if (flag & READ_LOCK) {
711 case SEQ_STATE_ACQUIRED:
712 case SEQ_STATE_ACQUIRING:
713 case SEQ_STATE_CONTENDED:
715 /* broken lock sequence */
718 bad_hist[BROKEN_ACQUIRE]++;
720 list_del_init(&seq->list);
724 BUG_ON("Unknown state of lock sequence found!\n");
729 seq->prev_event_time = sample->time;
734 static int report_lock_acquired_event(struct evsel *evsel,
735 struct perf_sample *sample)
737 struct lock_stat *ls;
738 struct thread_stat *ts;
739 struct lock_seq_stat *seq;
741 const char *name = evsel__strval(evsel, sample, "name");
742 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
746 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
750 ls = lock_stat_findnew(key, name, 0);
754 ts = thread_stat_findnew(sample->tid);
758 seq = get_seq(ts, addr);
762 switch (seq->state) {
763 case SEQ_STATE_UNINITIALIZED:
764 /* orphan event, do nothing */
766 case SEQ_STATE_ACQUIRING:
768 case SEQ_STATE_CONTENDED:
769 contended_term = sample->time - seq->prev_event_time;
770 ls->wait_time_total += contended_term;
771 if (contended_term < ls->wait_time_min)
772 ls->wait_time_min = contended_term;
773 if (ls->wait_time_max < contended_term)
774 ls->wait_time_max = contended_term;
776 case SEQ_STATE_RELEASED:
777 case SEQ_STATE_ACQUIRED:
778 case SEQ_STATE_READ_ACQUIRED:
779 /* broken lock sequence */
782 bad_hist[BROKEN_ACQUIRED]++;
784 list_del_init(&seq->list);
788 BUG_ON("Unknown state of lock sequence found!\n");
792 seq->state = SEQ_STATE_ACQUIRED;
794 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
795 seq->prev_event_time = sample->time;
800 static int report_lock_contended_event(struct evsel *evsel,
801 struct perf_sample *sample)
803 struct lock_stat *ls;
804 struct thread_stat *ts;
805 struct lock_seq_stat *seq;
806 const char *name = evsel__strval(evsel, sample, "name");
807 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
811 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
815 ls = lock_stat_findnew(key, name, 0);
819 ts = thread_stat_findnew(sample->tid);
823 seq = get_seq(ts, addr);
827 switch (seq->state) {
828 case SEQ_STATE_UNINITIALIZED:
829 /* orphan event, do nothing */
831 case SEQ_STATE_ACQUIRING:
833 case SEQ_STATE_RELEASED:
834 case SEQ_STATE_ACQUIRED:
835 case SEQ_STATE_READ_ACQUIRED:
836 case SEQ_STATE_CONTENDED:
837 /* broken lock sequence */
840 bad_hist[BROKEN_CONTENDED]++;
842 list_del_init(&seq->list);
846 BUG_ON("Unknown state of lock sequence found!\n");
850 seq->state = SEQ_STATE_CONTENDED;
852 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
853 seq->prev_event_time = sample->time;
858 static int report_lock_release_event(struct evsel *evsel,
859 struct perf_sample *sample)
861 struct lock_stat *ls;
862 struct thread_stat *ts;
863 struct lock_seq_stat *seq;
864 const char *name = evsel__strval(evsel, sample, "name");
865 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
869 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
873 ls = lock_stat_findnew(key, name, 0);
877 ts = thread_stat_findnew(sample->tid);
881 seq = get_seq(ts, addr);
885 switch (seq->state) {
886 case SEQ_STATE_UNINITIALIZED:
888 case SEQ_STATE_ACQUIRED:
890 case SEQ_STATE_READ_ACQUIRED:
892 BUG_ON(seq->read_count < 0);
893 if (seq->read_count) {
898 case SEQ_STATE_ACQUIRING:
899 case SEQ_STATE_CONTENDED:
900 case SEQ_STATE_RELEASED:
901 /* broken lock sequence */
904 bad_hist[BROKEN_RELEASE]++;
908 BUG_ON("Unknown state of lock sequence found!\n");
914 list_del_init(&seq->list);
920 static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,
925 if (map == NULL || sym == NULL) {
930 offset = map__map_ip(map, ip) - sym->start;
933 return scnprintf(buf, size, "%s+%#lx", sym->name, offset);
935 return strlcpy(buf, sym->name, size);
937 static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sample,
940 struct thread *thread;
941 struct callchain_cursor *cursor;
942 struct machine *machine = &session->machines.host;
947 /* lock names will be replaced to task name later */
948 if (show_thread_stats)
951 thread = machine__findnew_thread(machine, -1, sample->pid);
955 cursor = get_tls_callchain_cursor();
957 /* use caller function name from the callchain */
958 ret = thread__resolve_callchain(thread, cursor, evsel, sample,
959 NULL, NULL, max_stack_depth);
965 callchain_cursor_commit(cursor);
969 struct callchain_cursor_node *node;
971 node = callchain_cursor_current(cursor);
975 /* skip first few entries - for lock functions */
976 if (++skip <= stack_skip)
980 if (sym && !machine__is_lock_function(machine, node->ip)) {
981 get_symbol_name_offset(node->ms.map, sym, node->ip,
987 callchain_cursor_advance(cursor);
992 static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
994 struct callchain_cursor *cursor;
995 struct machine *machine = &session->machines.host;
996 struct thread *thread;
1001 thread = machine__findnew_thread(machine, -1, sample->pid);
1005 cursor = get_tls_callchain_cursor();
1006 /* use caller function name from the callchain */
1007 ret = thread__resolve_callchain(thread, cursor, evsel, sample,
1008 NULL, NULL, max_stack_depth);
1009 thread__put(thread);
1014 callchain_cursor_commit(cursor);
1017 struct callchain_cursor_node *node;
1019 node = callchain_cursor_current(cursor);
1023 /* skip first few entries - for lock functions */
1024 if (++skip <= stack_skip)
1027 if (node->ms.sym && machine__is_lock_function(machine, node->ip))
1030 hash ^= hash_long((unsigned long)node->ip, 64);
1033 callchain_cursor_advance(cursor);
1038 static u64 *get_callstack(struct perf_sample *sample, int max_stack)
1044 callstack = calloc(max_stack, sizeof(*callstack));
1045 if (callstack == NULL)
1048 for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) {
1049 u64 ip = sample->callchain->ips[i];
1051 if (ip >= PERF_CONTEXT_MAX)
1054 callstack[c++] = ip;
1059 static int report_lock_contention_begin_event(struct evsel *evsel,
1060 struct perf_sample *sample)
1062 struct lock_stat *ls;
1063 struct thread_stat *ts;
1064 struct lock_seq_stat *seq;
1065 u64 addr = evsel__intval(evsel, sample, "lock_addr");
1066 unsigned int flags = evsel__intval(evsel, sample, "flags");
1069 static bool kmap_loaded;
1070 struct machine *machine = &session->machines.host;
1074 ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1079 unsigned long *addrs;
1081 /* make sure it loads the kernel map to find lock symbols */
1082 map__load(machine__kernel_map(machine));
1085 /* convert (kernel) symbols to addresses */
1086 for (i = 0; i < filters.nr_syms; i++) {
1087 sym = machine__find_kernel_symbol_by_name(machine,
1091 pr_warning("ignore unknown symbol: %s\n",
1096 addrs = realloc(filters.addrs,
1097 (filters.nr_addrs + 1) * sizeof(*addrs));
1098 if (addrs == NULL) {
1099 pr_warning("memory allocation failure\n");
1103 addrs[filters.nr_addrs++] = map__unmap_ip(kmap, sym->start);
1104 filters.addrs = addrs;
1108 ls = lock_stat_find(key);
1111 const char *name = "";
1113 switch (aggr_mode) {
1114 case LOCK_AGGR_ADDR:
1115 sym = machine__find_kernel_symbol(machine, key, &kmap);
1119 case LOCK_AGGR_CALLER:
1121 if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0)
1124 case LOCK_AGGR_CGROUP:
1125 case LOCK_AGGR_TASK:
1130 ls = lock_stat_findnew(key, name, flags);
1135 if (filters.nr_types) {
1138 for (i = 0; i < filters.nr_types; i++) {
1139 if (flags == filters.types[i]) {
1149 if (filters.nr_addrs) {
1152 for (i = 0; i < filters.nr_addrs; i++) {
1153 if (addr == filters.addrs[i]) {
1163 if (needs_callstack()) {
1164 u64 *callstack = get_callstack(sample, max_stack_depth);
1165 if (callstack == NULL)
1168 if (!match_callstack_filter(machine, callstack)) {
1173 if (ls->callstack == NULL)
1174 ls->callstack = callstack;
1179 ts = thread_stat_findnew(sample->tid);
1183 seq = get_seq(ts, addr);
1187 switch (seq->state) {
1188 case SEQ_STATE_UNINITIALIZED:
1189 case SEQ_STATE_ACQUIRED:
1191 case SEQ_STATE_CONTENDED:
1193 * It can have nested contention begin with mutex spinning,
1194 * then we would use the original contention begin event and
1195 * ignore the second one.
1198 case SEQ_STATE_ACQUIRING:
1199 case SEQ_STATE_READ_ACQUIRED:
1200 case SEQ_STATE_RELEASED:
1201 /* broken lock sequence */
1204 bad_hist[BROKEN_CONTENDED]++;
1206 list_del_init(&seq->list);
1210 BUG_ON("Unknown state of lock sequence found!\n");
1214 if (seq->state != SEQ_STATE_CONTENDED) {
1215 seq->state = SEQ_STATE_CONTENDED;
1216 seq->prev_event_time = sample->time;
1223 static int report_lock_contention_end_event(struct evsel *evsel,
1224 struct perf_sample *sample)
1226 struct lock_stat *ls;
1227 struct thread_stat *ts;
1228 struct lock_seq_stat *seq;
1230 u64 addr = evsel__intval(evsel, sample, "lock_addr");
1234 ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1238 ls = lock_stat_find(key);
1242 ts = thread_stat_find(sample->tid);
1246 seq = get_seq(ts, addr);
1250 switch (seq->state) {
1251 case SEQ_STATE_UNINITIALIZED:
1253 case SEQ_STATE_CONTENDED:
1254 contended_term = sample->time - seq->prev_event_time;
1255 ls->wait_time_total += contended_term;
1256 if (contended_term < ls->wait_time_min)
1257 ls->wait_time_min = contended_term;
1258 if (ls->wait_time_max < contended_term)
1259 ls->wait_time_max = contended_term;
1261 case SEQ_STATE_ACQUIRING:
1262 case SEQ_STATE_ACQUIRED:
1263 case SEQ_STATE_READ_ACQUIRED:
1264 case SEQ_STATE_RELEASED:
1265 /* broken lock sequence */
1268 bad_hist[BROKEN_ACQUIRED]++;
1270 list_del_init(&seq->list);
1274 BUG_ON("Unknown state of lock sequence found!\n");
1278 seq->state = SEQ_STATE_ACQUIRED;
1280 ls->avg_wait_time = ls->wait_time_total/ls->nr_acquired;
1285 /* lock oriented handlers */
1286 /* TODO: handlers for CPU oriented, thread oriented */
1287 static struct trace_lock_handler report_lock_ops = {
1288 .acquire_event = report_lock_acquire_event,
1289 .acquired_event = report_lock_acquired_event,
1290 .contended_event = report_lock_contended_event,
1291 .release_event = report_lock_release_event,
1292 .contention_begin_event = report_lock_contention_begin_event,
1293 .contention_end_event = report_lock_contention_end_event,
1296 static struct trace_lock_handler contention_lock_ops = {
1297 .contention_begin_event = report_lock_contention_begin_event,
1298 .contention_end_event = report_lock_contention_end_event,
1302 static struct trace_lock_handler *trace_handler;
1304 static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
1306 if (trace_handler->acquire_event)
1307 return trace_handler->acquire_event(evsel, sample);
1311 static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
1313 if (trace_handler->acquired_event)
1314 return trace_handler->acquired_event(evsel, sample);
1318 static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
1320 if (trace_handler->contended_event)
1321 return trace_handler->contended_event(evsel, sample);
1325 static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
1327 if (trace_handler->release_event)
1328 return trace_handler->release_event(evsel, sample);
1332 static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample)
1334 if (trace_handler->contention_begin_event)
1335 return trace_handler->contention_begin_event(evsel, sample);
1339 static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample)
1341 if (trace_handler->contention_end_event)
1342 return trace_handler->contention_end_event(evsel, sample);
1346 static void print_bad_events(int bad, int total)
1348 /* Output for debug, this have to be removed */
1351 const char *name[4] =
1352 { "acquire", "acquired", "contended", "release" };
1354 for (i = 0; i < BROKEN_MAX; i++)
1355 broken += bad_hist[i];
1357 if (quiet || total == 0 || (broken == 0 && verbose <= 0))
1360 fprintf(lock_output, "\n=== output for debug ===\n\n");
1361 fprintf(lock_output, "bad: %d, total: %d\n", bad, total);
1362 fprintf(lock_output, "bad rate: %.2f %%\n", (double)bad / (double)total * 100);
1363 fprintf(lock_output, "histogram of events caused bad sequence\n");
1364 for (i = 0; i < BROKEN_MAX; i++)
1365 fprintf(lock_output, " %10s: %d\n", name[i], bad_hist[i]);
1368 /* TODO: various way to print, coloring, nano or milli sec */
1369 static void print_result(void)
1371 struct lock_stat *st;
1372 struct lock_key *key;
1374 int bad, total, printed;
1377 fprintf(lock_output, "%20s ", "Name");
1378 list_for_each_entry(key, &lock_keys, list)
1379 fprintf(lock_output, "%*s ", key->len, key->header);
1380 fprintf(lock_output, "\n\n");
1383 bad = total = printed = 0;
1384 while ((st = pop_from_result())) {
1388 if (!st->nr_acquired)
1391 bzero(cut_name, 20);
1393 if (strlen(st->name) < 20) {
1394 /* output raw name */
1395 const char *name = st->name;
1397 if (show_thread_stats) {
1400 /* st->addr contains tid of thread */
1401 t = perf_session__findnew(session, st->addr);
1402 name = thread__comm_str(t);
1405 fprintf(lock_output, "%20s ", name);
1407 strncpy(cut_name, st->name, 16);
1411 cut_name[19] = '\0';
1412 /* cut off name for saving output style */
1413 fprintf(lock_output, "%20s ", cut_name);
1416 list_for_each_entry(key, &lock_keys, list) {
1417 key->print(key, st);
1418 fprintf(lock_output, " ");
1420 fprintf(lock_output, "\n");
1422 if (++printed >= print_nr_entries)
1426 print_bad_events(bad, total);
1429 static bool info_threads, info_map;
1431 static void dump_threads(void)
1433 struct thread_stat *st;
1434 struct rb_node *node;
1437 fprintf(lock_output, "%10s: comm\n", "Thread ID");
1439 node = rb_first(&thread_stats);
1441 st = container_of(node, struct thread_stat, rb);
1442 t = perf_session__findnew(session, st->tid);
1443 fprintf(lock_output, "%10d: %s\n", st->tid, thread__comm_str(t));
1444 node = rb_next(node);
1449 static int compare_maps(struct lock_stat *a, struct lock_stat *b)
1453 if (a->name && b->name)
1454 ret = strcmp(a->name, b->name);
1456 ret = !!a->name - !!b->name;
1459 return a->addr < b->addr;
1464 static void dump_map(void)
1467 struct lock_stat *st;
1469 fprintf(lock_output, "Address of instance: name of class\n");
1470 for (i = 0; i < LOCKHASH_SIZE; i++) {
1471 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1472 insert_to_result(st, compare_maps);
1476 while ((st = pop_from_result()))
1477 fprintf(lock_output, " %#llx: %s\n", (unsigned long long)st->addr, st->name);
1480 static int dump_info(void)
1490 pr_err("Unknown type of information\n");
1496 static const struct evsel_str_handler lock_tracepoints[] = {
1497 { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
1498 { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1499 { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1500 { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
1503 static const struct evsel_str_handler contention_tracepoints[] = {
1504 { "lock:contention_begin", evsel__process_contention_begin, },
1505 { "lock:contention_end", evsel__process_contention_end, },
1508 static int process_event_update(struct perf_tool *tool,
1509 union perf_event *event,
1510 struct evlist **pevlist)
1514 ret = perf_event__process_event_update(tool, event, pevlist);
1518 /* this can return -EEXIST since we call it for each evsel */
1519 perf_session__set_tracepoints_handlers(session, lock_tracepoints);
1520 perf_session__set_tracepoints_handlers(session, contention_tracepoints);
1524 typedef int (*tracepoint_handler)(struct evsel *evsel,
1525 struct perf_sample *sample);
1527 static int process_sample_event(struct perf_tool *tool __maybe_unused,
1528 union perf_event *event,
1529 struct perf_sample *sample,
1530 struct evsel *evsel,
1531 struct machine *machine)
1534 struct thread *thread = machine__findnew_thread(machine, sample->pid,
1537 if (thread == NULL) {
1538 pr_debug("problem processing %d event, skipping it.\n",
1539 event->header.type);
1543 if (evsel->handler != NULL) {
1544 tracepoint_handler f = evsel->handler;
1545 err = f(evsel, sample);
1548 thread__put(thread);
1553 static void combine_result(void)
1556 struct lock_stat *st;
1561 for (i = 0; i < LOCKHASH_SIZE; i++) {
1562 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1563 combine_lock_stats(st);
1568 static void sort_result(void)
1571 struct lock_stat *st;
1573 for (i = 0; i < LOCKHASH_SIZE; i++) {
1574 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1575 insert_to_result(st, compare);
1580 static const struct {
1584 } lock_type_table[] = {
1585 { 0, "semaphore", "semaphore" },
1586 { LCB_F_SPIN, "spinlock", "spinlock" },
1587 { LCB_F_SPIN | LCB_F_READ, "rwlock:R", "rwlock" },
1588 { LCB_F_SPIN | LCB_F_WRITE, "rwlock:W", "rwlock" },
1589 { LCB_F_READ, "rwsem:R", "rwsem" },
1590 { LCB_F_WRITE, "rwsem:W", "rwsem" },
1591 { LCB_F_RT, "rt-mutex", "rt-mutex" },
1592 { LCB_F_RT | LCB_F_READ, "rwlock-rt:R", "rwlock-rt" },
1593 { LCB_F_RT | LCB_F_WRITE, "rwlock-rt:W", "rwlock-rt" },
1594 { LCB_F_PERCPU | LCB_F_READ, "pcpu-sem:R", "percpu-rwsem" },
1595 { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W", "percpu-rwsem" },
1596 { LCB_F_MUTEX, "mutex", "mutex" },
1597 { LCB_F_MUTEX | LCB_F_SPIN, "mutex", "mutex" },
1598 /* alias for get_type_flag() */
1599 { LCB_F_MUTEX | LCB_F_SPIN, "mutex-spin", "mutex" },
1602 static const char *get_type_str(unsigned int flags)
1604 flags &= LCB_F_MAX_FLAGS - 1;
1606 for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1607 if (lock_type_table[i].flags == flags)
1608 return lock_type_table[i].str;
1613 static const char *get_type_name(unsigned int flags)
1615 flags &= LCB_F_MAX_FLAGS - 1;
1617 for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1618 if (lock_type_table[i].flags == flags)
1619 return lock_type_table[i].name;
1624 static unsigned int get_type_flag(const char *str)
1626 for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1627 if (!strcmp(lock_type_table[i].name, str))
1628 return lock_type_table[i].flags;
1630 for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1631 if (!strcmp(lock_type_table[i].str, str))
1632 return lock_type_table[i].flags;
1637 static void lock_filter_finish(void)
1639 zfree(&filters.types);
1640 filters.nr_types = 0;
1642 zfree(&filters.addrs);
1643 filters.nr_addrs = 0;
1645 for (int i = 0; i < filters.nr_syms; i++)
1646 free(filters.syms[i]);
1648 zfree(&filters.syms);
1649 filters.nr_syms = 0;
1651 zfree(&filters.cgrps);
1652 filters.nr_cgrps = 0;
1655 static void sort_contention_result(void)
1660 static void print_header_stdio(void)
1662 struct lock_key *key;
1664 list_for_each_entry(key, &lock_keys, list)
1665 fprintf(lock_output, "%*s ", key->len, key->header);
1667 switch (aggr_mode) {
1668 case LOCK_AGGR_TASK:
1669 fprintf(lock_output, " %10s %s\n\n", "pid",
1670 show_lock_owner ? "owner" : "comm");
1672 case LOCK_AGGR_CALLER:
1673 fprintf(lock_output, " %10s %s\n\n", "type", "caller");
1675 case LOCK_AGGR_ADDR:
1676 fprintf(lock_output, " %16s %s\n\n", "address", "symbol");
1678 case LOCK_AGGR_CGROUP:
1679 fprintf(lock_output, " %s\n\n", "cgroup");
1686 static void print_header_csv(const char *sep)
1688 struct lock_key *key;
1690 fprintf(lock_output, "# output: ");
1691 list_for_each_entry(key, &lock_keys, list)
1692 fprintf(lock_output, "%s%s ", key->header, sep);
1694 switch (aggr_mode) {
1695 case LOCK_AGGR_TASK:
1696 fprintf(lock_output, "%s%s %s\n", "pid", sep,
1697 show_lock_owner ? "owner" : "comm");
1699 case LOCK_AGGR_CALLER:
1700 fprintf(lock_output, "%s%s %s", "type", sep, "caller");
1702 fprintf(lock_output, "%s %s", sep, "stacktrace");
1703 fprintf(lock_output, "\n");
1705 case LOCK_AGGR_ADDR:
1706 fprintf(lock_output, "%s%s %s%s %s\n", "address", sep, "symbol", sep, "type");
1708 case LOCK_AGGR_CGROUP:
1709 fprintf(lock_output, "%s\n", "cgroup");
1716 static void print_header(void)
1719 if (symbol_conf.field_sep)
1720 print_header_csv(symbol_conf.field_sep);
1722 print_header_stdio();
1726 static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat *st)
1728 struct lock_key *key;
1732 list_for_each_entry(key, &lock_keys, list) {
1733 key->print(key, st);
1734 fprintf(lock_output, " ");
1737 switch (aggr_mode) {
1738 case LOCK_AGGR_CALLER:
1739 fprintf(lock_output, " %10s %s\n", get_type_str(st->flags), st->name);
1741 case LOCK_AGGR_TASK:
1743 t = perf_session__findnew(session, pid);
1744 fprintf(lock_output, " %10d %s\n",
1745 pid, pid == -1 ? "Unknown" : thread__comm_str(t));
1747 case LOCK_AGGR_ADDR:
1748 fprintf(lock_output, " %016llx %s (%s)\n", (unsigned long long)st->addr,
1749 st->name, get_type_name(st->flags));
1751 case LOCK_AGGR_CGROUP:
1752 fprintf(lock_output, " %s\n", st->name);
1758 if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1764 for (int i = 0; i < max_stack_depth; i++) {
1765 if (!st->callstack || !st->callstack[i])
1768 ip = st->callstack[i];
1769 sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
1770 get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
1771 fprintf(lock_output, "\t\t\t%#lx %s\n", (unsigned long)ip, buf);
1776 static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *st,
1779 struct lock_key *key;
1783 list_for_each_entry(key, &lock_keys, list) {
1784 key->print(key, st);
1785 fprintf(lock_output, "%s ", sep);
1788 switch (aggr_mode) {
1789 case LOCK_AGGR_CALLER:
1790 fprintf(lock_output, "%s%s %s", get_type_str(st->flags), sep, st->name);
1792 fprintf(lock_output, "\n");
1794 case LOCK_AGGR_TASK:
1796 t = perf_session__findnew(session, pid);
1797 fprintf(lock_output, "%d%s %s\n", pid, sep,
1798 pid == -1 ? "Unknown" : thread__comm_str(t));
1800 case LOCK_AGGR_ADDR:
1801 fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
1802 st->name, sep, get_type_name(st->flags));
1804 case LOCK_AGGR_CGROUP:
1805 fprintf(lock_output, "%s\n",st->name);
1811 if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1817 for (int i = 0; i < max_stack_depth; i++) {
1818 if (!st->callstack || !st->callstack[i])
1821 ip = st->callstack[i];
1822 sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
1823 get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
1824 fprintf(lock_output, "%s %#lx %s", i ? ":" : sep, (unsigned long) ip, buf);
1826 fprintf(lock_output, "\n");
1830 static void print_lock_stat(struct lock_contention *con, struct lock_stat *st)
1832 if (symbol_conf.field_sep)
1833 print_lock_stat_csv(con, st, symbol_conf.field_sep);
1835 print_lock_stat_stdio(con, st);
1838 static void print_footer_stdio(int total, int bad, struct lock_contention_fails *fails)
1840 /* Output for debug, this have to be removed */
1841 int broken = fails->task + fails->stack + fails->time + fails->data;
1844 print_bad_events(bad, total);
1846 if (quiet || total == 0 || (broken == 0 && verbose <= 0))
1850 fprintf(lock_output, "\n=== output for debug ===\n\n");
1851 fprintf(lock_output, "bad: %d, total: %d\n", broken, total);
1852 fprintf(lock_output, "bad rate: %.2f %%\n", 100.0 * broken / total);
1854 fprintf(lock_output, "histogram of failure reasons\n");
1855 fprintf(lock_output, " %10s: %d\n", "task", fails->task);
1856 fprintf(lock_output, " %10s: %d\n", "stack", fails->stack);
1857 fprintf(lock_output, " %10s: %d\n", "time", fails->time);
1858 fprintf(lock_output, " %10s: %d\n", "data", fails->data);
1861 static void print_footer_csv(int total, int bad, struct lock_contention_fails *fails,
1864 /* Output for debug, this have to be removed */
1866 bad = fails->task + fails->stack + fails->time + fails->data;
1868 if (quiet || total == 0 || (bad == 0 && verbose <= 0))
1872 fprintf(lock_output, "# debug: total=%d%s bad=%d", total, sep, bad);
1875 fprintf(lock_output, "%s bad_%s=%d", sep, "task", fails->task);
1876 fprintf(lock_output, "%s bad_%s=%d", sep, "stack", fails->stack);
1877 fprintf(lock_output, "%s bad_%s=%d", sep, "time", fails->time);
1878 fprintf(lock_output, "%s bad_%s=%d", sep, "data", fails->data);
1881 const char *name[4] = { "acquire", "acquired", "contended", "release" };
1883 for (i = 0; i < BROKEN_MAX; i++)
1884 fprintf(lock_output, "%s bad_%s=%d", sep, name[i], bad_hist[i]);
1886 fprintf(lock_output, "\n");
1889 static void print_footer(int total, int bad, struct lock_contention_fails *fails)
1891 if (symbol_conf.field_sep)
1892 print_footer_csv(total, bad, fails, symbol_conf.field_sep);
1894 print_footer_stdio(total, bad, fails);
1897 static void print_contention_result(struct lock_contention *con)
1899 struct lock_stat *st;
1900 int bad, total, printed;
1905 bad = total = printed = 0;
1907 while ((st = pop_from_result())) {
1908 total += use_bpf ? st->nr_contended : 1;
1912 if (!st->wait_time_total)
1915 print_lock_stat(con, st);
1917 if (++printed >= print_nr_entries)
1921 if (print_nr_entries) {
1922 /* update the total/bad stats */
1923 while ((st = pop_from_result())) {
1924 total += use_bpf ? st->nr_contended : 1;
1929 /* some entries are collected but hidden by the callstack filter */
1930 total += con->nr_filtered;
1932 print_footer(total, bad, &con->fails);
1937 static int __cmd_report(bool display_info)
1940 struct perf_tool eops = {
1941 .attr = perf_event__process_attr,
1942 .event_update = process_event_update,
1943 .sample = process_sample_event,
1944 .comm = perf_event__process_comm,
1945 .mmap = perf_event__process_mmap,
1946 .namespaces = perf_event__process_namespaces,
1947 .tracing_data = perf_event__process_tracing_data,
1948 .ordered_events = true,
1950 struct perf_data data = {
1952 .mode = PERF_DATA_MODE_READ,
1956 session = perf_session__new(&data, &eops);
1957 if (IS_ERR(session)) {
1958 pr_err("Initializing perf session failed\n");
1959 return PTR_ERR(session);
1962 symbol_conf.allow_aliases = true;
1963 symbol__init(&session->header.env);
1965 if (!data.is_pipe) {
1966 if (!perf_session__has_traces(session, "lock record"))
1969 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
1970 pr_err("Initializing perf session tracepoint handlers failed\n");
1974 if (perf_session__set_tracepoints_handlers(session, contention_tracepoints)) {
1975 pr_err("Initializing perf session tracepoint handlers failed\n");
1980 if (setup_output_field(false, output_fields))
1983 if (select_key(false))
1986 if (show_thread_stats)
1987 aggr_mode = LOCK_AGGR_TASK;
1989 err = perf_session__process_events(session);
1994 if (display_info) /* used for info subcommand */
2003 perf_session__delete(session);
2007 static void sighandler(int sig __maybe_unused)
2011 static int check_lock_contention_options(const struct option *options,
2012 const char * const *usage)
2015 if (show_thread_stats && show_lock_addrs) {
2016 pr_err("Cannot use thread and addr mode together\n");
2017 parse_options_usage(usage, options, "threads", 0);
2018 parse_options_usage(NULL, options, "lock-addr", 0);
2022 if (show_lock_owner && !use_bpf) {
2023 pr_err("Lock owners are available only with BPF\n");
2024 parse_options_usage(usage, options, "lock-owner", 0);
2025 parse_options_usage(NULL, options, "use-bpf", 0);
2029 if (show_lock_owner && show_lock_addrs) {
2030 pr_err("Cannot use owner and addr mode together\n");
2031 parse_options_usage(usage, options, "lock-owner", 0);
2032 parse_options_usage(NULL, options, "lock-addr", 0);
2036 if (show_lock_cgroups && !use_bpf) {
2037 pr_err("Cgroups are available only with BPF\n");
2038 parse_options_usage(usage, options, "lock-cgroup", 0);
2039 parse_options_usage(NULL, options, "use-bpf", 0);
2043 if (show_lock_cgroups && show_lock_addrs) {
2044 pr_err("Cannot use cgroup and addr mode together\n");
2045 parse_options_usage(usage, options, "lock-cgroup", 0);
2046 parse_options_usage(NULL, options, "lock-addr", 0);
2050 if (show_lock_cgroups && show_thread_stats) {
2051 pr_err("Cannot use cgroup and thread mode together\n");
2052 parse_options_usage(usage, options, "lock-cgroup", 0);
2053 parse_options_usage(NULL, options, "threads", 0);
2057 if (symbol_conf.field_sep) {
2058 if (strstr(symbol_conf.field_sep, ":") || /* part of type flags */
2059 strstr(symbol_conf.field_sep, "+") || /* part of caller offset */
2060 strstr(symbol_conf.field_sep, ".")) { /* can be in a symbol name */
2061 pr_err("Cannot use the separator that is already used\n");
2062 parse_options_usage(usage, options, "x", 1);
2067 if (show_lock_owner)
2068 show_thread_stats = true;
2073 static int __cmd_contention(int argc, const char **argv)
2076 struct perf_tool eops = {
2077 .attr = perf_event__process_attr,
2078 .event_update = process_event_update,
2079 .sample = process_sample_event,
2080 .comm = perf_event__process_comm,
2081 .mmap = perf_event__process_mmap,
2082 .tracing_data = perf_event__process_tracing_data,
2083 .ordered_events = true,
2085 struct perf_data data = {
2087 .mode = PERF_DATA_MODE_READ,
2090 struct lock_contention con = {
2092 .map_nr_entries = bpf_map_entries,
2093 .max_stack = max_stack_depth,
2094 .stack_skip = stack_skip,
2095 .filters = &filters,
2096 .save_callstack = needs_callstack(),
2097 .owner = show_lock_owner,
2101 lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
2102 if (!lockhash_table)
2105 con.result = &lockhash_table[0];
2107 session = perf_session__new(use_bpf ? NULL : &data, &eops);
2108 if (IS_ERR(session)) {
2109 pr_err("Initializing perf session failed\n");
2110 err = PTR_ERR(session);
2115 con.machine = &session->machines.host;
2117 con.aggr_mode = aggr_mode = show_thread_stats ? LOCK_AGGR_TASK :
2118 show_lock_addrs ? LOCK_AGGR_ADDR :
2119 show_lock_cgroups ? LOCK_AGGR_CGROUP : LOCK_AGGR_CALLER;
2121 if (con.aggr_mode == LOCK_AGGR_CALLER)
2122 con.save_callstack = true;
2124 symbol_conf.allow_aliases = true;
2125 symbol__init(&session->header.env);
2128 err = target__validate(&target);
2132 target__strerror(&target, err, errbuf, 512);
2133 pr_err("%s\n", errbuf);
2137 signal(SIGINT, sighandler);
2138 signal(SIGCHLD, sighandler);
2139 signal(SIGTERM, sighandler);
2141 con.evlist = evlist__new();
2142 if (con.evlist == NULL) {
2147 err = evlist__create_maps(con.evlist, &target);
2152 err = evlist__prepare_workload(con.evlist, &target,
2158 if (lock_contention_prepare(&con) < 0) {
2159 pr_err("lock contention BPF setup failed\n");
2162 } else if (!data.is_pipe) {
2163 if (!perf_session__has_traces(session, "lock record"))
2166 if (!evlist__find_evsel_by_str(session->evlist,
2167 "lock:contention_begin")) {
2168 pr_err("lock contention evsel not found\n");
2172 if (perf_session__set_tracepoints_handlers(session,
2173 contention_tracepoints)) {
2174 pr_err("Initializing perf session tracepoint handlers failed\n");
2179 if (setup_output_field(true, output_fields))
2182 if (select_key(true))
2185 if (symbol_conf.field_sep) {
2187 struct lock_key *keys = contention_keys;
2189 /* do not align output in CSV format */
2190 for (i = 0; keys[i].name; i++)
2195 lock_contention_start();
2197 evlist__start_workload(con.evlist);
2199 /* wait for signal */
2202 lock_contention_stop();
2203 lock_contention_read(&con);
2205 err = perf_session__process_events(session);
2212 sort_contention_result();
2213 print_contention_result(&con);
2216 lock_filter_finish();
2217 evlist__delete(con.evlist);
2218 lock_contention_finish(&con);
2219 perf_session__delete(session);
2220 zfree(&lockhash_table);
2225 static int __cmd_record(int argc, const char **argv)
2227 const char *record_args[] = {
2228 "record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
2230 const char *callgraph_args[] = {
2231 "--call-graph", "fp," __stringify(CONTENTION_STACK_DEPTH),
2233 unsigned int rec_argc, i, j, ret;
2234 unsigned int nr_tracepoints;
2235 unsigned int nr_callgraph_args = 0;
2236 const char **rec_argv;
2237 bool has_lock_stat = true;
2239 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
2240 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
2241 pr_debug("tracepoint %s is not enabled. "
2242 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
2243 lock_tracepoints[i].name);
2244 has_lock_stat = false;
2252 for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) {
2253 if (!is_valid_tracepoint(contention_tracepoints[i].name)) {
2254 pr_err("tracepoint %s is not enabled.\n",
2255 contention_tracepoints[i].name);
2260 nr_callgraph_args = ARRAY_SIZE(callgraph_args);
2263 rec_argc = ARRAY_SIZE(record_args) + nr_callgraph_args + argc - 1;
2266 nr_tracepoints = ARRAY_SIZE(lock_tracepoints);
2268 nr_tracepoints = ARRAY_SIZE(contention_tracepoints);
2270 /* factor of 2 is for -e in front of each tracepoint */
2271 rec_argc += 2 * nr_tracepoints;
2273 rec_argv = calloc(rec_argc + 1, sizeof(char *));
2277 for (i = 0; i < ARRAY_SIZE(record_args); i++)
2278 rec_argv[i] = strdup(record_args[i]);
2280 for (j = 0; j < nr_tracepoints; j++) {
2281 const char *ev_name;
2284 ev_name = strdup(lock_tracepoints[j].name);
2286 ev_name = strdup(contention_tracepoints[j].name);
2291 rec_argv[i++] = "-e";
2292 rec_argv[i++] = ev_name;
2295 for (j = 0; j < nr_callgraph_args; j++, i++)
2296 rec_argv[i] = callgraph_args[j];
2298 for (j = 1; j < (unsigned int)argc; j++, i++)
2299 rec_argv[i] = argv[j];
2301 BUG_ON(i != rec_argc);
2303 ret = cmd_record(i, rec_argv);
2308 static int parse_map_entry(const struct option *opt, const char *str,
2309 int unset __maybe_unused)
2311 unsigned long *len = (unsigned long *)opt->value;
2316 val = strtoul(str, &endptr, 0);
2317 if (*endptr != '\0' || errno != 0) {
2318 pr_err("invalid BPF map length: %s\n", str);
2326 static int parse_max_stack(const struct option *opt, const char *str,
2327 int unset __maybe_unused)
2329 unsigned long *len = (unsigned long *)opt->value;
2334 val = strtol(str, &endptr, 0);
2335 if (*endptr != '\0' || errno != 0) {
2336 pr_err("invalid max stack depth: %s\n", str);
2340 if (val < 0 || val > sysctl__max_stack()) {
2341 pr_err("invalid max stack depth: %ld\n", val);
2349 static bool add_lock_type(unsigned int flags)
2353 tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
2357 tmp[filters.nr_types++] = flags;
2358 filters.types = tmp;
2362 static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
2363 int unset __maybe_unused)
2365 char *s, *tmp, *tok;
2372 for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2373 unsigned int flags = get_type_flag(tok);
2376 pr_err("Unknown lock flags: %s\n", tok);
2381 if (!add_lock_type(flags)) {
2391 static bool add_lock_addr(unsigned long addr)
2395 tmp = realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.addrs));
2397 pr_err("Memory allocation failure\n");
2401 tmp[filters.nr_addrs++] = addr;
2402 filters.addrs = tmp;
2406 static bool add_lock_sym(char *name)
2409 char *sym = strdup(name);
2412 pr_err("Memory allocation failure\n");
2416 tmp = realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.syms));
2418 pr_err("Memory allocation failure\n");
2423 tmp[filters.nr_syms++] = sym;
2428 static int parse_lock_addr(const struct option *opt __maybe_unused, const char *str,
2429 int unset __maybe_unused)
2431 char *s, *tmp, *tok;
2439 for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2442 addr = strtoul(tok, &end, 16);
2444 if (!add_lock_addr(addr)) {
2452 * At this moment, we don't have kernel symbols. Save the symbols
2453 * in a separate list and resolve them to addresses later.
2455 if (!add_lock_sym(tok)) {
2465 static int parse_call_stack(const struct option *opt __maybe_unused, const char *str,
2466 int unset __maybe_unused)
2468 char *s, *tmp, *tok;
2475 for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2476 struct callstack_filter *entry;
2478 entry = malloc(sizeof(*entry) + strlen(tok) + 1);
2479 if (entry == NULL) {
2480 pr_err("Memory allocation failure\n");
2485 strcpy(entry->name, tok);
2486 list_add_tail(&entry->list, &callstack_filters);
2493 static int parse_output(const struct option *opt __maybe_unused, const char *str,
2494 int unset __maybe_unused)
2496 const char **name = (const char **)opt->value;
2501 lock_output = fopen(str, "w");
2502 if (lock_output == NULL) {
2503 pr_err("Cannot open %s\n", str);
2511 static bool add_lock_cgroup(char *name)
2514 struct cgroup *cgrp;
2516 cgrp = cgroup__new(name, /*do_open=*/false);
2518 pr_err("Failed to create cgroup: %s\n", name);
2522 if (read_cgroup_id(cgrp) < 0) {
2523 pr_err("Failed to read cgroup id for %s\n", name);
2528 tmp = realloc(filters.cgrps, (filters.nr_cgrps + 1) * sizeof(*filters.cgrps));
2530 pr_err("Memory allocation failure\n");
2534 tmp[filters.nr_cgrps++] = cgrp->id;
2535 filters.cgrps = tmp;
2540 static int parse_cgroup_filter(const struct option *opt __maybe_unused, const char *str,
2541 int unset __maybe_unused)
2543 char *s, *tmp, *tok;
2550 for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2551 if (!add_lock_cgroup(tok)) {
2561 int cmd_lock(int argc, const char **argv)
2563 const struct option lock_options[] = {
2564 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2565 OPT_CALLBACK(0, "output", &output_name, "file", "output file name", parse_output),
2566 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
2567 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
2568 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
2569 OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
2570 "file", "vmlinux pathname"),
2571 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2572 "file", "kallsyms pathname"),
2573 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
2577 const struct option info_options[] = {
2578 OPT_BOOLEAN('t', "threads", &info_threads,
2579 "dump thread list in perf.data"),
2580 OPT_BOOLEAN('m', "map", &info_map,
2581 "map of lock instances (address:name table)"),
2582 OPT_PARENT(lock_options)
2585 const struct option report_options[] = {
2586 OPT_STRING('k', "key", &sort_key, "acquired",
2587 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2588 OPT_STRING('F', "field", &output_fields, NULL,
2589 "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2591 OPT_BOOLEAN('c', "combine-locks", &combine_locks,
2592 "combine locks in the same class"),
2593 OPT_BOOLEAN('t', "threads", &show_thread_stats,
2594 "show per-thread lock stats"),
2595 OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2596 OPT_PARENT(lock_options)
2599 struct option contention_options[] = {
2600 OPT_STRING('k', "key", &sort_key, "wait_total",
2601 "key for sorting (contended / wait_total / wait_max / wait_min / avg_wait)"),
2602 OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,avg_wait",
2603 "output fields (contended / wait_total / wait_max / wait_min / avg_wait)"),
2604 OPT_BOOLEAN('t', "threads", &show_thread_stats,
2605 "show per-thread lock stats"),
2606 OPT_BOOLEAN('b', "use-bpf", &use_bpf, "use BPF program to collect lock contention stats"),
2607 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
2608 "System-wide collection from all CPUs"),
2609 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2610 "List of cpus to monitor"),
2611 OPT_STRING('p', "pid", &target.pid, "pid",
2612 "Trace on existing process id"),
2613 OPT_STRING(0, "tid", &target.tid, "tid",
2614 "Trace on existing thread id (exclusive to --pid)"),
2615 OPT_CALLBACK('M', "map-nr-entries", &bpf_map_entries, "num",
2616 "Max number of BPF map entries", parse_map_entry),
2617 OPT_CALLBACK(0, "max-stack", &max_stack_depth, "num",
2618 "Set the maximum stack depth when collecting lock contention, "
2619 "Default: " __stringify(CONTENTION_STACK_DEPTH), parse_max_stack),
2620 OPT_INTEGER(0, "stack-skip", &stack_skip,
2621 "Set the number of stack depth to skip when finding a lock caller, "
2622 "Default: " __stringify(CONTENTION_STACK_SKIP)),
2623 OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2624 OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"),
2625 OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS",
2626 "Filter specific type of locks", parse_lock_type),
2627 OPT_CALLBACK('L', "lock-filter", NULL, "ADDRS/NAMES",
2628 "Filter specific address/symbol of locks", parse_lock_addr),
2629 OPT_CALLBACK('S', "callstack-filter", NULL, "NAMES",
2630 "Filter specific function in the callstack", parse_call_stack),
2631 OPT_BOOLEAN('o', "lock-owner", &show_lock_owner, "show lock owners instead of waiters"),
2632 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, "separator",
2633 "print result in CSV format with custom separator"),
2634 OPT_BOOLEAN(0, "lock-cgroup", &show_lock_cgroups, "show lock stats by cgroup"),
2635 OPT_CALLBACK('G', "cgroup-filter", NULL, "CGROUPS",
2636 "Filter specific cgroups", parse_cgroup_filter),
2637 OPT_PARENT(lock_options)
2640 const char * const info_usage[] = {
2641 "perf lock info [<options>]",
2644 const char *const lock_subcommands[] = { "record", "report", "script",
2645 "info", "contention", NULL };
2646 const char *lock_usage[] = {
2650 const char * const report_usage[] = {
2651 "perf lock report [<options>]",
2654 const char * const contention_usage[] = {
2655 "perf lock contention [<options>]",
2661 lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
2662 if (!lockhash_table)
2665 for (i = 0; i < LOCKHASH_SIZE; i++)
2666 INIT_HLIST_HEAD(lockhash_table + i);
2668 lock_output = stderr;
2669 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
2670 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
2672 usage_with_options(lock_usage, lock_options);
2674 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
2675 return __cmd_record(argc, argv);
2676 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
2677 trace_handler = &report_lock_ops;
2679 argc = parse_options(argc, argv,
2680 report_options, report_usage, 0);
2682 usage_with_options(report_usage, report_options);
2684 rc = __cmd_report(false);
2685 } else if (!strcmp(argv[0], "script")) {
2686 /* Aliased to 'perf script' */
2687 rc = cmd_script(argc, argv);
2688 } else if (!strcmp(argv[0], "info")) {
2690 argc = parse_options(argc, argv,
2691 info_options, info_usage, 0);
2693 usage_with_options(info_usage, info_options);
2695 /* recycling report_lock_ops */
2696 trace_handler = &report_lock_ops;
2697 rc = __cmd_report(true);
2698 } else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) {
2699 trace_handler = &contention_lock_ops;
2700 sort_key = "wait_total";
2701 output_fields = "contended,wait_total,wait_max,avg_wait";
2703 #ifndef HAVE_BPF_SKEL
2704 set_option_nobuild(contention_options, 'b', "use-bpf",
2705 "no BUILD_BPF_SKEL=1", false);
2708 argc = parse_options(argc, argv, contention_options,
2709 contention_usage, 0);
2712 if (check_lock_contention_options(contention_options,
2713 contention_usage) < 0)
2716 rc = __cmd_contention(argc, argv);
2718 usage_with_options(lock_usage, lock_options);
2721 zfree(&lockhash_table);