blob: ff98652484a77efaa86a835b7c8f9a796ab36b50 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09003#include "builtin.h"
4#include "perf.h"
5
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -03006#include "util/evlist.h"
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03007#include "util/evsel.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09008#include "util/util.h"
9#include "util/cache.h"
10#include "util/symbol.h"
11#include "util/thread.h"
12#include "util/header.h"
13
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060014#include <subcmd/parse-options.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090015#include "util/trace-event.h"
16
17#include "util/debug.h"
18#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020019#include "util/tool.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020020#include "util/data.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090021
22#include <sys/types.h>
23#include <sys/prctl.h>
24#include <semaphore.h>
25#include <pthread.h>
26#include <math.h>
27#include <limits.h>
28
29#include <linux/list.h>
30#include <linux/hash.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030031#include <linux/kernel.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090032
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090033static struct perf_session *session;
34
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090035/* based on kernel/lockdep.c */
36#define LOCKHASH_BITS 12
37#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
38
39static struct list_head lockhash_table[LOCKHASH_SIZE];
40
41#define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
42#define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
43
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090044struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010045 struct list_head hash_entry;
46 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090047
Ingo Molnar59f411b2010-01-31 08:27:58 +010048 /*
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -030049 * FIXME: perf_evsel__intval() returns u64,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090050 * so address of lockdep_map should be dealed as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010051 * Is there more better solution?
52 */
53 void *addr; /* address of lockdep_map, used as ID */
54 char *name; /* for strcpy(), we cannot use const */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090055
Ingo Molnar59f411b2010-01-31 08:27:58 +010056 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090057 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010058 unsigned int nr_contended;
59 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090060
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090061 unsigned int nr_readlock;
62 unsigned int nr_trylock;
Davidlohr Buesof37376c2013-09-08 19:19:19 -070063
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090064 /* these times are in nano sec. */
Davidlohr Buesof37376c2013-09-08 19:19:19 -070065 u64 avg_wait_time;
Ingo Molnar59f411b2010-01-31 08:27:58 +010066 u64 wait_time_total;
67 u64 wait_time_min;
68 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090069
70 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090071};
72
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090073/*
74 * States of lock_seq_stat
75 *
76 * UNINITIALIZED is required for detecting first event of acquire.
77 * As the nature of lock events, there is no guarantee
78 * that the first event for the locks are acquire,
79 * it can be acquired, contended or release.
80 */
81#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
82#define SEQ_STATE_RELEASED 1
83#define SEQ_STATE_ACQUIRING 2
84#define SEQ_STATE_ACQUIRED 3
85#define SEQ_STATE_READ_ACQUIRED 4
86#define SEQ_STATE_CONTENDED 5
87
88/*
89 * MAX_LOCK_DEPTH
90 * Imported from include/linux/sched.h.
91 * Should this be synchronized?
92 */
93#define MAX_LOCK_DEPTH 48
94
95/*
96 * struct lock_seq_stat:
97 * Place to put on state of one lock sequence
98 * 1) acquire -> acquired -> release
99 * 2) acquire -> contended -> acquired -> release
100 * 3) acquire (with read or try) -> release
101 * 4) Are there other patterns?
102 */
103struct lock_seq_stat {
104 struct list_head list;
105 int state;
106 u64 prev_event_time;
107 void *addr;
108
109 int read_count;
110};
111
112struct thread_stat {
113 struct rb_node rb;
114
115 u32 tid;
116 struct list_head seq_list;
117};
118
119static struct rb_root thread_stats;
120
121static struct thread_stat *thread_stat_find(u32 tid)
122{
123 struct rb_node *node;
124 struct thread_stat *st;
125
126 node = thread_stats.rb_node;
127 while (node) {
128 st = container_of(node, struct thread_stat, rb);
129 if (st->tid == tid)
130 return st;
131 else if (tid < st->tid)
132 node = node->rb_left;
133 else
134 node = node->rb_right;
135 }
136
137 return NULL;
138}
139
140static void thread_stat_insert(struct thread_stat *new)
141{
142 struct rb_node **rb = &thread_stats.rb_node;
143 struct rb_node *parent = NULL;
144 struct thread_stat *p;
145
146 while (*rb) {
147 p = container_of(*rb, struct thread_stat, rb);
148 parent = *rb;
149
150 if (new->tid < p->tid)
151 rb = &(*rb)->rb_left;
152 else if (new->tid > p->tid)
153 rb = &(*rb)->rb_right;
154 else
155 BUG_ON("inserting invalid thread_stat\n");
156 }
157
158 rb_link_node(&new->rb, parent, rb);
159 rb_insert_color(&new->rb, &thread_stats);
160}
161
162static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
163{
164 struct thread_stat *st;
165
166 st = thread_stat_find(tid);
167 if (st)
168 return st;
169
170 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600171 if (!st) {
172 pr_err("memory allocation failed\n");
173 return NULL;
174 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900175
176 st->tid = tid;
177 INIT_LIST_HEAD(&st->seq_list);
178
179 thread_stat_insert(st);
180
181 return st;
182}
183
184static struct thread_stat *thread_stat_findnew_first(u32 tid);
185static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
186 thread_stat_findnew_first;
187
188static struct thread_stat *thread_stat_findnew_first(u32 tid)
189{
190 struct thread_stat *st;
191
192 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600193 if (!st) {
194 pr_err("memory allocation failed\n");
195 return NULL;
196 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900197 st->tid = tid;
198 INIT_LIST_HEAD(&st->seq_list);
199
200 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
201 rb_insert_color(&st->rb, &thread_stats);
202
203 thread_stat_findnew = thread_stat_findnew_after_first;
204 return st;
205}
206
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900207/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100208#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900209 static int lock_stat_key_ ## member(struct lock_stat *one, \
210 struct lock_stat *two) \
211 { \
212 return one->member > two->member; \
213 }
214
215SINGLE_KEY(nr_acquired)
216SINGLE_KEY(nr_contended)
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700217SINGLE_KEY(avg_wait_time)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900218SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900219SINGLE_KEY(wait_time_max)
220
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100221static int lock_stat_key_wait_time_min(struct lock_stat *one,
222 struct lock_stat *two)
223{
224 u64 s1 = one->wait_time_min;
225 u64 s2 = two->wait_time_min;
226 if (s1 == ULLONG_MAX)
227 s1 = 0;
228 if (s2 == ULLONG_MAX)
229 s2 = 0;
230 return s1 > s2;
231}
232
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900233struct lock_key {
234 /*
235 * name: the value for specify by user
236 * this should be simpler than raw name of member
237 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
238 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100239 const char *name;
240 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900241};
242
Ingo Molnar59f411b2010-01-31 08:27:58 +0100243static const char *sort_key = "acquired";
244
245static int (*compare)(struct lock_stat *, struct lock_stat *);
246
247static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900248
249#define DEF_KEY_LOCK(name, fn_suffix) \
250 { #name, lock_stat_key_ ## fn_suffix }
251struct lock_key keys[] = {
252 DEF_KEY_LOCK(acquired, nr_acquired),
253 DEF_KEY_LOCK(contended, nr_contended),
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700254 DEF_KEY_LOCK(avg_wait, avg_wait_time),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900255 DEF_KEY_LOCK(wait_total, wait_time_total),
256 DEF_KEY_LOCK(wait_min, wait_time_min),
257 DEF_KEY_LOCK(wait_max, wait_time_max),
258
259 /* extra comparisons much complicated should be here */
260
261 { NULL, NULL }
262};
263
David Ahern33d6aef2012-08-26 12:24:43 -0600264static int select_key(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900265{
266 int i;
267
268 for (i = 0; keys[i].name; i++) {
269 if (!strcmp(keys[i].name, sort_key)) {
270 compare = keys[i].key;
David Ahern33d6aef2012-08-26 12:24:43 -0600271 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900272 }
273 }
274
David Ahern33d6aef2012-08-26 12:24:43 -0600275 pr_err("Unknown compare key: %s\n", sort_key);
276
277 return -1;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900278}
279
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900280static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100281 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900282{
283 struct rb_node **rb = &result.rb_node;
284 struct rb_node *parent = NULL;
285 struct lock_stat *p;
286
287 while (*rb) {
288 p = container_of(*rb, struct lock_stat, rb);
289 parent = *rb;
290
291 if (bigger(st, p))
292 rb = &(*rb)->rb_left;
293 else
294 rb = &(*rb)->rb_right;
295 }
296
297 rb_link_node(&st->rb, parent, rb);
298 rb_insert_color(&st->rb, &result);
299}
300
301/* returns left most element of result, and erase it */
302static struct lock_stat *pop_from_result(void)
303{
304 struct rb_node *node = result.rb_node;
305
306 if (!node)
307 return NULL;
308
309 while (node->rb_left)
310 node = node->rb_left;
311
312 rb_erase(node, &result);
313 return container_of(node, struct lock_stat, rb);
314}
315
Ingo Molnar59f411b2010-01-31 08:27:58 +0100316static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900317{
318 struct list_head *entry = lockhashentry(addr);
319 struct lock_stat *ret, *new;
320
321 list_for_each_entry(ret, entry, hash_entry) {
322 if (ret->addr == addr)
323 return ret;
324 }
325
326 new = zalloc(sizeof(struct lock_stat));
327 if (!new)
328 goto alloc_failed;
329
330 new->addr = addr;
331 new->name = zalloc(sizeof(char) * strlen(name) + 1);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700332 if (!new->name) {
333 free(new);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900334 goto alloc_failed;
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700335 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900336
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700337 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900338 new->wait_time_min = ULLONG_MAX;
339
340 list_add(&new->hash_entry, entry);
341 return new;
342
343alloc_failed:
David Ahern33d6aef2012-08-26 12:24:43 -0600344 pr_err("memory allocation failed\n");
345 return NULL;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900346}
347
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900348struct trace_lock_handler {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300349 int (*acquire_event)(struct perf_evsel *evsel,
350 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900351
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300352 int (*acquired_event)(struct perf_evsel *evsel,
353 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900354
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300355 int (*contended_event)(struct perf_evsel *evsel,
356 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900357
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300358 int (*release_event)(struct perf_evsel *evsel,
359 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900360};
361
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900362static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
363{
364 struct lock_seq_stat *seq;
365
366 list_for_each_entry(seq, &ts->seq_list, list) {
367 if (seq->addr == addr)
368 return seq;
369 }
370
371 seq = zalloc(sizeof(struct lock_seq_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600372 if (!seq) {
373 pr_err("memory allocation failed\n");
374 return NULL;
375 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900376 seq->state = SEQ_STATE_UNINITIALIZED;
377 seq->addr = addr;
378
379 list_add(&seq->list, &ts->seq_list);
380 return seq;
381}
382
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200383enum broken_state {
384 BROKEN_ACQUIRE,
385 BROKEN_ACQUIRED,
386 BROKEN_CONTENDED,
387 BROKEN_RELEASE,
388 BROKEN_MAX,
389};
390
391static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900392
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200393enum acquire_flags {
394 TRY_LOCK = 1,
395 READ_LOCK = 2,
396};
397
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300398static int report_lock_acquire_event(struct perf_evsel *evsel,
399 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900400{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300401 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900402 struct lock_stat *ls;
403 struct thread_stat *ts;
404 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300405 const char *name = perf_evsel__strval(evsel, sample, "name");
406 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
407 int flag = perf_evsel__intval(evsel, sample, "flag");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900408
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300409 memcpy(&addr, &tmp, sizeof(void *));
410
411 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600412 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700413 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900414 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600415 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900416
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300417 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600418 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700419 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600420
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300421 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600422 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700423 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900424
425 switch (seq->state) {
426 case SEQ_STATE_UNINITIALIZED:
427 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300428 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900429 seq->state = SEQ_STATE_ACQUIRING;
430 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300431 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900432 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300433 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900434 ls->nr_readlock++;
435 seq->state = SEQ_STATE_READ_ACQUIRED;
436 seq->read_count = 1;
437 ls->nr_acquired++;
438 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900439 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900440 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300441 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900442 seq->read_count++;
443 ls->nr_acquired++;
444 goto end;
445 } else {
446 goto broken;
447 }
448 break;
449 case SEQ_STATE_ACQUIRED:
450 case SEQ_STATE_ACQUIRING:
451 case SEQ_STATE_CONTENDED:
452broken:
453 /* broken lock sequence, discard it */
454 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200455 bad_hist[BROKEN_ACQUIRE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900456 list_del(&seq->list);
457 free(seq);
458 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900459 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900460 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900461 break;
462 }
463
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900464 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300465 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900466end:
David Ahern33d6aef2012-08-26 12:24:43 -0600467 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900468}
469
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300470static int report_lock_acquired_event(struct perf_evsel *evsel,
471 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900472{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300473 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900474 struct lock_stat *ls;
475 struct thread_stat *ts;
476 struct lock_seq_stat *seq;
477 u64 contended_term;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300478 const char *name = perf_evsel__strval(evsel, sample, "name");
479 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900480
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300481 memcpy(&addr, &tmp, sizeof(void *));
482
483 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600484 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700485 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900486 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600487 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900488
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300489 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600490 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700491 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600492
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300493 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600494 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700495 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900496
497 switch (seq->state) {
498 case SEQ_STATE_UNINITIALIZED:
499 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600500 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900501 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900502 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900503 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300504 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900505 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900506 if (contended_term < ls->wait_time_min)
507 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200508 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900509 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900510 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900511 case SEQ_STATE_RELEASED:
512 case SEQ_STATE_ACQUIRED:
513 case SEQ_STATE_READ_ACQUIRED:
514 /* broken lock sequence, discard it */
515 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200516 bad_hist[BROKEN_ACQUIRED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900517 list_del(&seq->list);
518 free(seq);
519 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900520 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900521 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900522 break;
523 }
524
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900525 seq->state = SEQ_STATE_ACQUIRED;
526 ls->nr_acquired++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700527 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300528 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900529end:
David Ahern33d6aef2012-08-26 12:24:43 -0600530 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900531}
532
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300533static int report_lock_contended_event(struct perf_evsel *evsel,
534 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900535{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300536 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900537 struct lock_stat *ls;
538 struct thread_stat *ts;
539 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300540 const char *name = perf_evsel__strval(evsel, sample, "name");
541 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900542
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300543 memcpy(&addr, &tmp, sizeof(void *));
544
545 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600546 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700547 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900548 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600549 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900550
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300551 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600552 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700553 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600554
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300555 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600556 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700557 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900558
559 switch (seq->state) {
560 case SEQ_STATE_UNINITIALIZED:
561 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600562 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900563 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900564 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900565 case SEQ_STATE_RELEASED:
566 case SEQ_STATE_ACQUIRED:
567 case SEQ_STATE_READ_ACQUIRED:
568 case SEQ_STATE_CONTENDED:
569 /* broken lock sequence, discard it */
570 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200571 bad_hist[BROKEN_CONTENDED]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900572 list_del(&seq->list);
573 free(seq);
574 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900575 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900576 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900577 break;
578 }
579
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900580 seq->state = SEQ_STATE_CONTENDED;
581 ls->nr_contended++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700582 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300583 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900584end:
David Ahern33d6aef2012-08-26 12:24:43 -0600585 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900586}
587
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300588static int report_lock_release_event(struct perf_evsel *evsel,
589 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900590{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300591 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900592 struct lock_stat *ls;
593 struct thread_stat *ts;
594 struct lock_seq_stat *seq;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300595 const char *name = perf_evsel__strval(evsel, sample, "name");
596 u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900597
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300598 memcpy(&addr, &tmp, sizeof(void *));
599
600 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600601 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700602 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900603 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600604 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900605
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300606 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600607 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700608 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600609
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300610 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600611 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700612 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900613
614 switch (seq->state) {
615 case SEQ_STATE_UNINITIALIZED:
616 goto end;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900617 case SEQ_STATE_ACQUIRED:
618 break;
619 case SEQ_STATE_READ_ACQUIRED:
620 seq->read_count--;
621 BUG_ON(seq->read_count < 0);
622 if (!seq->read_count) {
623 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900624 goto end;
625 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900626 break;
627 case SEQ_STATE_ACQUIRING:
628 case SEQ_STATE_CONTENDED:
629 case SEQ_STATE_RELEASED:
630 /* broken lock sequence, discard it */
631 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200632 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900633 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900634 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900635 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900636 break;
637 }
638
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900639 ls->nr_release++;
640free_seq:
641 list_del(&seq->list);
642 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900643end:
David Ahern33d6aef2012-08-26 12:24:43 -0600644 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900645}
646
647/* lock oriented handlers */
648/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100649static struct trace_lock_handler report_lock_ops = {
650 .acquire_event = report_lock_acquire_event,
651 .acquired_event = report_lock_acquired_event,
652 .contended_event = report_lock_contended_event,
653 .release_event = report_lock_release_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900654};
655
656static struct trace_lock_handler *trace_handler;
657
David Ahern33d6aef2012-08-26 12:24:43 -0600658static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300659 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900660{
Ingo Molnar59f411b2010-01-31 08:27:58 +0100661 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300662 return trace_handler->acquire_event(evsel, sample);
663 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900664}
665
David Ahern33d6aef2012-08-26 12:24:43 -0600666static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300667 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900668{
David Ahern33d6aef2012-08-26 12:24:43 -0600669 if (trace_handler->acquired_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300670 return trace_handler->acquired_event(evsel, sample);
671 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900672}
673
David Ahern33d6aef2012-08-26 12:24:43 -0600674static int perf_evsel__process_lock_contended(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300675 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900676{
David Ahern33d6aef2012-08-26 12:24:43 -0600677 if (trace_handler->contended_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300678 return trace_handler->contended_event(evsel, sample);
679 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900680}
681
David Ahern33d6aef2012-08-26 12:24:43 -0600682static int perf_evsel__process_lock_release(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300683 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900684{
David Ahern33d6aef2012-08-26 12:24:43 -0600685 if (trace_handler->release_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300686 return trace_handler->release_event(evsel, sample);
687 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900688}
689
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200690static void print_bad_events(int bad, int total)
691{
692 /* Output for debug, this have to be removed */
693 int i;
694 const char *name[4] =
695 { "acquire", "acquired", "contended", "release" };
696
697 pr_info("\n=== output for debug===\n\n");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200698 pr_info("bad: %d, total: %d\n", bad, total);
Davidlohr Bueso60a25cb2013-09-08 19:19:18 -0700699 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200700 pr_info("histogram of events caused bad sequence\n");
701 for (i = 0; i < BROKEN_MAX; i++)
702 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
703}
704
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900705/* TODO: various way to print, coloring, nano or milli sec */
706static void print_result(void)
707{
708 struct lock_stat *st;
709 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900710 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900711
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900712 pr_info("%20s ", "Name");
713 pr_info("%10s ", "acquired");
714 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900715
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700716 pr_info("%15s ", "avg wait (ns)");
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900717 pr_info("%15s ", "total wait (ns)");
718 pr_info("%15s ", "max wait (ns)");
719 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900720
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900721 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900722
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900723 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900724 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900725 total++;
726 if (st->discard) {
727 bad++;
728 continue;
729 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900730 bzero(cut_name, 20);
731
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900732 if (strlen(st->name) < 16) {
733 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900734 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900735 } else {
736 strncpy(cut_name, st->name, 16);
737 cut_name[16] = '.';
738 cut_name[17] = '.';
739 cut_name[18] = '.';
740 cut_name[19] = '\0';
741 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900742 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900743 }
744
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900745 pr_info("%10u ", st->nr_acquired);
746 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900747
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700748 pr_info("%15" PRIu64 " ", st->avg_wait_time);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200749 pr_info("%15" PRIu64 " ", st->wait_time_total);
750 pr_info("%15" PRIu64 " ", st->wait_time_max);
751 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900752 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900753 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900754 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900755
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200756 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900757}
758
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300759static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900760
761static void dump_threads(void)
762{
763 struct thread_stat *st;
764 struct rb_node *node;
765 struct thread *t;
766
767 pr_info("%10s: comm\n", "Thread ID");
768
769 node = rb_first(&thread_stats);
770 while (node) {
771 st = container_of(node, struct thread_stat, rb);
772 t = perf_session__findnew(session, st->tid);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200773 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900774 node = rb_next(node);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300775 thread__put(t);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900776 };
777}
778
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900779static void dump_map(void)
780{
781 unsigned int i;
782 struct lock_stat *st;
783
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900784 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900785 for (i = 0; i < LOCKHASH_SIZE; i++) {
786 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900787 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900788 }
789 }
790}
791
David Ahern33d6aef2012-08-26 12:24:43 -0600792static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900793{
David Ahern33d6aef2012-08-26 12:24:43 -0600794 int rc = 0;
795
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900796 if (info_threads)
797 dump_threads();
798 else if (info_map)
799 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600800 else {
801 rc = -1;
802 pr_err("Unknown type of information\n");
803 }
804
805 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900806}
807
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300808typedef int (*tracepoint_handler)(struct perf_evsel *evsel,
809 struct perf_sample *sample);
810
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300811static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200812 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300813 struct perf_sample *sample,
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -0300814 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200815 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200816{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300817 int err = 0;
Adrian Hunter314add62013-08-27 11:23:03 +0300818 struct thread *thread = machine__findnew_thread(machine, sample->pid,
819 sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200820
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200821 if (thread == NULL) {
822 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200823 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200824 return -1;
825 }
826
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300827 if (evsel->handler != NULL) {
828 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300829 err = f(evsel, sample);
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300830 }
831
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300832 thread__put(thread);
833
834 return err;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200835}
836
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900837static void sort_result(void)
838{
839 unsigned int i;
840 struct lock_stat *st;
841
842 for (i = 0; i < LOCKHASH_SIZE; i++) {
843 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
844 insert_to_result(st, compare);
845 }
846 }
847}
848
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700849static const struct perf_evsel_str_handler lock_tracepoints[] = {
850 { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
851 { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
852 { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
853 { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
854};
855
Yunlong Songc4ac7322015-04-02 21:47:14 +0800856static bool force;
857
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700858static int __cmd_report(bool display_info)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900859{
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700860 int err = -EINVAL;
861 struct perf_tool eops = {
862 .sample = process_sample_event,
863 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +0530864 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200865 .ordered_events = true,
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700866 };
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200867 struct perf_data_file file = {
868 .path = input_name,
869 .mode = PERF_DATA_MODE_READ,
Yunlong Songc4ac7322015-04-02 21:47:14 +0800870 .force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200871 };
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700872
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200873 session = perf_session__new(&file, false, &eops);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700874 if (!session) {
875 pr_err("Initializing perf session failed\n");
Taeung Song52e028342014-09-24 10:33:37 +0900876 return -1;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700877 }
878
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900879 symbol__init(&session->header.env);
Namhyung Kim6fd6c6b2014-08-12 15:40:40 +0900880
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700881 if (!perf_session__has_traces(session, "lock record"))
882 goto out_delete;
883
884 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
885 pr_err("Initializing perf session tracepoint handlers failed\n");
886 goto out_delete;
887 }
888
889 if (select_key())
890 goto out_delete;
891
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300892 err = perf_session__process_events(session);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700893 if (err)
894 goto out_delete;
895
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900896 setup_pager();
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700897 if (display_info) /* used for info subcommand */
898 err = dump_info();
899 else {
900 sort_result();
901 print_result();
902 }
David Ahern33d6aef2012-08-26 12:24:43 -0600903
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700904out_delete:
905 perf_session__delete(session);
906 return err;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900907}
908
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900909static int __cmd_record(int argc, const char **argv)
910{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300911 const char *record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200912 "record", "-R", "-m", "1024", "-c", "1",
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300913 };
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700914 unsigned int rec_argc, i, j, ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900915 const char **rec_argv;
916
David Ahernd25dcba2012-08-09 10:35:37 -0600917 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300918 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600919 pr_err("tracepoint %s is not enabled. "
920 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300921 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600922 return 1;
923 }
924 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900925
David Ahernd25dcba2012-08-09 10:35:37 -0600926 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
927 /* factor of 2 is for -e in front of each tracepoint */
928 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
929
930 rec_argv = calloc(rec_argc + 1, sizeof(char *));
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700931 if (!rec_argv)
Chris Samuelce47dc52010-11-13 13:35:06 +1100932 return -ENOMEM;
933
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900934 for (i = 0; i < ARRAY_SIZE(record_args); i++)
935 rec_argv[i] = strdup(record_args[i]);
936
David Ahernd25dcba2012-08-09 10:35:37 -0600937 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
938 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300939 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600940 }
941
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900942 for (j = 1; j < (unsigned int)argc; j++, i++)
943 rec_argv[i] = argv[j];
944
945 BUG_ON(i != rec_argc);
946
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300947 ret = cmd_record(i, rec_argv);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700948 free(rec_argv);
949 return ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900950}
951
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300952int cmd_lock(int argc, const char **argv)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900953{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300954 const struct option lock_options[] = {
955 OPT_STRING('i', "input", &input_name, "file", "input file name"),
956 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
957 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob40e3612017-03-17 11:16:02 -0300958 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300959 OPT_END()
960 };
Changbin Du249eed52017-03-17 13:53:42 +0800961
962 const struct option info_options[] = {
963 OPT_BOOLEAN('t', "threads", &info_threads,
964 "dump thread list in perf.data"),
965 OPT_BOOLEAN('m', "map", &info_map,
966 "map of lock instances (address:name table)"),
Changbin Du249eed52017-03-17 13:53:42 +0800967 OPT_PARENT(lock_options)
968 };
969
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300970 const struct option report_options[] = {
971 OPT_STRING('k', "key", &sort_key, "acquired",
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700972 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300973 /* TODO: type */
Changbin Du249eed52017-03-17 13:53:42 +0800974 OPT_PARENT(lock_options)
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300975 };
Changbin Du249eed52017-03-17 13:53:42 +0800976
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300977 const char * const info_usage[] = {
978 "perf lock info [<options>]",
979 NULL
980 };
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400981 const char *const lock_subcommands[] = { "record", "report", "script",
982 "info", NULL };
983 const char *lock_usage[] = {
984 NULL,
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300985 NULL
986 };
987 const char * const report_usage[] = {
988 "perf lock report [<options>]",
989 NULL
990 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900991 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600992 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900993
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900994 for (i = 0; i < LOCKHASH_SIZE; i++)
995 INIT_LIST_HEAD(lockhash_table + i);
996
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400997 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
998 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900999 if (!argc)
1000 usage_with_options(lock_usage, lock_options);
1001
1002 if (!strncmp(argv[0], "rec", 3)) {
1003 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +01001004 } else if (!strncmp(argv[0], "report", 6)) {
1005 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001006 if (argc) {
1007 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +01001008 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001009 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +01001010 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001011 }
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001012 rc = __cmd_report(false);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001013 } else if (!strcmp(argv[0], "script")) {
1014 /* Aliased to 'perf script' */
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001015 return cmd_script(argc, argv);
Hitoshi Mitake26242d82010-05-03 14:12:00 +09001016 } else if (!strcmp(argv[0], "info")) {
1017 if (argc) {
1018 argc = parse_options(argc, argv,
1019 info_options, info_usage, 0);
1020 if (argc)
1021 usage_with_options(info_usage, info_options);
1022 }
Ingo Molnar59f411b2010-01-31 08:27:58 +01001023 /* recycling report_lock_ops */
1024 trace_handler = &report_lock_ops;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001025 rc = __cmd_report(true);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001026 } else {
1027 usage_with_options(lock_usage, lock_options);
1028 }
1029
David Ahern33d6aef2012-08-26 12:24:43 -06001030 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001031}