blob: 6f5f328157aa623f34cbd1c112a3b218d5c0b9c1 [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;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090059 /* these times are in nano sec. */
Ingo Molnar59f411b2010-01-31 08:27:58 +010060 u64 wait_time_total;
61 u64 wait_time_min;
62 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090063
64 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090065};
66
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090067/*
68 * States of lock_seq_stat
69 *
70 * UNINITIALIZED is required for detecting first event of acquire.
71 * As the nature of lock events, there is no guarantee
72 * that the first event for the locks are acquire,
73 * it can be acquired, contended or release.
74 */
75#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
76#define SEQ_STATE_RELEASED 1
77#define SEQ_STATE_ACQUIRING 2
78#define SEQ_STATE_ACQUIRED 3
79#define SEQ_STATE_READ_ACQUIRED 4
80#define SEQ_STATE_CONTENDED 5
81
82/*
83 * MAX_LOCK_DEPTH
84 * Imported from include/linux/sched.h.
85 * Should this be synchronized?
86 */
87#define MAX_LOCK_DEPTH 48
88
89/*
90 * struct lock_seq_stat:
91 * Place to put on state of one lock sequence
92 * 1) acquire -> acquired -> release
93 * 2) acquire -> contended -> acquired -> release
94 * 3) acquire (with read or try) -> release
95 * 4) Are there other patterns?
96 */
97struct lock_seq_stat {
98 struct list_head list;
99 int state;
100 u64 prev_event_time;
101 void *addr;
102
103 int read_count;
104};
105
106struct thread_stat {
107 struct rb_node rb;
108
109 u32 tid;
110 struct list_head seq_list;
111};
112
113static struct rb_root thread_stats;
114
115static struct thread_stat *thread_stat_find(u32 tid)
116{
117 struct rb_node *node;
118 struct thread_stat *st;
119
120 node = thread_stats.rb_node;
121 while (node) {
122 st = container_of(node, struct thread_stat, rb);
123 if (st->tid == tid)
124 return st;
125 else if (tid < st->tid)
126 node = node->rb_left;
127 else
128 node = node->rb_right;
129 }
130
131 return NULL;
132}
133
134static void thread_stat_insert(struct thread_stat *new)
135{
136 struct rb_node **rb = &thread_stats.rb_node;
137 struct rb_node *parent = NULL;
138 struct thread_stat *p;
139
140 while (*rb) {
141 p = container_of(*rb, struct thread_stat, rb);
142 parent = *rb;
143
144 if (new->tid < p->tid)
145 rb = &(*rb)->rb_left;
146 else if (new->tid > p->tid)
147 rb = &(*rb)->rb_right;
148 else
149 BUG_ON("inserting invalid thread_stat\n");
150 }
151
152 rb_link_node(&new->rb, parent, rb);
153 rb_insert_color(&new->rb, &thread_stats);
154}
155
156static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
157{
158 struct thread_stat *st;
159
160 st = thread_stat_find(tid);
161 if (st)
162 return st;
163
164 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600165 if (!st) {
166 pr_err("memory allocation failed\n");
167 return NULL;
168 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900169
170 st->tid = tid;
171 INIT_LIST_HEAD(&st->seq_list);
172
173 thread_stat_insert(st);
174
175 return st;
176}
177
178static struct thread_stat *thread_stat_findnew_first(u32 tid);
179static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
180 thread_stat_findnew_first;
181
182static struct thread_stat *thread_stat_findnew_first(u32 tid)
183{
184 struct thread_stat *st;
185
186 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600187 if (!st) {
188 pr_err("memory allocation failed\n");
189 return NULL;
190 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900191 st->tid = tid;
192 INIT_LIST_HEAD(&st->seq_list);
193
194 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
195 rb_insert_color(&st->rb, &thread_stats);
196
197 thread_stat_findnew = thread_stat_findnew_after_first;
198 return st;
199}
200
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900201/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100202#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900203 static int lock_stat_key_ ## member(struct lock_stat *one, \
204 struct lock_stat *two) \
205 { \
206 return one->member > two->member; \
207 }
208
209SINGLE_KEY(nr_acquired)
210SINGLE_KEY(nr_contended)
211SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900212SINGLE_KEY(wait_time_max)
213
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100214static int lock_stat_key_wait_time_min(struct lock_stat *one,
215 struct lock_stat *two)
216{
217 u64 s1 = one->wait_time_min;
218 u64 s2 = two->wait_time_min;
219 if (s1 == ULLONG_MAX)
220 s1 = 0;
221 if (s2 == ULLONG_MAX)
222 s2 = 0;
223 return s1 > s2;
224}
225
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900226struct lock_key {
227 /*
228 * name: the value for specify by user
229 * this should be simpler than raw name of member
230 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
231 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100232 const char *name;
233 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900234};
235
Ingo Molnar59f411b2010-01-31 08:27:58 +0100236static const char *sort_key = "acquired";
237
238static int (*compare)(struct lock_stat *, struct lock_stat *);
239
240static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900241
242#define DEF_KEY_LOCK(name, fn_suffix) \
243 { #name, lock_stat_key_ ## fn_suffix }
244struct lock_key keys[] = {
245 DEF_KEY_LOCK(acquired, nr_acquired),
246 DEF_KEY_LOCK(contended, nr_contended),
247 DEF_KEY_LOCK(wait_total, wait_time_total),
248 DEF_KEY_LOCK(wait_min, wait_time_min),
249 DEF_KEY_LOCK(wait_max, wait_time_max),
250
251 /* extra comparisons much complicated should be here */
252
253 { NULL, NULL }
254};
255
David Ahern33d6aef2012-08-26 12:24:43 -0600256static int select_key(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900257{
258 int i;
259
260 for (i = 0; keys[i].name; i++) {
261 if (!strcmp(keys[i].name, sort_key)) {
262 compare = keys[i].key;
David Ahern33d6aef2012-08-26 12:24:43 -0600263 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900264 }
265 }
266
David Ahern33d6aef2012-08-26 12:24:43 -0600267 pr_err("Unknown compare key: %s\n", sort_key);
268
269 return -1;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900270}
271
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900272static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100273 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900274{
275 struct rb_node **rb = &result.rb_node;
276 struct rb_node *parent = NULL;
277 struct lock_stat *p;
278
279 while (*rb) {
280 p = container_of(*rb, struct lock_stat, rb);
281 parent = *rb;
282
283 if (bigger(st, p))
284 rb = &(*rb)->rb_left;
285 else
286 rb = &(*rb)->rb_right;
287 }
288
289 rb_link_node(&st->rb, parent, rb);
290 rb_insert_color(&st->rb, &result);
291}
292
293/* returns left most element of result, and erase it */
294static struct lock_stat *pop_from_result(void)
295{
296 struct rb_node *node = result.rb_node;
297
298 if (!node)
299 return NULL;
300
301 while (node->rb_left)
302 node = node->rb_left;
303
304 rb_erase(node, &result);
305 return container_of(node, struct lock_stat, rb);
306}
307
Ingo Molnar59f411b2010-01-31 08:27:58 +0100308static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900309{
310 struct list_head *entry = lockhashentry(addr);
311 struct lock_stat *ret, *new;
312
313 list_for_each_entry(ret, entry, hash_entry) {
314 if (ret->addr == addr)
315 return ret;
316 }
317
318 new = zalloc(sizeof(struct lock_stat));
319 if (!new)
320 goto alloc_failed;
321
322 new->addr = addr;
323 new->name = zalloc(sizeof(char) * strlen(name) + 1);
324 if (!new->name)
325 goto alloc_failed;
326 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900327
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900328 new->wait_time_min = ULLONG_MAX;
329
330 list_add(&new->hash_entry, entry);
331 return new;
332
333alloc_failed:
David Ahern33d6aef2012-08-26 12:24:43 -0600334 pr_err("memory allocation failed\n");
335 return NULL;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900336}
337
Robert Richterefad1412011-12-07 10:02:54 +0100338static const char *input_name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900339
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900340struct trace_lock_handler {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300341 int (*acquire_event)(struct perf_evsel *evsel,
342 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900343
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300344 int (*acquired_event)(struct perf_evsel *evsel,
345 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900346
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300347 int (*contended_event)(struct perf_evsel *evsel,
348 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900349
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300350 int (*release_event)(struct perf_evsel *evsel,
351 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900352};
353
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900354static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
355{
356 struct lock_seq_stat *seq;
357
358 list_for_each_entry(seq, &ts->seq_list, list) {
359 if (seq->addr == addr)
360 return seq;
361 }
362
363 seq = zalloc(sizeof(struct lock_seq_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600364 if (!seq) {
365 pr_err("memory allocation failed\n");
366 return NULL;
367 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900368 seq->state = SEQ_STATE_UNINITIALIZED;
369 seq->addr = addr;
370
371 list_add(&seq->list, &ts->seq_list);
372 return seq;
373}
374
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200375enum broken_state {
376 BROKEN_ACQUIRE,
377 BROKEN_ACQUIRED,
378 BROKEN_CONTENDED,
379 BROKEN_RELEASE,
380 BROKEN_MAX,
381};
382
383static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900384
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200385enum acquire_flags {
386 TRY_LOCK = 1,
387 READ_LOCK = 2,
388};
389
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300390static int report_lock_acquire_event(struct perf_evsel *evsel,
391 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900392{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300393 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900394 struct lock_stat *ls;
395 struct thread_stat *ts;
396 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300397 const char *name = perf_evsel__strval(evsel, sample, "name");
398 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
399 int flag = perf_evsel__intval(evsel, sample, "flag");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900400
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300401 memcpy(&addr, &tmp, sizeof(void *));
402
403 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600404 if (!ls)
405 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900406 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600407 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900408
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300409 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600410 if (!ts)
411 return -1;
412
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300413 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600414 if (!seq)
415 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900416
417 switch (seq->state) {
418 case SEQ_STATE_UNINITIALIZED:
419 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300420 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900421 seq->state = SEQ_STATE_ACQUIRING;
422 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300423 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900424 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300425 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900426 ls->nr_readlock++;
427 seq->state = SEQ_STATE_READ_ACQUIRED;
428 seq->read_count = 1;
429 ls->nr_acquired++;
430 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900431 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900432 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300433 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900434 seq->read_count++;
435 ls->nr_acquired++;
436 goto end;
437 } else {
438 goto broken;
439 }
440 break;
441 case SEQ_STATE_ACQUIRED:
442 case SEQ_STATE_ACQUIRING:
443 case SEQ_STATE_CONTENDED:
444broken:
445 /* broken lock sequence, discard it */
446 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200447 bad_hist[BROKEN_ACQUIRE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900448 list_del(&seq->list);
449 free(seq);
450 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900451 break;
452 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900453 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900454 break;
455 }
456
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900457 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300458 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900459end:
David Ahern33d6aef2012-08-26 12:24:43 -0600460 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900461}
462
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300463static int report_lock_acquired_event(struct perf_evsel *evsel,
464 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900465{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300466 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900467 struct lock_stat *ls;
468 struct thread_stat *ts;
469 struct lock_seq_stat *seq;
470 u64 contended_term;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300471 const char *name = perf_evsel__strval(evsel, sample, "name");
472 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900473
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300474 memcpy(&addr, &tmp, sizeof(void *));
475
476 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600477 if (!ls)
478 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900479 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600480 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900481
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300482 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600483 if (!ts)
484 return -1;
485
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300486 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600487 if (!seq)
488 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900489
490 switch (seq->state) {
491 case SEQ_STATE_UNINITIALIZED:
492 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600493 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900494 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900495 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900496 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300497 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900498 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900499 if (contended_term < ls->wait_time_min)
500 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200501 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900502 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900503 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900504 case SEQ_STATE_RELEASED:
505 case SEQ_STATE_ACQUIRED:
506 case SEQ_STATE_READ_ACQUIRED:
507 /* broken lock sequence, discard it */
508 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200509 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900510 list_del(&seq->list);
511 free(seq);
512 goto end;
513 break;
514
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900515 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900516 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900517 break;
518 }
519
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900520 seq->state = SEQ_STATE_ACQUIRED;
521 ls->nr_acquired++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300522 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900523end:
David Ahern33d6aef2012-08-26 12:24:43 -0600524 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900525}
526
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300527static int report_lock_contended_event(struct perf_evsel *evsel,
528 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900529{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300530 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900531 struct lock_stat *ls;
532 struct thread_stat *ts;
533 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300534 const char *name = perf_evsel__strval(evsel, sample, "name");
535 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900536
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300537 memcpy(&addr, &tmp, sizeof(void *));
538
539 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600540 if (!ls)
541 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900542 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600543 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900544
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300545 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600546 if (!ts)
547 return -1;
548
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300549 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600550 if (!seq)
551 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900552
553 switch (seq->state) {
554 case SEQ_STATE_UNINITIALIZED:
555 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600556 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900557 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900558 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900559 case SEQ_STATE_RELEASED:
560 case SEQ_STATE_ACQUIRED:
561 case SEQ_STATE_READ_ACQUIRED:
562 case SEQ_STATE_CONTENDED:
563 /* broken lock sequence, discard it */
564 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200565 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900566 list_del(&seq->list);
567 free(seq);
568 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900569 break;
570 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900571 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900572 break;
573 }
574
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900575 seq->state = SEQ_STATE_CONTENDED;
576 ls->nr_contended++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300577 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900578end:
David Ahern33d6aef2012-08-26 12:24:43 -0600579 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900580}
581
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300582static int report_lock_release_event(struct perf_evsel *evsel,
583 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900584{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300585 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900586 struct lock_stat *ls;
587 struct thread_stat *ts;
588 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300589 const char *name = perf_evsel__strval(evsel, sample, "name");
590 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900591
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300592 memcpy(&addr, &tmp, sizeof(void *));
593
594 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600595 if (!ls)
596 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900597 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600598 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900599
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300600 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600601 if (!ts)
602 return -1;
603
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300604 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600605 if (!seq)
606 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900607
608 switch (seq->state) {
609 case SEQ_STATE_UNINITIALIZED:
610 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900611 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900612 case SEQ_STATE_ACQUIRED:
613 break;
614 case SEQ_STATE_READ_ACQUIRED:
615 seq->read_count--;
616 BUG_ON(seq->read_count < 0);
617 if (!seq->read_count) {
618 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900619 goto end;
620 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900621 break;
622 case SEQ_STATE_ACQUIRING:
623 case SEQ_STATE_CONTENDED:
624 case SEQ_STATE_RELEASED:
625 /* broken lock sequence, discard it */
626 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200627 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900628 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900629 break;
630 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);
695 pr_info("bad rate: %f %%\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
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900712 pr_info("%15s ", "total wait (ns)");
713 pr_info("%15s ", "max wait (ns)");
714 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900715
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900716 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900717
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900718 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900719 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900720 total++;
721 if (st->discard) {
722 bad++;
723 continue;
724 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900725 bzero(cut_name, 20);
726
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900727 if (strlen(st->name) < 16) {
728 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900729 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900730 } else {
731 strncpy(cut_name, st->name, 16);
732 cut_name[16] = '.';
733 cut_name[17] = '.';
734 cut_name[18] = '.';
735 cut_name[19] = '\0';
736 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900737 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900738 }
739
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900740 pr_info("%10u ", st->nr_acquired);
741 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900742
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200743 pr_info("%15" PRIu64 " ", st->wait_time_total);
744 pr_info("%15" PRIu64 " ", st->wait_time_max);
745 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900746 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900747 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900748 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900749
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200750 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900751}
752
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300753static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900754
755static void dump_threads(void)
756{
757 struct thread_stat *st;
758 struct rb_node *node;
759 struct thread *t;
760
761 pr_info("%10s: comm\n", "Thread ID");
762
763 node = rb_first(&thread_stats);
764 while (node) {
765 st = container_of(node, struct thread_stat, rb);
766 t = perf_session__findnew(session, st->tid);
767 pr_info("%10d: %s\n", st->tid, t->comm);
768 node = rb_next(node);
769 };
770}
771
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900772static void dump_map(void)
773{
774 unsigned int i;
775 struct lock_stat *st;
776
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900777 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900778 for (i = 0; i < LOCKHASH_SIZE; i++) {
779 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900780 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900781 }
782 }
783}
784
David Ahern33d6aef2012-08-26 12:24:43 -0600785static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900786{
David Ahern33d6aef2012-08-26 12:24:43 -0600787 int rc = 0;
788
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900789 if (info_threads)
790 dump_threads();
791 else if (info_map)
792 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600793 else {
794 rc = -1;
795 pr_err("Unknown type of information\n");
796 }
797
798 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900799}
800
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300801typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
802 struct perf_sample *sample);
803
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300804static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200805 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300806 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300807 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200808 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200809{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200810 struct thread *thread = machine__findnew_thread(machine, sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200811
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200812 if (thread == NULL) {
813 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200814 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200815 return -1;
816 }
817
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300818 if (evsel->handler.func != NULL) {
819 tracepoint_handler f = evsel->handler.func;
820 return f(evsel, sample);
821 }
822
823 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200824}
825
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300826static const struct perf_evsel_str_handler lock_tracepoints[] = {
827 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
828 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
829 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
830 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
831};
832
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900833static int read_events(void)
834{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300835 struct perf_tool eops = {
836 .sample = process_sample_event,
837 .comm = perf_event__process_comm,
838 .ordered_samples = true,
839 };
Ian Munsie21ef97f2010-12-10 14:09:16 +1100840 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
David Ahern33d6aef2012-08-26 12:24:43 -0600841 if (!session) {
842 pr_err("Initializing perf session failed\n");
843 return -1;
844 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900845
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300846 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
847 pr_err("Initializing perf session tracepoint handlers failed\n");
848 return -1;
849 }
850
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900851 return perf_session__process_events(session, &eops);
852}
853
854static void sort_result(void)
855{
856 unsigned int i;
857 struct lock_stat *st;
858
859 for (i = 0; i < LOCKHASH_SIZE; i++) {
860 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
861 insert_to_result(st, compare);
862 }
863 }
864}
865
David Ahern33d6aef2012-08-26 12:24:43 -0600866static int __cmd_report(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900867{
868 setup_pager();
David Ahern33d6aef2012-08-26 12:24:43 -0600869
870 if ((select_key() != 0) ||
871 (read_events() != 0))
872 return -1;
873
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900874 sort_result();
875 print_result();
David Ahern33d6aef2012-08-26 12:24:43 -0600876
877 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900878}
879
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900880static int __cmd_record(int argc, const char **argv)
881{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300882 const char *record_args[] = {
883 "record", "-R", "-f", "-m", "1024", "-c", "1",
884 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900885 unsigned int rec_argc, i, j;
886 const char **rec_argv;
887
David Ahernd25dcba2012-08-09 10:35:37 -0600888 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300889 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600890 pr_err("tracepoint %s is not enabled. "
891 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300892 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600893 return 1;
894 }
895 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900896
David Ahernd25dcba2012-08-09 10:35:37 -0600897 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
898 /* factor of 2 is for -e in front of each tracepoint */
899 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
900
901 rec_argv = calloc(rec_argc + 1, sizeof(char *));
Chris Samuelce47dc52010-11-13 13:35:06 +1100902 if (rec_argv == NULL)
903 return -ENOMEM;
904
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900905 for (i = 0; i < ARRAY_SIZE(record_args); i++)
906 rec_argv[i] = strdup(record_args[i]);
907
David Ahernd25dcba2012-08-09 10:35:37 -0600908 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
909 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300910 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600911 }
912
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900913 for (j = 1; j < (unsigned int)argc; j++, i++)
914 rec_argv[i] = argv[j];
915
916 BUG_ON(i != rec_argc);
917
918 return cmd_record(i, rec_argv, NULL);
919}
920
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300921int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900922{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300923 const struct option info_options[] = {
924 OPT_BOOLEAN('t', "threads", &info_threads,
925 "dump thread list in perf.data"),
926 OPT_BOOLEAN('m', "map", &info_map,
927 "map of lock instances (address:name table)"),
928 OPT_END()
929 };
930 const struct option lock_options[] = {
931 OPT_STRING('i', "input", &input_name, "file", "input file name"),
932 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
933 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
934 OPT_END()
935 };
936 const struct option report_options[] = {
937 OPT_STRING('k', "key", &sort_key, "acquired",
938 "key for sorting (acquired / contended / wait_total / wait_max / wait_min)"),
939 /* TODO: type */
940 OPT_END()
941 };
942 const char * const info_usage[] = {
943 "perf lock info [<options>]",
944 NULL
945 };
946 const char * const lock_usage[] = {
947 "perf lock [<options>] {record|report|script|info}",
948 NULL
949 };
950 const char * const report_usage[] = {
951 "perf lock report [<options>]",
952 NULL
953 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900954 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600955 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900956
957 symbol__init();
958 for (i = 0; i < LOCKHASH_SIZE; i++)
959 INIT_LIST_HEAD(lockhash_table + i);
960
961 argc = parse_options(argc, argv, lock_options, lock_usage,
962 PARSE_OPT_STOP_AT_NON_OPTION);
963 if (!argc)
964 usage_with_options(lock_usage, lock_options);
965
966 if (!strncmp(argv[0], "rec", 3)) {
967 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +0100968 } else if (!strncmp(argv[0], "report", 6)) {
969 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900970 if (argc) {
971 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100972 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900973 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +0100974 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900975 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100976 __cmd_report();
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100977 } else if (!strcmp(argv[0], "script")) {
978 /* Aliased to 'perf script' */
979 return cmd_script(argc, argv, prefix);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900980 } else if (!strcmp(argv[0], "info")) {
981 if (argc) {
982 argc = parse_options(argc, argv,
983 info_options, info_usage, 0);
984 if (argc)
985 usage_with_options(info_usage, info_options);
986 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100987 /* recycling report_lock_ops */
988 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900989 setup_pager();
David Ahern33d6aef2012-08-26 12:24:43 -0600990 if (read_events() != 0)
991 rc = -1;
992 else
993 rc = dump_info();
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900994 } else {
995 usage_with_options(lock_usage, lock_options);
996 }
997
David Ahern33d6aef2012-08-26 12:24:43 -0600998 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900999}