blob: ce3bfb48b26f0f309b5c90cf49bb4870fbd76a4b [file] [log] [blame]
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001#include "builtin.h"
2#include "perf.h"
3
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -03004#include "util/evlist.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03005#include "util/evsel.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09006#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
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060012#include <subcmd/parse-options.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090013#include "util/trace-event.h"
14
15#include "util/debug.h"
16#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020017#include "util/tool.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020018#include "util/data.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090019
20#include <sys/types.h>
21#include <sys/prctl.h>
22#include <semaphore.h>
23#include <pthread.h>
24#include <math.h>
25#include <limits.h>
26
27#include <linux/list.h>
28#include <linux/hash.h>
29
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090030static struct perf_session *session;
31
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090032/* based on kernel/lockdep.c */
33#define LOCKHASH_BITS 12
34#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
35
36static struct list_head lockhash_table[LOCKHASH_SIZE];
37
38#define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
39#define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
40
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090041struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010042 struct list_head hash_entry;
43 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090044
Ingo Molnar59f411b2010-01-31 08:27:58 +010045 /*
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -030046 * FIXME: perf_evsel__intval() returns u64,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090047 * so address of lockdep_map should be dealed as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010048 * Is there more better solution?
49 */
50 void *addr; /* address of lockdep_map, used as ID */
51 char *name; /* for strcpy(), we cannot use const */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090052
Ingo Molnar59f411b2010-01-31 08:27:58 +010053 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090054 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010055 unsigned int nr_contended;
56 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090057
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090058 unsigned int nr_readlock;
59 unsigned int nr_trylock;
Davidlohr Buesof37376c2013-09-08 19:19:19 -070060
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090061 /* these times are in nano sec. */
Davidlohr Buesof37376c2013-09-08 19:19:19 -070062 u64 avg_wait_time;
Ingo Molnar59f411b2010-01-31 08:27:58 +010063 u64 wait_time_total;
64 u64 wait_time_min;
65 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090066
67 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090068};
69
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090070/*
71 * States of lock_seq_stat
72 *
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.
77 */
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
84
85/*
86 * MAX_LOCK_DEPTH
87 * Imported from include/linux/sched.h.
88 * Should this be synchronized?
89 */
90#define MAX_LOCK_DEPTH 48
91
92/*
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?
99 */
100struct lock_seq_stat {
101 struct list_head list;
102 int state;
103 u64 prev_event_time;
104 void *addr;
105
106 int read_count;
107};
108
109struct thread_stat {
110 struct rb_node rb;
111
112 u32 tid;
113 struct list_head seq_list;
114};
115
116static struct rb_root thread_stats;
117
118static struct thread_stat *thread_stat_find(u32 tid)
119{
120 struct rb_node *node;
121 struct thread_stat *st;
122
123 node = thread_stats.rb_node;
124 while (node) {
125 st = container_of(node, struct thread_stat, rb);
126 if (st->tid == tid)
127 return st;
128 else if (tid < st->tid)
129 node = node->rb_left;
130 else
131 node = node->rb_right;
132 }
133
134 return NULL;
135}
136
137static void thread_stat_insert(struct thread_stat *new)
138{
139 struct rb_node **rb = &thread_stats.rb_node;
140 struct rb_node *parent = NULL;
141 struct thread_stat *p;
142
143 while (*rb) {
144 p = container_of(*rb, struct thread_stat, rb);
145 parent = *rb;
146
147 if (new->tid < p->tid)
148 rb = &(*rb)->rb_left;
149 else if (new->tid > p->tid)
150 rb = &(*rb)->rb_right;
151 else
152 BUG_ON("inserting invalid thread_stat\n");
153 }
154
155 rb_link_node(&new->rb, parent, rb);
156 rb_insert_color(&new->rb, &thread_stats);
157}
158
159static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
160{
161 struct thread_stat *st;
162
163 st = thread_stat_find(tid);
164 if (st)
165 return st;
166
167 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600168 if (!st) {
169 pr_err("memory allocation failed\n");
170 return NULL;
171 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900172
173 st->tid = tid;
174 INIT_LIST_HEAD(&st->seq_list);
175
176 thread_stat_insert(st);
177
178 return st;
179}
180
181static struct thread_stat *thread_stat_findnew_first(u32 tid);
182static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
183 thread_stat_findnew_first;
184
185static struct thread_stat *thread_stat_findnew_first(u32 tid)
186{
187 struct thread_stat *st;
188
189 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600190 if (!st) {
191 pr_err("memory allocation failed\n");
192 return NULL;
193 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900194 st->tid = tid;
195 INIT_LIST_HEAD(&st->seq_list);
196
197 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
198 rb_insert_color(&st->rb, &thread_stats);
199
200 thread_stat_findnew = thread_stat_findnew_after_first;
201 return st;
202}
203
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900204/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100205#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900206 static int lock_stat_key_ ## member(struct lock_stat *one, \
207 struct lock_stat *two) \
208 { \
209 return one->member > two->member; \
210 }
211
212SINGLE_KEY(nr_acquired)
213SINGLE_KEY(nr_contended)
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700214SINGLE_KEY(avg_wait_time)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900215SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900216SINGLE_KEY(wait_time_max)
217
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100218static int lock_stat_key_wait_time_min(struct lock_stat *one,
219 struct lock_stat *two)
220{
221 u64 s1 = one->wait_time_min;
222 u64 s2 = two->wait_time_min;
223 if (s1 == ULLONG_MAX)
224 s1 = 0;
225 if (s2 == ULLONG_MAX)
226 s2 = 0;
227 return s1 > s2;
228}
229
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900230struct lock_key {
231 /*
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
235 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100236 const char *name;
237 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900238};
239
Ingo Molnar59f411b2010-01-31 08:27:58 +0100240static const char *sort_key = "acquired";
241
242static int (*compare)(struct lock_stat *, struct lock_stat *);
243
244static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900245
246#define DEF_KEY_LOCK(name, fn_suffix) \
247 { #name, lock_stat_key_ ## fn_suffix }
248struct lock_key keys[] = {
249 DEF_KEY_LOCK(acquired, nr_acquired),
250 DEF_KEY_LOCK(contended, nr_contended),
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700251 DEF_KEY_LOCK(avg_wait, avg_wait_time),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900252 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),
255
256 /* extra comparisons much complicated should be here */
257
258 { NULL, NULL }
259};
260
David Ahern33d6aef2012-08-26 12:24:43 -0600261static int select_key(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900262{
263 int i;
264
265 for (i = 0; keys[i].name; i++) {
266 if (!strcmp(keys[i].name, sort_key)) {
267 compare = keys[i].key;
David Ahern33d6aef2012-08-26 12:24:43 -0600268 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900269 }
270 }
271
David Ahern33d6aef2012-08-26 12:24:43 -0600272 pr_err("Unknown compare key: %s\n", sort_key);
273
274 return -1;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900275}
276
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900277static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100278 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900279{
280 struct rb_node **rb = &result.rb_node;
281 struct rb_node *parent = NULL;
282 struct lock_stat *p;
283
284 while (*rb) {
285 p = container_of(*rb, struct lock_stat, rb);
286 parent = *rb;
287
288 if (bigger(st, p))
289 rb = &(*rb)->rb_left;
290 else
291 rb = &(*rb)->rb_right;
292 }
293
294 rb_link_node(&st->rb, parent, rb);
295 rb_insert_color(&st->rb, &result);
296}
297
298/* returns left most element of result, and erase it */
299static struct lock_stat *pop_from_result(void)
300{
301 struct rb_node *node = result.rb_node;
302
303 if (!node)
304 return NULL;
305
306 while (node->rb_left)
307 node = node->rb_left;
308
309 rb_erase(node, &result);
310 return container_of(node, struct lock_stat, rb);
311}
312
Ingo Molnar59f411b2010-01-31 08:27:58 +0100313static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900314{
315 struct list_head *entry = lockhashentry(addr);
316 struct lock_stat *ret, *new;
317
318 list_for_each_entry(ret, entry, hash_entry) {
319 if (ret->addr == addr)
320 return ret;
321 }
322
323 new = zalloc(sizeof(struct lock_stat));
324 if (!new)
325 goto alloc_failed;
326
327 new->addr = addr;
328 new->name = zalloc(sizeof(char) * strlen(name) + 1);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700329 if (!new->name) {
330 free(new);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900331 goto alloc_failed;
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700332 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900333
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700334 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900335 new->wait_time_min = ULLONG_MAX;
336
337 list_add(&new->hash_entry, entry);
338 return new;
339
340alloc_failed:
David Ahern33d6aef2012-08-26 12:24:43 -0600341 pr_err("memory allocation failed\n");
342 return NULL;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900343}
344
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900345struct trace_lock_handler {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300346 int (*acquire_event)(struct perf_evsel *evsel,
347 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900348
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300349 int (*acquired_event)(struct perf_evsel *evsel,
350 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900351
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300352 int (*contended_event)(struct perf_evsel *evsel,
353 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900354
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300355 int (*release_event)(struct perf_evsel *evsel,
356 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900357};
358
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900359static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
360{
361 struct lock_seq_stat *seq;
362
363 list_for_each_entry(seq, &ts->seq_list, list) {
364 if (seq->addr == addr)
365 return seq;
366 }
367
368 seq = zalloc(sizeof(struct lock_seq_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600369 if (!seq) {
370 pr_err("memory allocation failed\n");
371 return NULL;
372 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900373 seq->state = SEQ_STATE_UNINITIALIZED;
374 seq->addr = addr;
375
376 list_add(&seq->list, &ts->seq_list);
377 return seq;
378}
379
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200380enum broken_state {
381 BROKEN_ACQUIRE,
382 BROKEN_ACQUIRED,
383 BROKEN_CONTENDED,
384 BROKEN_RELEASE,
385 BROKEN_MAX,
386};
387
388static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900389
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200390enum acquire_flags {
391 TRY_LOCK = 1,
392 READ_LOCK = 2,
393};
394
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300395static int report_lock_acquire_event(struct perf_evsel *evsel,
396 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900397{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300398 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900399 struct lock_stat *ls;
400 struct thread_stat *ts;
401 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300402 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");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900405
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300406 memcpy(&addr, &tmp, sizeof(void *));
407
408 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600409 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700410 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900411 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600412 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900413
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300414 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600415 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700416 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600417
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300418 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600419 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700420 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900421
422 switch (seq->state) {
423 case SEQ_STATE_UNINITIALIZED:
424 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300425 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900426 seq->state = SEQ_STATE_ACQUIRING;
427 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300428 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900429 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300430 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900431 ls->nr_readlock++;
432 seq->state = SEQ_STATE_READ_ACQUIRED;
433 seq->read_count = 1;
434 ls->nr_acquired++;
435 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900436 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900437 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300438 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900439 seq->read_count++;
440 ls->nr_acquired++;
441 goto end;
442 } else {
443 goto broken;
444 }
445 break;
446 case SEQ_STATE_ACQUIRED:
447 case SEQ_STATE_ACQUIRING:
448 case SEQ_STATE_CONTENDED:
449broken:
450 /* broken lock sequence, discard it */
451 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200452 bad_hist[BROKEN_ACQUIRE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900453 list_del(&seq->list);
454 free(seq);
455 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900456 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900457 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900458 break;
459 }
460
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900461 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300462 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900463end:
David Ahern33d6aef2012-08-26 12:24:43 -0600464 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900465}
466
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300467static int report_lock_acquired_event(struct perf_evsel *evsel,
468 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900469{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300470 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900471 struct lock_stat *ls;
472 struct thread_stat *ts;
473 struct lock_seq_stat *seq;
474 u64 contended_term;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300475 const char *name = perf_evsel__strval(evsel, sample, "name");
476 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900477
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300478 memcpy(&addr, &tmp, sizeof(void *));
479
480 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600481 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700482 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900483 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600484 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900485
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300486 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600487 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700488 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600489
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300490 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600491 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700492 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900493
494 switch (seq->state) {
495 case SEQ_STATE_UNINITIALIZED:
496 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600497 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900498 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900499 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900500 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300501 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900502 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900503 if (contended_term < ls->wait_time_min)
504 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200505 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900506 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900507 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900508 case SEQ_STATE_RELEASED:
509 case SEQ_STATE_ACQUIRED:
510 case SEQ_STATE_READ_ACQUIRED:
511 /* broken lock sequence, discard it */
512 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200513 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900514 list_del(&seq->list);
515 free(seq);
516 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900517 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900518 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900519 break;
520 }
521
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900522 seq->state = SEQ_STATE_ACQUIRED;
523 ls->nr_acquired++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700524 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300525 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900526end:
David Ahern33d6aef2012-08-26 12:24:43 -0600527 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900528}
529
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300530static int report_lock_contended_event(struct perf_evsel *evsel,
531 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900532{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300533 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900534 struct lock_stat *ls;
535 struct thread_stat *ts;
536 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300537 const char *name = perf_evsel__strval(evsel, sample, "name");
538 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900539
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300540 memcpy(&addr, &tmp, sizeof(void *));
541
542 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600543 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700544 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900545 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600546 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900547
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300548 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600549 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700550 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600551
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300552 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600553 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700554 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900555
556 switch (seq->state) {
557 case SEQ_STATE_UNINITIALIZED:
558 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600559 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900560 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900561 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900562 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 */
567 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200568 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900569 list_del(&seq->list);
570 free(seq);
571 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900572 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900573 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900574 break;
575 }
576
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900577 seq->state = SEQ_STATE_CONTENDED;
578 ls->nr_contended++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700579 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300580 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900581end:
David Ahern33d6aef2012-08-26 12:24:43 -0600582 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900583}
584
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300585static int report_lock_release_event(struct perf_evsel *evsel,
586 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900587{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300588 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900589 struct lock_stat *ls;
590 struct thread_stat *ts;
591 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300592 const char *name = perf_evsel__strval(evsel, sample, "name");
593 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900594
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300595 memcpy(&addr, &tmp, sizeof(void *));
596
597 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600598 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700599 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900600 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600601 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900602
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300603 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600604 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700605 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600606
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300607 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600608 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700609 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900610
611 switch (seq->state) {
612 case SEQ_STATE_UNINITIALIZED:
613 goto end;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900614 case SEQ_STATE_ACQUIRED:
615 break;
616 case SEQ_STATE_READ_ACQUIRED:
617 seq->read_count--;
618 BUG_ON(seq->read_count < 0);
619 if (!seq->read_count) {
620 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900621 goto end;
622 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900623 break;
624 case SEQ_STATE_ACQUIRING:
625 case SEQ_STATE_CONTENDED:
626 case SEQ_STATE_RELEASED:
627 /* broken lock sequence, discard it */
628 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200629 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900630 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900631 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900632 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900633 break;
634 }
635
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900636 ls->nr_release++;
637free_seq:
638 list_del(&seq->list);
639 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900640end:
David Ahern33d6aef2012-08-26 12:24:43 -0600641 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900642}
643
644/* lock oriented handlers */
645/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100646static 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,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900651};
652
653static struct trace_lock_handler *trace_handler;
654
David Ahern33d6aef2012-08-26 12:24:43 -0600655static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300656 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900657{
Ingo Molnar59f411b2010-01-31 08:27:58 +0100658 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300659 return trace_handler->acquire_event(evsel, sample);
660 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900661}
662
David Ahern33d6aef2012-08-26 12:24:43 -0600663static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300664 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900665{
David Ahern33d6aef2012-08-26 12:24:43 -0600666 if (trace_handler->acquired_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300667 return trace_handler->acquired_event(evsel, sample);
668 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900669}
670
David Ahern33d6aef2012-08-26 12:24:43 -0600671static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300672 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900673{
David Ahern33d6aef2012-08-26 12:24:43 -0600674 if (trace_handler->contended_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300675 return trace_handler->contended_event(evsel, sample);
676 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900677}
678
David Ahern33d6aef2012-08-26 12:24:43 -0600679static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300680 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900681{
David Ahern33d6aef2012-08-26 12:24:43 -0600682 if (trace_handler->release_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300683 return trace_handler->release_event(evsel, sample);
684 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900685}
686
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200687static void print_bad_events(int bad, int total)
688{
689 /* Output for debug, this have to be removed */
690 int i;
691 const char *name[4] =
692 { "acquire", "acquired", "contended", "release" };
693
694 pr_info("\n=== output for debug===\n\n");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200695 pr_info("bad: %d, total: %d\n", bad, total);
Davidlohr Bueso60a25cb2013-09-08 19:19:18 -0700696 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200697 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]);
700}
701
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900702/* TODO: various way to print, coloring, nano or milli sec */
703static void print_result(void)
704{
705 struct lock_stat *st;
706 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900707 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900708
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900709 pr_info("%20s ", "Name");
710 pr_info("%10s ", "acquired");
711 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900712
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700713 pr_info("%15s ", "avg wait (ns)");
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900714 pr_info("%15s ", "total wait (ns)");
715 pr_info("%15s ", "max wait (ns)");
716 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900717
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900718 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900719
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900720 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900721 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900722 total++;
723 if (st->discard) {
724 bad++;
725 continue;
726 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900727 bzero(cut_name, 20);
728
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900729 if (strlen(st->name) < 16) {
730 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900731 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900732 } else {
733 strncpy(cut_name, st->name, 16);
734 cut_name[16] = '.';
735 cut_name[17] = '.';
736 cut_name[18] = '.';
737 cut_name[19] = '\0';
738 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900739 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900740 }
741
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900742 pr_info("%10u ", st->nr_acquired);
743 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900744
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700745 pr_info("%15" PRIu64 " ", st->avg_wait_time);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200746 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 ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900749 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900750 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900751 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900752
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200753 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900754}
755
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300756static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900757
758static void dump_threads(void)
759{
760 struct thread_stat *st;
761 struct rb_node *node;
762 struct thread *t;
763
764 pr_info("%10s: comm\n", "Thread ID");
765
766 node = rb_first(&thread_stats);
767 while (node) {
768 st = container_of(node, struct thread_stat, rb);
769 t = perf_session__findnew(session, st->tid);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200770 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900771 node = rb_next(node);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300772 thread__put(t);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900773 };
774}
775
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900776static void dump_map(void)
777{
778 unsigned int i;
779 struct lock_stat *st;
780
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900781 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900782 for (i = 0; i < LOCKHASH_SIZE; i++) {
783 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900784 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900785 }
786 }
787}
788
David Ahern33d6aef2012-08-26 12:24:43 -0600789static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900790{
David Ahern33d6aef2012-08-26 12:24:43 -0600791 int rc = 0;
792
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900793 if (info_threads)
794 dump_threads();
795 else if (info_map)
796 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600797 else {
798 rc = -1;
799 pr_err("Unknown type of information\n");
800 }
801
802 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900803}
804
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300805typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
806 struct perf_sample *sample);
807
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300808static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200809 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300810 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300811 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200812 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200813{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300814 int err = 0;
Adrian Hunter314add62013-08-27 11:23:03 +0300815 struct thread *thread = machine__findnew_thread(machine, sample->pid,
816 sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200817
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200818 if (thread == NULL) {
819 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200820 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200821 return -1;
822 }
823
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300824 if (evsel->handler != NULL) {
825 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300826 err = f(evsel, sample);
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300827 }
828
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300829 thread__put(thread);
830
831 return err;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200832}
833
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900834static void sort_result(void)
835{
836 unsigned int i;
837 struct lock_stat *st;
838
839 for (i = 0; i < LOCKHASH_SIZE; i++) {
840 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
841 insert_to_result(st, compare);
842 }
843 }
844}
845
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700846static const struct perf_evsel_str_handler lock_tracepoints[] = {
847 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
848 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
849 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
850 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
851};
852
Yunlong Songc4ac7322015-04-02 21:47:14 +0800853static bool force;
854
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700855static int __cmd_report(bool display_info)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900856{
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700857 int err = -EINVAL;
858 struct perf_tool eops = {
859 .sample = process_sample_event,
860 .comm = perf_event__process_comm,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200861 .ordered_events = true,
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700862 };
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200863 struct perf_data_file file = {
864 .path = input_name,
865 .mode = PERF_DATA_MODE_READ,
Yunlong Songc4ac7322015-04-02 21:47:14 +0800866 .force = force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200867 };
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700868
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200869 session = perf_session__new(&file, false, &eops);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700870 if (!session) {
871 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +0900872 return -1;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700873 }
874
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900875 symbol__init(&session->header.env);
Namhyung Kim6fd6c6b2014-08-12 15:40:40 +0900876
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700877 if (!perf_session__has_traces(session, "lock record"))
878 goto out_delete;
879
880 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
881 pr_err("Initializing perf session tracepoint handlers failed\n");
882 goto out_delete;
883 }
884
885 if (select_key())
886 goto out_delete;
887
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300888 err = perf_session__process_events(session);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700889 if (err)
890 goto out_delete;
891
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900892 setup_pager();
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700893 if (display_info) /* used for info subcommand */
894 err = dump_info();
895 else {
896 sort_result();
897 print_result();
898 }
David Ahern33d6aef2012-08-26 12:24:43 -0600899
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700900out_delete:
901 perf_session__delete(session);
902 return err;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900903}
904
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900905static int __cmd_record(int argc, const char **argv)
906{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300907 const char *record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200908 "record", "-R", "-m", "1024", "-c", "1",
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300909 };
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700910 unsigned int rec_argc, i, j, ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900911 const char **rec_argv;
912
David Ahernd25dcba2012-08-09 10:35:37 -0600913 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300914 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600915 pr_err("tracepoint %s is not enabled. "
916 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300917 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600918 return 1;
919 }
920 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900921
David Ahernd25dcba2012-08-09 10:35:37 -0600922 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
923 /* factor of 2 is for -e in front of each tracepoint */
924 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
925
926 rec_argv = calloc(rec_argc + 1, sizeof(char *));
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700927 if (!rec_argv)
Chris Samuelce47dc52010-11-13 13:35:06 +1100928 return -ENOMEM;
929
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900930 for (i = 0; i < ARRAY_SIZE(record_args); i++)
931 rec_argv[i] = strdup(record_args[i]);
932
David Ahernd25dcba2012-08-09 10:35:37 -0600933 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
934 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300935 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600936 }
937
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900938 for (j = 1; j < (unsigned int)argc; j++, i++)
939 rec_argv[i] = argv[j];
940
941 BUG_ON(i != rec_argc);
942
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700943 ret = cmd_record(i, rec_argv, NULL);
944 free(rec_argv);
945 return ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900946}
947
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300948int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900949{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300950 const struct option info_options[] = {
951 OPT_BOOLEAN('t', "threads", &info_threads,
952 "dump thread list in perf.data"),
953 OPT_BOOLEAN('m', "map", &info_map,
954 "map of lock instances (address:name table)"),
Yunlong Songc4ac7322015-04-02 21:47:14 +0800955 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300956 OPT_END()
957 };
958 const struct option lock_options[] = {
959 OPT_STRING('i', "input", &input_name, "file", "input file name"),
960 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
961 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
962 OPT_END()
963 };
964 const struct option report_options[] = {
965 OPT_STRING('k', "key", &sort_key, "acquired",
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700966 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
Yunlong Songc4ac7322015-04-02 21:47:14 +0800967 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300968 /* TODO: type */
969 OPT_END()
970 };
971 const char * const info_usage[] = {
972 "perf lock info [<options>]",
973 NULL
974 };
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400975 const char *const lock_subcommands[] = { "record", "report", "script",
976 "info", NULL };
977 const char *lock_usage[] = {
978 NULL,
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300979 NULL
980 };
981 const char * const report_usage[] = {
982 "perf lock report [<options>]",
983 NULL
984 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900985 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600986 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900987
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900988 for (i = 0; i < LOCKHASH_SIZE; i++)
989 INIT_LIST_HEAD(lockhash_table + i);
990
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400991 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
992 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900993 if (!argc)
994 usage_with_options(lock_usage, lock_options);
995
996 if (!strncmp(argv[0], "rec", 3)) {
997 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +0100998 } else if (!strncmp(argv[0], "report", 6)) {
999 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001000 if (argc) {
1001 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +01001002 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001003 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +01001004 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001005 }
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001006 rc = __cmd_report(false);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001007 } else if (!strcmp(argv[0], "script")) {
1008 /* Aliased to 'perf script' */
1009 return cmd_script(argc, argv, prefix);
Hitoshi Mitake26242d82010-05-03 14:12:00 +09001010 } else if (!strcmp(argv[0], "info")) {
1011 if (argc) {
1012 argc = parse_options(argc, argv,
1013 info_options, info_usage, 0);
1014 if (argc)
1015 usage_with_options(info_usage, info_options);
1016 }
Ingo Molnar59f411b2010-01-31 08:27:58 +01001017 /* recycling report_lock_ops */
1018 trace_handler = &report_lock_ops;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001019 rc = __cmd_report(true);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001020 } else {
1021 usage_with_options(lock_usage, lock_options);
1022 }
1023
David Ahern33d6aef2012-08-26 12:24:43 -06001024 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001025}