blob: d70131b7b1b1e3411894fa1f3803d6d5774cd531 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09004#include "builtin.h"
5#include "perf.h"
6
Arnaldo Carvalho de Melo7ae811b2019-08-30 12:29:03 -03007#include "util/evlist.h" // for struct evsel_str_handler
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03008#include "util/evsel.h"
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09009#include "util/symbol.h"
10#include "util/thread.h"
11#include "util/header.h"
12
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030013#include <subcmd/pager.h>
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>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030032#include <linux/zalloc.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053033#include <linux/err.h>
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090034
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090035static struct perf_session *session;
36
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090037/* based on kernel/lockdep.c */
38#define LOCKHASH_BITS 12
39#define LOCKHASH_SIZE (1UL << LOCKHASH_BITS)
40
41static struct list_head lockhash_table[LOCKHASH_SIZE];
42
43#define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS)
44#define lockhashentry(key) (lockhash_table + __lockhashfn((key)))
45
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090046struct lock_stat {
Ingo Molnar59f411b2010-01-31 08:27:58 +010047 struct list_head hash_entry;
48 struct rb_node rb; /* used for sorting */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090049
Ingo Molnar59f411b2010-01-31 08:27:58 +010050 /*
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -030051 * FIXME: evsel__intval() returns u64,
Ingo Molnar4d39c892021-03-23 17:09:15 +010052 * so address of lockdep_map should be treated as 64bit.
Ingo Molnar59f411b2010-01-31 08:27:58 +010053 * Is there more better solution?
54 */
55 void *addr; /* address of lockdep_map, used as ID */
56 char *name; /* for strcpy(), we cannot use const */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090057
Ingo Molnar59f411b2010-01-31 08:27:58 +010058 unsigned int nr_acquire;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090059 unsigned int nr_acquired;
Ingo Molnar59f411b2010-01-31 08:27:58 +010060 unsigned int nr_contended;
61 unsigned int nr_release;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090062
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090063 unsigned int nr_readlock;
64 unsigned int nr_trylock;
Davidlohr Buesof37376c2013-09-08 19:19:19 -070065
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090066 /* these times are in nano sec. */
Davidlohr Buesof37376c2013-09-08 19:19:19 -070067 u64 avg_wait_time;
Ingo Molnar59f411b2010-01-31 08:27:58 +010068 u64 wait_time_total;
69 u64 wait_time_min;
70 u64 wait_time_max;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090071
72 int discard; /* flag of blacklist */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +090073};
74
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +090075/*
76 * States of lock_seq_stat
77 *
78 * UNINITIALIZED is required for detecting first event of acquire.
79 * As the nature of lock events, there is no guarantee
80 * that the first event for the locks are acquire,
81 * it can be acquired, contended or release.
82 */
83#define SEQ_STATE_UNINITIALIZED 0 /* initial state */
84#define SEQ_STATE_RELEASED 1
85#define SEQ_STATE_ACQUIRING 2
86#define SEQ_STATE_ACQUIRED 3
87#define SEQ_STATE_READ_ACQUIRED 4
88#define SEQ_STATE_CONTENDED 5
89
90/*
91 * MAX_LOCK_DEPTH
92 * Imported from include/linux/sched.h.
93 * Should this be synchronized?
94 */
95#define MAX_LOCK_DEPTH 48
96
97/*
98 * struct lock_seq_stat:
99 * Place to put on state of one lock sequence
100 * 1) acquire -> acquired -> release
101 * 2) acquire -> contended -> acquired -> release
102 * 3) acquire (with read or try) -> release
103 * 4) Are there other patterns?
104 */
105struct lock_seq_stat {
106 struct list_head list;
107 int state;
108 u64 prev_event_time;
109 void *addr;
110
111 int read_count;
112};
113
114struct thread_stat {
115 struct rb_node rb;
116
117 u32 tid;
118 struct list_head seq_list;
119};
120
121static struct rb_root thread_stats;
122
123static struct thread_stat *thread_stat_find(u32 tid)
124{
125 struct rb_node *node;
126 struct thread_stat *st;
127
128 node = thread_stats.rb_node;
129 while (node) {
130 st = container_of(node, struct thread_stat, rb);
131 if (st->tid == tid)
132 return st;
133 else if (tid < st->tid)
134 node = node->rb_left;
135 else
136 node = node->rb_right;
137 }
138
139 return NULL;
140}
141
142static void thread_stat_insert(struct thread_stat *new)
143{
144 struct rb_node **rb = &thread_stats.rb_node;
145 struct rb_node *parent = NULL;
146 struct thread_stat *p;
147
148 while (*rb) {
149 p = container_of(*rb, struct thread_stat, rb);
150 parent = *rb;
151
152 if (new->tid < p->tid)
153 rb = &(*rb)->rb_left;
154 else if (new->tid > p->tid)
155 rb = &(*rb)->rb_right;
156 else
157 BUG_ON("inserting invalid thread_stat\n");
158 }
159
160 rb_link_node(&new->rb, parent, rb);
161 rb_insert_color(&new->rb, &thread_stats);
162}
163
164static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
165{
166 struct thread_stat *st;
167
168 st = thread_stat_find(tid);
169 if (st)
170 return st;
171
172 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600173 if (!st) {
174 pr_err("memory allocation failed\n");
175 return NULL;
176 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900177
178 st->tid = tid;
179 INIT_LIST_HEAD(&st->seq_list);
180
181 thread_stat_insert(st);
182
183 return st;
184}
185
186static struct thread_stat *thread_stat_findnew_first(u32 tid);
187static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
188 thread_stat_findnew_first;
189
190static struct thread_stat *thread_stat_findnew_first(u32 tid)
191{
192 struct thread_stat *st;
193
194 st = zalloc(sizeof(struct thread_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600195 if (!st) {
196 pr_err("memory allocation failed\n");
197 return NULL;
198 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900199 st->tid = tid;
200 INIT_LIST_HEAD(&st->seq_list);
201
202 rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
203 rb_insert_color(&st->rb, &thread_stats);
204
205 thread_stat_findnew = thread_stat_findnew_after_first;
206 return st;
207}
208
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900209/* build simple key function one is bigger than two */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100210#define SINGLE_KEY(member) \
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900211 static int lock_stat_key_ ## member(struct lock_stat *one, \
212 struct lock_stat *two) \
213 { \
214 return one->member > two->member; \
215 }
216
217SINGLE_KEY(nr_acquired)
218SINGLE_KEY(nr_contended)
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700219SINGLE_KEY(avg_wait_time)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900220SINGLE_KEY(wait_time_total)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900221SINGLE_KEY(wait_time_max)
222
Marcin Slusarz9df03ab2011-02-22 18:47:15 +0100223static int lock_stat_key_wait_time_min(struct lock_stat *one,
224 struct lock_stat *two)
225{
226 u64 s1 = one->wait_time_min;
227 u64 s2 = two->wait_time_min;
228 if (s1 == ULLONG_MAX)
229 s1 = 0;
230 if (s2 == ULLONG_MAX)
231 s2 = 0;
232 return s1 > s2;
233}
234
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900235struct lock_key {
236 /*
237 * name: the value for specify by user
238 * this should be simpler than raw name of member
239 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
240 */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100241 const char *name;
242 int (*key)(struct lock_stat*, struct lock_stat*);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900243};
244
Ingo Molnar59f411b2010-01-31 08:27:58 +0100245static const char *sort_key = "acquired";
246
247static int (*compare)(struct lock_stat *, struct lock_stat *);
248
249static struct rb_root result; /* place to store sorted data */
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900250
251#define DEF_KEY_LOCK(name, fn_suffix) \
252 { #name, lock_stat_key_ ## fn_suffix }
253struct lock_key keys[] = {
254 DEF_KEY_LOCK(acquired, nr_acquired),
255 DEF_KEY_LOCK(contended, nr_contended),
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700256 DEF_KEY_LOCK(avg_wait, avg_wait_time),
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900257 DEF_KEY_LOCK(wait_total, wait_time_total),
258 DEF_KEY_LOCK(wait_min, wait_time_min),
259 DEF_KEY_LOCK(wait_max, wait_time_max),
260
261 /* extra comparisons much complicated should be here */
262
263 { NULL, NULL }
264};
265
David Ahern33d6aef2012-08-26 12:24:43 -0600266static int select_key(void)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900267{
268 int i;
269
270 for (i = 0; keys[i].name; i++) {
271 if (!strcmp(keys[i].name, sort_key)) {
272 compare = keys[i].key;
David Ahern33d6aef2012-08-26 12:24:43 -0600273 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900274 }
275 }
276
David Ahern33d6aef2012-08-26 12:24:43 -0600277 pr_err("Unknown compare key: %s\n", sort_key);
278
279 return -1;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900280}
281
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900282static void insert_to_result(struct lock_stat *st,
Ingo Molnar59f411b2010-01-31 08:27:58 +0100283 int (*bigger)(struct lock_stat *, struct lock_stat *))
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900284{
285 struct rb_node **rb = &result.rb_node;
286 struct rb_node *parent = NULL;
287 struct lock_stat *p;
288
289 while (*rb) {
290 p = container_of(*rb, struct lock_stat, rb);
291 parent = *rb;
292
293 if (bigger(st, p))
294 rb = &(*rb)->rb_left;
295 else
296 rb = &(*rb)->rb_right;
297 }
298
299 rb_link_node(&st->rb, parent, rb);
300 rb_insert_color(&st->rb, &result);
301}
302
303/* returns left most element of result, and erase it */
304static struct lock_stat *pop_from_result(void)
305{
306 struct rb_node *node = result.rb_node;
307
308 if (!node)
309 return NULL;
310
311 while (node->rb_left)
312 node = node->rb_left;
313
314 rb_erase(node, &result);
315 return container_of(node, struct lock_stat, rb);
316}
317
Ingo Molnar59f411b2010-01-31 08:27:58 +0100318static struct lock_stat *lock_stat_findnew(void *addr, const char *name)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900319{
320 struct list_head *entry = lockhashentry(addr);
321 struct lock_stat *ret, *new;
322
323 list_for_each_entry(ret, entry, hash_entry) {
324 if (ret->addr == addr)
325 return ret;
326 }
327
328 new = zalloc(sizeof(struct lock_stat));
329 if (!new)
330 goto alloc_failed;
331
332 new->addr = addr;
333 new->name = zalloc(sizeof(char) * strlen(name) + 1);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700334 if (!new->name) {
335 free(new);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900336 goto alloc_failed;
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700337 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900338
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700339 strcpy(new->name, name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900340 new->wait_time_min = ULLONG_MAX;
341
342 list_add(&new->hash_entry, entry);
343 return new;
344
345alloc_failed:
David Ahern33d6aef2012-08-26 12:24:43 -0600346 pr_err("memory allocation failed\n");
347 return NULL;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900348}
349
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900350struct trace_lock_handler {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200351 int (*acquire_event)(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300352 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900353
Jiri Olsa32dcd022019-07-21 13:23:51 +0200354 int (*acquired_event)(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300355 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900356
Jiri Olsa32dcd022019-07-21 13:23:51 +0200357 int (*contended_event)(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300358 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900359
Jiri Olsa32dcd022019-07-21 13:23:51 +0200360 int (*release_event)(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300361 struct perf_sample *sample);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900362};
363
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900364static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr)
365{
366 struct lock_seq_stat *seq;
367
368 list_for_each_entry(seq, &ts->seq_list, list) {
369 if (seq->addr == addr)
370 return seq;
371 }
372
373 seq = zalloc(sizeof(struct lock_seq_stat));
David Ahern33d6aef2012-08-26 12:24:43 -0600374 if (!seq) {
375 pr_err("memory allocation failed\n");
376 return NULL;
377 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900378 seq->state = SEQ_STATE_UNINITIALIZED;
379 seq->addr = addr;
380
381 list_add(&seq->list, &ts->seq_list);
382 return seq;
383}
384
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200385enum broken_state {
386 BROKEN_ACQUIRE,
387 BROKEN_ACQUIRED,
388 BROKEN_CONTENDED,
389 BROKEN_RELEASE,
390 BROKEN_MAX,
391};
392
393static int bad_hist[BROKEN_MAX];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900394
Frederic Weisbecker84c7a212010-05-05 23:57:25 +0200395enum acquire_flags {
396 TRY_LOCK = 1,
397 READ_LOCK = 2,
398};
399
Jiri Olsa32dcd022019-07-21 13:23:51 +0200400static int report_lock_acquire_event(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300401 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900402{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300403 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900404 struct lock_stat *ls;
405 struct thread_stat *ts;
406 struct lock_seq_stat *seq;
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300407 const char *name = evsel__strval(evsel, sample, "name");
408 u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
Leo Yane24a87b2020-11-04 17:42:28 +0800409 int flag = evsel__intval(evsel, sample, "flags");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900410
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300411 memcpy(&addr, &tmp, sizeof(void *));
412
413 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600414 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700415 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900416 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600417 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900418
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300419 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600420 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700421 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600422
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300423 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600424 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700425 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900426
427 switch (seq->state) {
428 case SEQ_STATE_UNINITIALIZED:
429 case SEQ_STATE_RELEASED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300430 if (!flag) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900431 seq->state = SEQ_STATE_ACQUIRING;
432 } else {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300433 if (flag & TRY_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900434 ls->nr_trylock++;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300435 if (flag & READ_LOCK)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900436 ls->nr_readlock++;
437 seq->state = SEQ_STATE_READ_ACQUIRED;
438 seq->read_count = 1;
439 ls->nr_acquired++;
440 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900441 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900442 case SEQ_STATE_READ_ACQUIRED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300443 if (flag & READ_LOCK) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900444 seq->read_count++;
445 ls->nr_acquired++;
446 goto end;
447 } else {
448 goto broken;
449 }
450 break;
451 case SEQ_STATE_ACQUIRED:
452 case SEQ_STATE_ACQUIRING:
453 case SEQ_STATE_CONTENDED:
454broken:
455 /* broken lock sequence, discard it */
456 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200457 bad_hist[BROKEN_ACQUIRE]++;
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300458 list_del_init(&seq->list);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900459 free(seq);
460 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900461 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900462 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900463 break;
464 }
465
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900466 ls->nr_acquire++;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300467 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900468end:
David Ahern33d6aef2012-08-26 12:24:43 -0600469 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900470}
471
Jiri Olsa32dcd022019-07-21 13:23:51 +0200472static int report_lock_acquired_event(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300473 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900474{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300475 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900476 struct lock_stat *ls;
477 struct thread_stat *ts;
478 struct lock_seq_stat *seq;
479 u64 contended_term;
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300480 const char *name = evsel__strval(evsel, sample, "name");
481 u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900482
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300483 memcpy(&addr, &tmp, sizeof(void *));
484
485 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600486 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700487 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900488 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600489 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900490
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300491 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600492 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700493 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600494
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300495 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600496 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700497 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900498
499 switch (seq->state) {
500 case SEQ_STATE_UNINITIALIZED:
501 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600502 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900503 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900504 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900505 case SEQ_STATE_CONTENDED:
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300506 contended_term = sample->time - seq->prev_event_time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900507 ls->wait_time_total += contended_term;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900508 if (contended_term < ls->wait_time_min)
509 ls->wait_time_min = contended_term;
Frederic Weisbecker90c0e5f2010-05-07 02:33:42 +0200510 if (ls->wait_time_max < contended_term)
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900511 ls->wait_time_max = contended_term;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900512 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900513 case SEQ_STATE_RELEASED:
514 case SEQ_STATE_ACQUIRED:
515 case SEQ_STATE_READ_ACQUIRED:
516 /* broken lock sequence, discard it */
517 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200518 bad_hist[BROKEN_ACQUIRED]++;
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300519 list_del_init(&seq->list);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900520 free(seq);
521 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900522 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900523 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900524 break;
525 }
526
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900527 seq->state = SEQ_STATE_ACQUIRED;
528 ls->nr_acquired++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700529 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300530 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900531end:
David Ahern33d6aef2012-08-26 12:24:43 -0600532 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900533}
534
Jiri Olsa32dcd022019-07-21 13:23:51 +0200535static int report_lock_contended_event(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300536 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900537{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300538 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900539 struct lock_stat *ls;
540 struct thread_stat *ts;
541 struct lock_seq_stat *seq;
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300542 const char *name = evsel__strval(evsel, sample, "name");
543 u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900544
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300545 memcpy(&addr, &tmp, sizeof(void *));
546
547 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600548 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700549 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900550 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600551 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900552
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300553 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600554 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700555 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600556
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300557 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600558 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700559 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900560
561 switch (seq->state) {
562 case SEQ_STATE_UNINITIALIZED:
563 /* orphan event, do nothing */
David Ahern33d6aef2012-08-26 12:24:43 -0600564 return 0;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900565 case SEQ_STATE_ACQUIRING:
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900566 break;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900567 case SEQ_STATE_RELEASED:
568 case SEQ_STATE_ACQUIRED:
569 case SEQ_STATE_READ_ACQUIRED:
570 case SEQ_STATE_CONTENDED:
571 /* broken lock sequence, discard it */
572 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200573 bad_hist[BROKEN_CONTENDED]++;
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300574 list_del_init(&seq->list);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900575 free(seq);
576 goto end;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900577 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900578 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900579 break;
580 }
581
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900582 seq->state = SEQ_STATE_CONTENDED;
583 ls->nr_contended++;
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700584 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300585 seq->prev_event_time = sample->time;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900586end:
David Ahern33d6aef2012-08-26 12:24:43 -0600587 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900588}
589
Jiri Olsa32dcd022019-07-21 13:23:51 +0200590static int report_lock_release_event(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300591 struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900592{
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300593 void *addr;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900594 struct lock_stat *ls;
595 struct thread_stat *ts;
596 struct lock_seq_stat *seq;
Arnaldo Carvalho de Meloefc0cdc2020-04-29 16:26:57 -0300597 const char *name = evsel__strval(evsel, sample, "name");
598 u64 tmp = evsel__intval(evsel, sample, "lockdep_addr");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900599
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300600 memcpy(&addr, &tmp, sizeof(void *));
601
602 ls = lock_stat_findnew(addr, name);
David Ahern33d6aef2012-08-26 12:24:43 -0600603 if (!ls)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700604 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900605 if (ls->discard)
David Ahern33d6aef2012-08-26 12:24:43 -0600606 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900607
Arnaldo Carvalho de Melo01d95522012-08-07 10:59:44 -0300608 ts = thread_stat_findnew(sample->tid);
David Ahern33d6aef2012-08-26 12:24:43 -0600609 if (!ts)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700610 return -ENOMEM;
David Ahern33d6aef2012-08-26 12:24:43 -0600611
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300612 seq = get_seq(ts, addr);
David Ahern33d6aef2012-08-26 12:24:43 -0600613 if (!seq)
Davidlohr Buesob33492a2013-09-08 19:19:14 -0700614 return -ENOMEM;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900615
616 switch (seq->state) {
617 case SEQ_STATE_UNINITIALIZED:
618 goto end;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900619 case SEQ_STATE_ACQUIRED:
620 break;
621 case SEQ_STATE_READ_ACQUIRED:
622 seq->read_count--;
623 BUG_ON(seq->read_count < 0);
Leo Yanb0e5a052020-11-04 17:42:29 +0800624 if (seq->read_count) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900625 ls->nr_release++;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900626 goto end;
627 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900628 break;
629 case SEQ_STATE_ACQUIRING:
630 case SEQ_STATE_CONTENDED:
631 case SEQ_STATE_RELEASED:
632 /* broken lock sequence, discard it */
633 ls->discard = 1;
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200634 bad_hist[BROKEN_RELEASE]++;
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900635 goto free_seq;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900636 default:
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900637 BUG_ON("Unknown state of lock sequence found!\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900638 break;
639 }
640
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900641 ls->nr_release++;
642free_seq:
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300643 list_del_init(&seq->list);
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900644 free(seq);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900645end:
David Ahern33d6aef2012-08-26 12:24:43 -0600646 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900647}
648
649/* lock oriented handlers */
650/* TODO: handlers for CPU oriented, thread oriented */
Ingo Molnar59f411b2010-01-31 08:27:58 +0100651static struct trace_lock_handler report_lock_ops = {
652 .acquire_event = report_lock_acquire_event,
653 .acquired_event = report_lock_acquired_event,
654 .contended_event = report_lock_contended_event,
655 .release_event = report_lock_release_event,
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900656};
657
658static struct trace_lock_handler *trace_handler;
659
Arnaldo Carvalho de Melo3d655812020-05-04 13:56:18 -0300660static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900661{
Ingo Molnar59f411b2010-01-31 08:27:58 +0100662 if (trace_handler->acquire_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300663 return trace_handler->acquire_event(evsel, sample);
664 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900665}
666
Arnaldo Carvalho de Melo3d655812020-05-04 13:56:18 -0300667static int evsel__process_lock_acquired(struct evsel *evsel, 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
Arnaldo Carvalho de Melo3d655812020-05-04 13:56:18 -0300674static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900675{
David Ahern33d6aef2012-08-26 12:24:43 -0600676 if (trace_handler->contended_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300677 return trace_handler->contended_event(evsel, sample);
678 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900679}
680
Arnaldo Carvalho de Melo3d655812020-05-04 13:56:18 -0300681static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900682{
David Ahern33d6aef2012-08-26 12:24:43 -0600683 if (trace_handler->release_event)
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300684 return trace_handler->release_event(evsel, sample);
685 return 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900686}
687
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200688static void print_bad_events(int bad, int total)
689{
690 /* Output for debug, this have to be removed */
691 int i;
692 const char *name[4] =
693 { "acquire", "acquired", "contended", "release" };
694
695 pr_info("\n=== output for debug===\n\n");
Frederic Weisbecker5efe08c2010-05-06 04:55:22 +0200696 pr_info("bad: %d, total: %d\n", bad, total);
Davidlohr Bueso60a25cb2013-09-08 19:19:18 -0700697 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200698 pr_info("histogram of events caused bad sequence\n");
699 for (i = 0; i < BROKEN_MAX; i++)
700 pr_info(" %10s: %d\n", name[i], bad_hist[i]);
701}
702
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900703/* TODO: various way to print, coloring, nano or milli sec */
704static void print_result(void)
705{
706 struct lock_stat *st;
707 char cut_name[20];
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900708 int bad, total;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900709
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900710 pr_info("%20s ", "Name");
711 pr_info("%10s ", "acquired");
712 pr_info("%10s ", "contended");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900713
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700714 pr_info("%15s ", "avg wait (ns)");
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900715 pr_info("%15s ", "total wait (ns)");
716 pr_info("%15s ", "max wait (ns)");
717 pr_info("%15s ", "min wait (ns)");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900718
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900719 pr_info("\n\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900720
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900721 bad = total = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900722 while ((st = pop_from_result())) {
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900723 total++;
724 if (st->discard) {
725 bad++;
726 continue;
727 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900728 bzero(cut_name, 20);
729
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900730 if (strlen(st->name) < 16) {
731 /* output raw name */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900732 pr_info("%20s ", st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900733 } else {
734 strncpy(cut_name, st->name, 16);
735 cut_name[16] = '.';
736 cut_name[17] = '.';
737 cut_name[18] = '.';
738 cut_name[19] = '\0';
739 /* cut off name for saving output style */
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900740 pr_info("%20s ", cut_name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900741 }
742
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900743 pr_info("%10u ", st->nr_acquired);
744 pr_info("%10u ", st->nr_contended);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900745
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700746 pr_info("%15" PRIu64 " ", st->avg_wait_time);
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200747 pr_info("%15" PRIu64 " ", st->wait_time_total);
748 pr_info("%15" PRIu64 " ", st->wait_time_max);
749 pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ?
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900750 0 : st->wait_time_min);
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900751 pr_info("\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900752 }
Hitoshi Mitakee4cef1f2010-04-21 21:23:54 +0900753
Frederic Weisbecker10350ec2010-05-05 23:47:28 +0200754 print_bad_events(bad, total);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900755}
756
Arnaldo Carvalho de Melo80354582010-05-17 15:51:10 -0300757static bool info_threads, info_map;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900758
759static void dump_threads(void)
760{
761 struct thread_stat *st;
762 struct rb_node *node;
763 struct thread *t;
764
765 pr_info("%10s: comm\n", "Thread ID");
766
767 node = rb_first(&thread_stats);
768 while (node) {
769 st = container_of(node, struct thread_stat, rb);
770 t = perf_session__findnew(session, st->tid);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200771 pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900772 node = rb_next(node);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300773 thread__put(t);
Zou Wei8284bbe2020-04-28 17:18:43 +0800774 }
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900775}
776
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900777static void dump_map(void)
778{
779 unsigned int i;
780 struct lock_stat *st;
781
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900782 pr_info("Address of instance: name of class\n");
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900783 for (i = 0; i < LOCKHASH_SIZE; i++) {
784 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900785 pr_info(" %p: %s\n", st->addr, st->name);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900786 }
787 }
788}
789
David Ahern33d6aef2012-08-26 12:24:43 -0600790static int dump_info(void)
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900791{
David Ahern33d6aef2012-08-26 12:24:43 -0600792 int rc = 0;
793
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900794 if (info_threads)
795 dump_threads();
796 else if (info_map)
797 dump_map();
David Ahern33d6aef2012-08-26 12:24:43 -0600798 else {
799 rc = -1;
800 pr_err("Unknown type of information\n");
801 }
802
803 return rc;
Hitoshi Mitake26242d82010-05-03 14:12:00 +0900804}
805
Jiri Olsa32dcd022019-07-21 13:23:51 +0200806typedef int (*tracepoint_handler)(struct evsel *evsel,
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300807 struct perf_sample *sample);
808
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300809static int process_sample_event(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200810 union perf_event *event,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300811 struct perf_sample *sample,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200812 struct evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200813 struct machine *machine)
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200814{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300815 int err = 0;
Adrian Hunter314add62013-08-27 11:23:03 +0300816 struct thread *thread = machine__findnew_thread(machine, sample->pid,
817 sample->tid);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200818
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200819 if (thread == NULL) {
820 pr_debug("problem processing %d event, skipping it.\n",
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200821 event->header.type);
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200822 return -1;
823 }
824
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300825 if (evsel->handler != NULL) {
826 tracepoint_handler f = evsel->handler;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300827 err = f(evsel, sample);
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300828 }
829
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300830 thread__put(thread);
831
832 return err;
Frederic Weisbeckerc61e52e2010-04-24 00:04:12 +0200833}
834
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900835static void sort_result(void)
836{
837 unsigned int i;
838 struct lock_stat *st;
839
840 for (i = 0; i < LOCKHASH_SIZE; i++) {
841 list_for_each_entry(st, &lockhash_table[i], hash_entry) {
842 insert_to_result(st, compare);
843 }
844 }
845}
846
Jiri Olsa32dcd022019-07-21 13:23:51 +0200847static const struct evsel_str_handler lock_tracepoints[] = {
Arnaldo Carvalho de Melo3d655812020-05-04 13:56:18 -0300848 { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */
849 { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
850 { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
851 { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700852};
853
Yunlong Songc4ac7322015-04-02 21:47:14 +0800854static bool force;
855
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700856static int __cmd_report(bool display_info)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900857{
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700858 int err = -EINVAL;
859 struct perf_tool eops = {
860 .sample = process_sample_event,
861 .comm = perf_event__process_comm,
Hari Bathinif3b36142017-03-08 02:11:43 +0530862 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200863 .ordered_events = true,
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700864 };
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100865 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +0100866 .path = input_name,
867 .mode = PERF_DATA_MODE_READ,
868 .force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200869 };
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700870
Namhyung Kim2681bd82021-07-19 15:31:49 -0700871 session = perf_session__new(&data, &eops);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +0530872 if (IS_ERR(session)) {
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700873 pr_err("Initializing perf session failed\n");
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +0530874 return PTR_ERR(session);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700875 }
876
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900877 symbol__init(&session->header.env);
Namhyung Kim6fd6c6b2014-08-12 15:40:40 +0900878
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700879 if (!perf_session__has_traces(session, "lock record"))
880 goto out_delete;
881
882 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
883 pr_err("Initializing perf session tracepoint handlers failed\n");
884 goto out_delete;
885 }
886
887 if (select_key())
888 goto out_delete;
889
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300890 err = perf_session__process_events(session);
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700891 if (err)
892 goto out_delete;
893
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900894 setup_pager();
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700895 if (display_info) /* used for info subcommand */
896 err = dump_info();
897 else {
898 sort_result();
899 print_result();
900 }
David Ahern33d6aef2012-08-26 12:24:43 -0600901
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -0700902out_delete:
903 perf_session__delete(session);
904 return err;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900905}
906
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900907static int __cmd_record(int argc, const char **argv)
908{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300909 const char *record_args[] = {
Jiri Olsa4a4d3712013-06-05 13:37:21 +0200910 "record", "-R", "-m", "1024", "-c", "1",
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300911 };
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700912 unsigned int rec_argc, i, j, ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900913 const char **rec_argv;
914
David Ahernd25dcba2012-08-09 10:35:37 -0600915 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300916 if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
David Ahernd25dcba2012-08-09 10:35:37 -0600917 pr_err("tracepoint %s is not enabled. "
918 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300919 lock_tracepoints[i].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600920 return 1;
921 }
922 }
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900923
David Ahernd25dcba2012-08-09 10:35:37 -0600924 rec_argc = ARRAY_SIZE(record_args) + argc - 1;
925 /* factor of 2 is for -e in front of each tracepoint */
926 rec_argc += 2 * ARRAY_SIZE(lock_tracepoints);
927
928 rec_argv = calloc(rec_argc + 1, sizeof(char *));
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700929 if (!rec_argv)
Chris Samuelce47dc52010-11-13 13:35:06 +1100930 return -ENOMEM;
931
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900932 for (i = 0; i < ARRAY_SIZE(record_args); i++)
933 rec_argv[i] = strdup(record_args[i]);
934
David Ahernd25dcba2012-08-09 10:35:37 -0600935 for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) {
936 rec_argv[i++] = "-e";
Arnaldo Carvalho de Melo746f16e2012-09-24 10:52:12 -0300937 rec_argv[i++] = strdup(lock_tracepoints[j].name);
David Ahernd25dcba2012-08-09 10:35:37 -0600938 }
939
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900940 for (j = 1; j < (unsigned int)argc; j++, i++)
941 rec_argv[i] = argv[j];
942
943 BUG_ON(i != rec_argc);
944
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300945 ret = cmd_record(i, rec_argv);
Davidlohr Bueso0a98c7f2013-09-08 19:19:15 -0700946 free(rec_argv);
947 return ret;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900948}
949
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300950int cmd_lock(int argc, const char **argv)
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900951{
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300952 const struct option lock_options[] = {
953 OPT_STRING('i', "input", &input_name, "file", "input file name"),
954 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
955 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
Arnaldo Carvalho de Melob40e3612017-03-17 11:16:02 -0300956 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300957 OPT_END()
958 };
Changbin Du249eed52017-03-17 13:53:42 +0800959
960 const struct option info_options[] = {
961 OPT_BOOLEAN('t', "threads", &info_threads,
962 "dump thread list in perf.data"),
963 OPT_BOOLEAN('m', "map", &info_map,
964 "map of lock instances (address:name table)"),
Changbin Du249eed52017-03-17 13:53:42 +0800965 OPT_PARENT(lock_options)
966 };
967
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300968 const struct option report_options[] = {
969 OPT_STRING('k', "key", &sort_key, "acquired",
Davidlohr Buesof37376c2013-09-08 19:19:19 -0700970 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300971 /* TODO: type */
Changbin Du249eed52017-03-17 13:53:42 +0800972 OPT_PARENT(lock_options)
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300973 };
Changbin Du249eed52017-03-17 13:53:42 +0800974
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300975 const char * const info_usage[] = {
976 "perf lock info [<options>]",
977 NULL
978 };
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400979 const char *const lock_subcommands[] = { "record", "report", "script",
980 "info", NULL };
981 const char *lock_usage[] = {
982 NULL,
Arnaldo Carvalho de Meloc75d98a2012-10-01 15:20:58 -0300983 NULL
984 };
985 const char * const report_usage[] = {
986 "perf lock report [<options>]",
987 NULL
988 };
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900989 unsigned int i;
David Ahern33d6aef2012-08-26 12:24:43 -0600990 int rc = 0;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900991
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900992 for (i = 0; i < LOCKHASH_SIZE; i++)
993 INIT_LIST_HEAD(lockhash_table + i);
994
Ramkumar Ramachandraa2368c32014-03-14 23:17:53 -0400995 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
996 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +0900997 if (!argc)
998 usage_with_options(lock_usage, lock_options);
999
1000 if (!strncmp(argv[0], "rec", 3)) {
1001 return __cmd_record(argc, argv);
Ingo Molnar59f411b2010-01-31 08:27:58 +01001002 } else if (!strncmp(argv[0], "report", 6)) {
1003 trace_handler = &report_lock_ops;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001004 if (argc) {
1005 argc = parse_options(argc, argv,
Ingo Molnar59f411b2010-01-31 08:27:58 +01001006 report_options, report_usage, 0);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001007 if (argc)
Ingo Molnar59f411b2010-01-31 08:27:58 +01001008 usage_with_options(report_usage, report_options);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001009 }
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001010 rc = __cmd_report(false);
Ingo Molnar133dc4c2010-11-16 18:45:39 +01001011 } else if (!strcmp(argv[0], "script")) {
1012 /* Aliased to 'perf script' */
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03001013 return cmd_script(argc, argv);
Hitoshi Mitake26242d82010-05-03 14:12:00 +09001014 } else if (!strcmp(argv[0], "info")) {
1015 if (argc) {
1016 argc = parse_options(argc, argv,
1017 info_options, info_usage, 0);
1018 if (argc)
1019 usage_with_options(info_usage, info_options);
1020 }
Ingo Molnar59f411b2010-01-31 08:27:58 +01001021 /* recycling report_lock_ops */
1022 trace_handler = &report_lock_ops;
Davidlohr Bueso375eb2b2013-09-08 19:19:16 -07001023 rc = __cmd_report(true);
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001024 } else {
1025 usage_with_options(lock_usage, lock_options);
1026 }
1027
David Ahern33d6aef2012-08-26 12:24:43 -06001028 return rc;
Hitoshi Mitake9b5e3502010-01-30 20:43:33 +09001029}