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"
13 #include <subcmd/pager.h>
14 #include <subcmd/parse-options.h>
15 #include "util/trace-event.h"
17 #include "util/debug.h"
18 #include "util/session.h"
19 #include "util/tool.h"
20 #include "util/data.h"
21 #include "util/string2.h"
23 #include <sys/types.h>
24 #include <sys/prctl.h>
25 #include <semaphore.h>
30 #include <linux/list.h>
31 #include <linux/hash.h>
32 #include <linux/kernel.h>
33 #include <linux/zalloc.h>
34 #include <linux/err.h>
36 static struct perf_session *session;
38 /* based on kernel/lockdep.c */
39 #define LOCKHASH_BITS 12
40 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
42 static struct hlist_head lockhash_table[LOCKHASH_SIZE];
44 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
45 #define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
48 struct hlist_node hash_entry;
49 struct rb_node rb; /* used for sorting */
51 u64 addr; /* address of lockdep_map, used as ID */
52 char *name; /* for strcpy(), we cannot use const */
54 unsigned int nr_acquire;
55 unsigned int nr_acquired;
56 unsigned int nr_contended;
57 unsigned int nr_release;
59 unsigned int nr_readlock;
60 unsigned int nr_trylock;
62 /* these times are in nano sec. */
68 int broken; /* flag of blacklist */
73 * States of lock_seq_stat
75 * UNINITIALIZED is required for detecting first event of acquire.
76 * As the nature of lock events, there is no guarantee
77 * that the first event for the locks are acquire,
78 * it can be acquired, contended or release.
80 #define SEQ_STATE_UNINITIALIZED 0 /* initial state */
81 #define SEQ_STATE_RELEASED 1
82 #define SEQ_STATE_ACQUIRING 2
83 #define SEQ_STATE_ACQUIRED 3
84 #define SEQ_STATE_READ_ACQUIRED 4
85 #define SEQ_STATE_CONTENDED 5
89 * Imported from include/linux/sched.h.
90 * Should this be synchronized?
92 #define MAX_LOCK_DEPTH 48
95 * struct lock_seq_stat:
96 * Place to put on state of one lock sequence
97 * 1) acquire -> acquired -> release
98 * 2) acquire -> contended -> acquired -> release
99 * 3) acquire (with read or try) -> release
100 * 4) Are there other patterns?
102 struct lock_seq_stat {
103 struct list_head list;
115 struct list_head seq_list;
118 static struct rb_root thread_stats;
120 static bool combine_locks;
121 static bool show_thread_stats;
123 static struct thread_stat *thread_stat_find(u32 tid)
125 struct rb_node *node;
126 struct thread_stat *st;
128 node = thread_stats.rb_node;
130 st = container_of(node, struct thread_stat, rb);
133 else if (tid < st->tid)
134 node = node->rb_left;
136 node = node->rb_right;
142 static void thread_stat_insert(struct thread_stat *new)
144 struct rb_node **rb = &thread_stats.rb_node;
145 struct rb_node *parent = NULL;
146 struct thread_stat *p;
149 p = container_of(*rb, struct thread_stat, rb);
152 if (new->tid < p->tid)
153 rb = &(*rb)->rb_left;
154 else if (new->tid > p->tid)
155 rb = &(*rb)->rb_right;
157 BUG_ON("inserting invalid thread_stat\n");
160 rb_link_node(&new->rb, parent, rb);
161 rb_insert_color(&new->rb, &thread_stats);
164 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
166 struct thread_stat *st;
168 st = thread_stat_find(tid);
172 st = zalloc(sizeof(struct thread_stat));
174 pr_err("memory allocation failed\n");
179 INIT_LIST_HEAD(&st->seq_list);
181 thread_stat_insert(st);
186 static struct thread_stat *thread_stat_findnew_first(u32 tid);
187 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
188 thread_stat_findnew_first;
190 static struct thread_stat *thread_stat_findnew_first(u32 tid)
192 struct thread_stat *st;
194 st = zalloc(sizeof(struct thread_stat));
196 pr_err("memory allocation failed\n");
200 INIT_LIST_HEAD(&st->seq_list);
202 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
203 rb_insert_color(&st->rb, &thread_stats);
205 thread_stat_findnew = thread_stat_findnew_after_first;
209 /* build simple key function one is bigger than two */
210 #define SINGLE_KEY(member) \
211 static int lock_stat_key_ ## member(struct lock_stat *one, \
212 struct lock_stat *two) \
214 return one->member > two->member; \
217 SINGLE_KEY(nr_acquired)
218 SINGLE_KEY(nr_contended)
219 SINGLE_KEY(avg_wait_time)
220 SINGLE_KEY(wait_time_total)
221 SINGLE_KEY(wait_time_max)
223 static int lock_stat_key_wait_time_min(struct lock_stat *one,
224 struct lock_stat *two)
226 u64 s1 = one->wait_time_min;
227 u64 s2 = two->wait_time_min;
228 if (s1 == ULLONG_MAX)
230 if (s2 == ULLONG_MAX)
237 * name: the value for specify by user
238 * this should be simpler than raw name of member
239 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
242 /* header: the string printed on the header line */
244 /* len: the printing width of the field */
246 /* key: a pointer to function to compare two lock stats for sorting */
247 int (*key)(struct lock_stat*, struct lock_stat*);
248 /* print: a pointer to function to print a given lock stats */
249 void (*print)(struct lock_key*, struct lock_stat*);
250 /* list: list entry to link this */
251 struct list_head list;
254 #define PRINT_KEY(member) \
255 static void lock_stat_key_print_ ## member(struct lock_key *key, \
256 struct lock_stat *ls) \
258 pr_info("%*llu", key->len, (unsigned long long)ls->member); \
261 PRINT_KEY(nr_acquired)
262 PRINT_KEY(nr_contended)
263 PRINT_KEY(avg_wait_time)
264 PRINT_KEY(wait_time_total)
265 PRINT_KEY(wait_time_max)
267 static void lock_stat_key_print_wait_time_min(struct lock_key *key,
268 struct lock_stat *ls)
270 u64 wait_time = ls->wait_time_min;
272 if (wait_time == ULLONG_MAX)
275 pr_info("%*"PRIu64, key->len, wait_time);
279 static const char *sort_key = "acquired";
281 static int (*compare)(struct lock_stat *, struct lock_stat *);
283 static struct rb_root sorted; /* place to store intermediate data */
284 static struct rb_root result; /* place to store sorted data */
286 static LIST_HEAD(lock_keys);
287 static const char *output_fields;
289 #define DEF_KEY_LOCK(name, header, fn_suffix, len) \
290 { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
291 struct lock_key keys[] = {
292 DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
293 DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
294 DEF_KEY_LOCK(avg_wait, "avg wait (ns)", avg_wait_time, 15),
295 DEF_KEY_LOCK(wait_total, "total wait (ns)", wait_time_total, 15),
296 DEF_KEY_LOCK(wait_max, "max wait (ns)", wait_time_max, 15),
297 DEF_KEY_LOCK(wait_min, "min wait (ns)", wait_time_min, 15),
299 /* extra comparisons much complicated should be here */
303 static int select_key(void)
307 for (i = 0; keys[i].name; i++) {
308 if (!strcmp(keys[i].name, sort_key)) {
309 compare = keys[i].key;
311 /* selected key should be in the output fields */
312 if (list_empty(&keys[i].list))
313 list_add_tail(&keys[i].list, &lock_keys);
319 pr_err("Unknown compare key: %s\n", sort_key);
323 static int add_output_field(struct list_head *head, char *name)
327 for (i = 0; keys[i].name; i++) {
328 if (strcmp(keys[i].name, name))
331 /* prevent double link */
332 if (list_empty(&keys[i].list))
333 list_add_tail(&keys[i].list, head);
338 pr_err("Unknown output field: %s\n", name);
342 static int setup_output_field(const char *str)
344 char *tok, *tmp, *orig;
347 /* no output field given: use all of them */
349 for (i = 0; keys[i].name; i++)
350 list_add_tail(&keys[i].list, &lock_keys);
354 for (i = 0; keys[i].name; i++)
355 INIT_LIST_HEAD(&keys[i].list);
357 orig = tmp = strdup(str);
361 while ((tok = strsep(&tmp, ",")) != NULL){
362 ret = add_output_field(&lock_keys, tok);
371 static void combine_lock_stats(struct lock_stat *st)
373 struct rb_node **rb = &sorted.rb_node;
374 struct rb_node *parent = NULL;
379 p = container_of(*rb, struct lock_stat, rb);
382 if (st->name && p->name)
383 ret = strcmp(st->name, p->name);
385 ret = !!st->name - !!p->name;
388 p->nr_acquired += st->nr_acquired;
389 p->nr_contended += st->nr_contended;
390 p->wait_time_total += st->wait_time_total;
393 p->avg_wait_time = p->wait_time_total / p->nr_contended;
395 if (p->wait_time_min > st->wait_time_min)
396 p->wait_time_min = st->wait_time_min;
397 if (p->wait_time_max < st->wait_time_max)
398 p->wait_time_max = st->wait_time_max;
400 p->broken |= st->broken;
406 rb = &(*rb)->rb_left;
408 rb = &(*rb)->rb_right;
411 rb_link_node(&st->rb, parent, rb);
412 rb_insert_color(&st->rb, &sorted);
415 static void insert_to_result(struct lock_stat *st,
416 int (*bigger)(struct lock_stat *, struct lock_stat *))
418 struct rb_node **rb = &result.rb_node;
419 struct rb_node *parent = NULL;
422 if (combine_locks && st->combined)
426 p = container_of(*rb, struct lock_stat, rb);
430 rb = &(*rb)->rb_left;
432 rb = &(*rb)->rb_right;
435 rb_link_node(&st->rb, parent, rb);
436 rb_insert_color(&st->rb, &result);
439 /* returns left most element of result, and erase it */
440 static struct lock_stat *pop_from_result(void)
442 struct rb_node *node = result.rb_node;
447 while (node->rb_left)
448 node = node->rb_left;
450 rb_erase(node, &result);
451 return container_of(node, struct lock_stat, rb);
454 static struct lock_stat *lock_stat_findnew(u64 addr, const char *name)
456 struct hlist_head *entry = lockhashentry(addr);
457 struct lock_stat *ret, *new;
459 hlist_for_each_entry(ret, entry, hash_entry) {
460 if (ret->addr == addr)
464 new = zalloc(sizeof(struct lock_stat));
469 new->name = zalloc(sizeof(char) * strlen(name) + 1);
475 strcpy(new->name, name);
476 new->wait_time_min = ULLONG_MAX;
478 hlist_add_head(&new->hash_entry, entry);
482 pr_err("memory allocation failed\n");
486 struct trace_lock_handler {
487 int (*acquire_event)(struct evsel *evsel,
488 struct perf_sample *sample);
490 int (*acquired_event)(struct evsel *evsel,
491 struct perf_sample *sample);
493 int (*contended_event)(struct evsel *evsel,
494 struct perf_sample *sample);
496 int (*release_event)(struct evsel *evsel,
497 struct perf_sample *sample);
500 static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
502 struct lock_seq_stat *seq;
504 list_for_each_entry(seq, &ts->seq_list, list) {
505 if (seq->addr == addr)
509 seq = zalloc(sizeof(struct lock_seq_stat));
511 pr_err("memory allocation failed\n");
514 seq->state = SEQ_STATE_UNINITIALIZED;
517 list_add(&seq->list, &ts->seq_list);
529 static int bad_hist[BROKEN_MAX];
536 static int report_lock_acquire_event(struct evsel *evsel,
537 struct perf_sample *sample)
539 struct lock_stat *ls;
540 struct thread_stat *ts;
541 struct lock_seq_stat *seq;
542 const char *name = evsel__strval(evsel, sample, "name");
543 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
544 int flag = evsel__intval(evsel, sample, "flags");
546 /* abuse ls->addr for tid */
547 if (show_thread_stats)
550 ls = lock_stat_findnew(addr, name);
554 ts = thread_stat_findnew(sample->tid);
558 seq = get_seq(ts, addr);
562 switch (seq->state) {
563 case SEQ_STATE_UNINITIALIZED:
564 case SEQ_STATE_RELEASED:
566 seq->state = SEQ_STATE_ACQUIRING;
570 if (flag & READ_LOCK)
572 seq->state = SEQ_STATE_READ_ACQUIRED;
577 case SEQ_STATE_READ_ACQUIRED:
578 if (flag & READ_LOCK) {
586 case SEQ_STATE_ACQUIRED:
587 case SEQ_STATE_ACQUIRING:
588 case SEQ_STATE_CONTENDED:
590 /* broken lock sequence */
593 bad_hist[BROKEN_ACQUIRE]++;
595 list_del_init(&seq->list);
599 BUG_ON("Unknown state of lock sequence found!\n");
604 seq->prev_event_time = sample->time;
609 static int report_lock_acquired_event(struct evsel *evsel,
610 struct perf_sample *sample)
612 struct lock_stat *ls;
613 struct thread_stat *ts;
614 struct lock_seq_stat *seq;
616 const char *name = evsel__strval(evsel, sample, "name");
617 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
619 if (show_thread_stats)
622 ls = lock_stat_findnew(addr, name);
626 ts = thread_stat_findnew(sample->tid);
630 seq = get_seq(ts, addr);
634 switch (seq->state) {
635 case SEQ_STATE_UNINITIALIZED:
636 /* orphan event, do nothing */
638 case SEQ_STATE_ACQUIRING:
640 case SEQ_STATE_CONTENDED:
641 contended_term = sample->time - seq->prev_event_time;
642 ls->wait_time_total += contended_term;
643 if (contended_term < ls->wait_time_min)
644 ls->wait_time_min = contended_term;
645 if (ls->wait_time_max < contended_term)
646 ls->wait_time_max = contended_term;
648 case SEQ_STATE_RELEASED:
649 case SEQ_STATE_ACQUIRED:
650 case SEQ_STATE_READ_ACQUIRED:
651 /* broken lock sequence */
654 bad_hist[BROKEN_ACQUIRED]++;
656 list_del_init(&seq->list);
660 BUG_ON("Unknown state of lock sequence found!\n");
664 seq->state = SEQ_STATE_ACQUIRED;
666 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
667 seq->prev_event_time = sample->time;
672 static int report_lock_contended_event(struct evsel *evsel,
673 struct perf_sample *sample)
675 struct lock_stat *ls;
676 struct thread_stat *ts;
677 struct lock_seq_stat *seq;
678 const char *name = evsel__strval(evsel, sample, "name");
679 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
681 if (show_thread_stats)
684 ls = lock_stat_findnew(addr, name);
688 ts = thread_stat_findnew(sample->tid);
692 seq = get_seq(ts, addr);
696 switch (seq->state) {
697 case SEQ_STATE_UNINITIALIZED:
698 /* orphan event, do nothing */
700 case SEQ_STATE_ACQUIRING:
702 case SEQ_STATE_RELEASED:
703 case SEQ_STATE_ACQUIRED:
704 case SEQ_STATE_READ_ACQUIRED:
705 case SEQ_STATE_CONTENDED:
706 /* broken lock sequence */
709 bad_hist[BROKEN_CONTENDED]++;
711 list_del_init(&seq->list);
715 BUG_ON("Unknown state of lock sequence found!\n");
719 seq->state = SEQ_STATE_CONTENDED;
721 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
722 seq->prev_event_time = sample->time;
727 static int report_lock_release_event(struct evsel *evsel,
728 struct perf_sample *sample)
730 struct lock_stat *ls;
731 struct thread_stat *ts;
732 struct lock_seq_stat *seq;
733 const char *name = evsel__strval(evsel, sample, "name");
734 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
736 if (show_thread_stats)
739 ls = lock_stat_findnew(addr, name);
743 ts = thread_stat_findnew(sample->tid);
747 seq = get_seq(ts, addr);
751 switch (seq->state) {
752 case SEQ_STATE_UNINITIALIZED:
754 case SEQ_STATE_ACQUIRED:
756 case SEQ_STATE_READ_ACQUIRED:
758 BUG_ON(seq->read_count < 0);
759 if (seq->read_count) {
764 case SEQ_STATE_ACQUIRING:
765 case SEQ_STATE_CONTENDED:
766 case SEQ_STATE_RELEASED:
767 /* broken lock sequence */
770 bad_hist[BROKEN_RELEASE]++;
774 BUG_ON("Unknown state of lock sequence found!\n");
780 list_del_init(&seq->list);
786 /* lock oriented handlers */
787 /* TODO: handlers for CPU oriented, thread oriented */
788 static struct trace_lock_handler report_lock_ops = {
789 .acquire_event = report_lock_acquire_event,
790 .acquired_event = report_lock_acquired_event,
791 .contended_event = report_lock_contended_event,
792 .release_event = report_lock_release_event,
795 static struct trace_lock_handler *trace_handler;
797 static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
799 if (trace_handler->acquire_event)
800 return trace_handler->acquire_event(evsel, sample);
804 static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
806 if (trace_handler->acquired_event)
807 return trace_handler->acquired_event(evsel, sample);
811 static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
813 if (trace_handler->contended_event)
814 return trace_handler->contended_event(evsel, sample);
818 static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
820 if (trace_handler->release_event)
821 return trace_handler->release_event(evsel, sample);
825 static void print_bad_events(int bad, int total)
827 /* Output for debug, this have to be removed */
829 const char *name[4] =
830 { "acquire", "acquired", "contended", "release" };
832 pr_info("\n=== output for debug===\n\n");
833 pr_info("bad: %d, total: %d\n", bad, total);
834 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
835 pr_info("histogram of events caused bad sequence\n");
836 for (i = 0; i < BROKEN_MAX; i++)
837 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
840 /* TODO: various way to print, coloring, nano or milli sec */
841 static void print_result(void)
843 struct lock_stat *st;
844 struct lock_key *key;
848 pr_info("%20s ", "Name");
849 list_for_each_entry(key, &lock_keys, list)
850 pr_info("%*s ", key->len, key->header);
854 while ((st = pop_from_result())) {
858 if (!st->nr_acquired)
863 if (strlen(st->name) < 20) {
864 /* output raw name */
865 const char *name = st->name;
867 if (show_thread_stats) {
870 /* st->addr contains tid of thread */
871 t = perf_session__findnew(session, st->addr);
872 name = thread__comm_str(t);
875 pr_info("%20s ", name);
877 strncpy(cut_name, st->name, 16);
882 /* cut off name for saving output style */
883 pr_info("%20s ", cut_name);
886 list_for_each_entry(key, &lock_keys, list) {
893 print_bad_events(bad, total);
896 static bool info_threads, info_map;
898 static void dump_threads(void)
900 struct thread_stat *st;
901 struct rb_node *node;
904 pr_info("%10s: comm\n", "Thread ID");
906 node = rb_first(&thread_stats);
908 st = container_of(node, struct thread_stat, rb);
909 t = perf_session__findnew(session, st->tid);
910 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
911 node = rb_next(node);
916 static int compare_maps(struct lock_stat *a, struct lock_stat *b)
920 if (a->name && b->name)
921 ret = strcmp(a->name, b->name);
923 ret = !!a->name - !!b->name;
926 return a->addr < b->addr;
931 static void dump_map(void)
934 struct lock_stat *st;
936 pr_info("Address of instance: name of class\n");
937 for (i = 0; i < LOCKHASH_SIZE; i++) {
938 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
939 insert_to_result(st, compare_maps);
943 while ((st = pop_from_result()))
944 pr_info(" %#llx: %s\n", (unsigned long long)st->addr, st->name);
947 static int dump_info(void)
957 pr_err("Unknown type of information\n");
963 typedef int (*tracepoint_handler)(struct evsel *evsel,
964 struct perf_sample *sample);
966 static int process_sample_event(struct perf_tool *tool __maybe_unused,
967 union perf_event *event,
968 struct perf_sample *sample,
970 struct machine *machine)
973 struct thread *thread = machine__findnew_thread(machine, sample->pid,
976 if (thread == NULL) {
977 pr_debug("problem processing %d event, skipping it.\n",
982 if (evsel->handler != NULL) {
983 tracepoint_handler f = evsel->handler;
984 err = f(evsel, sample);
992 static void combine_result(void)
995 struct lock_stat *st;
1000 for (i = 0; i < LOCKHASH_SIZE; i++) {
1001 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1002 combine_lock_stats(st);
1007 static void sort_result(void)
1010 struct lock_stat *st;
1012 for (i = 0; i < LOCKHASH_SIZE; i++) {
1013 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1014 insert_to_result(st, compare);
1019 static const struct evsel_str_handler lock_tracepoints[] = {
1020 { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
1021 { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1022 { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1023 { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
1028 static int __cmd_report(bool display_info)
1031 struct perf_tool eops = {
1032 .sample = process_sample_event,
1033 .comm = perf_event__process_comm,
1034 .namespaces = perf_event__process_namespaces,
1035 .ordered_events = true,
1037 struct perf_data data = {
1039 .mode = PERF_DATA_MODE_READ,
1043 session = perf_session__new(&data, &eops);
1044 if (IS_ERR(session)) {
1045 pr_err("Initializing perf session failed\n");
1046 return PTR_ERR(session);
1049 symbol__init(&session->header.env);
1051 if (!perf_session__has_traces(session, "lock record"))
1054 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
1055 pr_err("Initializing perf session tracepoint handlers failed\n");
1059 if (setup_output_field(output_fields))
1065 err = perf_session__process_events(session);
1070 if (display_info) /* used for info subcommand */
1079 perf_session__delete(session);
1083 static int __cmd_record(int argc, const char **argv)
1085 const char *record_args[] = {
1086 "record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
1088 unsigned int rec_argc, i, j, ret;
1089 const char **rec_argv;
1091 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
1092 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
1093 pr_err("tracepoint %s is not enabled. "
1094 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
1095 lock_tracepoints[i].name);
1100 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1101 /* factor of 2 is for -e in front of each tracepoint */
1102 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
1104 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1108 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1109 rec_argv[i] = strdup(record_args[i]);
1111 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
1112 rec_argv[i++] = "-e";
1113 rec_argv[i++] = strdup(lock_tracepoints[j].name);
1116 for (j = 1; j < (unsigned int)argc; j++, i++)
1117 rec_argv[i] = argv[j];
1119 BUG_ON(i != rec_argc);
1121 ret = cmd_record(i, rec_argv);
1126 int cmd_lock(int argc, const char **argv)
1128 const struct option lock_options[] = {
1129 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1130 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
1131 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
1132 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1136 const struct option info_options[] = {
1137 OPT_BOOLEAN('t', "threads", &info_threads,
1138 "dump thread list in perf.data"),
1139 OPT_BOOLEAN('m', "map", &info_map,
1140 "map of lock instances (address:name table)"),
1141 OPT_PARENT(lock_options)
1144 const struct option report_options[] = {
1145 OPT_STRING('k', "key", &sort_key, "acquired",
1146 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
1147 OPT_STRING('F', "field", &output_fields, NULL,
1148 "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
1150 OPT_BOOLEAN('c', "combine-locks", &combine_locks,
1151 "combine locks in the same class"),
1152 OPT_BOOLEAN('t', "threads", &show_thread_stats,
1153 "show per-thread lock stats"),
1154 OPT_PARENT(lock_options)
1157 const char * const info_usage[] = {
1158 "perf lock info [<options>]",
1161 const char *const lock_subcommands[] = { "record", "report", "script",
1163 const char *lock_usage[] = {
1167 const char * const report_usage[] = {
1168 "perf lock report [<options>]",
1174 for (i = 0; i < LOCKHASH_SIZE; i++)
1175 INIT_HLIST_HEAD(lockhash_table + i);
1177 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
1178 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
1180 usage_with_options(lock_usage, lock_options);
1182 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
1183 return __cmd_record(argc, argv);
1184 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
1185 trace_handler = &report_lock_ops;
1187 argc = parse_options(argc, argv,
1188 report_options, report_usage, 0);
1190 usage_with_options(report_usage, report_options);
1192 rc = __cmd_report(false);
1193 } else if (!strcmp(argv[0], "script")) {
1194 /* Aliased to 'perf script' */
1195 return cmd_script(argc, argv);
1196 } else if (!strcmp(argv[0], "info")) {
1198 argc = parse_options(argc, argv,
1199 info_options, info_usage, 0);
1201 usage_with_options(info_usage, info_options);
1203 /* recycling report_lock_ops */
1204 trace_handler = &report_lock_ops;
1205 rc = __cmd_report(true);
1207 usage_with_options(lock_usage, lock_options);