4 #include "util/evlist.h"
5 #include "util/evsel.h"
7 #include "util/cache.h"
8 #include "util/symbol.h"
9 #include "util/thread.h"
10 #include "util/header.h"
12 #include "util/parse-options.h"
13 #include "util/trace-event.h"
15 #include "util/debug.h"
16 #include "util/session.h"
17 #include "util/tool.h"
19 #include <sys/types.h>
20 #include <sys/prctl.h>
21 #include <semaphore.h>
26 #include <linux/list.h>
27 #include <linux/hash.h>
29 static struct perf_session *session;
31 /* based on kernel/lockdep.c */
32 #define LOCKHASH_BITS 12
33 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
35 static struct list_head lockhash_table[LOCKHASH_SIZE];
37 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
38 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
41 struct list_head hash_entry;
42 struct rb_node rb; /* used for sorting */
45 * FIXME: perf_evsel__intval() returns u64,
46 * so address of lockdep_map should be dealed as 64bit.
47 * Is there more better solution?
49 void *addr; /* address of lockdep_map, used as ID */
50 char *name; /* for strcpy(), we cannot use const */
52 unsigned int nr_acquire;
53 unsigned int nr_acquired;
54 unsigned int nr_contended;
55 unsigned int nr_release;
57 unsigned int nr_readlock;
58 unsigned int nr_trylock;
60 /* these times are in nano sec. */
66 int discard; /* flag of blacklist */
70 * States of lock_seq_stat
72 * UNINITIALIZED is required for detecting first event of acquire.
73 * As the nature of lock events, there is no guarantee
74 * that the first event for the locks are acquire,
75 * it can be acquired, contended or release.
77 #define SEQ_STATE_UNINITIALIZED 0 /* initial state */
78 #define SEQ_STATE_RELEASED 1
79 #define SEQ_STATE_ACQUIRING 2
80 #define SEQ_STATE_ACQUIRED 3
81 #define SEQ_STATE_READ_ACQUIRED 4
82 #define SEQ_STATE_CONTENDED 5
86 * Imported from include/linux/sched.h.
87 * Should this be synchronized?
89 #define MAX_LOCK_DEPTH 48
92 * struct lock_seq_stat:
93 * Place to put on state of one lock sequence
94 * 1) acquire -> acquired -> release
95 * 2) acquire -> contended -> acquired -> release
96 * 3) acquire (with read or try) -> release
97 * 4) Are there other patterns?
99 struct lock_seq_stat {
100 struct list_head list;
112 struct list_head seq_list;
115 static struct rb_root thread_stats;
117 static struct thread_stat *thread_stat_find(u32 tid)
119 struct rb_node *node;
120 struct thread_stat *st;
122 node = thread_stats.rb_node;
124 st = container_of(node, struct thread_stat, rb);
127 else if (tid < st->tid)
128 node = node->rb_left;
130 node = node->rb_right;
136 static void thread_stat_insert(struct thread_stat *new)
138 struct rb_node **rb = &thread_stats.rb_node;
139 struct rb_node *parent = NULL;
140 struct thread_stat *p;
143 p = container_of(*rb, struct thread_stat, rb);
146 if (new->tid < p->tid)
147 rb = &(*rb)->rb_left;
148 else if (new->tid > p->tid)
149 rb = &(*rb)->rb_right;
151 BUG_ON("inserting invalid thread_stat\n");
154 rb_link_node(&new->rb, parent, rb);
155 rb_insert_color(&new->rb, &thread_stats);
158 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
160 struct thread_stat *st;
162 st = thread_stat_find(tid);
166 st = zalloc(sizeof(struct thread_stat));
168 pr_err("memory allocation failed\n");
173 INIT_LIST_HEAD(&st->seq_list);
175 thread_stat_insert(st);
180 static struct thread_stat *thread_stat_findnew_first(u32 tid);
181 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
182 thread_stat_findnew_first;
184 static struct thread_stat *thread_stat_findnew_first(u32 tid)
186 struct thread_stat *st;
188 st = zalloc(sizeof(struct thread_stat));
190 pr_err("memory allocation failed\n");
194 INIT_LIST_HEAD(&st->seq_list);
196 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
197 rb_insert_color(&st->rb, &thread_stats);
199 thread_stat_findnew = thread_stat_findnew_after_first;
203 /* build simple key function one is bigger than two */
204 #define SINGLE_KEY(member) \
205 static int lock_stat_key_ ## member(struct lock_stat *one, \
206 struct lock_stat *two) \
208 return one->member > two->member; \
211 SINGLE_KEY(nr_acquired)
212 SINGLE_KEY(nr_contended)
213 SINGLE_KEY(avg_wait_time)
214 SINGLE_KEY(wait_time_total)
215 SINGLE_KEY(wait_time_max)
217 static int lock_stat_key_wait_time_min(struct lock_stat *one,
218 struct lock_stat *two)
220 u64 s1 = one->wait_time_min;
221 u64 s2 = two->wait_time_min;
222 if (s1 == ULLONG_MAX)
224 if (s2 == ULLONG_MAX)
231 * name: the value for specify by user
232 * this should be simpler than raw name of member
233 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
236 int (*key)(struct lock_stat*, struct lock_stat*);
239 static const char *sort_key = "acquired";
241 static int (*compare)(struct lock_stat *, struct lock_stat *);
243 static struct rb_root result; /* place to store sorted data */
245 #define DEF_KEY_LOCK(name, fn_suffix) \
246 { #name, lock_stat_key_ ## fn_suffix }
247 struct lock_key keys[] = {
248 DEF_KEY_LOCK(acquired, nr_acquired),
249 DEF_KEY_LOCK(contended, nr_contended),
250 DEF_KEY_LOCK(avg_wait, avg_wait_time),
251 DEF_KEY_LOCK(wait_total, wait_time_total),
252 DEF_KEY_LOCK(wait_min, wait_time_min),
253 DEF_KEY_LOCK(wait_max, wait_time_max),
255 /* extra comparisons much complicated should be here */
260 static int select_key(void)
264 for (i = 0; keys[i].name; i++) {
265 if (!strcmp(keys[i].name, sort_key)) {
266 compare = keys[i].key;
271 pr_err("Unknown compare key: %s\n", sort_key);
276 static void insert_to_result(struct lock_stat *st,
277 int (*bigger)(struct lock_stat *, struct lock_stat *))
279 struct rb_node **rb = &result.rb_node;
280 struct rb_node *parent = NULL;
284 p = container_of(*rb, struct lock_stat, rb);
288 rb = &(*rb)->rb_left;
290 rb = &(*rb)->rb_right;
293 rb_link_node(&st->rb, parent, rb);
294 rb_insert_color(&st->rb, &result);
297 /* returns left most element of result, and erase it */
298 static struct lock_stat *pop_from_result(void)
300 struct rb_node *node = result.rb_node;
305 while (node->rb_left)
306 node = node->rb_left;
308 rb_erase(node, &result);
309 return container_of(node, struct lock_stat, rb);
312 static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
314 struct list_head *entry = lockhashentry(addr);
315 struct lock_stat *ret, *new;
317 list_for_each_entry(ret, entry, hash_entry) {
318 if (ret->addr == addr)
322 new = zalloc(sizeof(struct lock_stat));
327 new->name = zalloc(sizeof(char) * strlen(name) + 1);
333 strcpy(new->name, name);
334 new->wait_time_min = ULLONG_MAX;
336 list_add(&new->hash_entry, entry);
340 pr_err("memory allocation failed\n");
344 struct trace_lock_handler {
345 int (*acquire_event)(struct perf_evsel *evsel,
346 struct perf_sample *sample);
348 int (*acquired_event)(struct perf_evsel *evsel,
349 struct perf_sample *sample);
351 int (*contended_event)(struct perf_evsel *evsel,
352 struct perf_sample *sample);
354 int (*release_event)(struct perf_evsel *evsel,
355 struct perf_sample *sample);
358 static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
360 struct lock_seq_stat *seq;
362 list_for_each_entry(seq, &ts->seq_list, list) {
363 if (seq->addr == addr)
367 seq = zalloc(sizeof(struct lock_seq_stat));
369 pr_err("memory allocation failed\n");
372 seq->state = SEQ_STATE_UNINITIALIZED;
375 list_add(&seq->list, &ts->seq_list);
387 static int bad_hist[BROKEN_MAX];
394 static int report_lock_acquire_event(struct perf_evsel *evsel,
395 struct perf_sample *sample)
398 struct lock_stat *ls;
399 struct thread_stat *ts;
400 struct lock_seq_stat *seq;
401 const char *name = perf_evsel__strval(evsel, sample, "name");
402 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
403 int flag = perf_evsel__intval(evsel, sample, "flag");
405 memcpy(&addr, &tmp, sizeof(void *));
407 ls = lock_stat_findnew(addr, name);
413 ts = thread_stat_findnew(sample->tid);
417 seq = get_seq(ts, addr);
421 switch (seq->state) {
422 case SEQ_STATE_UNINITIALIZED:
423 case SEQ_STATE_RELEASED:
425 seq->state = SEQ_STATE_ACQUIRING;
429 if (flag & READ_LOCK)
431 seq->state = SEQ_STATE_READ_ACQUIRED;
436 case SEQ_STATE_READ_ACQUIRED:
437 if (flag & READ_LOCK) {
445 case SEQ_STATE_ACQUIRED:
446 case SEQ_STATE_ACQUIRING:
447 case SEQ_STATE_CONTENDED:
449 /* broken lock sequence, discard it */
451 bad_hist[BROKEN_ACQUIRE]++;
452 list_del(&seq->list);
456 BUG_ON("Unknown state of lock sequence found!\n");
461 seq->prev_event_time = sample->time;
466 static int report_lock_acquired_event(struct perf_evsel *evsel,
467 struct perf_sample *sample)
470 struct lock_stat *ls;
471 struct thread_stat *ts;
472 struct lock_seq_stat *seq;
474 const char *name = perf_evsel__strval(evsel, sample, "name");
475 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
477 memcpy(&addr, &tmp, sizeof(void *));
479 ls = lock_stat_findnew(addr, name);
485 ts = thread_stat_findnew(sample->tid);
489 seq = get_seq(ts, addr);
493 switch (seq->state) {
494 case SEQ_STATE_UNINITIALIZED:
495 /* orphan event, do nothing */
497 case SEQ_STATE_ACQUIRING:
499 case SEQ_STATE_CONTENDED:
500 contended_term = sample->time - seq->prev_event_time;
501 ls->wait_time_total += contended_term;
502 if (contended_term < ls->wait_time_min)
503 ls->wait_time_min = contended_term;
504 if (ls->wait_time_max < contended_term)
505 ls->wait_time_max = contended_term;
507 case SEQ_STATE_RELEASED:
508 case SEQ_STATE_ACQUIRED:
509 case SEQ_STATE_READ_ACQUIRED:
510 /* broken lock sequence, discard it */
512 bad_hist[BROKEN_ACQUIRED]++;
513 list_del(&seq->list);
517 BUG_ON("Unknown state of lock sequence found!\n");
521 seq->state = SEQ_STATE_ACQUIRED;
523 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
524 seq->prev_event_time = sample->time;
529 static int report_lock_contended_event(struct perf_evsel *evsel,
530 struct perf_sample *sample)
533 struct lock_stat *ls;
534 struct thread_stat *ts;
535 struct lock_seq_stat *seq;
536 const char *name = perf_evsel__strval(evsel, sample, "name");
537 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
539 memcpy(&addr, &tmp, sizeof(void *));
541 ls = lock_stat_findnew(addr, name);
547 ts = thread_stat_findnew(sample->tid);
551 seq = get_seq(ts, addr);
555 switch (seq->state) {
556 case SEQ_STATE_UNINITIALIZED:
557 /* orphan event, do nothing */
559 case SEQ_STATE_ACQUIRING:
561 case SEQ_STATE_RELEASED:
562 case SEQ_STATE_ACQUIRED:
563 case SEQ_STATE_READ_ACQUIRED:
564 case SEQ_STATE_CONTENDED:
565 /* broken lock sequence, discard it */
567 bad_hist[BROKEN_CONTENDED]++;
568 list_del(&seq->list);
572 BUG_ON("Unknown state of lock sequence found!\n");
576 seq->state = SEQ_STATE_CONTENDED;
578 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
579 seq->prev_event_time = sample->time;
584 static int report_lock_release_event(struct perf_evsel *evsel,
585 struct perf_sample *sample)
588 struct lock_stat *ls;
589 struct thread_stat *ts;
590 struct lock_seq_stat *seq;
591 const char *name = perf_evsel__strval(evsel, sample, "name");
592 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
594 memcpy(&addr, &tmp, sizeof(void *));
596 ls = lock_stat_findnew(addr, name);
602 ts = thread_stat_findnew(sample->tid);
606 seq = get_seq(ts, addr);
610 switch (seq->state) {
611 case SEQ_STATE_UNINITIALIZED:
613 case SEQ_STATE_ACQUIRED:
615 case SEQ_STATE_READ_ACQUIRED:
617 BUG_ON(seq->read_count < 0);
618 if (!seq->read_count) {
623 case SEQ_STATE_ACQUIRING:
624 case SEQ_STATE_CONTENDED:
625 case SEQ_STATE_RELEASED:
626 /* broken lock sequence, discard it */
628 bad_hist[BROKEN_RELEASE]++;
631 BUG_ON("Unknown state of lock sequence found!\n");
637 list_del(&seq->list);
643 /* lock oriented handlers */
644 /* TODO: handlers for CPU oriented, thread oriented */
645 static struct trace_lock_handler report_lock_ops = {
646 .acquire_event = report_lock_acquire_event,
647 .acquired_event = report_lock_acquired_event,
648 .contended_event = report_lock_contended_event,
649 .release_event = report_lock_release_event,
652 static struct trace_lock_handler *trace_handler;
654 static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
655 struct perf_sample *sample)
657 if (trace_handler->acquire_event)
658 return trace_handler->acquire_event(evsel, sample);
662 static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
663 struct perf_sample *sample)
665 if (trace_handler->acquired_event)
666 return trace_handler->acquired_event(evsel, sample);
670 static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
671 struct perf_sample *sample)
673 if (trace_handler->contended_event)
674 return trace_handler->contended_event(evsel, sample);
678 static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
679 struct perf_sample *sample)
681 if (trace_handler->release_event)
682 return trace_handler->release_event(evsel, sample);
686 static void print_bad_events(int bad, int total)
688 /* Output for debug, this have to be removed */
690 const char *name[4] =
691 { "acquire", "acquired", "contended", "release" };
693 pr_info("\n=== output for debug===\n\n");
694 pr_info("bad: %d, total: %d\n", bad, total);
695 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
696 pr_info("histogram of events caused bad sequence\n");
697 for (i = 0; i < BROKEN_MAX; i++)
698 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
701 /* TODO: various way to print, coloring, nano or milli sec */
702 static void print_result(void)
704 struct lock_stat *st;
708 pr_info("%20s ", "Name");
709 pr_info("%10s ", "acquired");
710 pr_info("%10s ", "contended");
712 pr_info("%15s ", "avg wait (ns)");
713 pr_info("%15s ", "total wait (ns)");
714 pr_info("%15s ", "max wait (ns)");
715 pr_info("%15s ", "min wait (ns)");
720 while ((st = pop_from_result())) {
728 if (strlen(st->name) < 16) {
729 /* output raw name */
730 pr_info("%20s ", st->name);
732 strncpy(cut_name, st->name, 16);
737 /* cut off name for saving output style */
738 pr_info("%20s ", cut_name);
741 pr_info("%10u ", st->nr_acquired);
742 pr_info("%10u ", st->nr_contended);
744 pr_info("%15" PRIu64 " ", st->avg_wait_time);
745 pr_info("%15" PRIu64 " ", st->wait_time_total);
746 pr_info("%15" PRIu64 " ", st->wait_time_max);
747 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
748 0 : st->wait_time_min);
752 print_bad_events(bad, total);
755 static bool info_threads, info_map;
757 static void dump_threads(void)
759 struct thread_stat *st;
760 struct rb_node *node;
763 pr_info("%10s: comm\n", "Thread ID");
765 node = rb_first(&thread_stats);
767 st = container_of(node, struct thread_stat, rb);
768 t = perf_session__findnew(session, st->tid);
769 pr_info("%10d: %s\n", st->tid, t->comm);
770 node = rb_next(node);
774 static void dump_map(void)
777 struct lock_stat *st;
779 pr_info("Address of instance: name of class\n");
780 for (i = 0; i < LOCKHASH_SIZE; i++) {
781 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
782 pr_info(" %p: %s\n", st->addr, st->name);
787 static int dump_info(void)
797 pr_err("Unknown type of information\n");
803 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
804 struct perf_sample *sample);
806 static int process_sample_event(struct perf_tool *tool __maybe_unused,
807 union perf_event *event,
808 struct perf_sample *sample,
809 struct perf_evsel *evsel,
810 struct machine *machine)
812 struct thread *thread = machine__findnew_thread(machine, sample->pid,
815 if (thread == NULL) {
816 pr_debug("problem processing %d event, skipping it.\n",
821 if (evsel->handler.func != NULL) {
822 tracepoint_handler f = evsel->handler.func;
823 return f(evsel, sample);
829 static void sort_result(void)
832 struct lock_stat *st;
834 for (i = 0; i < LOCKHASH_SIZE; i++) {
835 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
836 insert_to_result(st, compare);
841 static const struct perf_evsel_str_handler lock_tracepoints[] = {
842 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
843 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
844 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
845 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
848 static int __cmd_report(bool display_info)
851 struct perf_tool eops = {
852 .sample = process_sample_event,
853 .comm = perf_event__process_comm,
854 .ordered_samples = true,
857 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
859 pr_err("Initializing perf session failed\n");
863 if (!perf_session__has_traces(session, "lock record"))
866 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
867 pr_err("Initializing perf session tracepoint handlers failed\n");
874 err = perf_session__process_events(session, &eops);
879 if (display_info) /* used for info subcommand */
887 perf_session__delete(session);
891 static int __cmd_record(int argc, const char **argv)
893 const char *record_args[] = {
894 "record", "-R", "-m", "1024", "-c", "1",
896 unsigned int rec_argc, i, j, ret;
897 const char **rec_argv;
899 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
900 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
901 pr_err("tracepoint %s is not enabled. "
902 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
903 lock_tracepoints[i].name);
908 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
909 /* factor of 2 is for -e in front of each tracepoint */
910 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
912 rec_argv = calloc(rec_argc + 1, sizeof(char *));
916 for (i = 0; i < ARRAY_SIZE(record_args); i++)
917 rec_argv[i] = strdup(record_args[i]);
919 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
920 rec_argv[i++] = "-e";
921 rec_argv[i++] = strdup(lock_tracepoints[j].name);
924 for (j = 1; j < (unsigned int)argc; j++, i++)
925 rec_argv[i] = argv[j];
927 BUG_ON(i != rec_argc);
929 ret = cmd_record(i, rec_argv, NULL);
934 int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
936 const struct option info_options[] = {
937 OPT_BOOLEAN('t', "threads", &info_threads,
938 "dump thread list in perf.data"),
939 OPT_BOOLEAN('m', "map", &info_map,
940 "map of lock instances (address:name table)"),
943 const struct option lock_options[] = {
944 OPT_STRING('i', "input", &input_name, "file", "input file name"),
945 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
946 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
949 const struct option report_options[] = {
950 OPT_STRING('k', "key", &sort_key, "acquired",
951 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
955 const char * const info_usage[] = {
956 "perf lock info [<options>]",
959 const char * const lock_usage[] = {
960 "perf lock [<options>] {record|report|script|info}",
963 const char * const report_usage[] = {
964 "perf lock report [<options>]",
971 for (i = 0; i < LOCKHASH_SIZE; i++)
972 INIT_LIST_HEAD(lockhash_table + i);
974 argc = parse_options(argc, argv, lock_options, lock_usage,
975 PARSE_OPT_STOP_AT_NON_OPTION);
977 usage_with_options(lock_usage, lock_options);
979 if (!strncmp(argv[0], "rec", 3)) {
980 return __cmd_record(argc, argv);
981 } else if (!strncmp(argv[0], "report", 6)) {
982 trace_handler = &report_lock_ops;
984 argc = parse_options(argc, argv,
985 report_options, report_usage, 0);
987 usage_with_options(report_usage, report_options);
989 rc = __cmd_report(false);
990 } else if (!strcmp(argv[0], "script")) {
991 /* Aliased to 'perf script' */
992 return cmd_script(argc, argv, prefix);
993 } else if (!strcmp(argv[0], "info")) {
995 argc = parse_options(argc, argv,
996 info_options, info_usage, 0);
998 usage_with_options(info_usage, info_options);
1000 /* recycling report_lock_ops */
1001 trace_handler = &report_lock_ops;
1002 rc = __cmd_report(true);
1004 usage_with_options(lock_usage, lock_options);