blob: 6a9076f165f44e7babfe8006a56765df4f2d1fb3 [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
12#include "util/parse-options.h"
13#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"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090018
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
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090029static struct perf_session *session;
30
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090031/* based on kernel/lockdep.c */
32#define LOCKHASH_BITS 12
33#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
34
35static 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090040struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010041 struct list_head hash_entry;
42 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090043
Ingo Molnar59f411b2010-01-31 08:27:58 +010044 /*
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -030045 * FIXME: perf_evsel__intval() returns u64,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090046 * so address of lockdep_map should be dealed as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010047 * 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 */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090051
Ingo Molnar59f411b2010-01-31 08:27:58 +010052 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090053 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010054 unsigned int nr_contended;
55 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090056
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090057 unsigned int nr_readlock;
58 unsigned int nr_trylock;
Davidlohr Buesof37376c2013-09-08 19:19:19 -070059
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090060 /* these times are in nano sec. */
Davidlohr Buesof37376c2013-09-08 19:19:19 -070061 u64 avg_wait_time;
Ingo Molnar59f411b2010-01-31 08:27:58 +010062 u64 wait_time_total;
63 u64 wait_time_min;
64 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090065
66 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090067};
68
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090069/*
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 */
99struct 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
108struct thread_stat {
109 struct rb_node rb;
110
111 u32 tid;
112 struct list_head seq_list;
113};
114
115static struct rb_root thread_stats;
116
117static 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
136static 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
158static 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));
David Ahern33d6aef2012-08-26 12:24:43 -0600167 if (!st) {
168 pr_err("memory allocation failed\n");
169 return NULL;
170 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900171
172 st->tid = tid;
173 INIT_LIST_HEAD(&st->seq_list);
174
175 thread_stat_insert(st);
176
177 return st;
178}
179
180static struct thread_stat *thread_stat_findnew_first(u32 tid);
181static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
182 thread_stat_findnew_first;
183
184static struct thread_stat *thread_stat_findnew_first(u32 tid)
185{
186 struct thread_stat *st;
187
188 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600189 if (!st) {
190 pr_err("memory allocation failed\n");
191 return NULL;
192 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900193 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900203/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100204#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900205 static int lock_stat_key_ ## member(struct lock_stat *one, \
206 struct lock_stat *two) \
207 { \
208 return one->member > two->member; \
209 }
210
211SINGLE_KEY(nr_acquired)
212SINGLE_KEY(nr_contended)
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700213SINGLE_KEY(avg_wait_time)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900214SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900215SINGLE_KEY(wait_time_max)
216
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100217static 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900229struct 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 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100235 const char *name;
236 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900237};
238
Ingo Molnar59f411b2010-01-31 08:27:58 +0100239static const char *sort_key = "acquired";
240
241static int (*compare)(struct lock_stat *, struct lock_stat *);
242
243static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900244
245#define DEF_KEY_LOCK(name, fn_suffix) \
246 { #name, lock_stat_key_ ## fn_suffix }
247struct lock_key keys[] = {
248 DEF_KEY_LOCK(acquired, nr_acquired),
249 DEF_KEY_LOCK(contended, nr_contended),
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700250 DEF_KEY_LOCK(avg_wait, avg_wait_time),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900251 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
David Ahern33d6aef2012-08-26 12:24:43 -0600260static int select_key(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900261{
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;
David Ahern33d6aef2012-08-26 12:24:43 -0600267 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900268 }
269 }
270
David Ahern33d6aef2012-08-26 12:24:43 -0600271 pr_err("Unknown compare key: %s\n", sort_key);
272
273 return -1;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900274}
275
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900276static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100277 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900278{
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 */
298static 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
Ingo Molnar59f411b2010-01-31 08:27:58 +0100312static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900313{
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);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700328 if (!new->name) {
329 free(new);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900330 goto alloc_failed;
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700331 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900332
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700333 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900334 new->wait_time_min = ULLONG_MAX;
335
336 list_add(&new->hash_entry, entry);
337 return new;
338
339alloc_failed:
David Ahern33d6aef2012-08-26 12:24:43 -0600340 pr_err("memory allocation failed\n");
341 return NULL;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900342}
343
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900344struct trace_lock_handler {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300345 int (*acquire_event)(struct perf_evsel *evsel,
346 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900347
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300348 int (*acquired_event)(struct perf_evsel *evsel,
349 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900350
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300351 int (*contended_event)(struct perf_evsel *evsel,
352 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900353
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300354 int (*release_event)(struct perf_evsel *evsel,
355 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900356};
357
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900358static 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));
David Ahern33d6aef2012-08-26 12:24:43 -0600368 if (!seq) {
369 pr_err("memory allocation failed\n");
370 return NULL;
371 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900372 seq->state = SEQ_STATE_UNINITIALIZED;
373 seq->addr = addr;
374
375 list_add(&seq->list, &ts->seq_list);
376 return seq;
377}
378
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200379enum broken_state {
380 BROKEN_ACQUIRE,
381 BROKEN_ACQUIRED,
382 BROKEN_CONTENDED,
383 BROKEN_RELEASE,
384 BROKEN_MAX,
385};
386
387static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900388
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200389enum acquire_flags {
390 TRY_LOCK = 1,
391 READ_LOCK = 2,
392};
393
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300394static int report_lock_acquire_event(struct perf_evsel *evsel,
395 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900396{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300397 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900398 struct lock_stat *ls;
399 struct thread_stat *ts;
400 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300401 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");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900404
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300405 memcpy(&addr, &tmp, sizeof(void *));
406
407 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600408 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700409 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900410 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600411 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900412
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300413 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600414 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700415 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600416
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300417 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600418 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700419 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900420
421 switch (seq->state) {
422 case SEQ_STATE_UNINITIALIZED:
423 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300424 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900425 seq->state = SEQ_STATE_ACQUIRING;
426 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300427 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900428 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300429 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900430 ls->nr_readlock++;
431 seq->state = SEQ_STATE_READ_ACQUIRED;
432 seq->read_count = 1;
433 ls->nr_acquired++;
434 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900435 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900436 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300437 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900438 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:
448broken:
449 /* broken lock sequence, discard it */
450 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200451 bad_hist[BROKEN_ACQUIRE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900452 list_del(&seq->list);
453 free(seq);
454 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900455 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900456 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900457 break;
458 }
459
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900460 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300461 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900462end:
David Ahern33d6aef2012-08-26 12:24:43 -0600463 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900464}
465
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300466static int report_lock_acquired_event(struct perf_evsel *evsel,
467 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900468{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300469 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900470 struct lock_stat *ls;
471 struct thread_stat *ts;
472 struct lock_seq_stat *seq;
473 u64 contended_term;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300474 const char *name = perf_evsel__strval(evsel, sample, "name");
475 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900476
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300477 memcpy(&addr, &tmp, sizeof(void *));
478
479 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600480 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700481 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900482 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600483 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900484
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300485 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600486 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700487 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600488
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300489 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600490 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700491 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900492
493 switch (seq->state) {
494 case SEQ_STATE_UNINITIALIZED:
495 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600496 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900497 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900498 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900499 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300500 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900501 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900502 if (contended_term < ls->wait_time_min)
503 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200504 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900505 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900506 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900507 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;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200512 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900513 list_del(&seq->list);
514 free(seq);
515 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900516 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900517 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900518 break;
519 }
520
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900521 seq->state = SEQ_STATE_ACQUIRED;
522 ls->nr_acquired++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700523 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300524 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900525end:
David Ahern33d6aef2012-08-26 12:24:43 -0600526 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900527}
528
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300529static int report_lock_contended_event(struct perf_evsel *evsel,
530 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900531{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300532 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900533 struct lock_stat *ls;
534 struct thread_stat *ts;
535 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300536 const char *name = perf_evsel__strval(evsel, sample, "name");
537 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900538
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300539 memcpy(&addr, &tmp, sizeof(void *));
540
541 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600542 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700543 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900544 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600545 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900546
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300547 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600548 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700549 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600550
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300551 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600552 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700553 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900554
555 switch (seq->state) {
556 case SEQ_STATE_UNINITIALIZED:
557 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600558 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900559 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900560 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900561 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;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200567 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900568 list_del(&seq->list);
569 free(seq);
570 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900571 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900572 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900573 break;
574 }
575
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900576 seq->state = SEQ_STATE_CONTENDED;
577 ls->nr_contended++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700578 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300579 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900580end:
David Ahern33d6aef2012-08-26 12:24:43 -0600581 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900582}
583
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300584static int report_lock_release_event(struct perf_evsel *evsel,
585 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900586{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300587 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900588 struct lock_stat *ls;
589 struct thread_stat *ts;
590 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300591 const char *name = perf_evsel__strval(evsel, sample, "name");
592 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900593
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300594 memcpy(&addr, &tmp, sizeof(void *));
595
596 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600597 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700598 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900599 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600600 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900601
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300602 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600603 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700604 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600605
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300606 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600607 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700608 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900609
610 switch (seq->state) {
611 case SEQ_STATE_UNINITIALIZED:
612 goto end;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900613 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++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900620 goto end;
621 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900622 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;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200628 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900629 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900630 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900631 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900632 break;
633 }
634
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900635 ls->nr_release++;
636free_seq:
637 list_del(&seq->list);
638 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900639end:
David Ahern33d6aef2012-08-26 12:24:43 -0600640 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900641}
642
643/* lock oriented handlers */
644/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100645static 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,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900650};
651
652static struct trace_lock_handler *trace_handler;
653
David Ahern33d6aef2012-08-26 12:24:43 -0600654static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300655 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900656{
Ingo Molnar59f411b2010-01-31 08:27:58 +0100657 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300658 return trace_handler->acquire_event(evsel, sample);
659 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900660}
661
David Ahern33d6aef2012-08-26 12:24:43 -0600662static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300663 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900664{
David Ahern33d6aef2012-08-26 12:24:43 -0600665 if (trace_handler->acquired_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300666 return trace_handler->acquired_event(evsel, sample);
667 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900668}
669
David Ahern33d6aef2012-08-26 12:24:43 -0600670static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300671 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900672{
David Ahern33d6aef2012-08-26 12:24:43 -0600673 if (trace_handler->contended_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300674 return trace_handler->contended_event(evsel, sample);
675 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900676}
677
David Ahern33d6aef2012-08-26 12:24:43 -0600678static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300679 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900680{
David Ahern33d6aef2012-08-26 12:24:43 -0600681 if (trace_handler->release_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300682 return trace_handler->release_event(evsel, sample);
683 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900684}
685
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200686static 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");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200694 pr_info("bad: %d, total: %d\n", bad, total);
Davidlohr Bueso60a25cb2013-09-08 19:19:18 -0700695 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200696 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900701/* TODO: various way to print, coloring, nano or milli sec */
702static void print_result(void)
703{
704 struct lock_stat *st;
705 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900706 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900707
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900708 pr_info("%20s ", "Name");
709 pr_info("%10s ", "acquired");
710 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900711
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700712 pr_info("%15s ", "avg wait (ns)");
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900713 pr_info("%15s ", "total wait (ns)");
714 pr_info("%15s ", "max wait (ns)");
715 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900716
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900717 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900718
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900719 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900720 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900721 total++;
722 if (st->discard) {
723 bad++;
724 continue;
725 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900726 bzero(cut_name, 20);
727
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900728 if (strlen(st->name) < 16) {
729 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900730 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900731 } 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 */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900738 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900739 }
740
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900741 pr_info("%10u ", st->nr_acquired);
742 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900743
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700744 pr_info("%15" PRIu64 " ", st->avg_wait_time);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200745 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 ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900748 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900749 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900750 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900751
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200752 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900753}
754
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300755static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900756
757static 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900774static void dump_map(void)
775{
776 unsigned int i;
777 struct lock_stat *st;
778
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900779 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900780 for (i = 0; i < LOCKHASH_SIZE; i++) {
781 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900782 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900783 }
784 }
785}
786
David Ahern33d6aef2012-08-26 12:24:43 -0600787static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900788{
David Ahern33d6aef2012-08-26 12:24:43 -0600789 int rc = 0;
790
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900791 if (info_threads)
792 dump_threads();
793 else if (info_map)
794 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600795 else {
796 rc = -1;
797 pr_err("Unknown type of information\n");
798 }
799
800 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900801}
802
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300803typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
804 struct perf_sample *sample);
805
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300806static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200807 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300808 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300809 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200810 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200811{
Adrian Hunter314add62013-08-27 11:23:03 +0300812 struct thread *thread = machine__findnew_thread(machine, sample->pid,
813 sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200814
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200815 if (thread == NULL) {
816 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200817 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200818 return -1;
819 }
820
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300821 if (evsel->handler.func != NULL) {
822 tracepoint_handler f = evsel->handler.func;
823 return f(evsel, sample);
824 }
825
826 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200827}
828
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900829static 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
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700841static 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
848static int __cmd_report(bool display_info)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900849{
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700850 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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900878 setup_pager();
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700879 if (display_info) /* used for info subcommand */
880 err = dump_info();
881 else {
882 sort_result();
883 print_result();
884 }
David Ahern33d6aef2012-08-26 12:24:43 -0600885
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700886out_delete:
887 perf_session__delete(session);
888 return err;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900889}
890
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900891static int __cmd_record(int argc, const char **argv)
892{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300893 const char *record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200894 "record", "-R", "-m", "1024", "-c", "1",
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300895 };
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700896 unsigned int rec_argc, i, j, ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900897 const char **rec_argv;
898
David Ahernd25dcba2012-08-09 10:35:37 -0600899 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300900 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600901 pr_err("tracepoint %s is not enabled. "
902 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300903 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600904 return 1;
905 }
906 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900907
David Ahernd25dcba2012-08-09 10:35:37 -0600908 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 *));
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700913 if (!rec_argv)
Chris Samuelce47dc52010-11-13 13:35:06 +1100914 return -ENOMEM;
915
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900916 for (i = 0; i < ARRAY_SIZE(record_args); i++)
917 rec_argv[i] = strdup(record_args[i]);
918
David Ahernd25dcba2012-08-09 10:35:37 -0600919 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
920 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300921 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600922 }
923
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900924 for (j = 1; j < (unsigned int)argc; j++, i++)
925 rec_argv[i] = argv[j];
926
927 BUG_ON(i != rec_argc);
928
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700929 ret = cmd_record(i, rec_argv, NULL);
930 free(rec_argv);
931 return ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900932}
933
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300934int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900935{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300936 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",
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700951 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300952 /* 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 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900967 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600968 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900969
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);
Ingo Molnar59f411b2010-01-31 08:27:58 +0100981 } else if (!strncmp(argv[0], "report", 6)) {
982 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900983 if (argc) {
984 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100985 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900986 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +0100987 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900988 }
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700989 rc = __cmd_report(false);
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100990 } else if (!strcmp(argv[0], "script")) {
991 /* Aliased to 'perf script' */
992 return cmd_script(argc, argv, prefix);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900993 } 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 }
Ingo Molnar59f411b2010-01-31 08:27:58 +01001000 /* recycling report_lock_ops */
1001 trace_handler = &report_lock_ops;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001002 rc = __cmd_report(true);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001003 } else {
1004 usage_with_options(lock_usage, lock_options);
1005 }
1006
David Ahern33d6aef2012-08-26 12:24:43 -06001007 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001008}