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;
122 static struct thread_stat *thread_stat_find(u32 tid)
124 struct rb_node *node;
125 struct thread_stat *st;
127 node = thread_stats.rb_node;
129 st = container_of(node, struct thread_stat, rb);
132 else if (tid < st->tid)
133 node = node->rb_left;
135 node = node->rb_right;
141 static void thread_stat_insert(struct thread_stat *new)
143 struct rb_node **rb = &thread_stats.rb_node;
144 struct rb_node *parent = NULL;
145 struct thread_stat *p;
148 p = container_of(*rb, struct thread_stat, rb);
151 if (new->tid < p->tid)
152 rb = &(*rb)->rb_left;
153 else if (new->tid > p->tid)
154 rb = &(*rb)->rb_right;
156 BUG_ON("inserting invalid thread_stat\n");
159 rb_link_node(&new->rb, parent, rb);
160 rb_insert_color(&new->rb, &thread_stats);
163 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
165 struct thread_stat *st;
167 st = thread_stat_find(tid);
171 st = zalloc(sizeof(struct thread_stat));
173 pr_err("memory allocation failed\n");
178 INIT_LIST_HEAD(&st->seq_list);
180 thread_stat_insert(st);
185 static struct thread_stat *thread_stat_findnew_first(u32 tid);
186 static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
187 thread_stat_findnew_first;
189 static struct thread_stat *thread_stat_findnew_first(u32 tid)
191 struct thread_stat *st;
193 st = zalloc(sizeof(struct thread_stat));
195 pr_err("memory allocation failed\n");
199 INIT_LIST_HEAD(&st->seq_list);
201 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
202 rb_insert_color(&st->rb, &thread_stats);
204 thread_stat_findnew = thread_stat_findnew_after_first;
208 /* build simple key function one is bigger than two */
209 #define SINGLE_KEY(member) \
210 static int lock_stat_key_ ## member(struct lock_stat *one, \
211 struct lock_stat *two) \
213 return one->member > two->member; \
216 SINGLE_KEY(nr_acquired)
217 SINGLE_KEY(nr_contended)
218 SINGLE_KEY(avg_wait_time)
219 SINGLE_KEY(wait_time_total)
220 SINGLE_KEY(wait_time_max)
222 static int lock_stat_key_wait_time_min(struct lock_stat *one,
223 struct lock_stat *two)
225 u64 s1 = one->wait_time_min;
226 u64 s2 = two->wait_time_min;
227 if (s1 == ULLONG_MAX)
229 if (s2 == ULLONG_MAX)
236 * name: the value for specify by user
237 * this should be simpler than raw name of member
238 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
241 /* header: the string printed on the header line */
243 /* len: the printing width of the field */
245 /* key: a pointer to function to compare two lock stats for sorting */
246 int (*key)(struct lock_stat*, struct lock_stat*);
247 /* print: a pointer to function to print a given lock stats */
248 void (*print)(struct lock_key*, struct lock_stat*);
249 /* list: list entry to link this */
250 struct list_head list;
253 #define PRINT_KEY(member) \
254 static void lock_stat_key_print_ ## member(struct lock_key *key, \
255 struct lock_stat *ls) \
257 pr_info("%*llu", key->len, (unsigned long long)ls->member); \
260 PRINT_KEY(nr_acquired)
261 PRINT_KEY(nr_contended)
262 PRINT_KEY(avg_wait_time)
263 PRINT_KEY(wait_time_total)
264 PRINT_KEY(wait_time_max)
266 static void lock_stat_key_print_wait_time_min(struct lock_key *key,
267 struct lock_stat *ls)
269 u64 wait_time = ls->wait_time_min;
271 if (wait_time == ULLONG_MAX)
274 pr_info("%*"PRIu64, key->len, wait_time);
278 static const char *sort_key = "acquired";
280 static int (*compare)(struct lock_stat *, struct lock_stat *);
282 static struct rb_root sorted; /* place to store intermediate data */
283 static struct rb_root result; /* place to store sorted data */
285 static LIST_HEAD(lock_keys);
286 static const char *output_fields;
288 #define DEF_KEY_LOCK(name, header, fn_suffix, len) \
289 { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
290 struct lock_key keys[] = {
291 DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
292 DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
293 DEF_KEY_LOCK(avg_wait, "avg wait (ns)", avg_wait_time, 15),
294 DEF_KEY_LOCK(wait_total, "total wait (ns)", wait_time_total, 15),
295 DEF_KEY_LOCK(wait_max, "max wait (ns)", wait_time_max, 15),
296 DEF_KEY_LOCK(wait_min, "min wait (ns)", wait_time_min, 15),
298 /* extra comparisons much complicated should be here */
302 static int select_key(void)
306 for (i = 0; keys[i].name; i++) {
307 if (!strcmp(keys[i].name, sort_key)) {
308 compare = keys[i].key;
310 /* selected key should be in the output fields */
311 if (list_empty(&keys[i].list))
312 list_add_tail(&keys[i].list, &lock_keys);
318 pr_err("Unknown compare key: %s\n", sort_key);
322 static int add_output_field(struct list_head *head, char *name)
326 for (i = 0; keys[i].name; i++) {
327 if (strcmp(keys[i].name, name))
330 /* prevent double link */
331 if (list_empty(&keys[i].list))
332 list_add_tail(&keys[i].list, head);
337 pr_err("Unknown output field: %s\n", name);
341 static int setup_output_field(const char *str)
343 char *tok, *tmp, *orig;
346 /* no output field given: use all of them */
348 for (i = 0; keys[i].name; i++)
349 list_add_tail(&keys[i].list, &lock_keys);
353 for (i = 0; keys[i].name; i++)
354 INIT_LIST_HEAD(&keys[i].list);
356 orig = tmp = strdup(str);
360 while ((tok = strsep(&tmp, ",")) != NULL){
361 ret = add_output_field(&lock_keys, tok);
370 static void combine_lock_stats(struct lock_stat *st)
372 struct rb_node **rb = &sorted.rb_node;
373 struct rb_node *parent = NULL;
378 p = container_of(*rb, struct lock_stat, rb);
381 if (st->name && p->name)
382 ret = strcmp(st->name, p->name);
384 ret = !!st->name - !!p->name;
387 p->nr_acquired += st->nr_acquired;
388 p->nr_contended += st->nr_contended;
389 p->wait_time_total += st->wait_time_total;
392 p->avg_wait_time = p->wait_time_total / p->nr_contended;
394 if (p->wait_time_min > st->wait_time_min)
395 p->wait_time_min = st->wait_time_min;
396 if (p->wait_time_max < st->wait_time_max)
397 p->wait_time_max = st->wait_time_max;
399 p->broken |= st->broken;
405 rb = &(*rb)->rb_left;
407 rb = &(*rb)->rb_right;
410 rb_link_node(&st->rb, parent, rb);
411 rb_insert_color(&st->rb, &sorted);
414 static void insert_to_result(struct lock_stat *st,
415 int (*bigger)(struct lock_stat *, struct lock_stat *))
417 struct rb_node **rb = &result.rb_node;
418 struct rb_node *parent = NULL;
421 if (combine_locks && st->combined)
425 p = container_of(*rb, struct lock_stat, rb);
429 rb = &(*rb)->rb_left;
431 rb = &(*rb)->rb_right;
434 rb_link_node(&st->rb, parent, rb);
435 rb_insert_color(&st->rb, &result);
438 /* returns left most element of result, and erase it */
439 static struct lock_stat *pop_from_result(void)
441 struct rb_node *node = result.rb_node;
446 while (node->rb_left)
447 node = node->rb_left;
449 rb_erase(node, &result);
450 return container_of(node, struct lock_stat, rb);
453 static struct lock_stat *lock_stat_findnew(u64 addr, const char *name)
455 struct hlist_head *entry = lockhashentry(addr);
456 struct lock_stat *ret, *new;
458 hlist_for_each_entry(ret, entry, hash_entry) {
459 if (ret->addr == addr)
463 new = zalloc(sizeof(struct lock_stat));
468 new->name = zalloc(sizeof(char) * strlen(name) + 1);
474 strcpy(new->name, name);
475 new->wait_time_min = ULLONG_MAX;
477 hlist_add_head(&new->hash_entry, entry);
481 pr_err("memory allocation failed\n");
485 struct trace_lock_handler {
486 int (*acquire_event)(struct evsel *evsel,
487 struct perf_sample *sample);
489 int (*acquired_event)(struct evsel *evsel,
490 struct perf_sample *sample);
492 int (*contended_event)(struct evsel *evsel,
493 struct perf_sample *sample);
495 int (*release_event)(struct evsel *evsel,
496 struct perf_sample *sample);
499 static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
501 struct lock_seq_stat *seq;
503 list_for_each_entry(seq, &ts->seq_list, list) {
504 if (seq->addr == addr)
508 seq = zalloc(sizeof(struct lock_seq_stat));
510 pr_err("memory allocation failed\n");
513 seq->state = SEQ_STATE_UNINITIALIZED;
516 list_add(&seq->list, &ts->seq_list);
528 static int bad_hist[BROKEN_MAX];
535 static int report_lock_acquire_event(struct evsel *evsel,
536 struct perf_sample *sample)
538 struct lock_stat *ls;
539 struct thread_stat *ts;
540 struct lock_seq_stat *seq;
541 const char *name = evsel__strval(evsel, sample, "name");
542 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
543 int flag = evsel__intval(evsel, sample, "flags");
545 ls = lock_stat_findnew(addr, name);
549 ts = thread_stat_findnew(sample->tid);
553 seq = get_seq(ts, addr);
557 switch (seq->state) {
558 case SEQ_STATE_UNINITIALIZED:
559 case SEQ_STATE_RELEASED:
561 seq->state = SEQ_STATE_ACQUIRING;
565 if (flag & READ_LOCK)
567 seq->state = SEQ_STATE_READ_ACQUIRED;
572 case SEQ_STATE_READ_ACQUIRED:
573 if (flag & READ_LOCK) {
581 case SEQ_STATE_ACQUIRED:
582 case SEQ_STATE_ACQUIRING:
583 case SEQ_STATE_CONTENDED:
585 /* broken lock sequence */
588 bad_hist[BROKEN_ACQUIRE]++;
590 list_del_init(&seq->list);
594 BUG_ON("Unknown state of lock sequence found!\n");
599 seq->prev_event_time = sample->time;
604 static int report_lock_acquired_event(struct evsel *evsel,
605 struct perf_sample *sample)
607 struct lock_stat *ls;
608 struct thread_stat *ts;
609 struct lock_seq_stat *seq;
611 const char *name = evsel__strval(evsel, sample, "name");
612 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
614 ls = lock_stat_findnew(addr, name);
618 ts = thread_stat_findnew(sample->tid);
622 seq = get_seq(ts, addr);
626 switch (seq->state) {
627 case SEQ_STATE_UNINITIALIZED:
628 /* orphan event, do nothing */
630 case SEQ_STATE_ACQUIRING:
632 case SEQ_STATE_CONTENDED:
633 contended_term = sample->time - seq->prev_event_time;
634 ls->wait_time_total += contended_term;
635 if (contended_term < ls->wait_time_min)
636 ls->wait_time_min = contended_term;
637 if (ls->wait_time_max < contended_term)
638 ls->wait_time_max = contended_term;
640 case SEQ_STATE_RELEASED:
641 case SEQ_STATE_ACQUIRED:
642 case SEQ_STATE_READ_ACQUIRED:
643 /* broken lock sequence */
646 bad_hist[BROKEN_ACQUIRED]++;
648 list_del_init(&seq->list);
652 BUG_ON("Unknown state of lock sequence found!\n");
656 seq->state = SEQ_STATE_ACQUIRED;
658 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
659 seq->prev_event_time = sample->time;
664 static int report_lock_contended_event(struct evsel *evsel,
665 struct perf_sample *sample)
667 struct lock_stat *ls;
668 struct thread_stat *ts;
669 struct lock_seq_stat *seq;
670 const char *name = evsel__strval(evsel, sample, "name");
671 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
673 ls = lock_stat_findnew(addr, name);
677 ts = thread_stat_findnew(sample->tid);
681 seq = get_seq(ts, addr);
685 switch (seq->state) {
686 case SEQ_STATE_UNINITIALIZED:
687 /* orphan event, do nothing */
689 case SEQ_STATE_ACQUIRING:
691 case SEQ_STATE_RELEASED:
692 case SEQ_STATE_ACQUIRED:
693 case SEQ_STATE_READ_ACQUIRED:
694 case SEQ_STATE_CONTENDED:
695 /* broken lock sequence */
698 bad_hist[BROKEN_CONTENDED]++;
700 list_del_init(&seq->list);
704 BUG_ON("Unknown state of lock sequence found!\n");
708 seq->state = SEQ_STATE_CONTENDED;
710 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
711 seq->prev_event_time = sample->time;
716 static int report_lock_release_event(struct evsel *evsel,
717 struct perf_sample *sample)
719 struct lock_stat *ls;
720 struct thread_stat *ts;
721 struct lock_seq_stat *seq;
722 const char *name = evsel__strval(evsel, sample, "name");
723 u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
725 ls = lock_stat_findnew(addr, name);
729 ts = thread_stat_findnew(sample->tid);
733 seq = get_seq(ts, addr);
737 switch (seq->state) {
738 case SEQ_STATE_UNINITIALIZED:
740 case SEQ_STATE_ACQUIRED:
742 case SEQ_STATE_READ_ACQUIRED:
744 BUG_ON(seq->read_count < 0);
745 if (seq->read_count) {
750 case SEQ_STATE_ACQUIRING:
751 case SEQ_STATE_CONTENDED:
752 case SEQ_STATE_RELEASED:
753 /* broken lock sequence */
756 bad_hist[BROKEN_RELEASE]++;
760 BUG_ON("Unknown state of lock sequence found!\n");
766 list_del_init(&seq->list);
772 /* lock oriented handlers */
773 /* TODO: handlers for CPU oriented, thread oriented */
774 static struct trace_lock_handler report_lock_ops = {
775 .acquire_event = report_lock_acquire_event,
776 .acquired_event = report_lock_acquired_event,
777 .contended_event = report_lock_contended_event,
778 .release_event = report_lock_release_event,
781 static struct trace_lock_handler *trace_handler;
783 static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
785 if (trace_handler->acquire_event)
786 return trace_handler->acquire_event(evsel, sample);
790 static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
792 if (trace_handler->acquired_event)
793 return trace_handler->acquired_event(evsel, sample);
797 static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
799 if (trace_handler->contended_event)
800 return trace_handler->contended_event(evsel, sample);
804 static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
806 if (trace_handler->release_event)
807 return trace_handler->release_event(evsel, sample);
811 static void print_bad_events(int bad, int total)
813 /* Output for debug, this have to be removed */
815 const char *name[4] =
816 { "acquire", "acquired", "contended", "release" };
818 pr_info("\n=== output for debug===\n\n");
819 pr_info("bad: %d, total: %d\n", bad, total);
820 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
821 pr_info("histogram of events caused bad sequence\n");
822 for (i = 0; i < BROKEN_MAX; i++)
823 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
826 /* TODO: various way to print, coloring, nano or milli sec */
827 static void print_result(void)
829 struct lock_stat *st;
830 struct lock_key *key;
834 pr_info("%20s ", "Name");
835 list_for_each_entry(key, &lock_keys, list)
836 pr_info("%*s ", key->len, key->header);
840 while ((st = pop_from_result())) {
844 if (!st->nr_acquired)
849 if (strlen(st->name) < 20) {
850 /* output raw name */
851 pr_info("%20s ", st->name);
853 strncpy(cut_name, st->name, 16);
858 /* cut off name for saving output style */
859 pr_info("%20s ", cut_name);
862 list_for_each_entry(key, &lock_keys, list) {
869 print_bad_events(bad, total);
872 static bool info_threads, info_map;
874 static void dump_threads(void)
876 struct thread_stat *st;
877 struct rb_node *node;
880 pr_info("%10s: comm\n", "Thread ID");
882 node = rb_first(&thread_stats);
884 st = container_of(node, struct thread_stat, rb);
885 t = perf_session__findnew(session, st->tid);
886 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
887 node = rb_next(node);
892 static int compare_maps(struct lock_stat *a, struct lock_stat *b)
896 if (a->name && b->name)
897 ret = strcmp(a->name, b->name);
899 ret = !!a->name - !!b->name;
902 return a->addr < b->addr;
907 static void dump_map(void)
910 struct lock_stat *st;
912 pr_info("Address of instance: name of class\n");
913 for (i = 0; i < LOCKHASH_SIZE; i++) {
914 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
915 insert_to_result(st, compare_maps);
919 while ((st = pop_from_result()))
920 pr_info(" %#llx: %s\n", (unsigned long long)st->addr, st->name);
923 static int dump_info(void)
933 pr_err("Unknown type of information\n");
939 typedef int (*tracepoint_handler)(struct evsel *evsel,
940 struct perf_sample *sample);
942 static int process_sample_event(struct perf_tool *tool __maybe_unused,
943 union perf_event *event,
944 struct perf_sample *sample,
946 struct machine *machine)
949 struct thread *thread = machine__findnew_thread(machine, sample->pid,
952 if (thread == NULL) {
953 pr_debug("problem processing %d event, skipping it.\n",
958 if (evsel->handler != NULL) {
959 tracepoint_handler f = evsel->handler;
960 err = f(evsel, sample);
968 static void combine_result(void)
971 struct lock_stat *st;
976 for (i = 0; i < LOCKHASH_SIZE; i++) {
977 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
978 combine_lock_stats(st);
983 static void sort_result(void)
986 struct lock_stat *st;
988 for (i = 0; i < LOCKHASH_SIZE; i++) {
989 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
990 insert_to_result(st, compare);
995 static const struct evsel_str_handler lock_tracepoints[] = {
996 { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
997 { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
998 { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
999 { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
1004 static int __cmd_report(bool display_info)
1007 struct perf_tool eops = {
1008 .sample = process_sample_event,
1009 .comm = perf_event__process_comm,
1010 .namespaces = perf_event__process_namespaces,
1011 .ordered_events = true,
1013 struct perf_data data = {
1015 .mode = PERF_DATA_MODE_READ,
1019 session = perf_session__new(&data, &eops);
1020 if (IS_ERR(session)) {
1021 pr_err("Initializing perf session failed\n");
1022 return PTR_ERR(session);
1025 symbol__init(&session->header.env);
1027 if (!perf_session__has_traces(session, "lock record"))
1030 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
1031 pr_err("Initializing perf session tracepoint handlers failed\n");
1035 if (setup_output_field(output_fields))
1041 err = perf_session__process_events(session);
1046 if (display_info) /* used for info subcommand */
1055 perf_session__delete(session);
1059 static int __cmd_record(int argc, const char **argv)
1061 const char *record_args[] = {
1062 "record", "-R", "-m", "1024", "-c", "1", "--synth", "no",
1064 unsigned int rec_argc, i, j, ret;
1065 const char **rec_argv;
1067 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
1068 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
1069 pr_err("tracepoint %s is not enabled. "
1070 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
1071 lock_tracepoints[i].name);
1076 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1077 /* factor of 2 is for -e in front of each tracepoint */
1078 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
1080 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1084 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1085 rec_argv[i] = strdup(record_args[i]);
1087 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
1088 rec_argv[i++] = "-e";
1089 rec_argv[i++] = strdup(lock_tracepoints[j].name);
1092 for (j = 1; j < (unsigned int)argc; j++, i++)
1093 rec_argv[i] = argv[j];
1095 BUG_ON(i != rec_argc);
1097 ret = cmd_record(i, rec_argv);
1102 int cmd_lock(int argc, const char **argv)
1104 const struct option lock_options[] = {
1105 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1106 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
1107 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
1108 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1112 const struct option info_options[] = {
1113 OPT_BOOLEAN('t', "threads", &info_threads,
1114 "dump thread list in perf.data"),
1115 OPT_BOOLEAN('m', "map", &info_map,
1116 "map of lock instances (address:name table)"),
1117 OPT_PARENT(lock_options)
1120 const struct option report_options[] = {
1121 OPT_STRING('k', "key", &sort_key, "acquired",
1122 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
1123 OPT_STRING('F', "field", &output_fields, NULL,
1124 "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
1126 OPT_BOOLEAN('c', "combine-locks", &combine_locks,
1127 "combine locks in the same class"),
1128 OPT_PARENT(lock_options)
1131 const char * const info_usage[] = {
1132 "perf lock info [<options>]",
1135 const char *const lock_subcommands[] = { "record", "report", "script",
1137 const char *lock_usage[] = {
1141 const char * const report_usage[] = {
1142 "perf lock report [<options>]",
1148 for (i = 0; i < LOCKHASH_SIZE; i++)
1149 INIT_HLIST_HEAD(lockhash_table + i);
1151 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
1152 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
1154 usage_with_options(lock_usage, lock_options);
1156 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
1157 return __cmd_record(argc, argv);
1158 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
1159 trace_handler = &report_lock_ops;
1161 argc = parse_options(argc, argv,
1162 report_options, report_usage, 0);
1164 usage_with_options(report_usage, report_options);
1166 rc = __cmd_report(false);
1167 } else if (!strcmp(argv[0], "script")) {
1168 /* Aliased to 'perf script' */
1169 return cmd_script(argc, argv);
1170 } else if (!strcmp(argv[0], "info")) {
1172 argc = parse_options(argc, argv,
1173 info_options, info_usage, 0);
1175 usage_with_options(info_usage, info_options);
1177 /* recycling report_lock_ops */
1178 trace_handler = &report_lock_ops;
1179 rc = __cmd_report(true);
1181 usage_with_options(lock_usage, lock_options);