]>
Commit | Line | Data |
---|---|---|
9b5e350c HM |
1 | #include "builtin.h" |
2 | #include "perf.h" | |
3 | ||
746f16ec | 4 | #include "util/evlist.h" |
fcf65bf1 | 5 | #include "util/evsel.h" |
9b5e350c HM |
6 | #include "util/util.h" |
7 | #include "util/cache.h" | |
8 | #include "util/symbol.h" | |
9 | #include "util/thread.h" | |
10 | #include "util/header.h" | |
11 | ||
12 | #include "util/parse-options.h" | |
13 | #include "util/trace-event.h" | |
14 | ||
15 | #include "util/debug.h" | |
16 | #include "util/session.h" | |
45694aa7 | 17 | #include "util/tool.h" |
f5fc1412 | 18 | #include "util/data.h" |
9b5e350c HM |
19 | |
20 | #include <sys/types.h> | |
21 | #include <sys/prctl.h> | |
22 | #include <semaphore.h> | |
23 | #include <pthread.h> | |
24 | #include <math.h> | |
25 | #include <limits.h> | |
26 | ||
27 | #include <linux/list.h> | |
28 | #include <linux/hash.h> | |
29 | ||
e4cef1f6 HM |
30 | static struct perf_session *session; |
31 | ||
9b5e350c HM |
32 | /* based on kernel/lockdep.c */ |
33 | #define LOCKHASH_BITS 12 | |
34 | #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS) | |
35 | ||
36 | static struct list_head lockhash_table[LOCKHASH_SIZE]; | |
37 | ||
38 | #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS) | |
39 | #define lockhashentry(key) (lockhash_table + __lockhashfn((key))) | |
40 | ||
9b5e350c | 41 | struct lock_stat { |
59f411b6 IM |
42 | struct list_head hash_entry; |
43 | struct rb_node rb; /* used for sorting */ | |
9b5e350c | 44 | |
59f411b6 | 45 | /* |
746f16ec | 46 | * FIXME: perf_evsel__intval() returns u64, |
9b5e350c | 47 | * so address of lockdep_map should be dealed as 64bit. |
59f411b6 IM |
48 | * Is there more better solution? |
49 | */ | |
50 | void *addr; /* address of lockdep_map, used as ID */ | |
51 | char *name; /* for strcpy(), we cannot use const */ | |
9b5e350c | 52 | |
59f411b6 | 53 | unsigned int nr_acquire; |
e4cef1f6 | 54 | unsigned int nr_acquired; |
59f411b6 IM |
55 | unsigned int nr_contended; |
56 | unsigned int nr_release; | |
9b5e350c | 57 | |
e4cef1f6 HM |
58 | unsigned int nr_readlock; |
59 | unsigned int nr_trylock; | |
f37376cd | 60 | |
9b5e350c | 61 | /* these times are in nano sec. */ |
f37376cd | 62 | u64 avg_wait_time; |
59f411b6 IM |
63 | u64 wait_time_total; |
64 | u64 wait_time_min; | |
65 | u64 wait_time_max; | |
e4cef1f6 HM |
66 | |
67 | int discard; /* flag of blacklist */ | |
68 | }; | |
69 | ||
70 | /* | |
71 | * States of lock_seq_stat | |
72 | * | |
73 | * UNINITIALIZED is required for detecting first event of acquire. | |
74 | * As the nature of lock events, there is no guarantee | |
75 | * that the first event for the locks are acquire, | |
76 | * it can be acquired, contended or release. | |
77 | */ | |
78 | #define SEQ_STATE_UNINITIALIZED 0 /* initial state */ | |
79 | #define SEQ_STATE_RELEASED 1 | |
80 | #define SEQ_STATE_ACQUIRING 2 | |
81 | #define SEQ_STATE_ACQUIRED 3 | |
82 | #define SEQ_STATE_READ_ACQUIRED 4 | |
83 | #define SEQ_STATE_CONTENDED 5 | |
84 | ||
85 | /* | |
86 | * MAX_LOCK_DEPTH | |
87 | * Imported from include/linux/sched.h. | |
88 | * Should this be synchronized? | |
89 | */ | |
90 | #define MAX_LOCK_DEPTH 48 | |
91 | ||
92 | /* | |
93 | * struct lock_seq_stat: | |
94 | * Place to put on state of one lock sequence | |
95 | * 1) acquire -> acquired -> release | |
96 | * 2) acquire -> contended -> acquired -> release | |
97 | * 3) acquire (with read or try) -> release | |
98 | * 4) Are there other patterns? | |
99 | */ | |
100 | struct lock_seq_stat { | |
101 | struct list_head list; | |
102 | int state; | |
103 | u64 prev_event_time; | |
104 | void *addr; | |
105 | ||
106 | int read_count; | |
9b5e350c HM |
107 | }; |
108 | ||
e4cef1f6 HM |
109 | struct thread_stat { |
110 | struct rb_node rb; | |
111 | ||
112 | u32 tid; | |
113 | struct list_head seq_list; | |
114 | }; | |
115 | ||
116 | static struct rb_root thread_stats; | |
117 | ||
118 | static struct thread_stat *thread_stat_find(u32 tid) | |
119 | { | |
120 | struct rb_node *node; | |
121 | struct thread_stat *st; | |
122 | ||
123 | node = thread_stats.rb_node; | |
124 | while (node) { | |
125 | st = container_of(node, struct thread_stat, rb); | |
126 | if (st->tid == tid) | |
127 | return st; | |
128 | else if (tid < st->tid) | |
129 | node = node->rb_left; | |
130 | else | |
131 | node = node->rb_right; | |
132 | } | |
133 | ||
134 | return NULL; | |
135 | } | |
136 | ||
137 | static void thread_stat_insert(struct thread_stat *new) | |
138 | { | |
139 | struct rb_node **rb = &thread_stats.rb_node; | |
140 | struct rb_node *parent = NULL; | |
141 | struct thread_stat *p; | |
142 | ||
143 | while (*rb) { | |
144 | p = container_of(*rb, struct thread_stat, rb); | |
145 | parent = *rb; | |
146 | ||
147 | if (new->tid < p->tid) | |
148 | rb = &(*rb)->rb_left; | |
149 | else if (new->tid > p->tid) | |
150 | rb = &(*rb)->rb_right; | |
151 | else | |
152 | BUG_ON("inserting invalid thread_stat\n"); | |
153 | } | |
154 | ||
155 | rb_link_node(&new->rb, parent, rb); | |
156 | rb_insert_color(&new->rb, &thread_stats); | |
157 | } | |
158 | ||
159 | static struct thread_stat *thread_stat_findnew_after_first(u32 tid) | |
160 | { | |
161 | struct thread_stat *st; | |
162 | ||
163 | st = thread_stat_find(tid); | |
164 | if (st) | |
165 | return st; | |
166 | ||
167 | st = zalloc(sizeof(struct thread_stat)); | |
33d6aef5 DA |
168 | if (!st) { |
169 | pr_err("memory allocation failed\n"); | |
170 | return NULL; | |
171 | } | |
e4cef1f6 HM |
172 | |
173 | st->tid = tid; | |
174 | INIT_LIST_HEAD(&st->seq_list); | |
175 | ||
176 | thread_stat_insert(st); | |
177 | ||
178 | return st; | |
179 | } | |
180 | ||
181 | static struct thread_stat *thread_stat_findnew_first(u32 tid); | |
182 | static struct thread_stat *(*thread_stat_findnew)(u32 tid) = | |
183 | thread_stat_findnew_first; | |
184 | ||
185 | static struct thread_stat *thread_stat_findnew_first(u32 tid) | |
186 | { | |
187 | struct thread_stat *st; | |
188 | ||
189 | st = zalloc(sizeof(struct thread_stat)); | |
33d6aef5 DA |
190 | if (!st) { |
191 | pr_err("memory allocation failed\n"); | |
192 | return NULL; | |
193 | } | |
e4cef1f6 HM |
194 | st->tid = tid; |
195 | INIT_LIST_HEAD(&st->seq_list); | |
196 | ||
197 | rb_link_node(&st->rb, NULL, &thread_stats.rb_node); | |
198 | rb_insert_color(&st->rb, &thread_stats); | |
199 | ||
200 | thread_stat_findnew = thread_stat_findnew_after_first; | |
201 | return st; | |
202 | } | |
203 | ||
9b5e350c | 204 | /* build simple key function one is bigger than two */ |
59f411b6 | 205 | #define SINGLE_KEY(member) \ |
9b5e350c HM |
206 | static int lock_stat_key_ ## member(struct lock_stat *one, \ |
207 | struct lock_stat *two) \ | |
208 | { \ | |
209 | return one->member > two->member; \ | |
210 | } | |
211 | ||
212 | SINGLE_KEY(nr_acquired) | |
213 | SINGLE_KEY(nr_contended) | |
f37376cd | 214 | SINGLE_KEY(avg_wait_time) |
9b5e350c | 215 | SINGLE_KEY(wait_time_total) |
9b5e350c HM |
216 | SINGLE_KEY(wait_time_max) |
217 | ||
9df03abe MS |
218 | static int lock_stat_key_wait_time_min(struct lock_stat *one, |
219 | struct lock_stat *two) | |
220 | { | |
221 | u64 s1 = one->wait_time_min; | |
222 | u64 s2 = two->wait_time_min; | |
223 | if (s1 == ULLONG_MAX) | |
224 | s1 = 0; | |
225 | if (s2 == ULLONG_MAX) | |
226 | s2 = 0; | |
227 | return s1 > s2; | |
228 | } | |
229 | ||
9b5e350c HM |
230 | struct lock_key { |
231 | /* | |
232 | * name: the value for specify by user | |
233 | * this should be simpler than raw name of member | |
234 | * e.g. nr_acquired -> acquired, wait_time_total -> wait_total | |
235 | */ | |
59f411b6 IM |
236 | const char *name; |
237 | int (*key)(struct lock_stat*, struct lock_stat*); | |
9b5e350c HM |
238 | }; |
239 | ||
59f411b6 IM |
240 | static const char *sort_key = "acquired"; |
241 | ||
242 | static int (*compare)(struct lock_stat *, struct lock_stat *); | |
243 | ||
244 | static struct rb_root result; /* place to store sorted data */ | |
9b5e350c HM |
245 | |
246 | #define DEF_KEY_LOCK(name, fn_suffix) \ | |
247 | { #name, lock_stat_key_ ## fn_suffix } | |
248 | struct lock_key keys[] = { | |
249 | DEF_KEY_LOCK(acquired, nr_acquired), | |
250 | DEF_KEY_LOCK(contended, nr_contended), | |
f37376cd | 251 | DEF_KEY_LOCK(avg_wait, avg_wait_time), |
9b5e350c HM |
252 | DEF_KEY_LOCK(wait_total, wait_time_total), |
253 | DEF_KEY_LOCK(wait_min, wait_time_min), | |
254 | DEF_KEY_LOCK(wait_max, wait_time_max), | |
255 | ||
256 | /* extra comparisons much complicated should be here */ | |
257 | ||
258 | { NULL, NULL } | |
259 | }; | |
260 | ||
33d6aef5 | 261 | static int select_key(void) |
9b5e350c HM |
262 | { |
263 | int i; | |
264 | ||
265 | for (i = 0; keys[i].name; i++) { | |
266 | if (!strcmp(keys[i].name, sort_key)) { | |
267 | compare = keys[i].key; | |
33d6aef5 | 268 | return 0; |
9b5e350c HM |
269 | } |
270 | } | |
271 | ||
33d6aef5 DA |
272 | pr_err("Unknown compare key: %s\n", sort_key); |
273 | ||
274 | return -1; | |
9b5e350c HM |
275 | } |
276 | ||
9b5e350c | 277 | static void insert_to_result(struct lock_stat *st, |
59f411b6 | 278 | int (*bigger)(struct lock_stat *, struct lock_stat *)) |
9b5e350c HM |
279 | { |
280 | struct rb_node **rb = &result.rb_node; | |
281 | struct rb_node *parent = NULL; | |
282 | struct lock_stat *p; | |
283 | ||
284 | while (*rb) { | |
285 | p = container_of(*rb, struct lock_stat, rb); | |
286 | parent = *rb; | |
287 | ||
288 | if (bigger(st, p)) | |
289 | rb = &(*rb)->rb_left; | |
290 | else | |
291 | rb = &(*rb)->rb_right; | |
292 | } | |
293 | ||
294 | rb_link_node(&st->rb, parent, rb); | |
295 | rb_insert_color(&st->rb, &result); | |
296 | } | |
297 | ||
298 | /* returns left most element of result, and erase it */ | |
299 | static struct lock_stat *pop_from_result(void) | |
300 | { | |
301 | struct rb_node *node = result.rb_node; | |
302 | ||
303 | if (!node) | |
304 | return NULL; | |
305 | ||
306 | while (node->rb_left) | |
307 | node = node->rb_left; | |
308 | ||
309 | rb_erase(node, &result); | |
310 | return container_of(node, struct lock_stat, rb); | |
311 | } | |
312 | ||
59f411b6 | 313 | static struct lock_stat *lock_stat_findnew(void *addr, const char *name) |
9b5e350c HM |
314 | { |
315 | struct list_head *entry = lockhashentry(addr); | |
316 | struct lock_stat *ret, *new; | |
317 | ||
318 | list_for_each_entry(ret, entry, hash_entry) { | |
319 | if (ret->addr == addr) | |
320 | return ret; | |
321 | } | |
322 | ||
323 | new = zalloc(sizeof(struct lock_stat)); | |
324 | if (!new) | |
325 | goto alloc_failed; | |
326 | ||
327 | new->addr = addr; | |
328 | new->name = zalloc(sizeof(char) * strlen(name) + 1); | |
0a98c7fe DB |
329 | if (!new->name) { |
330 | free(new); | |
9b5e350c | 331 | goto alloc_failed; |
0a98c7fe | 332 | } |
9b5e350c | 333 | |
0a98c7fe | 334 | strcpy(new->name, name); |
9b5e350c HM |
335 | new->wait_time_min = ULLONG_MAX; |
336 | ||
337 | list_add(&new->hash_entry, entry); | |
338 | return new; | |
339 | ||
340 | alloc_failed: | |
33d6aef5 DA |
341 | pr_err("memory allocation failed\n"); |
342 | return NULL; | |
9b5e350c HM |
343 | } |
344 | ||
9b5e350c | 345 | struct trace_lock_handler { |
746f16ec ACM |
346 | int (*acquire_event)(struct perf_evsel *evsel, |
347 | struct perf_sample *sample); | |
9b5e350c | 348 | |
746f16ec ACM |
349 | int (*acquired_event)(struct perf_evsel *evsel, |
350 | struct perf_sample *sample); | |
9b5e350c | 351 | |
746f16ec ACM |
352 | int (*contended_event)(struct perf_evsel *evsel, |
353 | struct perf_sample *sample); | |
9b5e350c | 354 | |
746f16ec ACM |
355 | int (*release_event)(struct perf_evsel *evsel, |
356 | struct perf_sample *sample); | |
9b5e350c HM |
357 | }; |
358 | ||
e4cef1f6 HM |
359 | static struct lock_seq_stat *get_seq(struct thread_stat *ts, void *addr) |
360 | { | |
361 | struct lock_seq_stat *seq; | |
362 | ||
363 | list_for_each_entry(seq, &ts->seq_list, list) { | |
364 | if (seq->addr == addr) | |
365 | return seq; | |
366 | } | |
367 | ||
368 | seq = zalloc(sizeof(struct lock_seq_stat)); | |
33d6aef5 DA |
369 | if (!seq) { |
370 | pr_err("memory allocation failed\n"); | |
371 | return NULL; | |
372 | } | |
e4cef1f6 HM |
373 | seq->state = SEQ_STATE_UNINITIALIZED; |
374 | seq->addr = addr; | |
375 | ||
376 | list_add(&seq->list, &ts->seq_list); | |
377 | return seq; | |
378 | } | |
379 | ||
10350ec3 FW |
380 | enum broken_state { |
381 | BROKEN_ACQUIRE, | |
382 | BROKEN_ACQUIRED, | |
383 | BROKEN_CONTENDED, | |
384 | BROKEN_RELEASE, | |
385 | BROKEN_MAX, | |
386 | }; | |
387 | ||
388 | static int bad_hist[BROKEN_MAX]; | |
e4cef1f6 | 389 | |
84c7a217 FW |
390 | enum acquire_flags { |
391 | TRY_LOCK = 1, | |
392 | READ_LOCK = 2, | |
393 | }; | |
394 | ||
746f16ec ACM |
395 | static int report_lock_acquire_event(struct perf_evsel *evsel, |
396 | struct perf_sample *sample) | |
9b5e350c | 397 | { |
746f16ec | 398 | void *addr; |
e4cef1f6 HM |
399 | struct lock_stat *ls; |
400 | struct thread_stat *ts; | |
401 | struct lock_seq_stat *seq; | |
746f16ec ACM |
402 | const char *name = perf_evsel__strval(evsel, sample, "name"); |
403 | u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr"); | |
404 | int flag = perf_evsel__intval(evsel, sample, "flag"); | |
e4cef1f6 | 405 | |
746f16ec ACM |
406 | memcpy(&addr, &tmp, sizeof(void *)); |
407 | ||
408 | ls = lock_stat_findnew(addr, name); | |
33d6aef5 | 409 | if (!ls) |
b33492ad | 410 | return -ENOMEM; |
e4cef1f6 | 411 | if (ls->discard) |
33d6aef5 | 412 | return 0; |
9b5e350c | 413 | |
01d95524 | 414 | ts = thread_stat_findnew(sample->tid); |
33d6aef5 | 415 | if (!ts) |
b33492ad | 416 | return -ENOMEM; |
33d6aef5 | 417 | |
746f16ec | 418 | seq = get_seq(ts, addr); |
33d6aef5 | 419 | if (!seq) |
b33492ad | 420 | return -ENOMEM; |
9b5e350c | 421 | |
e4cef1f6 HM |
422 | switch (seq->state) { |
423 | case SEQ_STATE_UNINITIALIZED: | |
424 | case SEQ_STATE_RELEASED: | |
746f16ec | 425 | if (!flag) { |
e4cef1f6 HM |
426 | seq->state = SEQ_STATE_ACQUIRING; |
427 | } else { | |
746f16ec | 428 | if (flag & TRY_LOCK) |
e4cef1f6 | 429 | ls->nr_trylock++; |
746f16ec | 430 | if (flag & READ_LOCK) |
e4cef1f6 HM |
431 | ls->nr_readlock++; |
432 | seq->state = SEQ_STATE_READ_ACQUIRED; | |
433 | seq->read_count = 1; | |
434 | ls->nr_acquired++; | |
435 | } | |
436 | break; | |
437 | case SEQ_STATE_READ_ACQUIRED: | |
746f16ec | 438 | if (flag & READ_LOCK) { |
e4cef1f6 HM |
439 | seq->read_count++; |
440 | ls->nr_acquired++; | |
441 | goto end; | |
442 | } else { | |
443 | goto broken; | |
444 | } | |
9b5e350c | 445 | break; |
e4cef1f6 HM |
446 | case SEQ_STATE_ACQUIRED: |
447 | case SEQ_STATE_ACQUIRING: | |
448 | case SEQ_STATE_CONTENDED: | |
449 | broken: | |
450 | /* broken lock sequence, discard it */ | |
451 | ls->discard = 1; | |
10350ec3 | 452 | bad_hist[BROKEN_ACQUIRE]++; |
e4cef1f6 HM |
453 | list_del(&seq->list); |
454 | free(seq); | |
455 | goto end; | |
9b5e350c | 456 | default: |
e4cef1f6 | 457 | BUG_ON("Unknown state of lock sequence found!\n"); |
9b5e350c HM |
458 | break; |
459 | } | |
460 | ||
e4cef1f6 | 461 | ls->nr_acquire++; |
01d95524 | 462 | seq->prev_event_time = sample->time; |
e4cef1f6 | 463 | end: |
33d6aef5 | 464 | return 0; |
9b5e350c HM |
465 | } |
466 | ||
746f16ec ACM |
467 | static int report_lock_acquired_event(struct perf_evsel *evsel, |
468 | struct perf_sample *sample) | |
9b5e350c | 469 | { |
746f16ec | 470 | void *addr; |
e4cef1f6 HM |
471 | struct lock_stat *ls; |
472 | struct thread_stat *ts; | |
473 | struct lock_seq_stat *seq; | |
474 | u64 contended_term; | |
746f16ec ACM |
475 | const char *name = perf_evsel__strval(evsel, sample, "name"); |
476 | u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr"); | |
477 | ||
478 | memcpy(&addr, &tmp, sizeof(void *)); | |
9b5e350c | 479 | |
746f16ec | 480 | ls = lock_stat_findnew(addr, name); |
33d6aef5 | 481 | if (!ls) |
b33492ad | 482 | return -ENOMEM; |
e4cef1f6 | 483 | if (ls->discard) |
33d6aef5 | 484 | return 0; |
e4cef1f6 | 485 | |
01d95524 | 486 | ts = thread_stat_findnew(sample->tid); |
33d6aef5 | 487 | if (!ts) |
b33492ad | 488 | return -ENOMEM; |
33d6aef5 | 489 | |
746f16ec | 490 | seq = get_seq(ts, addr); |
33d6aef5 | 491 | if (!seq) |
b33492ad | 492 | return -ENOMEM; |
9b5e350c | 493 | |
e4cef1f6 HM |
494 | switch (seq->state) { |
495 | case SEQ_STATE_UNINITIALIZED: | |
496 | /* orphan event, do nothing */ | |
33d6aef5 | 497 | return 0; |
e4cef1f6 | 498 | case SEQ_STATE_ACQUIRING: |
9b5e350c | 499 | break; |
e4cef1f6 | 500 | case SEQ_STATE_CONTENDED: |
746f16ec | 501 | contended_term = sample->time - seq->prev_event_time; |
e4cef1f6 | 502 | ls->wait_time_total += contended_term; |
e4cef1f6 HM |
503 | if (contended_term < ls->wait_time_min) |
504 | ls->wait_time_min = contended_term; | |
90c0e5fc | 505 | if (ls->wait_time_max < contended_term) |
e4cef1f6 | 506 | ls->wait_time_max = contended_term; |
9b5e350c | 507 | break; |
e4cef1f6 HM |
508 | case SEQ_STATE_RELEASED: |
509 | case SEQ_STATE_ACQUIRED: | |
510 | case SEQ_STATE_READ_ACQUIRED: | |
511 | /* broken lock sequence, discard it */ | |
512 | ls->discard = 1; | |
10350ec3 | 513 | bad_hist[BROKEN_ACQUIRED]++; |
e4cef1f6 HM |
514 | list_del(&seq->list); |
515 | free(seq); | |
516 | goto end; | |
9b5e350c | 517 | default: |
e4cef1f6 | 518 | BUG_ON("Unknown state of lock sequence found!\n"); |
9b5e350c HM |
519 | break; |
520 | } | |
521 | ||
e4cef1f6 HM |
522 | seq->state = SEQ_STATE_ACQUIRED; |
523 | ls->nr_acquired++; | |
f37376cd | 524 | ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0; |
746f16ec | 525 | seq->prev_event_time = sample->time; |
e4cef1f6 | 526 | end: |
33d6aef5 | 527 | return 0; |
9b5e350c HM |
528 | } |
529 | ||
746f16ec ACM |
530 | static int report_lock_contended_event(struct perf_evsel *evsel, |
531 | struct perf_sample *sample) | |
9b5e350c | 532 | { |
746f16ec | 533 | void *addr; |
e4cef1f6 HM |
534 | struct lock_stat *ls; |
535 | struct thread_stat *ts; | |
536 | struct lock_seq_stat *seq; | |
746f16ec ACM |
537 | const char *name = perf_evsel__strval(evsel, sample, "name"); |
538 | u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr"); | |
539 | ||
540 | memcpy(&addr, &tmp, sizeof(void *)); | |
e4cef1f6 | 541 | |
746f16ec | 542 | ls = lock_stat_findnew(addr, name); |
33d6aef5 | 543 | if (!ls) |
b33492ad | 544 | return -ENOMEM; |
e4cef1f6 | 545 | if (ls->discard) |
33d6aef5 | 546 | return 0; |
9b5e350c | 547 | |
01d95524 | 548 | ts = thread_stat_findnew(sample->tid); |
33d6aef5 | 549 | if (!ts) |
b33492ad | 550 | return -ENOMEM; |
33d6aef5 | 551 | |
746f16ec | 552 | seq = get_seq(ts, addr); |
33d6aef5 | 553 | if (!seq) |
b33492ad | 554 | return -ENOMEM; |
9b5e350c | 555 | |
e4cef1f6 HM |
556 | switch (seq->state) { |
557 | case SEQ_STATE_UNINITIALIZED: | |
558 | /* orphan event, do nothing */ | |
33d6aef5 | 559 | return 0; |
e4cef1f6 | 560 | case SEQ_STATE_ACQUIRING: |
9b5e350c | 561 | break; |
e4cef1f6 HM |
562 | case SEQ_STATE_RELEASED: |
563 | case SEQ_STATE_ACQUIRED: | |
564 | case SEQ_STATE_READ_ACQUIRED: | |
565 | case SEQ_STATE_CONTENDED: | |
566 | /* broken lock sequence, discard it */ | |
567 | ls->discard = 1; | |
10350ec3 | 568 | bad_hist[BROKEN_CONTENDED]++; |
e4cef1f6 HM |
569 | list_del(&seq->list); |
570 | free(seq); | |
571 | goto end; | |
9b5e350c | 572 | default: |
e4cef1f6 | 573 | BUG_ON("Unknown state of lock sequence found!\n"); |
9b5e350c HM |
574 | break; |
575 | } | |
576 | ||
e4cef1f6 HM |
577 | seq->state = SEQ_STATE_CONTENDED; |
578 | ls->nr_contended++; | |
f37376cd | 579 | ls->avg_wait_time = ls->wait_time_total/ls->nr_contended; |
01d95524 | 580 | seq->prev_event_time = sample->time; |
e4cef1f6 | 581 | end: |
33d6aef5 | 582 | return 0; |
9b5e350c HM |
583 | } |
584 | ||
746f16ec ACM |
585 | static int report_lock_release_event(struct perf_evsel *evsel, |
586 | struct perf_sample *sample) | |
9b5e350c | 587 | { |
746f16ec | 588 | void *addr; |
e4cef1f6 HM |
589 | struct lock_stat *ls; |
590 | struct thread_stat *ts; | |
591 | struct lock_seq_stat *seq; | |
746f16ec ACM |
592 | const char *name = perf_evsel__strval(evsel, sample, "name"); |
593 | u64 tmp = perf_evsel__intval(evsel, sample, "lockdep_addr"); | |
9b5e350c | 594 | |
746f16ec ACM |
595 | memcpy(&addr, &tmp, sizeof(void *)); |
596 | ||
597 | ls = lock_stat_findnew(addr, name); | |
33d6aef5 | 598 | if (!ls) |
b33492ad | 599 | return -ENOMEM; |
e4cef1f6 | 600 | if (ls->discard) |
33d6aef5 | 601 | return 0; |
9b5e350c | 602 | |
01d95524 | 603 | ts = thread_stat_findnew(sample->tid); |
33d6aef5 | 604 | if (!ts) |
b33492ad | 605 | return -ENOMEM; |
33d6aef5 | 606 | |
746f16ec | 607 | seq = get_seq(ts, addr); |
33d6aef5 | 608 | if (!seq) |
b33492ad | 609 | return -ENOMEM; |
9b5e350c | 610 | |
e4cef1f6 HM |
611 | switch (seq->state) { |
612 | case SEQ_STATE_UNINITIALIZED: | |
613 | goto end; | |
e4cef1f6 HM |
614 | case SEQ_STATE_ACQUIRED: |
615 | break; | |
616 | case SEQ_STATE_READ_ACQUIRED: | |
617 | seq->read_count--; | |
618 | BUG_ON(seq->read_count < 0); | |
619 | if (!seq->read_count) { | |
620 | ls->nr_release++; | |
9b5e350c HM |
621 | goto end; |
622 | } | |
e4cef1f6 HM |
623 | break; |
624 | case SEQ_STATE_ACQUIRING: | |
625 | case SEQ_STATE_CONTENDED: | |
626 | case SEQ_STATE_RELEASED: | |
627 | /* broken lock sequence, discard it */ | |
628 | ls->discard = 1; | |
10350ec3 | 629 | bad_hist[BROKEN_RELEASE]++; |
e4cef1f6 | 630 | goto free_seq; |
9b5e350c | 631 | default: |
e4cef1f6 | 632 | BUG_ON("Unknown state of lock sequence found!\n"); |
9b5e350c HM |
633 | break; |
634 | } | |
635 | ||
e4cef1f6 HM |
636 | ls->nr_release++; |
637 | free_seq: | |
638 | list_del(&seq->list); | |
639 | free(seq); | |
9b5e350c | 640 | end: |
33d6aef5 | 641 | return 0; |
9b5e350c HM |
642 | } |
643 | ||
644 | /* lock oriented handlers */ | |
645 | /* TODO: handlers for CPU oriented, thread oriented */ | |
59f411b6 IM |
646 | static struct trace_lock_handler report_lock_ops = { |
647 | .acquire_event = report_lock_acquire_event, | |
648 | .acquired_event = report_lock_acquired_event, | |
649 | .contended_event = report_lock_contended_event, | |
650 | .release_event = report_lock_release_event, | |
9b5e350c HM |
651 | }; |
652 | ||
653 | static struct trace_lock_handler *trace_handler; | |
654 | ||
33d6aef5 | 655 | static int perf_evsel__process_lock_acquire(struct perf_evsel *evsel, |
01d95524 | 656 | struct perf_sample *sample) |
9b5e350c | 657 | { |
59f411b6 | 658 | if (trace_handler->acquire_event) |
746f16ec ACM |
659 | return trace_handler->acquire_event(evsel, sample); |
660 | return 0; | |
9b5e350c HM |
661 | } |
662 | ||
33d6aef5 | 663 | static int perf_evsel__process_lock_acquired(struct perf_evsel *evsel, |
01d95524 | 664 | struct perf_sample *sample) |
9b5e350c | 665 | { |
33d6aef5 | 666 | if (trace_handler->acquired_event) |
746f16ec ACM |
667 | return trace_handler->acquired_event(evsel, sample); |
668 | return 0; | |
9b5e350c HM |
669 | } |
670 | ||
33d6aef5 | 671 | static int perf_evsel__process_lock_contended(struct perf_evsel *evsel, |
746f16ec | 672 | struct perf_sample *sample) |
9b5e350c | 673 | { |
33d6aef5 | 674 | if (trace_handler->contended_event) |
746f16ec ACM |
675 | return trace_handler->contended_event(evsel, sample); |
676 | return 0; | |
9b5e350c HM |
677 | } |
678 | ||
33d6aef5 | 679 | static int perf_evsel__process_lock_release(struct perf_evsel *evsel, |
746f16ec | 680 | struct perf_sample *sample) |
9b5e350c | 681 | { |
33d6aef5 | 682 | if (trace_handler->release_event) |
746f16ec ACM |
683 | return trace_handler->release_event(evsel, sample); |
684 | return 0; | |
9b5e350c HM |
685 | } |
686 | ||
10350ec3 FW |
687 | static void print_bad_events(int bad, int total) |
688 | { | |
689 | /* Output for debug, this have to be removed */ | |
690 | int i; | |
691 | const char *name[4] = | |
692 | { "acquire", "acquired", "contended", "release" }; | |
693 | ||
694 | pr_info("\n=== output for debug===\n\n"); | |
5efe08cf | 695 | pr_info("bad: %d, total: %d\n", bad, total); |
60a25cbc | 696 | pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100); |
10350ec3 FW |
697 | pr_info("histogram of events caused bad sequence\n"); |
698 | for (i = 0; i < BROKEN_MAX; i++) | |
699 | pr_info(" %10s: %d\n", name[i], bad_hist[i]); | |
700 | } | |
701 | ||
9b5e350c HM |
702 | /* TODO: various way to print, coloring, nano or milli sec */ |
703 | static void print_result(void) | |
704 | { | |
705 | struct lock_stat *st; | |
706 | char cut_name[20]; | |
e4cef1f6 | 707 | int bad, total; |
9b5e350c | 708 | |
26242d85 HM |
709 | pr_info("%20s ", "Name"); |
710 | pr_info("%10s ", "acquired"); | |
711 | pr_info("%10s ", "contended"); | |
9b5e350c | 712 | |
f37376cd | 713 | pr_info("%15s ", "avg wait (ns)"); |
26242d85 HM |
714 | pr_info("%15s ", "total wait (ns)"); |
715 | pr_info("%15s ", "max wait (ns)"); | |
716 | pr_info("%15s ", "min wait (ns)"); | |
9b5e350c | 717 | |
26242d85 | 718 | pr_info("\n\n"); |
9b5e350c | 719 | |
e4cef1f6 | 720 | bad = total = 0; |
9b5e350c | 721 | while ((st = pop_from_result())) { |
e4cef1f6 HM |
722 | total++; |
723 | if (st->discard) { | |
724 | bad++; | |
725 | continue; | |
726 | } | |
9b5e350c HM |
727 | bzero(cut_name, 20); |
728 | ||
9b5e350c HM |
729 | if (strlen(st->name) < 16) { |
730 | /* output raw name */ | |
26242d85 | 731 | pr_info("%20s ", st->name); |
9b5e350c HM |
732 | } else { |
733 | strncpy(cut_name, st->name, 16); | |
734 | cut_name[16] = '.'; | |
735 | cut_name[17] = '.'; | |
736 | cut_name[18] = '.'; | |
737 | cut_name[19] = '\0'; | |
738 | /* cut off name for saving output style */ | |
26242d85 | 739 | pr_info("%20s ", cut_name); |
9b5e350c HM |
740 | } |
741 | ||
26242d85 HM |
742 | pr_info("%10u ", st->nr_acquired); |
743 | pr_info("%10u ", st->nr_contended); | |
9b5e350c | 744 | |
f37376cd | 745 | pr_info("%15" PRIu64 " ", st->avg_wait_time); |
9486aa38 ACM |
746 | pr_info("%15" PRIu64 " ", st->wait_time_total); |
747 | pr_info("%15" PRIu64 " ", st->wait_time_max); | |
748 | pr_info("%15" PRIu64 " ", st->wait_time_min == ULLONG_MAX ? | |
9b5e350c | 749 | 0 : st->wait_time_min); |
26242d85 | 750 | pr_info("\n"); |
9b5e350c | 751 | } |
e4cef1f6 | 752 | |
10350ec3 | 753 | print_bad_events(bad, total); |
9b5e350c HM |
754 | } |
755 | ||
8035458f | 756 | static bool info_threads, info_map; |
26242d85 HM |
757 | |
758 | static void dump_threads(void) | |
759 | { | |
760 | struct thread_stat *st; | |
761 | struct rb_node *node; | |
762 | struct thread *t; | |
763 | ||
764 | pr_info("%10s: comm\n", "Thread ID"); | |
765 | ||
766 | node = rb_first(&thread_stats); | |
767 | while (node) { | |
768 | st = container_of(node, struct thread_stat, rb); | |
769 | t = perf_session__findnew(session, st->tid); | |
b9c5143a | 770 | pr_info("%10d: %s\n", st->tid, thread__comm_str(t)); |
26242d85 | 771 | node = rb_next(node); |
b91fc39f | 772 | thread__put(t); |
26242d85 HM |
773 | }; |
774 | } | |
775 | ||
9b5e350c HM |
776 | static void dump_map(void) |
777 | { | |
778 | unsigned int i; | |
779 | struct lock_stat *st; | |
780 | ||
26242d85 | 781 | pr_info("Address of instance: name of class\n"); |
9b5e350c HM |
782 | for (i = 0; i < LOCKHASH_SIZE; i++) { |
783 | list_for_each_entry(st, &lockhash_table[i], hash_entry) { | |
26242d85 | 784 | pr_info(" %p: %s\n", st->addr, st->name); |
9b5e350c HM |
785 | } |
786 | } | |
787 | } | |
788 | ||
33d6aef5 | 789 | static int dump_info(void) |
26242d85 | 790 | { |
33d6aef5 DA |
791 | int rc = 0; |
792 | ||
26242d85 HM |
793 | if (info_threads) |
794 | dump_threads(); | |
795 | else if (info_map) | |
796 | dump_map(); | |
33d6aef5 DA |
797 | else { |
798 | rc = -1; | |
799 | pr_err("Unknown type of information\n"); | |
800 | } | |
801 | ||
802 | return rc; | |
26242d85 HM |
803 | } |
804 | ||
746f16ec ACM |
805 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
806 | struct perf_sample *sample); | |
807 | ||
1d037ca1 | 808 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 809 | union perf_event *event, |
9e69c210 | 810 | struct perf_sample *sample, |
fcf65bf1 | 811 | struct perf_evsel *evsel, |
743eb868 | 812 | struct machine *machine) |
c61e52ee | 813 | { |
b91fc39f | 814 | int err = 0; |
314add6b AH |
815 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
816 | sample->tid); | |
c61e52ee | 817 | |
c61e52ee FW |
818 | if (thread == NULL) { |
819 | pr_debug("problem processing %d event, skipping it.\n", | |
8115d60c | 820 | event->header.type); |
c61e52ee FW |
821 | return -1; |
822 | } | |
823 | ||
744a9719 ACM |
824 | if (evsel->handler != NULL) { |
825 | tracepoint_handler f = evsel->handler; | |
b91fc39f | 826 | err = f(evsel, sample); |
746f16ec ACM |
827 | } |
828 | ||
b91fc39f ACM |
829 | thread__put(thread); |
830 | ||
831 | return err; | |
c61e52ee FW |
832 | } |
833 | ||
375eb2be DB |
834 | static void sort_result(void) |
835 | { | |
836 | unsigned int i; | |
837 | struct lock_stat *st; | |
838 | ||
839 | for (i = 0; i < LOCKHASH_SIZE; i++) { | |
840 | list_for_each_entry(st, &lockhash_table[i], hash_entry) { | |
841 | insert_to_result(st, compare); | |
842 | } | |
843 | } | |
844 | } | |
845 | ||
746f16ec ACM |
846 | static const struct perf_evsel_str_handler lock_tracepoints[] = { |
847 | { "lock:lock_acquire", perf_evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */ | |
848 | { "lock:lock_acquired", perf_evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */ | |
849 | { "lock:lock_contended", perf_evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */ | |
850 | { "lock:lock_release", perf_evsel__process_lock_release, }, /* CONFIG_LOCKDEP */ | |
851 | }; | |
852 | ||
c4ac732a YS |
853 | static bool force; |
854 | ||
375eb2be | 855 | static int __cmd_report(bool display_info) |
9b5e350c | 856 | { |
375eb2be | 857 | int err = -EINVAL; |
c75d98af ACM |
858 | struct perf_tool eops = { |
859 | .sample = process_sample_event, | |
860 | .comm = perf_event__process_comm, | |
0a8cb85c | 861 | .ordered_events = true, |
c75d98af | 862 | }; |
f5fc1412 JO |
863 | struct perf_data_file file = { |
864 | .path = input_name, | |
865 | .mode = PERF_DATA_MODE_READ, | |
c4ac732a | 866 | .force = force, |
f5fc1412 | 867 | }; |
375eb2be | 868 | |
f5fc1412 | 869 | session = perf_session__new(&file, false, &eops); |
33d6aef5 DA |
870 | if (!session) { |
871 | pr_err("Initializing perf session failed\n"); | |
52e02834 | 872 | return -1; |
33d6aef5 | 873 | } |
9b5e350c | 874 | |
0a7e6d1b | 875 | symbol__init(&session->header.env); |
6fd6c6b4 | 876 | |
375eb2be DB |
877 | if (!perf_session__has_traces(session, "lock record")) |
878 | goto out_delete; | |
879 | ||
746f16ec ACM |
880 | if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) { |
881 | pr_err("Initializing perf session tracepoint handlers failed\n"); | |
375eb2be | 882 | goto out_delete; |
746f16ec ACM |
883 | } |
884 | ||
375eb2be DB |
885 | if (select_key()) |
886 | goto out_delete; | |
9b5e350c | 887 | |
b7b61cbe | 888 | err = perf_session__process_events(session); |
375eb2be DB |
889 | if (err) |
890 | goto out_delete; | |
9b5e350c | 891 | |
9b5e350c | 892 | setup_pager(); |
375eb2be DB |
893 | if (display_info) /* used for info subcommand */ |
894 | err = dump_info(); | |
895 | else { | |
896 | sort_result(); | |
897 | print_result(); | |
898 | } | |
33d6aef5 | 899 | |
375eb2be DB |
900 | out_delete: |
901 | perf_session__delete(session); | |
902 | return err; | |
9b5e350c HM |
903 | } |
904 | ||
9b5e350c HM |
905 | static int __cmd_record(int argc, const char **argv) |
906 | { | |
c75d98af | 907 | const char *record_args[] = { |
4a4d371a | 908 | "record", "-R", "-m", "1024", "-c", "1", |
c75d98af | 909 | }; |
0a98c7fe | 910 | unsigned int rec_argc, i, j, ret; |
9b5e350c HM |
911 | const char **rec_argv; |
912 | ||
d25dcba8 | 913 | for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) { |
746f16ec | 914 | if (!is_valid_tracepoint(lock_tracepoints[i].name)) { |
d25dcba8 DA |
915 | pr_err("tracepoint %s is not enabled. " |
916 | "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n", | |
746f16ec | 917 | lock_tracepoints[i].name); |
d25dcba8 DA |
918 | return 1; |
919 | } | |
920 | } | |
921 | ||
9b5e350c | 922 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; |
d25dcba8 DA |
923 | /* factor of 2 is for -e in front of each tracepoint */ |
924 | rec_argc += 2 * ARRAY_SIZE(lock_tracepoints); | |
9b5e350c | 925 | |
d25dcba8 | 926 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
0a98c7fe | 927 | if (!rec_argv) |
ce47dc56 CS |
928 | return -ENOMEM; |
929 | ||
9b5e350c HM |
930 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
931 | rec_argv[i] = strdup(record_args[i]); | |
932 | ||
d25dcba8 DA |
933 | for (j = 0; j < ARRAY_SIZE(lock_tracepoints); j++) { |
934 | rec_argv[i++] = "-e"; | |
746f16ec | 935 | rec_argv[i++] = strdup(lock_tracepoints[j].name); |
d25dcba8 DA |
936 | } |
937 | ||
9b5e350c HM |
938 | for (j = 1; j < (unsigned int)argc; j++, i++) |
939 | rec_argv[i] = argv[j]; | |
940 | ||
941 | BUG_ON(i != rec_argc); | |
942 | ||
0a98c7fe DB |
943 | ret = cmd_record(i, rec_argv, NULL); |
944 | free(rec_argv); | |
945 | return ret; | |
9b5e350c HM |
946 | } |
947 | ||
1d037ca1 | 948 | int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused) |
9b5e350c | 949 | { |
c75d98af ACM |
950 | const struct option info_options[] = { |
951 | OPT_BOOLEAN('t', "threads", &info_threads, | |
952 | "dump thread list in perf.data"), | |
953 | OPT_BOOLEAN('m', "map", &info_map, | |
954 | "map of lock instances (address:name table)"), | |
c4ac732a | 955 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
c75d98af ACM |
956 | OPT_END() |
957 | }; | |
958 | const struct option lock_options[] = { | |
959 | OPT_STRING('i', "input", &input_name, "file", "input file name"), | |
960 | OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"), | |
961 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"), | |
962 | OPT_END() | |
963 | }; | |
964 | const struct option report_options[] = { | |
965 | OPT_STRING('k', "key", &sort_key, "acquired", | |
f37376cd | 966 | "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"), |
c4ac732a | 967 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
c75d98af ACM |
968 | /* TODO: type */ |
969 | OPT_END() | |
970 | }; | |
971 | const char * const info_usage[] = { | |
972 | "perf lock info [<options>]", | |
973 | NULL | |
974 | }; | |
a2368c31 RR |
975 | const char *const lock_subcommands[] = { "record", "report", "script", |
976 | "info", NULL }; | |
977 | const char *lock_usage[] = { | |
978 | NULL, | |
c75d98af ACM |
979 | NULL |
980 | }; | |
981 | const char * const report_usage[] = { | |
982 | "perf lock report [<options>]", | |
983 | NULL | |
984 | }; | |
9b5e350c | 985 | unsigned int i; |
33d6aef5 | 986 | int rc = 0; |
9b5e350c | 987 | |
9b5e350c HM |
988 | for (i = 0; i < LOCKHASH_SIZE; i++) |
989 | INIT_LIST_HEAD(lockhash_table + i); | |
990 | ||
a2368c31 RR |
991 | argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands, |
992 | lock_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
9b5e350c HM |
993 | if (!argc) |
994 | usage_with_options(lock_usage, lock_options); | |
995 | ||
996 | if (!strncmp(argv[0], "rec", 3)) { | |
997 | return __cmd_record(argc, argv); | |
59f411b6 IM |
998 | } else if (!strncmp(argv[0], "report", 6)) { |
999 | trace_handler = &report_lock_ops; | |
9b5e350c HM |
1000 | if (argc) { |
1001 | argc = parse_options(argc, argv, | |
59f411b6 | 1002 | report_options, report_usage, 0); |
9b5e350c | 1003 | if (argc) |
59f411b6 | 1004 | usage_with_options(report_usage, report_options); |
9b5e350c | 1005 | } |
375eb2be | 1006 | rc = __cmd_report(false); |
133dc4c3 IM |
1007 | } else if (!strcmp(argv[0], "script")) { |
1008 | /* Aliased to 'perf script' */ | |
1009 | return cmd_script(argc, argv, prefix); | |
26242d85 HM |
1010 | } else if (!strcmp(argv[0], "info")) { |
1011 | if (argc) { | |
1012 | argc = parse_options(argc, argv, | |
1013 | info_options, info_usage, 0); | |
1014 | if (argc) | |
1015 | usage_with_options(info_usage, info_options); | |
1016 | } | |
59f411b6 IM |
1017 | /* recycling report_lock_ops */ |
1018 | trace_handler = &report_lock_ops; | |
375eb2be | 1019 | rc = __cmd_report(true); |
9b5e350c HM |
1020 | } else { |
1021 | usage_with_options(lock_usage, lock_options); | |
1022 | } | |
1023 | ||
33d6aef5 | 1024 | return rc; |
9b5e350c | 1025 | } |