blob: 716d8c544a56a75fdece457e90191f09913e9ec6 [file] [log] [blame]
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001#include "builtin.h"
2#include "perf.h"
3
4#include "util/util.h"
5#include "util/cache.h"
6#include "util/symbol.h"
7#include "util/thread.h"
8#include "util/header.h"
9
10#include "util/parse-options.h"
11#include "util/trace-event.h"
12
13#include "util/debug.h"
14#include "util/session.h"
15
16#include <sys/types.h>
17#include <sys/prctl.h>
18#include <semaphore.h>
19#include <pthread.h>
20#include <math.h>
21#include <limits.h>
22
23#include <linux/list.h>
24#include <linux/hash.h>
25
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090026static struct perf_session *session;
27
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090028/* based on kernel/lockdep.c */
29#define LOCKHASH_BITS 12
30#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
31
32static struct list_head lockhash_table[LOCKHASH_SIZE];
33
34#define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
35#define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
36
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090037struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010038 struct list_head hash_entry;
39 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090040
Ingo Molnar59f411b2010-01-31 08:27:58 +010041 /*
42 * FIXME: raw_field_value() returns unsigned long long,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090043 * so address of lockdep_map should be dealed as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010044 * Is there more better solution?
45 */
46 void *addr; /* address of lockdep_map, used as ID */
47 char *name; /* for strcpy(), we cannot use const */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090048
Ingo Molnar59f411b2010-01-31 08:27:58 +010049 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090050 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010051 unsigned int nr_contended;
52 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090053
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090054 unsigned int nr_readlock;
55 unsigned int nr_trylock;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090056 /* these times are in nano sec. */
Ingo Molnar59f411b2010-01-31 08:27:58 +010057 u64 wait_time_total;
58 u64 wait_time_min;
59 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090060
61 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090062};
63
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090064/*
65 * States of lock_seq_stat
66 *
67 * UNINITIALIZED is required for detecting first event of acquire.
68 * As the nature of lock events, there is no guarantee
69 * that the first event for the locks are acquire,
70 * it can be acquired, contended or release.
71 */
72#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
73#define SEQ_STATE_RELEASED 1
74#define SEQ_STATE_ACQUIRING 2
75#define SEQ_STATE_ACQUIRED 3
76#define SEQ_STATE_READ_ACQUIRED 4
77#define SEQ_STATE_CONTENDED 5
78
79/*
80 * MAX_LOCK_DEPTH
81 * Imported from include/linux/sched.h.
82 * Should this be synchronized?
83 */
84#define MAX_LOCK_DEPTH 48
85
86/*
87 * struct lock_seq_stat:
88 * Place to put on state of one lock sequence
89 * 1) acquire -> acquired -> release
90 * 2) acquire -> contended -> acquired -> release
91 * 3) acquire (with read or try) -> release
92 * 4) Are there other patterns?
93 */
94struct lock_seq_stat {
95 struct list_head list;
96 int state;
97 u64 prev_event_time;
98 void *addr;
99
100 int read_count;
101};
102
103struct thread_stat {
104 struct rb_node rb;
105
106 u32 tid;
107 struct list_head seq_list;
108};
109
110static struct rb_root thread_stats;
111
112static struct thread_stat *thread_stat_find(u32 tid)
113{
114 struct rb_node *node;
115 struct thread_stat *st;
116
117 node = thread_stats.rb_node;
118 while (node) {
119 st = container_of(node, struct thread_stat, rb);
120 if (st->tid == tid)
121 return st;
122 else if (tid < st->tid)
123 node = node->rb_left;
124 else
125 node = node->rb_right;
126 }
127
128 return NULL;
129}
130
131static void thread_stat_insert(struct thread_stat *new)
132{
133 struct rb_node **rb = &thread_stats.rb_node;
134 struct rb_node *parent = NULL;
135 struct thread_stat *p;
136
137 while (*rb) {
138 p = container_of(*rb, struct thread_stat, rb);
139 parent = *rb;
140
141 if (new->tid < p->tid)
142 rb = &(*rb)->rb_left;
143 else if (new->tid > p->tid)
144 rb = &(*rb)->rb_right;
145 else
146 BUG_ON("inserting invalid thread_stat\n");
147 }
148
149 rb_link_node(&new->rb, parent, rb);
150 rb_insert_color(&new->rb, &thread_stats);
151}
152
153static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
154{
155 struct thread_stat *st;
156
157 st = thread_stat_find(tid);
158 if (st)
159 return st;
160
161 st = zalloc(sizeof(struct thread_stat));
162 if (!st)
163 die("memory allocation failed\n");
164
165 st->tid = tid;
166 INIT_LIST_HEAD(&st->seq_list);
167
168 thread_stat_insert(st);
169
170 return st;
171}
172
173static struct thread_stat *thread_stat_findnew_first(u32 tid);
174static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
175 thread_stat_findnew_first;
176
177static struct thread_stat *thread_stat_findnew_first(u32 tid)
178{
179 struct thread_stat *st;
180
181 st = zalloc(sizeof(struct thread_stat));
182 if (!st)
183 die("memory allocation failed\n");
184 st->tid = tid;
185 INIT_LIST_HEAD(&st->seq_list);
186
187 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
188 rb_insert_color(&st->rb, &thread_stats);
189
190 thread_stat_findnew = thread_stat_findnew_after_first;
191 return st;
192}
193
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900194/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100195#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900196 static int lock_stat_key_ ## member(struct lock_stat *one, \
197 struct lock_stat *two) \
198 { \
199 return one->member > two->member; \
200 }
201
202SINGLE_KEY(nr_acquired)
203SINGLE_KEY(nr_contended)
204SINGLE_KEY(wait_time_total)
205SINGLE_KEY(wait_time_min)
206SINGLE_KEY(wait_time_max)
207
208struct lock_key {
209 /*
210 * name: the value for specify by user
211 * this should be simpler than raw name of member
212 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
213 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100214 const char *name;
215 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900216};
217
Ingo Molnar59f411b2010-01-31 08:27:58 +0100218static const char *sort_key = "acquired";
219
220static int (*compare)(struct lock_stat *, struct lock_stat *);
221
222static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900223
224#define DEF_KEY_LOCK(name, fn_suffix) \
225 { #name, lock_stat_key_ ## fn_suffix }
226struct lock_key keys[] = {
227 DEF_KEY_LOCK(acquired, nr_acquired),
228 DEF_KEY_LOCK(contended, nr_contended),
229 DEF_KEY_LOCK(wait_total, wait_time_total),
230 DEF_KEY_LOCK(wait_min, wait_time_min),
231 DEF_KEY_LOCK(wait_max, wait_time_max),
232
233 /* extra comparisons much complicated should be here */
234
235 { NULL, NULL }
236};
237
238static void select_key(void)
239{
240 int i;
241
242 for (i = 0; keys[i].name; i++) {
243 if (!strcmp(keys[i].name, sort_key)) {
244 compare = keys[i].key;
245 return;
246 }
247 }
248
249 die("Unknown compare key:%s\n", sort_key);
250}
251
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900252static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100253 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900254{
255 struct rb_node **rb = &result.rb_node;
256 struct rb_node *parent = NULL;
257 struct lock_stat *p;
258
259 while (*rb) {
260 p = container_of(*rb, struct lock_stat, rb);
261 parent = *rb;
262
263 if (bigger(st, p))
264 rb = &(*rb)->rb_left;
265 else
266 rb = &(*rb)->rb_right;
267 }
268
269 rb_link_node(&st->rb, parent, rb);
270 rb_insert_color(&st->rb, &result);
271}
272
273/* returns left most element of result, and erase it */
274static struct lock_stat *pop_from_result(void)
275{
276 struct rb_node *node = result.rb_node;
277
278 if (!node)
279 return NULL;
280
281 while (node->rb_left)
282 node = node->rb_left;
283
284 rb_erase(node, &result);
285 return container_of(node, struct lock_stat, rb);
286}
287
Ingo Molnar59f411b2010-01-31 08:27:58 +0100288static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900289{
290 struct list_head *entry = lockhashentry(addr);
291 struct lock_stat *ret, *new;
292
293 list_for_each_entry(ret, entry, hash_entry) {
294 if (ret->addr == addr)
295 return ret;
296 }
297
298 new = zalloc(sizeof(struct lock_stat));
299 if (!new)
300 goto alloc_failed;
301
302 new->addr = addr;
303 new->name = zalloc(sizeof(char) * strlen(name) + 1);
304 if (!new->name)
305 goto alloc_failed;
306 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900307
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900308 new->wait_time_min = ULLONG_MAX;
309
310 list_add(&new->hash_entry, entry);
311 return new;
312
313alloc_failed:
314 die("memory allocation failed\n");
315}
316
317static char const *input_name = "perf.data";
318
319static int profile_cpu = -1;
320
321struct raw_event_sample {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100322 u32 size;
323 char data[0];
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900324};
325
326struct trace_acquire_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100327 void *addr;
328 const char *name;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900329 int flag;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900330};
331
332struct trace_acquired_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100333 void *addr;
334 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900335};
336
337struct trace_contended_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100338 void *addr;
339 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900340};
341
342struct trace_release_event {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100343 void *addr;
344 const char *name;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900345};
346
347struct trace_lock_handler {
348 void (*acquire_event)(struct trace_acquire_event *,
349 struct event *,
350 int cpu,
351 u64 timestamp,
352 struct thread *thread);
353
354 void (*acquired_event)(struct trace_acquired_event *,
355 struct event *,
356 int cpu,
357 u64 timestamp,
358 struct thread *thread);
359
360 void (*contended_event)(struct trace_contended_event *,
361 struct event *,
362 int cpu,
363 u64 timestamp,
364 struct thread *thread);
365
366 void (*release_event)(struct trace_release_event *,
367 struct event *,
368 int cpu,
369 u64 timestamp,
370 struct thread *thread);
371};
372
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900373static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
374{
375 struct lock_seq_stat *seq;
376
377 list_for_each_entry(seq, &ts->seq_list, list) {
378 if (seq->addr == addr)
379 return seq;
380 }
381
382 seq = zalloc(sizeof(struct lock_seq_stat));
383 if (!seq)
384 die("Not enough memory\n");
385 seq->state = SEQ_STATE_UNINITIALIZED;
386 seq->addr = addr;
387
388 list_add(&seq->list, &ts->seq_list);
389 return seq;
390}
391
392static int bad_hist[4];
393
Ingo Molnar59f411b2010-01-31 08:27:58 +0100394static void
395report_lock_acquire_event(struct trace_acquire_event *acquire_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900396 struct event *__event __used,
397 int cpu __used,
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900398 u64 timestamp __used,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900399 struct thread *thread __used)
400{
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900401 struct lock_stat *ls;
402 struct thread_stat *ts;
403 struct lock_seq_stat *seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900404
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900405 ls = lock_stat_findnew(acquire_event->addr, acquire_event->name);
406 if (ls->discard)
407 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900408
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900409 ts = thread_stat_findnew(thread->pid);
410 seq = get_seq(ts, acquire_event->addr);
411
412 switch (seq->state) {
413 case SEQ_STATE_UNINITIALIZED:
414 case SEQ_STATE_RELEASED:
415 if (!acquire_event->flag) {
416 seq->state = SEQ_STATE_ACQUIRING;
417 } else {
418 if (acquire_event->flag & 1)
419 ls->nr_trylock++;
420 if (acquire_event->flag & 2)
421 ls->nr_readlock++;
422 seq->state = SEQ_STATE_READ_ACQUIRED;
423 seq->read_count = 1;
424 ls->nr_acquired++;
425 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900426 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900427 case SEQ_STATE_READ_ACQUIRED:
428 if (acquire_event->flag & 2) {
429 seq->read_count++;
430 ls->nr_acquired++;
431 goto end;
432 } else {
433 goto broken;
434 }
435 break;
436 case SEQ_STATE_ACQUIRED:
437 case SEQ_STATE_ACQUIRING:
438 case SEQ_STATE_CONTENDED:
439broken:
440 /* broken lock sequence, discard it */
441 ls->discard = 1;
442 bad_hist[0]++;
443 list_del(&seq->list);
444 free(seq);
445 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900446 break;
447 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900448 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900449 break;
450 }
451
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900452 ls->nr_acquire++;
453 seq->prev_event_time = timestamp;
454end:
455 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900456}
457
Ingo Molnar59f411b2010-01-31 08:27:58 +0100458static void
459report_lock_acquired_event(struct trace_acquired_event *acquired_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900460 struct event *__event __used,
461 int cpu __used,
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900462 u64 timestamp __used,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900463 struct thread *thread __used)
464{
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;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900469
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900470 ls = lock_stat_findnew(acquired_event->addr, acquired_event->name);
471 if (ls->discard)
472 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900473
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900474 ts = thread_stat_findnew(thread->pid);
475 seq = get_seq(ts, acquired_event->addr);
476
477 switch (seq->state) {
478 case SEQ_STATE_UNINITIALIZED:
479 /* orphan event, do nothing */
480 return;
481 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900482 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900483 case SEQ_STATE_CONTENDED:
484 contended_term = timestamp - seq->prev_event_time;
485 ls->wait_time_total += contended_term;
486
487 if (contended_term < ls->wait_time_min)
488 ls->wait_time_min = contended_term;
489 else if (ls->wait_time_max < contended_term)
490 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900491 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900492 case SEQ_STATE_RELEASED:
493 case SEQ_STATE_ACQUIRED:
494 case SEQ_STATE_READ_ACQUIRED:
495 /* broken lock sequence, discard it */
496 ls->discard = 1;
497 bad_hist[1]++;
498 list_del(&seq->list);
499 free(seq);
500 goto end;
501 break;
502
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900503 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900504 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900505 break;
506 }
507
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900508 seq->state = SEQ_STATE_ACQUIRED;
509 ls->nr_acquired++;
510 seq->prev_event_time = timestamp;
511end:
512 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900513}
514
Ingo Molnar59f411b2010-01-31 08:27:58 +0100515static void
516report_lock_contended_event(struct trace_contended_event *contended_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900517 struct event *__event __used,
518 int cpu __used,
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900519 u64 timestamp __used,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900520 struct thread *thread __used)
521{
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900522 struct lock_stat *ls;
523 struct thread_stat *ts;
524 struct lock_seq_stat *seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900525
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900526 ls = lock_stat_findnew(contended_event->addr, contended_event->name);
527 if (ls->discard)
528 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900529
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900530 ts = thread_stat_findnew(thread->pid);
531 seq = get_seq(ts, contended_event->addr);
532
533 switch (seq->state) {
534 case SEQ_STATE_UNINITIALIZED:
535 /* orphan event, do nothing */
536 return;
537 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900538 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900539 case SEQ_STATE_RELEASED:
540 case SEQ_STATE_ACQUIRED:
541 case SEQ_STATE_READ_ACQUIRED:
542 case SEQ_STATE_CONTENDED:
543 /* broken lock sequence, discard it */
544 ls->discard = 1;
545 bad_hist[2]++;
546 list_del(&seq->list);
547 free(seq);
548 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900549 break;
550 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900551 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900552 break;
553 }
554
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900555 seq->state = SEQ_STATE_CONTENDED;
556 ls->nr_contended++;
557 seq->prev_event_time = timestamp;
558end:
559 return;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900560}
561
Ingo Molnar59f411b2010-01-31 08:27:58 +0100562static void
563report_lock_release_event(struct trace_release_event *release_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900564 struct event *__event __used,
565 int cpu __used,
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900566 u64 timestamp __used,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900567 struct thread *thread __used)
568{
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
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900577 ts = thread_stat_findnew(thread->pid);
578 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;
599 bad_hist[3]++;
600 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
626static void
627process_lock_acquire_event(void *data,
628 struct event *event __used,
629 int cpu __used,
630 u64 timestamp __used,
631 struct thread *thread __used)
632{
633 struct trace_acquire_event acquire_event;
634 u64 tmp; /* this is required for casting... */
635
636 tmp = raw_field_value(event, "lockdep_addr", data);
637 memcpy(&acquire_event.addr, &tmp, sizeof(void *));
638 acquire_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900639 acquire_event.flag = (int)raw_field_value(event, "flag", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900640
Ingo Molnar59f411b2010-01-31 08:27:58 +0100641 if (trace_handler->acquire_event)
642 trace_handler->acquire_event(&acquire_event, event, cpu, timestamp, thread);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900643}
644
645static void
646process_lock_acquired_event(void *data,
647 struct event *event __used,
648 int cpu __used,
649 u64 timestamp __used,
650 struct thread *thread __used)
651{
652 struct trace_acquired_event acquired_event;
653 u64 tmp; /* this is required for casting... */
654
655 tmp = raw_field_value(event, "lockdep_addr", data);
656 memcpy(&acquired_event.addr, &tmp, sizeof(void *));
657 acquired_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900658
Ingo Molnar59f411b2010-01-31 08:27:58 +0100659 if (trace_handler->acquire_event)
660 trace_handler->acquired_event(&acquired_event, event, cpu, timestamp, thread);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900661}
662
663static void
664process_lock_contended_event(void *data,
665 struct event *event __used,
666 int cpu __used,
667 u64 timestamp __used,
668 struct thread *thread __used)
669{
670 struct trace_contended_event contended_event;
671 u64 tmp; /* this is required for casting... */
672
673 tmp = raw_field_value(event, "lockdep_addr", data);
674 memcpy(&contended_event.addr, &tmp, sizeof(void *));
675 contended_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900676
Ingo Molnar59f411b2010-01-31 08:27:58 +0100677 if (trace_handler->acquire_event)
678 trace_handler->contended_event(&contended_event, event, cpu, timestamp, thread);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900679}
680
681static void
682process_lock_release_event(void *data,
683 struct event *event __used,
684 int cpu __used,
685 u64 timestamp __used,
686 struct thread *thread __used)
687{
688 struct trace_release_event release_event;
689 u64 tmp; /* this is required for casting... */
690
691 tmp = raw_field_value(event, "lockdep_addr", data);
692 memcpy(&release_event.addr, &tmp, sizeof(void *));
693 release_event.name = (char *)raw_field_ptr(event, "name", data);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900694
Ingo Molnar59f411b2010-01-31 08:27:58 +0100695 if (trace_handler->acquire_event)
696 trace_handler->release_event(&release_event, event, cpu, timestamp, thread);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900697}
698
699static void
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900700process_raw_event(void *data, int cpu __used,
701 u64 timestamp __used, struct thread *thread __used)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900702{
703 struct event *event;
704 int type;
705
706 type = trace_parse_common_type(data);
707 event = trace_find_event(type);
708
709 if (!strcmp(event->name, "lock_acquire"))
710 process_lock_acquire_event(data, event, cpu, timestamp, thread);
711 if (!strcmp(event->name, "lock_acquired"))
712 process_lock_acquired_event(data, event, cpu, timestamp, thread);
713 if (!strcmp(event->name, "lock_contended"))
714 process_lock_contended_event(data, event, cpu, timestamp, thread);
715 if (!strcmp(event->name, "lock_release"))
716 process_lock_release_event(data, event, cpu, timestamp, thread);
717}
718
Frederic Weisbeckerb67577d2010-02-03 09:09:33 +0100719struct raw_event_queue {
720 u64 timestamp;
721 int cpu;
722 void *data;
723 struct thread *thread;
724 struct list_head list;
725};
726
727static LIST_HEAD(raw_event_head);
728
729#define FLUSH_PERIOD (5 * NSEC_PER_SEC)
730
731static u64 flush_limit = ULLONG_MAX;
732static u64 last_flush = 0;
733struct raw_event_queue *last_inserted;
734
735static void flush_raw_event_queue(u64 limit)
736{
737 struct raw_event_queue *tmp, *iter;
738
739 list_for_each_entry_safe(iter, tmp, &raw_event_head, list) {
740 if (iter->timestamp > limit)
741 return;
742
743 if (iter == last_inserted)
744 last_inserted = NULL;
745
746 process_raw_event(iter->data, iter->cpu, iter->timestamp,
747 iter->thread);
748
749 last_flush = iter->timestamp;
750 list_del(&iter->list);
751 free(iter->data);
752 free(iter);
753 }
754}
755
756static void __queue_raw_event_end(struct raw_event_queue *new)
757{
758 struct raw_event_queue *iter;
759
760 list_for_each_entry_reverse(iter, &raw_event_head, list) {
761 if (iter->timestamp < new->timestamp) {
762 list_add(&new->list, &iter->list);
763 return;
764 }
765 }
766
767 list_add(&new->list, &raw_event_head);
768}
769
770static void __queue_raw_event_before(struct raw_event_queue *new,
771 struct raw_event_queue *iter)
772{
773 list_for_each_entry_continue_reverse(iter, &raw_event_head, list) {
774 if (iter->timestamp < new->timestamp) {
775 list_add(&new->list, &iter->list);
776 return;
777 }
778 }
779
780 list_add(&new->list, &raw_event_head);
781}
782
783static void __queue_raw_event_after(struct raw_event_queue *new,
784 struct raw_event_queue *iter)
785{
786 list_for_each_entry_continue(iter, &raw_event_head, list) {
787 if (iter->timestamp > new->timestamp) {
788 list_add_tail(&new->list, &iter->list);
789 return;
790 }
791 }
792 list_add_tail(&new->list, &raw_event_head);
793}
794
795/* The queue is ordered by time */
796static void __queue_raw_event(struct raw_event_queue *new)
797{
798 if (!last_inserted) {
799 __queue_raw_event_end(new);
800 return;
801 }
802
803 /*
804 * Most of the time the current event has a timestamp
805 * very close to the last event inserted, unless we just switched
806 * to another event buffer. Having a sorting based on a list and
807 * on the last inserted event that is close to the current one is
808 * probably more efficient than an rbtree based sorting.
809 */
810 if (last_inserted->timestamp >= new->timestamp)
811 __queue_raw_event_before(new, last_inserted);
812 else
813 __queue_raw_event_after(new, last_inserted);
814}
815
816static void queue_raw_event(void *data, int raw_size, int cpu,
817 u64 timestamp, struct thread *thread)
818{
819 struct raw_event_queue *new;
820
821 if (flush_limit == ULLONG_MAX)
822 flush_limit = timestamp + FLUSH_PERIOD;
823
824 if (timestamp < last_flush) {
825 printf("Warning: Timestamp below last timeslice flush\n");
826 return;
827 }
828
829 new = malloc(sizeof(*new));
830 if (!new)
831 die("Not enough memory\n");
832
833 new->timestamp = timestamp;
834 new->cpu = cpu;
835 new->thread = thread;
836
837 new->data = malloc(raw_size);
838 if (!new->data)
839 die("Not enough memory\n");
840
841 memcpy(new->data, data, raw_size);
842
843 __queue_raw_event(new);
844 last_inserted = new;
845
846 /*
847 * We want to have a slice of events covering 2 * FLUSH_PERIOD
848 * If FLUSH_PERIOD is big enough, it ensures every events that occured
849 * in the first half of the timeslice have all been buffered and there
850 * are none remaining (we need that because of the weakly ordered
851 * event recording we have). Then once we reach the 2 * FLUSH_PERIOD
852 * timeslice, we flush the first half to be gentle with the memory
853 * (the second half can still get new events in the middle, so wait
854 * another period to flush it)
855 */
856 if (new->timestamp > flush_limit &&
857 new->timestamp - flush_limit > FLUSH_PERIOD) {
858 flush_limit += FLUSH_PERIOD;
859 flush_raw_event_queue(flush_limit);
860 }
861}
862
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900863static int process_sample_event(event_t *event, struct perf_session *s)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900864{
865 struct thread *thread;
866 struct sample_data data;
867
868 bzero(&data, sizeof(struct sample_data));
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900869 event__parse_sample(event, s->sample_type, &data);
870 /* CAUTION: using tid as thread.pid */
871 thread = perf_session__findnew(s, data.tid);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900872
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900873 if (thread == NULL) {
874 pr_debug("problem processing %d event, skipping it.\n",
875 event->header.type);
876 return -1;
877 }
878
879 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
880
881 if (profile_cpu != -1 && profile_cpu != (int) data.cpu)
882 return 0;
883
Frederic Weisbeckerb67577d2010-02-03 09:09:33 +0100884 queue_raw_event(data.raw_data, data.raw_size, data.cpu, data.time, thread);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900885
886 return 0;
887}
888
889/* TODO: various way to print, coloring, nano or milli sec */
890static void print_result(void)
891{
892 struct lock_stat *st;
893 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900894 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900895
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900896 printf("%20s ", "Name");
897 printf("%10s ", "acquired");
898 printf("%10s ", "contended");
899
900 printf("%15s ", "total wait (ns)");
901 printf("%15s ", "max wait (ns)");
902 printf("%15s ", "min wait (ns)");
903
904 printf("\n\n");
905
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900906 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900907 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900908 total++;
909 if (st->discard) {
910 bad++;
911 continue;
912 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900913 bzero(cut_name, 20);
914
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900915 if (strlen(st->name) < 16) {
916 /* output raw name */
917 printf("%20s ", st->name);
918 } else {
919 strncpy(cut_name, st->name, 16);
920 cut_name[16] = '.';
921 cut_name[17] = '.';
922 cut_name[18] = '.';
923 cut_name[19] = '\0';
924 /* cut off name for saving output style */
925 printf("%20s ", cut_name);
926 }
927
928 printf("%10u ", st->nr_acquired);
929 printf("%10u ", st->nr_contended);
930
931 printf("%15llu ", st->wait_time_total);
932 printf("%15llu ", st->wait_time_max);
933 printf("%15llu ", st->wait_time_min == ULLONG_MAX ?
934 0 : st->wait_time_min);
935 printf("\n");
936 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900937
938 {
939 /* Output for debug, this have to be removed */
940 int i;
941 const char *name[4] =
942 { "acquire", "acquired", "contended", "release" };
943
944 printf("\n=== output for debug===\n\n");
945 printf("bad:%d, total:%d\n", bad, total);
946 printf("bad rate:%f\n", (double)(bad / total));
947
948 printf("histogram of events caused bad sequence\n");
949 for (i = 0; i < 4; i++)
950 printf(" %10s: %d\n", name[i], bad_hist[i]);
951 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900952}
953
954static void dump_map(void)
955{
956 unsigned int i;
957 struct lock_stat *st;
958
959 for (i = 0; i < LOCKHASH_SIZE; i++) {
960 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100961 printf("%p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900962 }
963 }
964}
965
966static struct perf_event_ops eops = {
Ingo Molnar59f411b2010-01-31 08:27:58 +0100967 .sample = process_sample_event,
968 .comm = event__process_comm,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900969};
970
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900971static int read_events(void)
972{
973 session = perf_session__new(input_name, O_RDONLY, 0);
974 if (!session)
975 die("Initializing perf session failed\n");
976
977 return perf_session__process_events(session, &eops);
978}
979
980static void sort_result(void)
981{
982 unsigned int i;
983 struct lock_stat *st;
984
985 for (i = 0; i < LOCKHASH_SIZE; i++) {
986 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
987 insert_to_result(st, compare);
988 }
989 }
990}
991
Ingo Molnar59f411b2010-01-31 08:27:58 +0100992static void __cmd_report(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900993{
994 setup_pager();
995 select_key();
996 read_events();
Frederic Weisbeckerb67577d2010-02-03 09:09:33 +0100997 flush_raw_event_queue(ULLONG_MAX);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900998 sort_result();
999 print_result();
1000}
1001
Ingo Molnar59f411b2010-01-31 08:27:58 +01001002static const char * const report_usage[] = {
1003 "perf lock report [<options>]",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001004 NULL
1005};
1006
Ingo Molnar59f411b2010-01-31 08:27:58 +01001007static const struct option report_options[] = {
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001008 OPT_STRING('k', "key", &sort_key, "acquired",
1009 "key for sorting"),
1010 /* TODO: type */
1011 OPT_END()
1012};
1013
1014static const char * const lock_usage[] = {
Ingo Molnar59f411b2010-01-31 08:27:58 +01001015 "perf lock [<options>] {record|trace|report}",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001016 NULL
1017};
1018
1019static const struct option lock_options[] = {
Ingo Molnar59f411b2010-01-31 08:27:58 +01001020 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Ian Munsiec0555642010-04-13 18:37:33 +10001021 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
Ingo Molnar59f411b2010-01-31 08:27:58 +01001022 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001023 OPT_END()
1024};
1025
1026static const char *record_args[] = {
1027 "record",
1028 "-a",
1029 "-R",
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001030 "-f",
1031 "-m", "1024",
1032 "-c", "1",
1033 "-e", "lock:lock_acquire:r",
1034 "-e", "lock:lock_acquired:r",
1035 "-e", "lock:lock_contended:r",
1036 "-e", "lock:lock_release:r",
1037};
1038
1039static int __cmd_record(int argc, const char **argv)
1040{
1041 unsigned int rec_argc, i, j;
1042 const char **rec_argv;
1043
1044 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
1045 rec_argv = calloc(rec_argc + 1, sizeof(char *));
1046
1047 for (i = 0; i < ARRAY_SIZE(record_args); i++)
1048 rec_argv[i] = strdup(record_args[i]);
1049
1050 for (j = 1; j < (unsigned int)argc; j++, i++)
1051 rec_argv[i] = argv[j];
1052
1053 BUG_ON(i != rec_argc);
1054
1055 return cmd_record(i, rec_argv, NULL);
1056}
1057
1058int cmd_lock(int argc, const char **argv, const char *prefix __used)
1059{
1060 unsigned int i;
1061
1062 symbol__init();
1063 for (i = 0; i < LOCKHASH_SIZE; i++)
1064 INIT_LIST_HEAD(lockhash_table + i);
1065
1066 argc = parse_options(argc, argv, lock_options, lock_usage,
1067 PARSE_OPT_STOP_AT_NON_OPTION);
1068 if (!argc)
1069 usage_with_options(lock_usage, lock_options);
1070
1071 if (!strncmp(argv[0], "rec", 3)) {
1072 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +01001073 } else if (!strncmp(argv[0], "report", 6)) {
1074 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001075 if (argc) {
1076 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +01001077 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001078 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +01001079 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001080 }
Ingo Molnar59f411b2010-01-31 08:27:58 +01001081 __cmd_report();
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001082 } else if (!strcmp(argv[0], "trace")) {
1083 /* Aliased to 'perf trace' */
1084 return cmd_trace(argc, argv, prefix);
1085 } else if (!strcmp(argv[0], "map")) {
Ingo Molnar59f411b2010-01-31 08:27:58 +01001086 /* recycling report_lock_ops */
1087 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001088 setup_pager();
1089 read_events();
1090 dump_map();
1091 } else {
1092 usage_with_options(lock_usage, lock_options);
1093 }
1094
1095 return 0;
1096}