]> Pileus Git - ~andy/linux/blob - tools/perf/builtin-lock.c
perf lock: Account for lock average wait time
[~andy/linux] / tools / perf / builtin-lock.c
1 #include "builtin.h"
2 #include "perf.h"
3
4 #include "util/evlist.h"
5 #include "util/evsel.h"
6 #include "util/util.h"
7 #include "util/cache.h"
8 #include "util/symbol.h"
9 #include "util/thread.h"
10 #include "util/header.h"
11
12 #include "util/parse-options.h"
13 #include "util/trace-event.h"
14
15 #include "util/debug.h"
16 #include "util/session.h"
17 #include "util/tool.h"
18
19 #include <sys/types.h>
20 #include <sys/prctl.h>
21 #include <semaphore.h>
22 #include <pthread.h>
23 #include <math.h>
24 #include <limits.h>
25
26 #include <linux/list.h>
27 #include <linux/hash.h>
28
29 static struct perf_session *session;
30
31 /* based on kernel/lockdep.c */
32 #define LOCKHASH_BITS           12
33 #define LOCKHASH_SIZE           (1UL << LOCKHASH_BITS)
34
35 static struct list_head lockhash_table[LOCKHASH_SIZE];
36
37 #define __lockhashfn(key)       hash_long((unsigned long)key, LOCKHASH_BITS)
38 #define lockhashentry(key)      (lockhash_table + __lockhashfn((key)))
39
40 struct lock_stat {
41         struct list_head        hash_entry;
42         struct rb_node          rb;             /* used for sorting */
43
44         /*
45          * FIXME: perf_evsel__intval() returns u64,
46          * so address of lockdep_map should be dealed as 64bit.
47          * Is there more better solution?
48          */
49         void                    *addr;          /* address of lockdep_map, used as ID */
50         char                    *name;          /* for strcpy(), we cannot use const */
51
52         unsigned int            nr_acquire;
53         unsigned int            nr_acquired;
54         unsigned int            nr_contended;
55         unsigned int            nr_release;
56
57         unsigned int            nr_readlock;
58         unsigned int            nr_trylock;
59
60         /* these times are in nano sec. */
61         u64                     avg_wait_time;
62         u64                     wait_time_total;
63         u64                     wait_time_min;
64         u64                     wait_time_max;
65
66         int                     discard; /* flag of blacklist */
67 };
68
69 /*
70  * States of lock_seq_stat
71  *
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.
76  */
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
83
84 /*
85  * MAX_LOCK_DEPTH
86  * Imported from include/linux/sched.h.
87  * Should this be synchronized?
88  */
89 #define MAX_LOCK_DEPTH 48
90
91 /*
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?
98  */
99 struct lock_seq_stat {
100         struct list_head        list;
101         int                     state;
102         u64                     prev_event_time;
103         void                    *addr;
104
105         int                     read_count;
106 };
107
108 struct thread_stat {
109         struct rb_node          rb;
110
111         u32                     tid;
112         struct list_head        seq_list;
113 };
114
115 static struct rb_root           thread_stats;
116
117 static struct thread_stat *thread_stat_find(u32 tid)
118 {
119         struct rb_node *node;
120         struct thread_stat *st;
121
122         node = thread_stats.rb_node;
123         while (node) {
124                 st = container_of(node, struct thread_stat, rb);
125                 if (st->tid == tid)
126                         return st;
127                 else if (tid < st->tid)
128                         node = node->rb_left;
129                 else
130                         node = node->rb_right;
131         }
132
133         return NULL;
134 }
135
136 static void thread_stat_insert(struct thread_stat *new)
137 {
138         struct rb_node **rb = &thread_stats.rb_node;
139         struct rb_node *parent = NULL;
140         struct thread_stat *p;
141
142         while (*rb) {
143                 p = container_of(*rb, struct thread_stat, rb);
144                 parent = *rb;
145
146                 if (new->tid < p->tid)
147                         rb = &(*rb)->rb_left;
148                 else if (new->tid > p->tid)
149                         rb = &(*rb)->rb_right;
150                 else
151                         BUG_ON("inserting invalid thread_stat\n");
152         }
153
154         rb_link_node(&new->rb, parent, rb);
155         rb_insert_color(&new->rb, &thread_stats);
156 }
157
158 static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
159 {
160         struct thread_stat *st;
161
162         st = thread_stat_find(tid);
163         if (st)
164                 return st;
165
166         st = zalloc(sizeof(struct thread_stat));
167         if (!st) {
168                 pr_err("memory allocation failed\n");
169                 return NULL;
170         }
171
172         st->tid = tid;
173         INIT_LIST_HEAD(&st->seq_list);
174
175         thread_stat_insert(st);
176
177         return st;
178 }
179
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;
183
184 static struct thread_stat *thread_stat_findnew_first(u32 tid)
185 {
186         struct thread_stat *st;
187
188         st = zalloc(sizeof(struct thread_stat));
189         if (!st) {
190                 pr_err("memory allocation failed\n");
191                 return NULL;
192         }
193         st->tid = tid;
194         INIT_LIST_HEAD(&st->seq_list);
195
196         rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
197         rb_insert_color(&st->rb, &thread_stats);
198
199         thread_stat_findnew = thread_stat_findnew_after_first;
200         return st;
201 }
202
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)         \
207         {                                                               \
208                 return one->member > two->member;                       \
209         }
210
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)
216
217 static int lock_stat_key_wait_time_min(struct lock_stat *one,
218                                         struct lock_stat *two)
219 {
220         u64 s1 = one->wait_time_min;
221         u64 s2 = two->wait_time_min;
222         if (s1 == ULLONG_MAX)
223                 s1 = 0;
224         if (s2 == ULLONG_MAX)
225                 s2 = 0;
226         return s1 > s2;
227 }
228
229 struct lock_key {
230         /*
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
234          */
235         const char              *name;
236         int                     (*key)(struct lock_stat*, struct lock_stat*);
237 };
238
239 static const char               *sort_key = "acquired";
240
241 static int                      (*compare)(struct lock_stat *, struct lock_stat *);
242
243 static struct rb_root           result; /* place to store sorted data */
244
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),
254
255         /* extra comparisons much complicated should be here */
256
257         { NULL, NULL }
258 };
259
260 static int select_key(void)
261 {
262         int i;
263
264         for (i = 0; keys[i].name; i++) {
265                 if (!strcmp(keys[i].name, sort_key)) {
266                         compare = keys[i].key;
267                         return 0;
268                 }
269         }
270
271         pr_err("Unknown compare key: %s\n", sort_key);
272
273         return -1;
274 }
275
276 static void insert_to_result(struct lock_stat *st,
277                              int (*bigger)(struct lock_stat *, struct lock_stat *))
278 {
279         struct rb_node **rb = &result.rb_node;
280         struct rb_node *parent = NULL;
281         struct lock_stat *p;
282
283         while (*rb) {
284                 p = container_of(*rb, struct lock_stat, rb);
285                 parent = *rb;
286
287                 if (bigger(st, p))
288                         rb = &(*rb)->rb_left;
289                 else
290                         rb = &(*rb)->rb_right;
291         }
292
293         rb_link_node(&st->rb, parent, rb);
294         rb_insert_color(&st->rb, &result);
295 }
296
297 /* returns left most element of result, and erase it */
298 static struct lock_stat *pop_from_result(void)
299 {
300         struct rb_node *node = result.rb_node;
301
302         if (!node)
303                 return NULL;
304
305         while (node->rb_left)
306                 node = node->rb_left;
307
308         rb_erase(node, &result);
309         return container_of(node, struct lock_stat, rb);
310 }
311
312 static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
313 {
314         struct list_head *entry = lockhashentry(addr);
315         struct lock_stat *ret, *new;
316
317         list_for_each_entry(ret, entry, hash_entry) {
318                 if (ret->addr == addr)
319                         return ret;
320         }
321
322         new = zalloc(sizeof(struct lock_stat));
323         if (!new)
324                 goto alloc_failed;
325
326         new->addr = addr;
327         new->name = zalloc(sizeof(char) * strlen(name) + 1);
328         if (!new->name) {
329                 free(new);
330                 goto alloc_failed;
331         }
332
333         strcpy(new->name, name);
334         new->wait_time_min = ULLONG_MAX;
335
336         list_add(&new->hash_entry, entry);
337         return new;
338
339 alloc_failed:
340         pr_err("memory allocation failed\n");
341         return NULL;
342 }
343
344 struct trace_lock_handler {
345         int (*acquire_event)(struct perf_evsel *evsel,
346                              struct perf_sample *sample);
347
348         int (*acquired_event)(struct perf_evsel *evsel,
349                               struct perf_sample *sample);
350
351         int (*contended_event)(struct perf_evsel *evsel,
352                                struct perf_sample *sample);
353
354         int (*release_event)(struct perf_evsel *evsel,
355                              struct perf_sample *sample);
356 };
357
358 static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
359 {
360         struct lock_seq_stat *seq;
361
362         list_for_each_entry(seq, &ts->seq_list, list) {
363                 if (seq->addr == addr)
364                         return seq;
365         }
366
367         seq = zalloc(sizeof(struct lock_seq_stat));
368         if (!seq) {
369                 pr_err("memory allocation failed\n");
370                 return NULL;
371         }
372         seq->state = SEQ_STATE_UNINITIALIZED;
373         seq->addr = addr;
374
375         list_add(&seq->list, &ts->seq_list);
376         return seq;
377 }
378
379 enum broken_state {
380         BROKEN_ACQUIRE,
381         BROKEN_ACQUIRED,
382         BROKEN_CONTENDED,
383         BROKEN_RELEASE,
384         BROKEN_MAX,
385 };
386
387 static int bad_hist[BROKEN_MAX];
388
389 enum acquire_flags {
390         TRY_LOCK = 1,
391         READ_LOCK = 2,
392 };
393
394 static int report_lock_acquire_event(struct perf_evsel *evsel,
395                                      struct perf_sample *sample)
396 {
397         void *addr;
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");
404
405         memcpy(&addr, &tmp, sizeof(void *));
406
407         ls = lock_stat_findnew(addr, name);
408         if (!ls)
409                 return -ENOMEM;
410         if (ls->discard)
411                 return 0;
412
413         ts = thread_stat_findnew(sample->tid);
414         if (!ts)
415                 return -ENOMEM;
416
417         seq = get_seq(ts, addr);
418         if (!seq)
419                 return -ENOMEM;
420
421         switch (seq->state) {
422         case SEQ_STATE_UNINITIALIZED:
423         case SEQ_STATE_RELEASED:
424                 if (!flag) {
425                         seq->state = SEQ_STATE_ACQUIRING;
426                 } else {
427                         if (flag & TRY_LOCK)
428                                 ls->nr_trylock++;
429                         if (flag & READ_LOCK)
430                                 ls->nr_readlock++;
431                         seq->state = SEQ_STATE_READ_ACQUIRED;
432                         seq->read_count = 1;
433                         ls->nr_acquired++;
434                 }
435                 break;
436         case SEQ_STATE_READ_ACQUIRED:
437                 if (flag & READ_LOCK) {
438                         seq->read_count++;
439                         ls->nr_acquired++;
440                         goto end;
441                 } else {
442                         goto broken;
443                 }
444                 break;
445         case SEQ_STATE_ACQUIRED:
446         case SEQ_STATE_ACQUIRING:
447         case SEQ_STATE_CONTENDED:
448 broken:
449                 /* broken lock sequence, discard it */
450                 ls->discard = 1;
451                 bad_hist[BROKEN_ACQUIRE]++;
452                 list_del(&seq->list);
453                 free(seq);
454                 goto end;
455         default:
456                 BUG_ON("Unknown state of lock sequence found!\n");
457                 break;
458         }
459
460         ls->nr_acquire++;
461         seq->prev_event_time = sample->time;
462 end:
463         return 0;
464 }
465
466 static int report_lock_acquired_event(struct perf_evsel *evsel,
467                                       struct perf_sample *sample)
468 {
469         void *addr;
470         struct lock_stat *ls;
471         struct thread_stat *ts;
472         struct lock_seq_stat *seq;
473         u64 contended_term;
474         const char *name = perf_evsel__strval(evsel, sample, "name");
475         u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
476
477         memcpy(&addr, &tmp, sizeof(void *));
478
479         ls = lock_stat_findnew(addr, name);
480         if (!ls)
481                 return -ENOMEM;
482         if (ls->discard)
483                 return 0;
484
485         ts = thread_stat_findnew(sample->tid);
486         if (!ts)
487                 return -ENOMEM;
488
489         seq = get_seq(ts, addr);
490         if (!seq)
491                 return -ENOMEM;
492
493         switch (seq->state) {
494         case SEQ_STATE_UNINITIALIZED:
495                 /* orphan event, do nothing */
496                 return 0;
497         case SEQ_STATE_ACQUIRING:
498                 break;
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;
506                 break;
507         case SEQ_STATE_RELEASED:
508         case SEQ_STATE_ACQUIRED:
509         case SEQ_STATE_READ_ACQUIRED:
510                 /* broken lock sequence, discard it */
511                 ls->discard = 1;
512                 bad_hist[BROKEN_ACQUIRED]++;
513                 list_del(&seq->list);
514                 free(seq);
515                 goto end;
516         default:
517                 BUG_ON("Unknown state of lock sequence found!\n");
518                 break;
519         }
520
521         seq->state = SEQ_STATE_ACQUIRED;
522         ls->nr_acquired++;
523         ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
524         seq->prev_event_time = sample->time;
525 end:
526         return 0;
527 }
528
529 static int report_lock_contended_event(struct perf_evsel *evsel,
530                                        struct perf_sample *sample)
531 {
532         void *addr;
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");
538
539         memcpy(&addr, &tmp, sizeof(void *));
540
541         ls = lock_stat_findnew(addr, name);
542         if (!ls)
543                 return -ENOMEM;
544         if (ls->discard)
545                 return 0;
546
547         ts = thread_stat_findnew(sample->tid);
548         if (!ts)
549                 return -ENOMEM;
550
551         seq = get_seq(ts, addr);
552         if (!seq)
553                 return -ENOMEM;
554
555         switch (seq->state) {
556         case SEQ_STATE_UNINITIALIZED:
557                 /* orphan event, do nothing */
558                 return 0;
559         case SEQ_STATE_ACQUIRING:
560                 break;
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 */
566                 ls->discard = 1;
567                 bad_hist[BROKEN_CONTENDED]++;
568                 list_del(&seq->list);
569                 free(seq);
570                 goto end;
571         default:
572                 BUG_ON("Unknown state of lock sequence found!\n");
573                 break;
574         }
575
576         seq->state = SEQ_STATE_CONTENDED;
577         ls->nr_contended++;
578         ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
579         seq->prev_event_time = sample->time;
580 end:
581         return 0;
582 }
583
584 static int report_lock_release_event(struct perf_evsel *evsel,
585                                      struct perf_sample *sample)
586 {
587         void *addr;
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");
593
594         memcpy(&addr, &tmp, sizeof(void *));
595
596         ls = lock_stat_findnew(addr, name);
597         if (!ls)
598                 return -ENOMEM;
599         if (ls->discard)
600                 return 0;
601
602         ts = thread_stat_findnew(sample->tid);
603         if (!ts)
604                 return -ENOMEM;
605
606         seq = get_seq(ts, addr);
607         if (!seq)
608                 return -ENOMEM;
609
610         switch (seq->state) {
611         case SEQ_STATE_UNINITIALIZED:
612                 goto end;
613         case SEQ_STATE_ACQUIRED:
614                 break;
615         case SEQ_STATE_READ_ACQUIRED:
616                 seq->read_count--;
617                 BUG_ON(seq->read_count < 0);
618                 if (!seq->read_count) {
619                         ls->nr_release++;
620                         goto end;
621                 }
622                 break;
623         case SEQ_STATE_ACQUIRING:
624         case SEQ_STATE_CONTENDED:
625         case SEQ_STATE_RELEASED:
626                 /* broken lock sequence, discard it */
627                 ls->discard = 1;
628                 bad_hist[BROKEN_RELEASE]++;
629                 goto free_seq;
630         default:
631                 BUG_ON("Unknown state of lock sequence found!\n");
632                 break;
633         }
634
635         ls->nr_release++;
636 free_seq:
637         list_del(&seq->list);
638         free(seq);
639 end:
640         return 0;
641 }
642
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,
650 };
651
652 static struct trace_lock_handler *trace_handler;
653
654 static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
655                                              struct perf_sample *sample)
656 {
657         if (trace_handler->acquire_event)
658                 return trace_handler->acquire_event(evsel, sample);
659         return 0;
660 }
661
662 static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
663                                               struct perf_sample *sample)
664 {
665         if (trace_handler->acquired_event)
666                 return trace_handler->acquired_event(evsel, sample);
667         return 0;
668 }
669
670 static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
671                                               struct perf_sample *sample)
672 {
673         if (trace_handler->contended_event)
674                 return trace_handler->contended_event(evsel, sample);
675         return 0;
676 }
677
678 static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
679                                             struct perf_sample *sample)
680 {
681         if (trace_handler->release_event)
682                 return trace_handler->release_event(evsel, sample);
683         return 0;
684 }
685
686 static void print_bad_events(int bad, int total)
687 {
688         /* Output for debug, this have to be removed */
689         int i;
690         const char *name[4] =
691                 { "acquire", "acquired", "contended", "release" };
692
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]);
699 }
700
701 /* TODO: various way to print, coloring, nano or milli sec */
702 static void print_result(void)
703 {
704         struct lock_stat *st;
705         char cut_name[20];
706         int bad, total;
707
708         pr_info("%20s ", "Name");
709         pr_info("%10s ", "acquired");
710         pr_info("%10s ", "contended");
711
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)");
716
717         pr_info("\n\n");
718
719         bad = total = 0;
720         while ((st = pop_from_result())) {
721                 total++;
722                 if (st->discard) {
723                         bad++;
724                         continue;
725                 }
726                 bzero(cut_name, 20);
727
728                 if (strlen(st->name) < 16) {
729                         /* output raw name */
730                         pr_info("%20s ", st->name);
731                 } else {
732                         strncpy(cut_name, st->name, 16);
733                         cut_name[16] = '.';
734                         cut_name[17] = '.';
735                         cut_name[18] = '.';
736                         cut_name[19] = '\0';
737                         /* cut off name for saving output style */
738                         pr_info("%20s ", cut_name);
739                 }
740
741                 pr_info("%10u ", st->nr_acquired);
742                 pr_info("%10u ", st->nr_contended);
743
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);
749                 pr_info("\n");
750         }
751
752         print_bad_events(bad, total);
753 }
754
755 static bool info_threads, info_map;
756
757 static void dump_threads(void)
758 {
759         struct thread_stat *st;
760         struct rb_node *node;
761         struct thread *t;
762
763         pr_info("%10s: comm\n", "Thread ID");
764
765         node = rb_first(&thread_stats);
766         while (node) {
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);
771         };
772 }
773
774 static void dump_map(void)
775 {
776         unsigned int i;
777         struct lock_stat *st;
778
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);
783                 }
784         }
785 }
786
787 static int dump_info(void)
788 {
789         int rc = 0;
790
791         if (info_threads)
792                 dump_threads();
793         else if (info_map)
794                 dump_map();
795         else {
796                 rc = -1;
797                 pr_err("Unknown type of information\n");
798         }
799
800         return rc;
801 }
802
803 typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
804                                   struct perf_sample *sample);
805
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)
811 {
812         struct thread *thread = machine__findnew_thread(machine, sample->pid,
813                                                         sample->tid);
814
815         if (thread == NULL) {
816                 pr_debug("problem processing %d event, skipping it.\n",
817                         event->header.type);
818                 return -1;
819         }
820
821         if (evsel->handler.func != NULL) {
822                 tracepoint_handler f = evsel->handler.func;
823                 return f(evsel, sample);
824         }
825
826         return 0;
827 }
828
829 static void sort_result(void)
830 {
831         unsigned int i;
832         struct lock_stat *st;
833
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);
837                 }
838         }
839 }
840
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 */
846 };
847
848 static int __cmd_report(bool display_info)
849 {
850         int err = -EINVAL;
851         struct perf_tool eops = {
852                 .sample          = process_sample_event,
853                 .comm            = perf_event__process_comm,
854                 .ordered_samples = true,
855         };
856
857         session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
858         if (!session) {
859                 pr_err("Initializing perf session failed\n");
860                 return -ENOMEM;
861         }
862
863         if (!perf_session__has_traces(session, "lock record"))
864                 goto out_delete;
865
866         if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
867                 pr_err("Initializing perf session tracepoint handlers failed\n");
868                 goto out_delete;
869         }
870
871         if (select_key())
872                 goto out_delete;
873
874         err = perf_session__process_events(session, &eops);
875         if (err)
876                 goto out_delete;
877
878         setup_pager();
879         if (display_info) /* used for info subcommand */
880                 err = dump_info();
881         else {
882                 sort_result();
883                 print_result();
884         }
885
886 out_delete:
887         perf_session__delete(session);
888         return err;
889 }
890
891 static int __cmd_record(int argc, const char **argv)
892 {
893         const char *record_args[] = {
894                 "record", "-R", "-m", "1024", "-c", "1",
895         };
896         unsigned int rec_argc, i, j, ret;
897         const char **rec_argv;
898
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);
904                                 return 1;
905                 }
906         }
907
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);
911
912         rec_argv = calloc(rec_argc + 1, sizeof(char *));
913         if (!rec_argv)
914                 return -ENOMEM;
915
916         for (i = 0; i < ARRAY_SIZE(record_args); i++)
917                 rec_argv[i] = strdup(record_args[i]);
918
919         for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
920                 rec_argv[i++] = "-e";
921                 rec_argv[i++] = strdup(lock_tracepoints[j].name);
922         }
923
924         for (j = 1; j < (unsigned int)argc; j++, i++)
925                 rec_argv[i] = argv[j];
926
927         BUG_ON(i != rec_argc);
928
929         ret = cmd_record(i, rec_argv, NULL);
930         free(rec_argv);
931         return ret;
932 }
933
934 int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
935 {
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)"),
941         OPT_END()
942         };
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"),
947         OPT_END()
948         };
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)"),
952         /* TODO: type */
953         OPT_END()
954         };
955         const char * const info_usage[] = {
956                 "perf lock info [<options>]",
957                 NULL
958         };
959         const char * const lock_usage[] = {
960                 "perf lock [<options>] {record|report|script|info}",
961                 NULL
962         };
963         const char * const report_usage[] = {
964                 "perf lock report [<options>]",
965                 NULL
966         };
967         unsigned int i;
968         int rc = 0;
969
970         symbol__init();
971         for (i = 0; i < LOCKHASH_SIZE; i++)
972                 INIT_LIST_HEAD(lockhash_table + i);
973
974         argc = parse_options(argc, argv, lock_options, lock_usage,
975                              PARSE_OPT_STOP_AT_NON_OPTION);
976         if (!argc)
977                 usage_with_options(lock_usage, lock_options);
978
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;
983                 if (argc) {
984                         argc = parse_options(argc, argv,
985                                              report_options, report_usage, 0);
986                         if (argc)
987                                 usage_with_options(report_usage, report_options);
988                 }
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")) {
994                 if (argc) {
995                         argc = parse_options(argc, argv,
996                                              info_options, info_usage, 0);
997                         if (argc)
998                                 usage_with_options(info_usage, info_options);
999                 }
1000                 /* recycling report_lock_ops */
1001                 trace_handler = &report_lock_ops;
1002                 rc = __cmd_report(true);
1003         } else {
1004                 usage_with_options(lock_usage, lock_options);
1005         }
1006
1007         return rc;
1008 }