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;
59 /* these times are in nano sec. */
64 int discard; /* flag of blacklist */
68 * States of lock_seq_stat
70 * UNINITIALIZED is required for detecting first event of acquire.
71 * As the nature of lock events, there is no guarantee
72 * that the first event for the locks are acquire,
73 * it can be acquired, contended or release.
75 #define SEQ_STATE_UNINITIALIZED 0 /* initial state */
76 #define SEQ_STATE_RELEASED 1
77 #define SEQ_STATE_ACQUIRING 2
78 #define SEQ_STATE_ACQUIRED 3
79 #define SEQ_STATE_READ_ACQUIRED 4
80 #define SEQ_STATE_CONTENDED 5
84 * Imported from include/linux/sched.h.
85 * Should this be synchronized?
87 #define MAX_LOCK_DEPTH 48
90 * struct lock_seq_stat:
91 * Place to put on state of one lock sequence
92 * 1) acquire -> acquired -> release
93 * 2) acquire -> contended -> acquired -> release
94 * 3) acquire (with read or try) -> release
95 * 4) Are there other patterns?
97 struct lock_seq_stat {
98 struct list_head list;
110 struct list_head seq_list;
113 static struct rb_root thread_stats;
115 static struct thread_stat *thread_stat_find(u32 tid)
117 struct rb_node *node;
118 struct thread_stat *st;
120 node = thread_stats.rb_node;
122 st = container_of(node, struct thread_stat, rb);
125 else if (tid < st->tid)
126 node = node->rb_left;
128 node = node->rb_right;
134 static void thread_stat_insert(struct thread_stat *new)
136 struct rb_node **rb = &thread_stats.rb_node;
137 struct rb_node *parent = NULL;
138 struct thread_stat *p;
141 p = container_of(*rb, struct thread_stat, rb);
144 if (new->tid < p->tid)
145 rb = &(*rb)->rb_left;
146 else if (new->tid > p->tid)
147 rb = &(*rb)->rb_right;
149 BUG_ON("inserting invalid thread_stat\n");
152 rb_link_node(&new->rb, parent, rb);
153 rb_insert_color(&new->rb, &thread_stats);
156 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
158 struct thread_stat *st;
160 st = thread_stat_find(tid);
164 st = zalloc(sizeof(struct thread_stat));
166 pr_err("memory allocation failed\n");
171 INIT_LIST_HEAD(&st->seq_list);
173 thread_stat_insert(st);
178 static struct thread_stat *thread_stat_findnew_first(u32 tid);
179 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
180 thread_stat_findnew_first;
182 static struct thread_stat *thread_stat_findnew_first(u32 tid)
184 struct thread_stat *st;
186 st = zalloc(sizeof(struct thread_stat));
188 pr_err("memory allocation failed\n");
192 INIT_LIST_HEAD(&st->seq_list);
194 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
195 rb_insert_color(&st->rb, &thread_stats);
197 thread_stat_findnew = thread_stat_findnew_after_first;
201 /* build simple key function one is bigger than two */
202 #define SINGLE_KEY(member) \
203 static int lock_stat_key_ ## member(struct lock_stat *one, \
204 struct lock_stat *two) \
206 return one->member > two->member; \
209 SINGLE_KEY(nr_acquired)
210 SINGLE_KEY(nr_contended)
211 SINGLE_KEY(wait_time_total)
212 SINGLE_KEY(wait_time_max)
214 static int lock_stat_key_wait_time_min(struct lock_stat *one,
215 struct lock_stat *two)
217 u64 s1 = one->wait_time_min;
218 u64 s2 = two->wait_time_min;
219 if (s1 == ULLONG_MAX)
221 if (s2 == ULLONG_MAX)
228 * name: the value for specify by user
229 * this should be simpler than raw name of member
230 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
233 int (*key)(struct lock_stat*, struct lock_stat*);
236 static const char *sort_key = "acquired";
238 static int (*compare)(struct lock_stat *, struct lock_stat *);
240 static struct rb_root result; /* place to store sorted data */
242 #define DEF_KEY_LOCK(name, fn_suffix) \
243 { #name, lock_stat_key_ ## fn_suffix }
244 struct lock_key keys[] = {
245 DEF_KEY_LOCK(acquired, nr_acquired),
246 DEF_KEY_LOCK(contended, nr_contended),
247 DEF_KEY_LOCK(wait_total, wait_time_total),
248 DEF_KEY_LOCK(wait_min, wait_time_min),
249 DEF_KEY_LOCK(wait_max, wait_time_max),
251 /* extra comparisons much complicated should be here */
256 static int select_key(void)
260 for (i = 0; keys[i].name; i++) {
261 if (!strcmp(keys[i].name, sort_key)) {
262 compare = keys[i].key;
267 pr_err("Unknown compare key: %s\n", sort_key);
272 static void insert_to_result(struct lock_stat *st,
273 int (*bigger)(struct lock_stat *, struct lock_stat *))
275 struct rb_node **rb = &result.rb_node;
276 struct rb_node *parent = NULL;
280 p = container_of(*rb, struct lock_stat, rb);
284 rb = &(*rb)->rb_left;
286 rb = &(*rb)->rb_right;
289 rb_link_node(&st->rb, parent, rb);
290 rb_insert_color(&st->rb, &result);
293 /* returns left most element of result, and erase it */
294 static struct lock_stat *pop_from_result(void)
296 struct rb_node *node = result.rb_node;
301 while (node->rb_left)
302 node = node->rb_left;
304 rb_erase(node, &result);
305 return container_of(node, struct lock_stat, rb);
308 static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
310 struct list_head *entry = lockhashentry(addr);
311 struct lock_stat *ret, *new;
313 list_for_each_entry(ret, entry, hash_entry) {
314 if (ret->addr == addr)
318 new = zalloc(sizeof(struct lock_stat));
323 new->name = zalloc(sizeof(char) * strlen(name) + 1);
329 strcpy(new->name, name);
330 new->wait_time_min = ULLONG_MAX;
332 list_add(&new->hash_entry, entry);
336 pr_err("memory allocation failed\n");
340 struct trace_lock_handler {
341 int (*acquire_event)(struct perf_evsel *evsel,
342 struct perf_sample *sample);
344 int (*acquired_event)(struct perf_evsel *evsel,
345 struct perf_sample *sample);
347 int (*contended_event)(struct perf_evsel *evsel,
348 struct perf_sample *sample);
350 int (*release_event)(struct perf_evsel *evsel,
351 struct perf_sample *sample);
354 static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
356 struct lock_seq_stat *seq;
358 list_for_each_entry(seq, &ts->seq_list, list) {
359 if (seq->addr == addr)
363 seq = zalloc(sizeof(struct lock_seq_stat));
365 pr_err("memory allocation failed\n");
368 seq->state = SEQ_STATE_UNINITIALIZED;
371 list_add(&seq->list, &ts->seq_list);
383 static int bad_hist[BROKEN_MAX];
390 static int report_lock_acquire_event(struct perf_evsel *evsel,
391 struct perf_sample *sample)
394 struct lock_stat *ls;
395 struct thread_stat *ts;
396 struct lock_seq_stat *seq;
397 const char *name = perf_evsel__strval(evsel, sample, "name");
398 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
399 int flag = perf_evsel__intval(evsel, sample, "flag");
401 memcpy(&addr, &tmp, sizeof(void *));
403 ls = lock_stat_findnew(addr, name);
409 ts = thread_stat_findnew(sample->tid);
413 seq = get_seq(ts, addr);
417 switch (seq->state) {
418 case SEQ_STATE_UNINITIALIZED:
419 case SEQ_STATE_RELEASED:
421 seq->state = SEQ_STATE_ACQUIRING;
425 if (flag & READ_LOCK)
427 seq->state = SEQ_STATE_READ_ACQUIRED;
432 case SEQ_STATE_READ_ACQUIRED:
433 if (flag & READ_LOCK) {
441 case SEQ_STATE_ACQUIRED:
442 case SEQ_STATE_ACQUIRING:
443 case SEQ_STATE_CONTENDED:
445 /* broken lock sequence, discard it */
447 bad_hist[BROKEN_ACQUIRE]++;
448 list_del(&seq->list);
452 BUG_ON("Unknown state of lock sequence found!\n");
457 seq->prev_event_time = sample->time;
462 static int report_lock_acquired_event(struct perf_evsel *evsel,
463 struct perf_sample *sample)
466 struct lock_stat *ls;
467 struct thread_stat *ts;
468 struct lock_seq_stat *seq;
470 const char *name = perf_evsel__strval(evsel, sample, "name");
471 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
473 memcpy(&addr, &tmp, sizeof(void *));
475 ls = lock_stat_findnew(addr, name);
481 ts = thread_stat_findnew(sample->tid);
485 seq = get_seq(ts, addr);
489 switch (seq->state) {
490 case SEQ_STATE_UNINITIALIZED:
491 /* orphan event, do nothing */
493 case SEQ_STATE_ACQUIRING:
495 case SEQ_STATE_CONTENDED:
496 contended_term = sample->time - seq->prev_event_time;
497 ls->wait_time_total += contended_term;
498 if (contended_term < ls->wait_time_min)
499 ls->wait_time_min = contended_term;
500 if (ls->wait_time_max < contended_term)
501 ls->wait_time_max = contended_term;
503 case SEQ_STATE_RELEASED:
504 case SEQ_STATE_ACQUIRED:
505 case SEQ_STATE_READ_ACQUIRED:
506 /* broken lock sequence, discard it */
508 bad_hist[BROKEN_ACQUIRED]++;
509 list_del(&seq->list);
513 BUG_ON("Unknown state of lock sequence found!\n");
517 seq->state = SEQ_STATE_ACQUIRED;
519 seq->prev_event_time = sample->time;
524 static int report_lock_contended_event(struct perf_evsel *evsel,
525 struct perf_sample *sample)
528 struct lock_stat *ls;
529 struct thread_stat *ts;
530 struct lock_seq_stat *seq;
531 const char *name = perf_evsel__strval(evsel, sample, "name");
532 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
534 memcpy(&addr, &tmp, sizeof(void *));
536 ls = lock_stat_findnew(addr, name);
542 ts = thread_stat_findnew(sample->tid);
546 seq = get_seq(ts, addr);
550 switch (seq->state) {
551 case SEQ_STATE_UNINITIALIZED:
552 /* orphan event, do nothing */
554 case SEQ_STATE_ACQUIRING:
556 case SEQ_STATE_RELEASED:
557 case SEQ_STATE_ACQUIRED:
558 case SEQ_STATE_READ_ACQUIRED:
559 case SEQ_STATE_CONTENDED:
560 /* broken lock sequence, discard it */
562 bad_hist[BROKEN_CONTENDED]++;
563 list_del(&seq->list);
567 BUG_ON("Unknown state of lock sequence found!\n");
571 seq->state = SEQ_STATE_CONTENDED;
573 seq->prev_event_time = sample->time;
578 static int report_lock_release_event(struct perf_evsel *evsel,
579 struct perf_sample *sample)
582 struct lock_stat *ls;
583 struct thread_stat *ts;
584 struct lock_seq_stat *seq;
585 const char *name = perf_evsel__strval(evsel, sample, "name");
586 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
588 memcpy(&addr, &tmp, sizeof(void *));
590 ls = lock_stat_findnew(addr, name);
596 ts = thread_stat_findnew(sample->tid);
600 seq = get_seq(ts, addr);
604 switch (seq->state) {
605 case SEQ_STATE_UNINITIALIZED:
607 case SEQ_STATE_ACQUIRED:
609 case SEQ_STATE_READ_ACQUIRED:
611 BUG_ON(seq->read_count < 0);
612 if (!seq->read_count) {
617 case SEQ_STATE_ACQUIRING:
618 case SEQ_STATE_CONTENDED:
619 case SEQ_STATE_RELEASED:
620 /* broken lock sequence, discard it */
622 bad_hist[BROKEN_RELEASE]++;
625 BUG_ON("Unknown state of lock sequence found!\n");
631 list_del(&seq->list);
637 /* lock oriented handlers */
638 /* TODO: handlers for CPU oriented, thread oriented */
639 static struct trace_lock_handler report_lock_ops = {
640 .acquire_event = report_lock_acquire_event,
641 .acquired_event = report_lock_acquired_event,
642 .contended_event = report_lock_contended_event,
643 .release_event = report_lock_release_event,
646 static struct trace_lock_handler *trace_handler;
648 static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
649 struct perf_sample *sample)
651 if (trace_handler->acquire_event)
652 return trace_handler->acquire_event(evsel, sample);
656 static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
657 struct perf_sample *sample)
659 if (trace_handler->acquired_event)
660 return trace_handler->acquired_event(evsel, sample);
664 static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
665 struct perf_sample *sample)
667 if (trace_handler->contended_event)
668 return trace_handler->contended_event(evsel, sample);
672 static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
673 struct perf_sample *sample)
675 if (trace_handler->release_event)
676 return trace_handler->release_event(evsel, sample);
680 static void print_bad_events(int bad, int total)
682 /* Output for debug, this have to be removed */
684 const char *name[4] =
685 { "acquire", "acquired", "contended", "release" };
687 pr_info("\n=== output for debug===\n\n");
688 pr_info("bad: %d, total: %d\n", bad, total);
689 pr_info("bad rate: %f %%\n", (double)bad / (double)total * 100);
690 pr_info("histogram of events caused bad sequence\n");
691 for (i = 0; i < BROKEN_MAX; i++)
692 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
695 /* TODO: various way to print, coloring, nano or milli sec */
696 static void print_result(void)
698 struct lock_stat *st;
702 pr_info("%20s ", "Name");
703 pr_info("%10s ", "acquired");
704 pr_info("%10s ", "contended");
706 pr_info("%15s ", "total wait (ns)");
707 pr_info("%15s ", "max wait (ns)");
708 pr_info("%15s ", "min wait (ns)");
713 while ((st = pop_from_result())) {
721 if (strlen(st->name) < 16) {
722 /* output raw name */
723 pr_info("%20s ", st->name);
725 strncpy(cut_name, st->name, 16);
730 /* cut off name for saving output style */
731 pr_info("%20s ", cut_name);
734 pr_info("%10u ", st->nr_acquired);
735 pr_info("%10u ", st->nr_contended);
737 pr_info("%15" PRIu64 " ", st->wait_time_total);
738 pr_info("%15" PRIu64 " ", st->wait_time_max);
739 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
740 0 : st->wait_time_min);
744 print_bad_events(bad, total);
747 static bool info_threads, info_map;
749 static void dump_threads(void)
751 struct thread_stat *st;
752 struct rb_node *node;
755 pr_info("%10s: comm\n", "Thread ID");
757 node = rb_first(&thread_stats);
759 st = container_of(node, struct thread_stat, rb);
760 t = perf_session__findnew(session, st->tid);
761 pr_info("%10d: %s\n", st->tid, t->comm);
762 node = rb_next(node);
766 static void dump_map(void)
769 struct lock_stat *st;
771 pr_info("Address of instance: name of class\n");
772 for (i = 0; i < LOCKHASH_SIZE; i++) {
773 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
774 pr_info(" %p: %s\n", st->addr, st->name);
779 static int dump_info(void)
789 pr_err("Unknown type of information\n");
795 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
796 struct perf_sample *sample);
798 static int process_sample_event(struct perf_tool *tool __maybe_unused,
799 union perf_event *event,
800 struct perf_sample *sample,
801 struct perf_evsel *evsel,
802 struct machine *machine)
804 struct thread *thread = machine__findnew_thread(machine, sample->pid,
807 if (thread == NULL) {
808 pr_debug("problem processing %d event, skipping it.\n",
813 if (evsel->handler.func != NULL) {
814 tracepoint_handler f = evsel->handler.func;
815 return f(evsel, sample);
821 static void sort_result(void)
824 struct lock_stat *st;
826 for (i = 0; i < LOCKHASH_SIZE; i++) {
827 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
828 insert_to_result(st, compare);
833 static const struct perf_evsel_str_handler lock_tracepoints[] = {
834 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
835 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
836 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
837 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
840 static int __cmd_report(bool display_info)
843 struct perf_tool eops = {
844 .sample = process_sample_event,
845 .comm = perf_event__process_comm,
846 .ordered_samples = true,
849 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
851 pr_err("Initializing perf session failed\n");
855 if (!perf_session__has_traces(session, "lock record"))
858 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
859 pr_err("Initializing perf session tracepoint handlers failed\n");
866 err = perf_session__process_events(session, &eops);
871 if (display_info) /* used for info subcommand */
879 perf_session__delete(session);
883 static int __cmd_record(int argc, const char **argv)
885 const char *record_args[] = {
886 "record", "-R", "-m", "1024", "-c", "1",
888 unsigned int rec_argc, i, j, ret;
889 const char **rec_argv;
891 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
892 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
893 pr_err("tracepoint %s is not enabled. "
894 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
895 lock_tracepoints[i].name);
900 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
901 /* factor of 2 is for -e in front of each tracepoint */
902 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
904 rec_argv = calloc(rec_argc + 1, sizeof(char *));
908 for (i = 0; i < ARRAY_SIZE(record_args); i++)
909 rec_argv[i] = strdup(record_args[i]);
911 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
912 rec_argv[i++] = "-e";
913 rec_argv[i++] = strdup(lock_tracepoints[j].name);
916 for (j = 1; j < (unsigned int)argc; j++, i++)
917 rec_argv[i] = argv[j];
919 BUG_ON(i != rec_argc);
921 ret = cmd_record(i, rec_argv, NULL);
926 int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
928 const struct option info_options[] = {
929 OPT_BOOLEAN('t', "threads", &info_threads,
930 "dump thread list in perf.data"),
931 OPT_BOOLEAN('m', "map", &info_map,
932 "map of lock instances (address:name table)"),
935 const struct option lock_options[] = {
936 OPT_STRING('i', "input", &input_name, "file", "input file name"),
937 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
938 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
941 const struct option report_options[] = {
942 OPT_STRING('k', "key", &sort_key, "acquired",
943 "key for sorting (acquired / contended / wait_total / wait_max / wait_min)"),
947 const char * const info_usage[] = {
948 "perf lock info [<options>]",
951 const char * const lock_usage[] = {
952 "perf lock [<options>] {record|report|script|info}",
955 const char * const report_usage[] = {
956 "perf lock report [<options>]",
963 for (i = 0; i < LOCKHASH_SIZE; i++)
964 INIT_LIST_HEAD(lockhash_table + i);
966 argc = parse_options(argc, argv, lock_options, lock_usage,
967 PARSE_OPT_STOP_AT_NON_OPTION);
969 usage_with_options(lock_usage, lock_options);
971 if (!strncmp(argv[0], "rec", 3)) {
972 return __cmd_record(argc, argv);
973 } else if (!strncmp(argv[0], "report", 6)) {
974 trace_handler = &report_lock_ops;
976 argc = parse_options(argc, argv,
977 report_options, report_usage, 0);
979 usage_with_options(report_usage, report_options);
981 rc = __cmd_report(false);
982 } else if (!strcmp(argv[0], "script")) {
983 /* Aliased to 'perf script' */
984 return cmd_script(argc, argv, prefix);
985 } else if (!strcmp(argv[0], "info")) {
987 argc = parse_options(argc, argv,
988 info_options, info_usage, 0);
990 usage_with_options(info_usage, info_options);
992 /* recycling report_lock_ops */
993 trace_handler = &report_lock_ops;
994 rc = __cmd_report(true);
996 usage_with_options(lock_usage, lock_options);