blob: 3f8b9550a6ef68cfcf3103f8b1d93dd7582ae78b [file] [log] [blame]
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001#include "builtin.h"
2#include "perf.h"
3
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03004#include "util/evsel.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09005#include "util/util.h"
6#include "util/cache.h"
7#include "util/symbol.h"
8#include "util/thread.h"
9#include "util/header.h"
10
11#include "util/parse-options.h"
12#include "util/trace-event.h"
13
14#include "util/debug.h"
15#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020016#include "util/tool.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090017
18#include <sys/types.h>
19#include <sys/prctl.h>
20#include <semaphore.h>
21#include <pthread.h>
22#include <math.h>
23#include <limits.h>
24
25#include <linux/list.h>
26#include <linux/hash.h>
27
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090028static struct perf_session *session;
29
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090030/* based on kernel/lockdep.c */
31#define LOCKHASH_BITS 12
32#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
33
34static struct list_head lockhash_table[LOCKHASH_SIZE];
35
36#define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
37#define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
38
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090039struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010040 struct list_head hash_entry;
41 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090042
Ingo Molnar59f411b2010-01-31 08:27:58 +010043 /*
44 * FIXME: raw_field_value() returns unsigned long long,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090045 * so address of lockdep_map should be dealed as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010046 * Is there more better solution?
47 */
48 void *addr; /* address of lockdep_map, used as ID */
49 char *name; /* for strcpy(), we cannot use const */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090050
Ingo Molnar59f411b2010-01-31 08:27:58 +010051 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090052 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010053 unsigned int nr_contended;
54 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090055
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090056 unsigned int nr_readlock;
57 unsigned int nr_trylock;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090058 /* these times are in nano sec. */
Ingo Molnar59f411b2010-01-31 08:27:58 +010059 u64 wait_time_total;
60 u64 wait_time_min;
61 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090062
63 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090064};
65
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090066/*
67 * States of lock_seq_stat
68 *
69 * UNINITIALIZED is required for detecting first event of acquire.
70 * As the nature of lock events, there is no guarantee
71 * that the first event for the locks are acquire,
72 * it can be acquired, contended or release.
73 */
74#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
75#define SEQ_STATE_RELEASED 1
76#define SEQ_STATE_ACQUIRING 2
77#define SEQ_STATE_ACQUIRED 3
78#define SEQ_STATE_READ_ACQUIRED 4
79#define SEQ_STATE_CONTENDED 5
80
81/*
82 * MAX_LOCK_DEPTH
83 * Imported from include/linux/sched.h.
84 * Should this be synchronized?
85 */
86#define MAX_LOCK_DEPTH 48
87
88/*
89 * struct lock_seq_stat:
90 * Place to put on state of one lock sequence
91 * 1) acquire -> acquired -> release
92 * 2) acquire -> contended -> acquired -> release
93 * 3) acquire (with read or try) -> release
94 * 4) Are there other patterns?
95 */
96struct lock_seq_stat {
97 struct list_head list;
98 int state;
99 u64 prev_event_time;
100 void *addr;
101
102 int read_count;
103};
104
105struct thread_stat {
106 struct rb_node rb;
107
108 u32 tid;
109 struct list_head seq_list;
110};
111
112static struct rb_root thread_stats;
113
114static struct thread_stat *thread_stat_find(u32 tid)
115{
116 struct rb_node *node;
117 struct thread_stat *st;
118
119 node = thread_stats.rb_node;
120 while (node) {
121 st = container_of(node, struct thread_stat, rb);
122 if (st->tid == tid)
123 return st;
124 else if (tid < st->tid)
125 node = node->rb_left;
126 else
127 node = node->rb_right;
128 }
129
130 return NULL;
131}
132
133static void thread_stat_insert(struct thread_stat *new)
134{
135 struct rb_node **rb = &thread_stats.rb_node;
136 struct rb_node *parent = NULL;
137 struct thread_stat *p;
138
139 while (*rb) {
140 p = container_of(*rb, struct thread_stat, rb);
141 parent = *rb;
142
143 if (new->tid < p->tid)
144 rb = &(*rb)->rb_left;
145 else if (new->tid > p->tid)
146 rb = &(*rb)->rb_right;
147 else
148 BUG_ON("inserting invalid thread_stat\n");
149 }
150
151 rb_link_node(&new->rb, parent, rb);
152 rb_insert_color(&new->rb, &thread_stats);
153}
154
155static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
156{
157 struct thread_stat *st;
158
159 st = thread_stat_find(tid);
160 if (st)
161 return st;
162
163 st = zalloc(sizeof(struct thread_stat));
164 if (!st)
165 die("memory allocation failed\n");
166
167 st->tid = tid;
168 INIT_LIST_HEAD(&st->seq_list);
169
170 thread_stat_insert(st);
171
172 return st;
173}
174
175static struct thread_stat *thread_stat_findnew_first(u32 tid);
176static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
177 thread_stat_findnew_first;
178
179static struct thread_stat *thread_stat_findnew_first(u32 tid)
180{
181 struct thread_stat *st;
182
183 st = zalloc(sizeof(struct thread_stat));
184 if (!st)
185 die("memory allocation failed\n");
186 st->tid = tid;
187 INIT_LIST_HEAD(&st->seq_list);
188
189 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
190 rb_insert_color(&st->rb, &thread_stats);
191
192 thread_stat_findnew = thread_stat_findnew_after_first;
193 return st;
194}
195
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900196/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100197#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900198 static int lock_stat_key_ ## member(struct lock_stat *one, \
199 struct lock_stat *two) \
200 { \
201 return one->member > two->member; \
202 }
203
204SINGLE_KEY(nr_acquired)
205SINGLE_KEY(nr_contended)
206SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900207SINGLE_KEY(wait_time_max)
208
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100209static int lock_stat_key_wait_time_min(struct lock_stat *one,
210 struct lock_stat *two)
211{
212 u64 s1 = one->wait_time_min;
213 u64 s2 = two->wait_time_min;
214 if (s1 == ULLONG_MAX)
215 s1 = 0;
216 if (s2 == ULLONG_MAX)
217 s2 = 0;
218 return s1 > s2;
219}
220
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900221struct lock_key {
222 /*
223 * name: the value for specify by user
224 * this should be simpler than raw name of member
225 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
226 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100227 const char *name;
228 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900229};
230
Ingo Molnar59f411b2010-01-31 08:27:58 +0100231static const char *sort_key = "acquired";
232
233static int (*compare)(struct lock_stat *, struct lock_stat *);
234
235static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900236
237#define DEF_KEY_LOCK(name, fn_suffix) \
238 { #name, lock_stat_key_ ## fn_suffix }
239struct lock_key keys[] = {
240 DEF_KEY_LOCK(acquired, nr_acquired),
241 DEF_KEY_LOCK(contended, nr_contended),
242 DEF_KEY_LOCK(wait_total, wait_time_total),
243 DEF_KEY_LOCK(wait_min, wait_time_min),
244 DEF_KEY_LOCK(wait_max, wait_time_max),
245
246 /* extra comparisons much complicated should be here */
247
248 { NULL, NULL }
249};
250
251static void select_key(void)
252{
253 int i;
254
255 for (i = 0; keys[i].name; i++) {
256 if (!strcmp(keys[i].name, sort_key)) {
257 compare = keys[i].key;
258 return;
259 }
260 }
261
262 die("Unknown compare key:%s\n", sort_key);
263}
264
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900265static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100266 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900267{
268 struct rb_node **rb = &result.rb_node;
269 struct rb_node *parent = NULL;
270 struct lock_stat *p;
271
272 while (*rb) {
273 p = container_of(*rb, struct lock_stat, rb);
274 parent = *rb;
275
276 if (bigger(st, p))
277 rb = &(*rb)->rb_left;
278 else
279 rb = &(*rb)->rb_right;
280 }
281
282 rb_link_node(&st->rb, parent, rb);
283 rb_insert_color(&st->rb, &result);
284}
285
286/* returns left most element of result, and erase it */
287static struct lock_stat *pop_from_result(void)
288{
289 struct rb_node *node = result.rb_node;
290
291 if (!node)
292 return NULL;
293
294 while (node->rb_left)
295 node = node->rb_left;
296
297 rb_erase(node, &result);
298 return container_of(node, struct lock_stat, rb);
299}
300
Ingo Molnar59f411b2010-01-31 08:27:58 +0100301static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900302{
303 struct list_head *entry = lockhashentry(addr);
304 struct lock_stat *ret, *new;
305
306 list_for_each_entry(ret, entry, hash_entry) {
307 if (ret->addr == addr)
308 return ret;
309 }
310
311 new = zalloc(sizeof(struct lock_stat));
312 if (!new)
313 goto alloc_failed;
314
315 new->addr = addr;
316 new->name = zalloc(sizeof(char) * strlen(name) + 1);
317 if (!new->name)
318 goto alloc_failed;
319 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900320
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900321 new->wait_time_min = ULLONG_MAX;
322
323 list_add(&new->hash_entry, entry);
324 return new;
325
326alloc_failed:
327 die("memory allocation failed\n");
328}
329
Robert Richterefad1412011-12-07 10:02:54 +0100330static const char *input_name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900331
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900332struct raw_event_sample {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100333 u32 size;
334 char data[0];
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900335};
336
337struct trace_acquire_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100338 void *addr;
339 const char *name;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900340 int flag;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900341};
342
343struct trace_acquired_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100344 void *addr;
345 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900346};
347
348struct trace_contended_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100349 void *addr;
350 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900351};
352
353struct trace_release_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100354 void *addr;
355 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900356};
357
358struct trace_lock_handler {
359 void (*acquire_event)(struct trace_acquire_event *,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300360 const struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900361
362 void (*acquired_event)(struct trace_acquired_event *,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300363 const struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900364
365 void (*contended_event)(struct trace_contended_event *,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300366 const struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900367
368 void (*release_event)(struct trace_release_event *,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300369 const struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900370};
371
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900372static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
373{
374 struct lock_seq_stat *seq;
375
376 list_for_each_entry(seq, &ts->seq_list, list) {
377 if (seq->addr == addr)
378 return seq;
379 }
380
381 seq = zalloc(sizeof(struct lock_seq_stat));
382 if (!seq)
383 die("Not enough memory\n");
384 seq->state = SEQ_STATE_UNINITIALIZED;
385 seq->addr = addr;
386
387 list_add(&seq->list, &ts->seq_list);
388 return seq;
389}
390
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200391enum broken_state {
392 BROKEN_ACQUIRE,
393 BROKEN_ACQUIRED,
394 BROKEN_CONTENDED,
395 BROKEN_RELEASE,
396 BROKEN_MAX,
397};
398
399static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900400
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200401enum acquire_flags {
402 TRY_LOCK = 1,
403 READ_LOCK = 2,
404};
405
Ingo Molnar59f411b2010-01-31 08:27:58 +0100406static void
407report_lock_acquire_event(struct trace_acquire_event *acquire_event,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300408 const struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900409{
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900410 struct lock_stat *ls;
411 struct thread_stat *ts;
412 struct lock_seq_stat *seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900413
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900414 ls = lock_stat_findnew(acquire_event->addr, acquire_event->name);
415 if (ls->discard)
416 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900417
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300418 ts = thread_stat_findnew(sample->tid);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900419 seq = get_seq(ts, acquire_event->addr);
420
421 switch (seq->state) {
422 case SEQ_STATE_UNINITIALIZED:
423 case SEQ_STATE_RELEASED:
424 if (!acquire_event->flag) {
425 seq->state = SEQ_STATE_ACQUIRING;
426 } else {
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200427 if (acquire_event->flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900428 ls->nr_trylock++;
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200429 if (acquire_event->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:
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200437 if (acquire_event->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 break;
456 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:
464 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900465}
466
Ingo Molnar59f411b2010-01-31 08:27:58 +0100467static void
468report_lock_acquired_event(struct trace_acquired_event *acquired_event,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300469 const struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900470{
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300471 u64 timestamp = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900472 struct lock_stat *ls;
473 struct thread_stat *ts;
474 struct lock_seq_stat *seq;
475 u64 contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900476
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900477 ls = lock_stat_findnew(acquired_event->addr, acquired_event->name);
478 if (ls->discard)
479 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900480
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300481 ts = thread_stat_findnew(sample->tid);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900482 seq = get_seq(ts, acquired_event->addr);
483
484 switch (seq->state) {
485 case SEQ_STATE_UNINITIALIZED:
486 /* orphan event, do nothing */
487 return;
488 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900489 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900490 case SEQ_STATE_CONTENDED:
491 contended_term = timestamp - seq->prev_event_time;
492 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900493 if (contended_term < ls->wait_time_min)
494 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200495 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900496 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900497 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900498 case SEQ_STATE_RELEASED:
499 case SEQ_STATE_ACQUIRED:
500 case SEQ_STATE_READ_ACQUIRED:
501 /* broken lock sequence, discard it */
502 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200503 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900504 list_del(&seq->list);
505 free(seq);
506 goto end;
507 break;
508
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900509 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900510 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900511 break;
512 }
513
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900514 seq->state = SEQ_STATE_ACQUIRED;
515 ls->nr_acquired++;
516 seq->prev_event_time = timestamp;
517end:
518 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900519}
520
Ingo Molnar59f411b2010-01-31 08:27:58 +0100521static void
522report_lock_contended_event(struct trace_contended_event *contended_event,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300523 const struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900524{
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900525 struct lock_stat *ls;
526 struct thread_stat *ts;
527 struct lock_seq_stat *seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900528
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900529 ls = lock_stat_findnew(contended_event->addr, contended_event->name);
530 if (ls->discard)
531 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900532
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300533 ts = thread_stat_findnew(sample->tid);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900534 seq = get_seq(ts, contended_event->addr);
535
536 switch (seq->state) {
537 case SEQ_STATE_UNINITIALIZED:
538 /* orphan event, do nothing */
539 return;
540 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900541 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900542 case SEQ_STATE_RELEASED:
543 case SEQ_STATE_ACQUIRED:
544 case SEQ_STATE_READ_ACQUIRED:
545 case SEQ_STATE_CONTENDED:
546 /* broken lock sequence, discard it */
547 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200548 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900549 list_del(&seq->list);
550 free(seq);
551 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900552 break;
553 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900554 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900555 break;
556 }
557
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900558 seq->state = SEQ_STATE_CONTENDED;
559 ls->nr_contended++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300560 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900561end:
562 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900563}
564
Ingo Molnar59f411b2010-01-31 08:27:58 +0100565static void
566report_lock_release_event(struct trace_release_event *release_event,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300567 const struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900568{
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900569 struct lock_stat *ls;
570 struct thread_stat *ts;
571 struct lock_seq_stat *seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900572
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900573 ls = lock_stat_findnew(release_event->addr, release_event->name);
574 if (ls->discard)
575 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900576
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300577 ts = thread_stat_findnew(sample->tid);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900578 seq = get_seq(ts, release_event->addr);
579
580 switch (seq->state) {
581 case SEQ_STATE_UNINITIALIZED:
582 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900583 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900584 case SEQ_STATE_ACQUIRED:
585 break;
586 case SEQ_STATE_READ_ACQUIRED:
587 seq->read_count--;
588 BUG_ON(seq->read_count < 0);
589 if (!seq->read_count) {
590 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900591 goto end;
592 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900593 break;
594 case SEQ_STATE_ACQUIRING:
595 case SEQ_STATE_CONTENDED:
596 case SEQ_STATE_RELEASED:
597 /* broken lock sequence, discard it */
598 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200599 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900600 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900601 break;
602 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900603 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900604 break;
605 }
606
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900607 ls->nr_release++;
608free_seq:
609 list_del(&seq->list);
610 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900611end:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900612 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900613}
614
615/* lock oriented handlers */
616/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100617static struct trace_lock_handler report_lock_ops = {
618 .acquire_event = report_lock_acquire_event,
619 .acquired_event = report_lock_acquired_event,
620 .contended_event = report_lock_contended_event,
621 .release_event = report_lock_release_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900622};
623
624static struct trace_lock_handler *trace_handler;
625
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300626static void perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
627 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900628{
629 struct trace_acquire_event acquire_event;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300630 struct event_format *event = evsel->tp_format;
631 void *data = sample->raw_data;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900632 u64 tmp; /* this is required for casting... */
633
634 tmp = raw_field_value(event, "lockdep_addr", data);
635 memcpy(&acquire_event.addr, &tmp, sizeof(void *));
636 acquire_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900637 acquire_event.flag = (int)raw_field_value(event, "flag", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900638
Ingo Molnar59f411b2010-01-31 08:27:58 +0100639 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300640 trace_handler->acquire_event(&acquire_event, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900641}
642
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300643static void perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
644 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900645{
646 struct trace_acquired_event acquired_event;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300647 struct event_format *event = evsel->tp_format;
648 void *data = sample->raw_data;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900649 u64 tmp; /* this is required for casting... */
650
651 tmp = raw_field_value(event, "lockdep_addr", data);
652 memcpy(&acquired_event.addr, &tmp, sizeof(void *));
653 acquired_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900654
Ingo Molnar59f411b2010-01-31 08:27:58 +0100655 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300656 trace_handler->acquired_event(&acquired_event, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900657}
658
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300659static void perf_evsel__process_lock_contended(struct perf_evsel *evsel,
660 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900661{
662 struct trace_contended_event contended_event;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300663 struct event_format *event = evsel->tp_format;
664 void *data = sample->raw_data;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900665 u64 tmp; /* this is required for casting... */
666
667 tmp = raw_field_value(event, "lockdep_addr", data);
668 memcpy(&contended_event.addr, &tmp, sizeof(void *));
669 contended_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900670
Ingo Molnar59f411b2010-01-31 08:27:58 +0100671 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300672 trace_handler->contended_event(&contended_event, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900673}
674
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300675static void perf_evsel__process_lock_release(struct perf_evsel *evsel,
676 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900677{
678 struct trace_release_event release_event;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300679 struct event_format *event = evsel->tp_format;
680 void *data = sample->raw_data;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900681 u64 tmp; /* this is required for casting... */
682
683 tmp = raw_field_value(event, "lockdep_addr", data);
684 memcpy(&release_event.addr, &tmp, sizeof(void *));
685 release_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900686
Ingo Molnar59f411b2010-01-31 08:27:58 +0100687 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300688 trace_handler->release_event(&release_event, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900689}
690
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300691static void perf_evsel__process_lock_event(struct perf_evsel *evsel,
692 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900693{
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300694 struct event_format *event = evsel->tp_format;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900695
696 if (!strcmp(event->name, "lock_acquire"))
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300697 perf_evsel__process_lock_acquire(evsel, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900698 if (!strcmp(event->name, "lock_acquired"))
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300699 perf_evsel__process_lock_acquired(evsel, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900700 if (!strcmp(event->name, "lock_contended"))
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300701 perf_evsel__process_lock_contended(evsel, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900702 if (!strcmp(event->name, "lock_release"))
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300703 perf_evsel__process_lock_release(evsel, sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900704}
705
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200706static void print_bad_events(int bad, int total)
707{
708 /* Output for debug, this have to be removed */
709 int i;
710 const char *name[4] =
711 { "acquire", "acquired", "contended", "release" };
712
713 pr_info("\n=== output for debug===\n\n");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200714 pr_info("bad: %d, total: %d\n", bad, total);
715 pr_info("bad rate: %f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200716 pr_info("histogram of events caused bad sequence\n");
717 for (i = 0; i < BROKEN_MAX; i++)
718 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
719}
720
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900721/* TODO: various way to print, coloring, nano or milli sec */
722static void print_result(void)
723{
724 struct lock_stat *st;
725 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900726 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900727
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900728 pr_info("%20s ", "Name");
729 pr_info("%10s ", "acquired");
730 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900731
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900732 pr_info("%15s ", "total wait (ns)");
733 pr_info("%15s ", "max wait (ns)");
734 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900735
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900736 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900737
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900738 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900739 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900740 total++;
741 if (st->discard) {
742 bad++;
743 continue;
744 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900745 bzero(cut_name, 20);
746
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900747 if (strlen(st->name) < 16) {
748 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900749 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900750 } else {
751 strncpy(cut_name, st->name, 16);
752 cut_name[16] = '.';
753 cut_name[17] = '.';
754 cut_name[18] = '.';
755 cut_name[19] = '\0';
756 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900757 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900758 }
759
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900760 pr_info("%10u ", st->nr_acquired);
761 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900762
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200763 pr_info("%15" PRIu64 " ", st->wait_time_total);
764 pr_info("%15" PRIu64 " ", st->wait_time_max);
765 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900766 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900767 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900768 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900769
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200770 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900771}
772
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300773static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900774
775static void dump_threads(void)
776{
777 struct thread_stat *st;
778 struct rb_node *node;
779 struct thread *t;
780
781 pr_info("%10s: comm\n", "Thread ID");
782
783 node = rb_first(&thread_stats);
784 while (node) {
785 st = container_of(node, struct thread_stat, rb);
786 t = perf_session__findnew(session, st->tid);
787 pr_info("%10d: %s\n", st->tid, t->comm);
788 node = rb_next(node);
789 };
790}
791
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900792static void dump_map(void)
793{
794 unsigned int i;
795 struct lock_stat *st;
796
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900797 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900798 for (i = 0; i < LOCKHASH_SIZE; i++) {
799 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900800 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900801 }
802 }
803}
804
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900805static void dump_info(void)
806{
807 if (info_threads)
808 dump_threads();
809 else if (info_map)
810 dump_map();
811 else
812 die("Unknown type of information\n");
813}
814
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200815static int process_sample_event(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200816 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300817 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300818 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200819 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200820{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200821 struct thread *thread = machine__findnew_thread(machine, sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200822
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200823 if (thread == NULL) {
824 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200825 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200826 return -1;
827 }
828
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300829 perf_evsel__process_lock_event(evsel, sample);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200830 return 0;
831}
832
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200833static struct perf_tool eops = {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100834 .sample = process_sample_event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200835 .comm = perf_event__process_comm,
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200836 .ordered_samples = true,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900837};
838
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900839static int read_events(void)
840{
Ian Munsie21ef97f2010-12-10 14:09:16 +1100841 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900842 if (!session)
843 die("Initializing perf session failed\n");
844
845 return perf_session__process_events(session, &eops);
846}
847
848static void sort_result(void)
849{
850 unsigned int i;
851 struct lock_stat *st;
852
853 for (i = 0; i < LOCKHASH_SIZE; i++) {
854 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
855 insert_to_result(st, compare);
856 }
857 }
858}
859
Ingo Molnar59f411b2010-01-31 08:27:58 +0100860static void __cmd_report(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900861{
862 setup_pager();
863 select_key();
864 read_events();
865 sort_result();
866 print_result();
867}
868
Ingo Molnar59f411b2010-01-31 08:27:58 +0100869static const char * const report_usage[] = {
870 "perf lock report [<options>]",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900871 NULL
872};
873
Ingo Molnar59f411b2010-01-31 08:27:58 +0100874static const struct option report_options[] = {
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900875 OPT_STRING('k', "key", &sort_key, "acquired",
Marcin Slusarz9826e832011-02-22 21:53:12 +0100876 "key for sorting (acquired / contended / wait_total / wait_max / wait_min)"),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900877 /* TODO: type */
878 OPT_END()
879};
880
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900881static const char * const info_usage[] = {
882 "perf lock info [<options>]",
883 NULL
884};
885
886static const struct option info_options[] = {
887 OPT_BOOLEAN('t', "threads", &info_threads,
888 "dump thread list in perf.data"),
889 OPT_BOOLEAN('m', "map", &info_map,
Namhyung Kimd1eec3e2012-01-29 17:55:56 +0900890 "map of lock instances (address:name table)"),
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900891 OPT_END()
892};
893
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900894static const char * const lock_usage[] = {
Namhyung Kimd1eec3e2012-01-29 17:55:56 +0900895 "perf lock [<options>] {record|report|script|info}",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900896 NULL
897};
898
899static const struct option lock_options[] = {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100900 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +1000901 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
Ingo Molnar59f411b2010-01-31 08:27:58 +0100902 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900903 OPT_END()
904};
905
906static const char *record_args[] = {
907 "record",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900908 "-R",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900909 "-f",
910 "-m", "1024",
911 "-c", "1",
Zhu Yanhaicf8dc9f2011-07-30 22:13:52 +0800912 "-e", "lock:lock_acquire",
913 "-e", "lock:lock_acquired",
914 "-e", "lock:lock_contended",
915 "-e", "lock:lock_release",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900916};
917
918static int __cmd_record(int argc, const char **argv)
919{
920 unsigned int rec_argc, i, j;
921 const char **rec_argv;
922
923 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
924 rec_argv = calloc(rec_argc + 1, sizeof(char *));
925
Chris Samuelce47dc52010-11-13 13:35:06 +1100926 if (rec_argv == NULL)
927 return -ENOMEM;
928
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900929 for (i = 0; i < ARRAY_SIZE(record_args); i++)
930 rec_argv[i] = strdup(record_args[i]);
931
932 for (j = 1; j < (unsigned int)argc; j++, i++)
933 rec_argv[i] = argv[j];
934
935 BUG_ON(i != rec_argc);
936
937 return cmd_record(i, rec_argv, NULL);
938}
939
940int cmd_lock(int argc, const char **argv, const char *prefix __used)
941{
942 unsigned int i;
943
944 symbol__init();
945 for (i = 0; i < LOCKHASH_SIZE; i++)
946 INIT_LIST_HEAD(lockhash_table + i);
947
948 argc = parse_options(argc, argv, lock_options, lock_usage,
949 PARSE_OPT_STOP_AT_NON_OPTION);
950 if (!argc)
951 usage_with_options(lock_usage, lock_options);
952
953 if (!strncmp(argv[0], "rec", 3)) {
954 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +0100955 } else if (!strncmp(argv[0], "report", 6)) {
956 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900957 if (argc) {
958 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100959 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900960 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +0100961 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900962 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100963 __cmd_report();
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100964 } else if (!strcmp(argv[0], "script")) {
965 /* Aliased to 'perf script' */
966 return cmd_script(argc, argv, prefix);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900967 } else if (!strcmp(argv[0], "info")) {
968 if (argc) {
969 argc = parse_options(argc, argv,
970 info_options, info_usage, 0);
971 if (argc)
972 usage_with_options(info_usage, info_options);
973 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100974 /* recycling report_lock_ops */
975 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900976 setup_pager();
977 read_events();
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900978 dump_info();
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900979 } else {
980 usage_with_options(lock_usage, lock_options);
981 }
982
983 return 0;
984}