blob: 76543a4a7a306fc3aa60c44f5ce33383e9fba081 [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
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900338struct trace_lock_handler {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300339 int (*acquire_event)(struct perf_evsel *evsel,
340 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900341
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300342 int (*acquired_event)(struct perf_evsel *evsel,
343 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900344
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300345 int (*contended_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 (*release_event)(struct perf_evsel *evsel,
349 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900350};
351
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900352static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
353{
354 struct lock_seq_stat *seq;
355
356 list_for_each_entry(seq, &ts->seq_list, list) {
357 if (seq->addr == addr)
358 return seq;
359 }
360
361 seq = zalloc(sizeof(struct lock_seq_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600362 if (!seq) {
363 pr_err("memory allocation failed\n");
364 return NULL;
365 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900366 seq->state = SEQ_STATE_UNINITIALIZED;
367 seq->addr = addr;
368
369 list_add(&seq->list, &ts->seq_list);
370 return seq;
371}
372
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200373enum broken_state {
374 BROKEN_ACQUIRE,
375 BROKEN_ACQUIRED,
376 BROKEN_CONTENDED,
377 BROKEN_RELEASE,
378 BROKEN_MAX,
379};
380
381static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900382
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200383enum acquire_flags {
384 TRY_LOCK = 1,
385 READ_LOCK = 2,
386};
387
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300388static int report_lock_acquire_event(struct perf_evsel *evsel,
389 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900390{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300391 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900392 struct lock_stat *ls;
393 struct thread_stat *ts;
394 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300395 const char *name = perf_evsel__strval(evsel, sample, "name");
396 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
397 int flag = perf_evsel__intval(evsel, sample, "flag");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900398
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300399 memcpy(&addr, &tmp, sizeof(void *));
400
401 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600402 if (!ls)
403 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900404 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600405 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900406
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300407 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600408 if (!ts)
409 return -1;
410
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300411 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600412 if (!seq)
413 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900414
415 switch (seq->state) {
416 case SEQ_STATE_UNINITIALIZED:
417 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300418 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900419 seq->state = SEQ_STATE_ACQUIRING;
420 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300421 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900422 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300423 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900424 ls->nr_readlock++;
425 seq->state = SEQ_STATE_READ_ACQUIRED;
426 seq->read_count = 1;
427 ls->nr_acquired++;
428 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900429 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900430 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300431 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900432 seq->read_count++;
433 ls->nr_acquired++;
434 goto end;
435 } else {
436 goto broken;
437 }
438 break;
439 case SEQ_STATE_ACQUIRED:
440 case SEQ_STATE_ACQUIRING:
441 case SEQ_STATE_CONTENDED:
442broken:
443 /* broken lock sequence, discard it */
444 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200445 bad_hist[BROKEN_ACQUIRE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900446 list_del(&seq->list);
447 free(seq);
448 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900449 break;
450 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900451 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900452 break;
453 }
454
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900455 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300456 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900457end:
David Ahern33d6aef2012-08-26 12:24:43 -0600458 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900459}
460
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300461static int report_lock_acquired_event(struct perf_evsel *evsel,
462 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900463{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300464 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900465 struct lock_stat *ls;
466 struct thread_stat *ts;
467 struct lock_seq_stat *seq;
468 u64 contended_term;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300469 const char *name = perf_evsel__strval(evsel, sample, "name");
470 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900471
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300472 memcpy(&addr, &tmp, sizeof(void *));
473
474 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600475 if (!ls)
476 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900477 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600478 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900479
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300480 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600481 if (!ts)
482 return -1;
483
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300484 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600485 if (!seq)
486 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900487
488 switch (seq->state) {
489 case SEQ_STATE_UNINITIALIZED:
490 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600491 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900492 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900493 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900494 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300495 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900496 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900497 if (contended_term < ls->wait_time_min)
498 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200499 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900500 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900501 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900502 case SEQ_STATE_RELEASED:
503 case SEQ_STATE_ACQUIRED:
504 case SEQ_STATE_READ_ACQUIRED:
505 /* broken lock sequence, discard it */
506 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200507 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900508 list_del(&seq->list);
509 free(seq);
510 goto end;
511 break;
512
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900513 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900514 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900515 break;
516 }
517
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900518 seq->state = SEQ_STATE_ACQUIRED;
519 ls->nr_acquired++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300520 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900521end:
David Ahern33d6aef2012-08-26 12:24:43 -0600522 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900523}
524
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300525static int report_lock_contended_event(struct perf_evsel *evsel,
526 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900527{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300528 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900529 struct lock_stat *ls;
530 struct thread_stat *ts;
531 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300532 const char *name = perf_evsel__strval(evsel, sample, "name");
533 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900534
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300535 memcpy(&addr, &tmp, sizeof(void *));
536
537 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600538 if (!ls)
539 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900540 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600541 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900542
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300543 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600544 if (!ts)
545 return -1;
546
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300547 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600548 if (!seq)
549 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900550
551 switch (seq->state) {
552 case SEQ_STATE_UNINITIALIZED:
553 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600554 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900555 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900556 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900557 case SEQ_STATE_RELEASED:
558 case SEQ_STATE_ACQUIRED:
559 case SEQ_STATE_READ_ACQUIRED:
560 case SEQ_STATE_CONTENDED:
561 /* broken lock sequence, discard it */
562 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200563 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900564 list_del(&seq->list);
565 free(seq);
566 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900567 break;
568 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900569 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900570 break;
571 }
572
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900573 seq->state = SEQ_STATE_CONTENDED;
574 ls->nr_contended++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300575 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900576end:
David Ahern33d6aef2012-08-26 12:24:43 -0600577 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900578}
579
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300580static int report_lock_release_event(struct perf_evsel *evsel,
581 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900582{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300583 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900584 struct lock_stat *ls;
585 struct thread_stat *ts;
586 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300587 const char *name = perf_evsel__strval(evsel, sample, "name");
588 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900589
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300590 memcpy(&addr, &tmp, sizeof(void *));
591
592 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600593 if (!ls)
594 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900595 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600596 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900597
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300598 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600599 if (!ts)
600 return -1;
601
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300602 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600603 if (!seq)
604 return -1;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900605
606 switch (seq->state) {
607 case SEQ_STATE_UNINITIALIZED:
608 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900609 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900610 case SEQ_STATE_ACQUIRED:
611 break;
612 case SEQ_STATE_READ_ACQUIRED:
613 seq->read_count--;
614 BUG_ON(seq->read_count < 0);
615 if (!seq->read_count) {
616 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900617 goto end;
618 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900619 break;
620 case SEQ_STATE_ACQUIRING:
621 case SEQ_STATE_CONTENDED:
622 case SEQ_STATE_RELEASED:
623 /* broken lock sequence, discard it */
624 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200625 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900626 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900627 break;
628 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900629 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900630 break;
631 }
632
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900633 ls->nr_release++;
634free_seq:
635 list_del(&seq->list);
636 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900637end:
David Ahern33d6aef2012-08-26 12:24:43 -0600638 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900639}
640
641/* lock oriented handlers */
642/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100643static struct trace_lock_handler report_lock_ops = {
644 .acquire_event = report_lock_acquire_event,
645 .acquired_event = report_lock_acquired_event,
646 .contended_event = report_lock_contended_event,
647 .release_event = report_lock_release_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900648};
649
650static struct trace_lock_handler *trace_handler;
651
David Ahern33d6aef2012-08-26 12:24:43 -0600652static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300653 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900654{
Ingo Molnar59f411b2010-01-31 08:27:58 +0100655 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300656 return trace_handler->acquire_event(evsel, sample);
657 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900658}
659
David Ahern33d6aef2012-08-26 12:24:43 -0600660static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300661 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900662{
David Ahern33d6aef2012-08-26 12:24:43 -0600663 if (trace_handler->acquired_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300664 return trace_handler->acquired_event(evsel, sample);
665 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900666}
667
David Ahern33d6aef2012-08-26 12:24:43 -0600668static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300669 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900670{
David Ahern33d6aef2012-08-26 12:24:43 -0600671 if (trace_handler->contended_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300672 return trace_handler->contended_event(evsel, sample);
673 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900674}
675
David Ahern33d6aef2012-08-26 12:24:43 -0600676static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300677 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900678{
David Ahern33d6aef2012-08-26 12:24:43 -0600679 if (trace_handler->release_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300680 return trace_handler->release_event(evsel, sample);
681 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900682}
683
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200684static void print_bad_events(int bad, int total)
685{
686 /* Output for debug, this have to be removed */
687 int i;
688 const char *name[4] =
689 { "acquire", "acquired", "contended", "release" };
690
691 pr_info("\n=== output for debug===\n\n");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200692 pr_info("bad: %d, total: %d\n", bad, total);
693 pr_info("bad rate: %f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200694 pr_info("histogram of events caused bad sequence\n");
695 for (i = 0; i < BROKEN_MAX; i++)
696 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
697}
698
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900699/* TODO: various way to print, coloring, nano or milli sec */
700static void print_result(void)
701{
702 struct lock_stat *st;
703 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900704 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900705
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900706 pr_info("%20s ", "Name");
707 pr_info("%10s ", "acquired");
708 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900709
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900710 pr_info("%15s ", "total wait (ns)");
711 pr_info("%15s ", "max wait (ns)");
712 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900713
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900714 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900715
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900716 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900717 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900718 total++;
719 if (st->discard) {
720 bad++;
721 continue;
722 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900723 bzero(cut_name, 20);
724
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900725 if (strlen(st->name) < 16) {
726 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900727 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900728 } else {
729 strncpy(cut_name, st->name, 16);
730 cut_name[16] = '.';
731 cut_name[17] = '.';
732 cut_name[18] = '.';
733 cut_name[19] = '\0';
734 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900735 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900736 }
737
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900738 pr_info("%10u ", st->nr_acquired);
739 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900740
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200741 pr_info("%15" PRIu64 " ", st->wait_time_total);
742 pr_info("%15" PRIu64 " ", st->wait_time_max);
743 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900744 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900745 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900746 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900747
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200748 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900749}
750
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300751static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900752
753static void dump_threads(void)
754{
755 struct thread_stat *st;
756 struct rb_node *node;
757 struct thread *t;
758
759 pr_info("%10s: comm\n", "Thread ID");
760
761 node = rb_first(&thread_stats);
762 while (node) {
763 st = container_of(node, struct thread_stat, rb);
764 t = perf_session__findnew(session, st->tid);
765 pr_info("%10d: %s\n", st->tid, t->comm);
766 node = rb_next(node);
767 };
768}
769
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900770static void dump_map(void)
771{
772 unsigned int i;
773 struct lock_stat *st;
774
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900775 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900776 for (i = 0; i < LOCKHASH_SIZE; i++) {
777 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900778 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900779 }
780 }
781}
782
David Ahern33d6aef2012-08-26 12:24:43 -0600783static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900784{
David Ahern33d6aef2012-08-26 12:24:43 -0600785 int rc = 0;
786
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900787 if (info_threads)
788 dump_threads();
789 else if (info_map)
790 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600791 else {
792 rc = -1;
793 pr_err("Unknown type of information\n");
794 }
795
796 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900797}
798
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300799typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
800 struct perf_sample *sample);
801
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300802static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200803 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300804 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300805 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200806 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200807{
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200808 struct thread *thread = machine__findnew_thread(machine, sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200809
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200810 if (thread == NULL) {
811 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200812 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200813 return -1;
814 }
815
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300816 if (evsel->handler.func != NULL) {
817 tracepoint_handler f = evsel->handler.func;
818 return f(evsel, sample);
819 }
820
821 return 0;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200822}
823
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300824static const struct perf_evsel_str_handler lock_tracepoints[] = {
825 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
826 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
827 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
828 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
829};
830
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900831static int read_events(void)
832{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300833 struct perf_tool eops = {
834 .sample = process_sample_event,
835 .comm = perf_event__process_comm,
836 .ordered_samples = true,
837 };
Ian Munsie21ef97f2010-12-10 14:09:16 +1100838 session = perf_session__new(input_name, O_RDONLY, 0, false, &eops);
David Ahern33d6aef2012-08-26 12:24:43 -0600839 if (!session) {
840 pr_err("Initializing perf session failed\n");
841 return -1;
842 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900843
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300844 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
845 pr_err("Initializing perf session tracepoint handlers failed\n");
846 return -1;
847 }
848
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900849 return perf_session__process_events(session, &eops);
850}
851
852static void sort_result(void)
853{
854 unsigned int i;
855 struct lock_stat *st;
856
857 for (i = 0; i < LOCKHASH_SIZE; i++) {
858 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
859 insert_to_result(st, compare);
860 }
861 }
862}
863
David Ahern33d6aef2012-08-26 12:24:43 -0600864static int __cmd_report(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900865{
866 setup_pager();
David Ahern33d6aef2012-08-26 12:24:43 -0600867
868 if ((select_key() != 0) ||
869 (read_events() != 0))
870 return -1;
871
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900872 sort_result();
873 print_result();
David Ahern33d6aef2012-08-26 12:24:43 -0600874
875 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900876}
877
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900878static int __cmd_record(int argc, const char **argv)
879{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300880 const char *record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200881 "record", "-R", "-m", "1024", "-c", "1",
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300882 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900883 unsigned int rec_argc, i, j;
884 const char **rec_argv;
885
David Ahernd25dcba2012-08-09 10:35:37 -0600886 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300887 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600888 pr_err("tracepoint %s is not enabled. "
889 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300890 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600891 return 1;
892 }
893 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900894
David Ahernd25dcba2012-08-09 10:35:37 -0600895 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
896 /* factor of 2 is for -e in front of each tracepoint */
897 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
898
899 rec_argv = calloc(rec_argc + 1, sizeof(char *));
Chris Samuelce47dc52010-11-13 13:35:06 +1100900 if (rec_argv == NULL)
901 return -ENOMEM;
902
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900903 for (i = 0; i < ARRAY_SIZE(record_args); i++)
904 rec_argv[i] = strdup(record_args[i]);
905
David Ahernd25dcba2012-08-09 10:35:37 -0600906 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
907 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300908 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600909 }
910
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900911 for (j = 1; j < (unsigned int)argc; j++, i++)
912 rec_argv[i] = argv[j];
913
914 BUG_ON(i != rec_argc);
915
916 return cmd_record(i, rec_argv, NULL);
917}
918
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300919int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900920{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300921 const struct option info_options[] = {
922 OPT_BOOLEAN('t', "threads", &info_threads,
923 "dump thread list in perf.data"),
924 OPT_BOOLEAN('m', "map", &info_map,
925 "map of lock instances (address:name table)"),
926 OPT_END()
927 };
928 const struct option lock_options[] = {
929 OPT_STRING('i', "input", &input_name, "file", "input file name"),
930 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
931 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
932 OPT_END()
933 };
934 const struct option report_options[] = {
935 OPT_STRING('k', "key", &sort_key, "acquired",
936 "key for sorting (acquired / contended / wait_total / wait_max / wait_min)"),
937 /* TODO: type */
938 OPT_END()
939 };
940 const char * const info_usage[] = {
941 "perf lock info [<options>]",
942 NULL
943 };
944 const char * const lock_usage[] = {
945 "perf lock [<options>] {record|report|script|info}",
946 NULL
947 };
948 const char * const report_usage[] = {
949 "perf lock report [<options>]",
950 NULL
951 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900952 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600953 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900954
955 symbol__init();
956 for (i = 0; i < LOCKHASH_SIZE; i++)
957 INIT_LIST_HEAD(lockhash_table + i);
958
959 argc = parse_options(argc, argv, lock_options, lock_usage,
960 PARSE_OPT_STOP_AT_NON_OPTION);
961 if (!argc)
962 usage_with_options(lock_usage, lock_options);
963
964 if (!strncmp(argv[0], "rec", 3)) {
965 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +0100966 } else if (!strncmp(argv[0], "report", 6)) {
967 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900968 if (argc) {
969 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100970 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900971 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +0100972 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900973 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100974 __cmd_report();
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100975 } else if (!strcmp(argv[0], "script")) {
976 /* Aliased to 'perf script' */
977 return cmd_script(argc, argv, prefix);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900978 } else if (!strcmp(argv[0], "info")) {
979 if (argc) {
980 argc = parse_options(argc, argv,
981 info_options, info_usage, 0);
982 if (argc)
983 usage_with_options(info_usage, info_options);
984 }
Ingo Molnar59f411b2010-01-31 08:27:58 +0100985 /* recycling report_lock_ops */
986 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900987 setup_pager();
David Ahern33d6aef2012-08-26 12:24:43 -0600988 if (read_events() != 0)
989 rc = -1;
990 else
991 rc = dump_info();
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900992 } else {
993 usage_with_options(lock_usage, lock_options);
994 }
995
David Ahern33d6aef2012-08-26 12:24:43 -0600996 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900997}