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"
18 #include "util/data.h"
20 #include <sys/types.h>
21 #include <sys/prctl.h>
22 #include <semaphore.h>
27 #include <linux/list.h>
28 #include <linux/hash.h>
30 static struct perf_session *session;
32 /* based on kernel/lockdep.c */
33 #define LOCKHASH_BITS 12
34 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
36 static struct list_head lockhash_table[LOCKHASH_SIZE];
38 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
39 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
42 struct list_head hash_entry;
43 struct rb_node rb; /* used for sorting */
46 * FIXME: perf_evsel__intval() returns u64,
47 * so address of lockdep_map should be dealed as 64bit.
48 * Is there more better solution?
50 void *addr; /* address of lockdep_map, used as ID */
51 char *name; /* for strcpy(), we cannot use const */
53 unsigned int nr_acquire;
54 unsigned int nr_acquired;
55 unsigned int nr_contended;
56 unsigned int nr_release;
58 unsigned int nr_readlock;
59 unsigned int nr_trylock;
61 /* these times are in nano sec. */
67 int discard; /* flag of blacklist */
71 * States of lock_seq_stat
73 * UNINITIALIZED is required for detecting first event of acquire.
74 * As the nature of lock events, there is no guarantee
75 * that the first event for the locks are acquire,
76 * it can be acquired, contended or release.
78 #define SEQ_STATE_UNINITIALIZED 0 /* initial state */
79 #define SEQ_STATE_RELEASED 1
80 #define SEQ_STATE_ACQUIRING 2
81 #define SEQ_STATE_ACQUIRED 3
82 #define SEQ_STATE_READ_ACQUIRED 4
83 #define SEQ_STATE_CONTENDED 5
87 * Imported from include/linux/sched.h.
88 * Should this be synchronized?
90 #define MAX_LOCK_DEPTH 48
93 * struct lock_seq_stat:
94 * Place to put on state of one lock sequence
95 * 1) acquire -> acquired -> release
96 * 2) acquire -> contended -> acquired -> release
97 * 3) acquire (with read or try) -> release
98 * 4) Are there other patterns?
100 struct lock_seq_stat {
101 struct list_head list;
113 struct list_head seq_list;
116 static struct rb_root thread_stats;
118 static struct thread_stat *thread_stat_find(u32 tid)
120 struct rb_node *node;
121 struct thread_stat *st;
123 node = thread_stats.rb_node;
125 st = container_of(node, struct thread_stat, rb);
128 else if (tid < st->tid)
129 node = node->rb_left;
131 node = node->rb_right;
137 static void thread_stat_insert(struct thread_stat *new)
139 struct rb_node **rb = &thread_stats.rb_node;
140 struct rb_node *parent = NULL;
141 struct thread_stat *p;
144 p = container_of(*rb, struct thread_stat, rb);
147 if (new->tid < p->tid)
148 rb = &(*rb)->rb_left;
149 else if (new->tid > p->tid)
150 rb = &(*rb)->rb_right;
152 BUG_ON("inserting invalid thread_stat\n");
155 rb_link_node(&new->rb, parent, rb);
156 rb_insert_color(&new->rb, &thread_stats);
159 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
161 struct thread_stat *st;
163 st = thread_stat_find(tid);
167 st = zalloc(sizeof(struct thread_stat));
169 pr_err("memory allocation failed\n");
174 INIT_LIST_HEAD(&st->seq_list);
176 thread_stat_insert(st);
181 static struct thread_stat *thread_stat_findnew_first(u32 tid);
182 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
183 thread_stat_findnew_first;
185 static struct thread_stat *thread_stat_findnew_first(u32 tid)
187 struct thread_stat *st;
189 st = zalloc(sizeof(struct thread_stat));
191 pr_err("memory allocation failed\n");
195 INIT_LIST_HEAD(&st->seq_list);
197 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
198 rb_insert_color(&st->rb, &thread_stats);
200 thread_stat_findnew = thread_stat_findnew_after_first;
204 /* build simple key function one is bigger than two */
205 #define SINGLE_KEY(member) \
206 static int lock_stat_key_ ## member(struct lock_stat *one, \
207 struct lock_stat *two) \
209 return one->member > two->member; \
212 SINGLE_KEY(nr_acquired)
213 SINGLE_KEY(nr_contended)
214 SINGLE_KEY(avg_wait_time)
215 SINGLE_KEY(wait_time_total)
216 SINGLE_KEY(wait_time_max)
218 static int lock_stat_key_wait_time_min(struct lock_stat *one,
219 struct lock_stat *two)
221 u64 s1 = one->wait_time_min;
222 u64 s2 = two->wait_time_min;
223 if (s1 == ULLONG_MAX)
225 if (s2 == ULLONG_MAX)
232 * name: the value for specify by user
233 * this should be simpler than raw name of member
234 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
237 int (*key)(struct lock_stat*, struct lock_stat*);
240 static const char *sort_key = "acquired";
242 static int (*compare)(struct lock_stat *, struct lock_stat *);
244 static struct rb_root result; /* place to store sorted data */
246 #define DEF_KEY_LOCK(name, fn_suffix) \
247 { #name, lock_stat_key_ ## fn_suffix }
248 struct lock_key keys[] = {
249 DEF_KEY_LOCK(acquired, nr_acquired),
250 DEF_KEY_LOCK(contended, nr_contended),
251 DEF_KEY_LOCK(avg_wait, avg_wait_time),
252 DEF_KEY_LOCK(wait_total, wait_time_total),
253 DEF_KEY_LOCK(wait_min, wait_time_min),
254 DEF_KEY_LOCK(wait_max, wait_time_max),
256 /* extra comparisons much complicated should be here */
261 static int select_key(void)
265 for (i = 0; keys[i].name; i++) {
266 if (!strcmp(keys[i].name, sort_key)) {
267 compare = keys[i].key;
272 pr_err("Unknown compare key: %s\n", sort_key);
277 static void insert_to_result(struct lock_stat *st,
278 int (*bigger)(struct lock_stat *, struct lock_stat *))
280 struct rb_node **rb = &result.rb_node;
281 struct rb_node *parent = NULL;
285 p = container_of(*rb, struct lock_stat, rb);
289 rb = &(*rb)->rb_left;
291 rb = &(*rb)->rb_right;
294 rb_link_node(&st->rb, parent, rb);
295 rb_insert_color(&st->rb, &result);
298 /* returns left most element of result, and erase it */
299 static struct lock_stat *pop_from_result(void)
301 struct rb_node *node = result.rb_node;
306 while (node->rb_left)
307 node = node->rb_left;
309 rb_erase(node, &result);
310 return container_of(node, struct lock_stat, rb);
313 static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
315 struct list_head *entry = lockhashentry(addr);
316 struct lock_stat *ret, *new;
318 list_for_each_entry(ret, entry, hash_entry) {
319 if (ret->addr == addr)
323 new = zalloc(sizeof(struct lock_stat));
328 new->name = zalloc(sizeof(char) * strlen(name) + 1);
334 strcpy(new->name, name);
335 new->wait_time_min = ULLONG_MAX;
337 list_add(&new->hash_entry, entry);
341 pr_err("memory allocation failed\n");
345 struct trace_lock_handler {
346 int (*acquire_event)(struct perf_evsel *evsel,
347 struct perf_sample *sample);
349 int (*acquired_event)(struct perf_evsel *evsel,
350 struct perf_sample *sample);
352 int (*contended_event)(struct perf_evsel *evsel,
353 struct perf_sample *sample);
355 int (*release_event)(struct perf_evsel *evsel,
356 struct perf_sample *sample);
359 static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
361 struct lock_seq_stat *seq;
363 list_for_each_entry(seq, &ts->seq_list, list) {
364 if (seq->addr == addr)
368 seq = zalloc(sizeof(struct lock_seq_stat));
370 pr_err("memory allocation failed\n");
373 seq->state = SEQ_STATE_UNINITIALIZED;
376 list_add(&seq->list, &ts->seq_list);
388 static int bad_hist[BROKEN_MAX];
395 static int report_lock_acquire_event(struct perf_evsel *evsel,
396 struct perf_sample *sample)
399 struct lock_stat *ls;
400 struct thread_stat *ts;
401 struct lock_seq_stat *seq;
402 const char *name = perf_evsel__strval(evsel, sample, "name");
403 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
404 int flag = perf_evsel__intval(evsel, sample, "flag");
406 memcpy(&addr, &tmp, sizeof(void *));
408 ls = lock_stat_findnew(addr, name);
414 ts = thread_stat_findnew(sample->tid);
418 seq = get_seq(ts, addr);
422 switch (seq->state) {
423 case SEQ_STATE_UNINITIALIZED:
424 case SEQ_STATE_RELEASED:
426 seq->state = SEQ_STATE_ACQUIRING;
430 if (flag & READ_LOCK)
432 seq->state = SEQ_STATE_READ_ACQUIRED;
437 case SEQ_STATE_READ_ACQUIRED:
438 if (flag & READ_LOCK) {
446 case SEQ_STATE_ACQUIRED:
447 case SEQ_STATE_ACQUIRING:
448 case SEQ_STATE_CONTENDED:
450 /* broken lock sequence, discard it */
452 bad_hist[BROKEN_ACQUIRE]++;
453 list_del(&seq->list);
457 BUG_ON("Unknown state of lock sequence found!\n");
462 seq->prev_event_time = sample->time;
467 static int report_lock_acquired_event(struct perf_evsel *evsel,
468 struct perf_sample *sample)
471 struct lock_stat *ls;
472 struct thread_stat *ts;
473 struct lock_seq_stat *seq;
475 const char *name = perf_evsel__strval(evsel, sample, "name");
476 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
478 memcpy(&addr, &tmp, sizeof(void *));
480 ls = lock_stat_findnew(addr, name);
486 ts = thread_stat_findnew(sample->tid);
490 seq = get_seq(ts, addr);
494 switch (seq->state) {
495 case SEQ_STATE_UNINITIALIZED:
496 /* orphan event, do nothing */
498 case SEQ_STATE_ACQUIRING:
500 case SEQ_STATE_CONTENDED:
501 contended_term = sample->time - seq->prev_event_time;
502 ls->wait_time_total += contended_term;
503 if (contended_term < ls->wait_time_min)
504 ls->wait_time_min = contended_term;
505 if (ls->wait_time_max < contended_term)
506 ls->wait_time_max = contended_term;
508 case SEQ_STATE_RELEASED:
509 case SEQ_STATE_ACQUIRED:
510 case SEQ_STATE_READ_ACQUIRED:
511 /* broken lock sequence, discard it */
513 bad_hist[BROKEN_ACQUIRED]++;
514 list_del(&seq->list);
518 BUG_ON("Unknown state of lock sequence found!\n");
522 seq->state = SEQ_STATE_ACQUIRED;
524 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
525 seq->prev_event_time = sample->time;
530 static int report_lock_contended_event(struct perf_evsel *evsel,
531 struct perf_sample *sample)
534 struct lock_stat *ls;
535 struct thread_stat *ts;
536 struct lock_seq_stat *seq;
537 const char *name = perf_evsel__strval(evsel, sample, "name");
538 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
540 memcpy(&addr, &tmp, sizeof(void *));
542 ls = lock_stat_findnew(addr, name);
548 ts = thread_stat_findnew(sample->tid);
552 seq = get_seq(ts, addr);
556 switch (seq->state) {
557 case SEQ_STATE_UNINITIALIZED:
558 /* orphan event, do nothing */
560 case SEQ_STATE_ACQUIRING:
562 case SEQ_STATE_RELEASED:
563 case SEQ_STATE_ACQUIRED:
564 case SEQ_STATE_READ_ACQUIRED:
565 case SEQ_STATE_CONTENDED:
566 /* broken lock sequence, discard it */
568 bad_hist[BROKEN_CONTENDED]++;
569 list_del(&seq->list);
573 BUG_ON("Unknown state of lock sequence found!\n");
577 seq->state = SEQ_STATE_CONTENDED;
579 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
580 seq->prev_event_time = sample->time;
585 static int report_lock_release_event(struct perf_evsel *evsel,
586 struct perf_sample *sample)
589 struct lock_stat *ls;
590 struct thread_stat *ts;
591 struct lock_seq_stat *seq;
592 const char *name = perf_evsel__strval(evsel, sample, "name");
593 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
595 memcpy(&addr, &tmp, sizeof(void *));
597 ls = lock_stat_findnew(addr, name);
603 ts = thread_stat_findnew(sample->tid);
607 seq = get_seq(ts, addr);
611 switch (seq->state) {
612 case SEQ_STATE_UNINITIALIZED:
614 case SEQ_STATE_ACQUIRED:
616 case SEQ_STATE_READ_ACQUIRED:
618 BUG_ON(seq->read_count < 0);
619 if (!seq->read_count) {
624 case SEQ_STATE_ACQUIRING:
625 case SEQ_STATE_CONTENDED:
626 case SEQ_STATE_RELEASED:
627 /* broken lock sequence, discard it */
629 bad_hist[BROKEN_RELEASE]++;
632 BUG_ON("Unknown state of lock sequence found!\n");
638 list_del(&seq->list);
644 /* lock oriented handlers */
645 /* TODO: handlers for CPU oriented, thread oriented */
646 static struct trace_lock_handler report_lock_ops = {
647 .acquire_event = report_lock_acquire_event,
648 .acquired_event = report_lock_acquired_event,
649 .contended_event = report_lock_contended_event,
650 .release_event = report_lock_release_event,
653 static struct trace_lock_handler *trace_handler;
655 static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
656 struct perf_sample *sample)
658 if (trace_handler->acquire_event)
659 return trace_handler->acquire_event(evsel, sample);
663 static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
664 struct perf_sample *sample)
666 if (trace_handler->acquired_event)
667 return trace_handler->acquired_event(evsel, sample);
671 static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
672 struct perf_sample *sample)
674 if (trace_handler->contended_event)
675 return trace_handler->contended_event(evsel, sample);
679 static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
680 struct perf_sample *sample)
682 if (trace_handler->release_event)
683 return trace_handler->release_event(evsel, sample);
687 static void print_bad_events(int bad, int total)
689 /* Output for debug, this have to be removed */
691 const char *name[4] =
692 { "acquire", "acquired", "contended", "release" };
694 pr_info("\n=== output for debug===\n\n");
695 pr_info("bad: %d, total: %d\n", bad, total);
696 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
697 pr_info("histogram of events caused bad sequence\n");
698 for (i = 0; i < BROKEN_MAX; i++)
699 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
702 /* TODO: various way to print, coloring, nano or milli sec */
703 static void print_result(void)
705 struct lock_stat *st;
709 pr_info("%20s ", "Name");
710 pr_info("%10s ", "acquired");
711 pr_info("%10s ", "contended");
713 pr_info("%15s ", "avg wait (ns)");
714 pr_info("%15s ", "total wait (ns)");
715 pr_info("%15s ", "max wait (ns)");
716 pr_info("%15s ", "min wait (ns)");
721 while ((st = pop_from_result())) {
729 if (strlen(st->name) < 16) {
730 /* output raw name */
731 pr_info("%20s ", st->name);
733 strncpy(cut_name, st->name, 16);
738 /* cut off name for saving output style */
739 pr_info("%20s ", cut_name);
742 pr_info("%10u ", st->nr_acquired);
743 pr_info("%10u ", st->nr_contended);
745 pr_info("%15" PRIu64 " ", st->avg_wait_time);
746 pr_info("%15" PRIu64 " ", st->wait_time_total);
747 pr_info("%15" PRIu64 " ", st->wait_time_max);
748 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
749 0 : st->wait_time_min);
753 print_bad_events(bad, total);
756 static bool info_threads, info_map;
758 static void dump_threads(void)
760 struct thread_stat *st;
761 struct rb_node *node;
764 pr_info("%10s: comm\n", "Thread ID");
766 node = rb_first(&thread_stats);
768 st = container_of(node, struct thread_stat, rb);
769 t = perf_session__findnew(session, st->tid);
770 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
771 node = rb_next(node);
775 static void dump_map(void)
778 struct lock_stat *st;
780 pr_info("Address of instance: name of class\n");
781 for (i = 0; i < LOCKHASH_SIZE; i++) {
782 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
783 pr_info(" %p: %s\n", st->addr, st->name);
788 static int dump_info(void)
798 pr_err("Unknown type of information\n");
804 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
805 struct perf_sample *sample);
807 static int process_sample_event(struct perf_tool *tool __maybe_unused,
808 union perf_event *event,
809 struct perf_sample *sample,
810 struct perf_evsel *evsel,
811 struct machine *machine)
813 struct thread *thread = machine__findnew_thread(machine, sample->pid,
816 if (thread == NULL) {
817 pr_debug("problem processing %d event, skipping it.\n",
822 if (evsel->handler != NULL) {
823 tracepoint_handler f = evsel->handler;
824 return f(evsel, sample);
830 static void sort_result(void)
833 struct lock_stat *st;
835 for (i = 0; i < LOCKHASH_SIZE; i++) {
836 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
837 insert_to_result(st, compare);
842 static const struct perf_evsel_str_handler lock_tracepoints[] = {
843 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
844 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
845 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
846 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
849 static int __cmd_report(bool display_info)
852 struct perf_tool eops = {
853 .sample = process_sample_event,
854 .comm = perf_event__process_comm,
855 .ordered_samples = true,
857 struct perf_data_file file = {
859 .mode = PERF_DATA_MODE_READ,
862 session = perf_session__new(&file, false, &eops);
864 pr_err("Initializing perf session failed\n");
868 if (!perf_session__has_traces(session, "lock record"))
871 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
872 pr_err("Initializing perf session tracepoint handlers failed\n");
879 err = perf_session__process_events(session, &eops);
884 if (display_info) /* used for info subcommand */
892 perf_session__delete(session);
896 static int __cmd_record(int argc, const char **argv)
898 const char *record_args[] = {
899 "record", "-R", "-m", "1024", "-c", "1",
901 unsigned int rec_argc, i, j, ret;
902 const char **rec_argv;
904 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
905 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
906 pr_err("tracepoint %s is not enabled. "
907 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
908 lock_tracepoints[i].name);
913 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
914 /* factor of 2 is for -e in front of each tracepoint */
915 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
917 rec_argv = calloc(rec_argc + 1, sizeof(char *));
921 for (i = 0; i < ARRAY_SIZE(record_args); i++)
922 rec_argv[i] = strdup(record_args[i]);
924 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
925 rec_argv[i++] = "-e";
926 rec_argv[i++] = strdup(lock_tracepoints[j].name);
929 for (j = 1; j < (unsigned int)argc; j++, i++)
930 rec_argv[i] = argv[j];
932 BUG_ON(i != rec_argc);
934 ret = cmd_record(i, rec_argv, NULL);
939 int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
941 const struct option info_options[] = {
942 OPT_BOOLEAN('t', "threads", &info_threads,
943 "dump thread list in perf.data"),
944 OPT_BOOLEAN('m', "map", &info_map,
945 "map of lock instances (address:name table)"),
948 const struct option lock_options[] = {
949 OPT_STRING('i', "input", &input_name, "file", "input file name"),
950 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
951 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
954 const struct option report_options[] = {
955 OPT_STRING('k', "key", &sort_key, "acquired",
956 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
960 const char * const info_usage[] = {
961 "perf lock info [<options>]",
964 const char *const lock_subcommands[] = { "record", "report", "script",
966 const char *lock_usage[] = {
970 const char * const report_usage[] = {
971 "perf lock report [<options>]",
978 for (i = 0; i < LOCKHASH_SIZE; i++)
979 INIT_LIST_HEAD(lockhash_table + i);
981 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
982 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
984 usage_with_options(lock_usage, lock_options);
986 if (!strncmp(argv[0], "rec", 3)) {
987 return __cmd_record(argc, argv);
988 } else if (!strncmp(argv[0], "report", 6)) {
989 trace_handler = &report_lock_ops;
991 argc = parse_options(argc, argv,
992 report_options, report_usage, 0);
994 usage_with_options(report_usage, report_options);
996 rc = __cmd_report(false);
997 } else if (!strcmp(argv[0], "script")) {
998 /* Aliased to 'perf script' */
999 return cmd_script(argc, argv, prefix);
1000 } else if (!strcmp(argv[0], "info")) {
1002 argc = parse_options(argc, argv,
1003 info_options, info_usage, 0);
1005 usage_with_options(info_usage, info_options);
1007 /* recycling report_lock_ops */
1008 trace_handler = &report_lock_ops;
1009 rc = __cmd_report(true);
1011 usage_with_options(lock_usage, lock_options);