1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include "builtin.h" 5 #include "perf.h" 6 7 #include "util/evlist.h" // for struct evsel_str_handler 8 #include "util/evsel.h" 9 #include "util/symbol.h" 10 #include "util/thread.h" 11 #include "util/header.h" 12 #include "util/target.h" 13 #include "util/callchain.h" 14 #include "util/lock-contention.h" 15 16 #include <subcmd/pager.h> 17 #include <subcmd/parse-options.h> 18 #include "util/trace-event.h" 19 #include "util/tracepoint.h" 20 21 #include "util/debug.h" 22 #include "util/session.h" 23 #include "util/tool.h" 24 #include "util/data.h" 25 #include "util/string2.h" 26 #include "util/map.h" 27 #include "util/util.h" 28 29 #include <sys/types.h> 30 #include <sys/prctl.h> 31 #include <semaphore.h> 32 #include <math.h> 33 #include <limits.h> 34 35 #include <linux/list.h> 36 #include <linux/hash.h> 37 #include <linux/kernel.h> 38 #include <linux/zalloc.h> 39 #include <linux/err.h> 40 #include <linux/stringify.h> 41 42 static struct perf_session *session; 43 static struct target target; 44 45 /* based on kernel/lockdep.c */ 46 #define LOCKHASH_BITS 12 47 #define LOCKHASH_SIZE (1UL << LOCKHASH_BITS) 48 49 static struct hlist_head lockhash_table[LOCKHASH_SIZE]; 50 51 #define __lockhashfn(key) hash_long((unsigned long)key, LOCKHASH_BITS) 52 #define lockhashentry(key) (lockhash_table + __lockhashfn((key))) 53 54 static struct rb_root thread_stats; 55 56 static bool combine_locks; 57 static bool show_thread_stats; 58 static bool use_bpf; 59 static unsigned long bpf_map_entries = 10240; 60 static int max_stack_depth = CONTENTION_STACK_DEPTH; 61 static int stack_skip = CONTENTION_STACK_SKIP; 62 static int print_nr_entries = INT_MAX / 2; 63 64 static enum { 65 LOCK_AGGR_ADDR, 66 LOCK_AGGR_TASK, 67 LOCK_AGGR_CALLER, 68 } aggr_mode = LOCK_AGGR_ADDR; 69 70 static struct thread_stat *thread_stat_find(u32 tid) 71 { 72 struct rb_node *node; 73 struct thread_stat *st; 74 75 node = thread_stats.rb_node; 76 while (node) { 77 st = container_of(node, struct thread_stat, rb); 78 if (st->tid == tid) 79 return st; 80 else if (tid < st->tid) 81 node = node->rb_left; 82 else 83 node = node->rb_right; 84 } 85 86 return NULL; 87 } 88 89 static void thread_stat_insert(struct thread_stat *new) 90 { 91 struct rb_node **rb = &thread_stats.rb_node; 92 struct rb_node *parent = NULL; 93 struct thread_stat *p; 94 95 while (*rb) { 96 p = container_of(*rb, struct thread_stat, rb); 97 parent = *rb; 98 99 if (new->tid < p->tid) 100 rb = &(*rb)->rb_left; 101 else if (new->tid > p->tid) 102 rb = &(*rb)->rb_right; 103 else 104 BUG_ON("inserting invalid thread_stat\n"); 105 } 106 107 rb_link_node(&new->rb, parent, rb); 108 rb_insert_color(&new->rb, &thread_stats); 109 } 110 111 static struct thread_stat *thread_stat_findnew_after_first(u32 tid) 112 { 113 struct thread_stat *st; 114 115 st = thread_stat_find(tid); 116 if (st) 117 return st; 118 119 st = zalloc(sizeof(struct thread_stat)); 120 if (!st) { 121 pr_err("memory allocation failed\n"); 122 return NULL; 123 } 124 125 st->tid = tid; 126 INIT_LIST_HEAD(&st->seq_list); 127 128 thread_stat_insert(st); 129 130 return st; 131 } 132 133 static struct thread_stat *thread_stat_findnew_first(u32 tid); 134 static struct thread_stat *(*thread_stat_findnew)(u32 tid) = 135 thread_stat_findnew_first; 136 137 static struct thread_stat *thread_stat_findnew_first(u32 tid) 138 { 139 struct thread_stat *st; 140 141 st = zalloc(sizeof(struct thread_stat)); 142 if (!st) { 143 pr_err("memory allocation failed\n"); 144 return NULL; 145 } 146 st->tid = tid; 147 INIT_LIST_HEAD(&st->seq_list); 148 149 rb_link_node(&st->rb, NULL, &thread_stats.rb_node); 150 rb_insert_color(&st->rb, &thread_stats); 151 152 thread_stat_findnew = thread_stat_findnew_after_first; 153 return st; 154 } 155 156 /* build simple key function one is bigger than two */ 157 #define SINGLE_KEY(member) \ 158 static int lock_stat_key_ ## member(struct lock_stat *one, \ 159 struct lock_stat *two) \ 160 { \ 161 return one->member > two->member; \ 162 } 163 164 SINGLE_KEY(nr_acquired) 165 SINGLE_KEY(nr_contended) 166 SINGLE_KEY(avg_wait_time) 167 SINGLE_KEY(wait_time_total) 168 SINGLE_KEY(wait_time_max) 169 170 static int lock_stat_key_wait_time_min(struct lock_stat *one, 171 struct lock_stat *two) 172 { 173 u64 s1 = one->wait_time_min; 174 u64 s2 = two->wait_time_min; 175 if (s1 == ULLONG_MAX) 176 s1 = 0; 177 if (s2 == ULLONG_MAX) 178 s2 = 0; 179 return s1 > s2; 180 } 181 182 struct lock_key { 183 /* 184 * name: the value for specify by user 185 * this should be simpler than raw name of member 186 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total 187 */ 188 const char *name; 189 /* header: the string printed on the header line */ 190 const char *header; 191 /* len: the printing width of the field */ 192 int len; 193 /* key: a pointer to function to compare two lock stats for sorting */ 194 int (*key)(struct lock_stat*, struct lock_stat*); 195 /* print: a pointer to function to print a given lock stats */ 196 void (*print)(struct lock_key*, struct lock_stat*); 197 /* list: list entry to link this */ 198 struct list_head list; 199 }; 200 201 static void lock_stat_key_print_time(unsigned long long nsec, int len) 202 { 203 static const struct { 204 float base; 205 const char *unit; 206 } table[] = { 207 { 1e9 * 3600, "h " }, 208 { 1e9 * 60, "m " }, 209 { 1e9, "s " }, 210 { 1e6, "ms" }, 211 { 1e3, "us" }, 212 { 0, NULL }, 213 }; 214 215 for (int i = 0; table[i].unit; i++) { 216 if (nsec < table[i].base) 217 continue; 218 219 pr_info("%*.2f %s", len - 3, nsec / table[i].base, table[i].unit); 220 return; 221 } 222 223 pr_info("%*llu %s", len - 3, nsec, "ns"); 224 } 225 226 #define PRINT_KEY(member) \ 227 static void lock_stat_key_print_ ## member(struct lock_key *key, \ 228 struct lock_stat *ls) \ 229 { \ 230 pr_info("%*llu", key->len, (unsigned long long)ls->member); \ 231 } 232 233 #define PRINT_TIME(member) \ 234 static void lock_stat_key_print_ ## member(struct lock_key *key, \ 235 struct lock_stat *ls) \ 236 { \ 237 lock_stat_key_print_time((unsigned long long)ls->member, key->len); \ 238 } 239 240 PRINT_KEY(nr_acquired) 241 PRINT_KEY(nr_contended) 242 PRINT_TIME(avg_wait_time) 243 PRINT_TIME(wait_time_total) 244 PRINT_TIME(wait_time_max) 245 246 static void lock_stat_key_print_wait_time_min(struct lock_key *key, 247 struct lock_stat *ls) 248 { 249 u64 wait_time = ls->wait_time_min; 250 251 if (wait_time == ULLONG_MAX) 252 wait_time = 0; 253 254 lock_stat_key_print_time(wait_time, key->len); 255 } 256 257 258 static const char *sort_key = "acquired"; 259 260 static int (*compare)(struct lock_stat *, struct lock_stat *); 261 262 static struct rb_root sorted; /* place to store intermediate data */ 263 static struct rb_root result; /* place to store sorted data */ 264 265 static LIST_HEAD(lock_keys); 266 static const char *output_fields; 267 268 #define DEF_KEY_LOCK(name, header, fn_suffix, len) \ 269 { #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} } 270 static struct lock_key report_keys[] = { 271 DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10), 272 DEF_KEY_LOCK(contended, "contended", nr_contended, 10), 273 DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12), 274 DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12), 275 DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12), 276 DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12), 277 278 /* extra comparisons much complicated should be here */ 279 { } 280 }; 281 282 static struct lock_key contention_keys[] = { 283 DEF_KEY_LOCK(contended, "contended", nr_contended, 10), 284 DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12), 285 DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12), 286 DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12), 287 DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12), 288 289 /* extra comparisons much complicated should be here */ 290 { } 291 }; 292 293 static int select_key(bool contention) 294 { 295 int i; 296 struct lock_key *keys = report_keys; 297 298 if (contention) 299 keys = contention_keys; 300 301 for (i = 0; keys[i].name; i++) { 302 if (!strcmp(keys[i].name, sort_key)) { 303 compare = keys[i].key; 304 305 /* selected key should be in the output fields */ 306 if (list_empty(&keys[i].list)) 307 list_add_tail(&keys[i].list, &lock_keys); 308 309 return 0; 310 } 311 } 312 313 pr_err("Unknown compare key: %s\n", sort_key); 314 return -1; 315 } 316 317 static int add_output_field(bool contention, char *name) 318 { 319 int i; 320 struct lock_key *keys = report_keys; 321 322 if (contention) 323 keys = contention_keys; 324 325 for (i = 0; keys[i].name; i++) { 326 if (strcmp(keys[i].name, name)) 327 continue; 328 329 /* prevent double link */ 330 if (list_empty(&keys[i].list)) 331 list_add_tail(&keys[i].list, &lock_keys); 332 333 return 0; 334 } 335 336 pr_err("Unknown output field: %s\n", name); 337 return -1; 338 } 339 340 static int setup_output_field(bool contention, const char *str) 341 { 342 char *tok, *tmp, *orig; 343 int i, ret = 0; 344 struct lock_key *keys = report_keys; 345 346 if (contention) 347 keys = contention_keys; 348 349 /* no output field given: use all of them */ 350 if (str == NULL) { 351 for (i = 0; keys[i].name; i++) 352 list_add_tail(&keys[i].list, &lock_keys); 353 return 0; 354 } 355 356 for (i = 0; keys[i].name; i++) 357 INIT_LIST_HEAD(&keys[i].list); 358 359 orig = tmp = strdup(str); 360 if (orig == NULL) 361 return -ENOMEM; 362 363 while ((tok = strsep(&tmp, ",")) != NULL){ 364 ret = add_output_field(contention, tok); 365 if (ret < 0) 366 break; 367 } 368 free(orig); 369 370 return ret; 371 } 372 373 static void combine_lock_stats(struct lock_stat *st) 374 { 375 struct rb_node **rb = &sorted.rb_node; 376 struct rb_node *parent = NULL; 377 struct lock_stat *p; 378 int ret; 379 380 while (*rb) { 381 p = container_of(*rb, struct lock_stat, rb); 382 parent = *rb; 383 384 if (st->name && p->name) 385 ret = strcmp(st->name, p->name); 386 else 387 ret = !!st->name - !!p->name; 388 389 if (ret == 0) { 390 p->nr_acquired += st->nr_acquired; 391 p->nr_contended += st->nr_contended; 392 p->wait_time_total += st->wait_time_total; 393 394 if (p->nr_contended) 395 p->avg_wait_time = p->wait_time_total / p->nr_contended; 396 397 if (p->wait_time_min > st->wait_time_min) 398 p->wait_time_min = st->wait_time_min; 399 if (p->wait_time_max < st->wait_time_max) 400 p->wait_time_max = st->wait_time_max; 401 402 p->broken |= st->broken; 403 st->combined = 1; 404 return; 405 } 406 407 if (ret < 0) 408 rb = &(*rb)->rb_left; 409 else 410 rb = &(*rb)->rb_right; 411 } 412 413 rb_link_node(&st->rb, parent, rb); 414 rb_insert_color(&st->rb, &sorted); 415 } 416 417 static void insert_to_result(struct lock_stat *st, 418 int (*bigger)(struct lock_stat *, struct lock_stat *)) 419 { 420 struct rb_node **rb = &result.rb_node; 421 struct rb_node *parent = NULL; 422 struct lock_stat *p; 423 424 if (combine_locks && st->combined) 425 return; 426 427 while (*rb) { 428 p = container_of(*rb, struct lock_stat, rb); 429 parent = *rb; 430 431 if (bigger(st, p)) 432 rb = &(*rb)->rb_left; 433 else 434 rb = &(*rb)->rb_right; 435 } 436 437 rb_link_node(&st->rb, parent, rb); 438 rb_insert_color(&st->rb, &result); 439 } 440 441 /* returns left most element of result, and erase it */ 442 static struct lock_stat *pop_from_result(void) 443 { 444 struct rb_node *node = result.rb_node; 445 446 if (!node) 447 return NULL; 448 449 while (node->rb_left) 450 node = node->rb_left; 451 452 rb_erase(node, &result); 453 return container_of(node, struct lock_stat, rb); 454 } 455 456 static struct lock_stat *lock_stat_find(u64 addr) 457 { 458 struct hlist_head *entry = lockhashentry(addr); 459 struct lock_stat *ret; 460 461 hlist_for_each_entry(ret, entry, hash_entry) { 462 if (ret->addr == addr) 463 return ret; 464 } 465 return NULL; 466 } 467 468 static struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags) 469 { 470 struct hlist_head *entry = lockhashentry(addr); 471 struct lock_stat *ret, *new; 472 473 hlist_for_each_entry(ret, entry, hash_entry) { 474 if (ret->addr == addr) 475 return ret; 476 } 477 478 new = zalloc(sizeof(struct lock_stat)); 479 if (!new) 480 goto alloc_failed; 481 482 new->addr = addr; 483 new->name = strdup(name); 484 if (!new->name) { 485 free(new); 486 goto alloc_failed; 487 } 488 489 new->flags = flags; 490 new->wait_time_min = ULLONG_MAX; 491 492 hlist_add_head(&new->hash_entry, entry); 493 return new; 494 495 alloc_failed: 496 pr_err("memory allocation failed\n"); 497 return NULL; 498 } 499 500 struct trace_lock_handler { 501 /* it's used on CONFIG_LOCKDEP */ 502 int (*acquire_event)(struct evsel *evsel, 503 struct perf_sample *sample); 504 505 /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */ 506 int (*acquired_event)(struct evsel *evsel, 507 struct perf_sample *sample); 508 509 /* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */ 510 int (*contended_event)(struct evsel *evsel, 511 struct perf_sample *sample); 512 513 /* it's used on CONFIG_LOCKDEP */ 514 int (*release_event)(struct evsel *evsel, 515 struct perf_sample *sample); 516 517 /* it's used when CONFIG_LOCKDEP is off */ 518 int (*contention_begin_event)(struct evsel *evsel, 519 struct perf_sample *sample); 520 521 /* it's used when CONFIG_LOCKDEP is off */ 522 int (*contention_end_event)(struct evsel *evsel, 523 struct perf_sample *sample); 524 }; 525 526 static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr) 527 { 528 struct lock_seq_stat *seq; 529 530 list_for_each_entry(seq, &ts->seq_list, list) { 531 if (seq->addr == addr) 532 return seq; 533 } 534 535 seq = zalloc(sizeof(struct lock_seq_stat)); 536 if (!seq) { 537 pr_err("memory allocation failed\n"); 538 return NULL; 539 } 540 seq->state = SEQ_STATE_UNINITIALIZED; 541 seq->addr = addr; 542 543 list_add(&seq->list, &ts->seq_list); 544 return seq; 545 } 546 547 enum broken_state { 548 BROKEN_ACQUIRE, 549 BROKEN_ACQUIRED, 550 BROKEN_CONTENDED, 551 BROKEN_RELEASE, 552 BROKEN_MAX, 553 }; 554 555 static int bad_hist[BROKEN_MAX]; 556 557 enum acquire_flags { 558 TRY_LOCK = 1, 559 READ_LOCK = 2, 560 }; 561 562 static int get_key_by_aggr_mode_simple(u64 *key, u64 addr, u32 tid) 563 { 564 switch (aggr_mode) { 565 case LOCK_AGGR_ADDR: 566 *key = addr; 567 break; 568 case LOCK_AGGR_TASK: 569 *key = tid; 570 break; 571 case LOCK_AGGR_CALLER: 572 default: 573 pr_err("Invalid aggregation mode: %d\n", aggr_mode); 574 return -EINVAL; 575 } 576 return 0; 577 } 578 579 static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample); 580 581 static int get_key_by_aggr_mode(u64 *key, u64 addr, struct evsel *evsel, 582 struct perf_sample *sample) 583 { 584 if (aggr_mode == LOCK_AGGR_CALLER) { 585 *key = callchain_id(evsel, sample); 586 return 0; 587 } 588 return get_key_by_aggr_mode_simple(key, addr, sample->tid); 589 } 590 591 static int report_lock_acquire_event(struct evsel *evsel, 592 struct perf_sample *sample) 593 { 594 struct lock_stat *ls; 595 struct thread_stat *ts; 596 struct lock_seq_stat *seq; 597 const char *name = evsel__strval(evsel, sample, "name"); 598 u64 addr = evsel__intval(evsel, sample, "lockdep_addr"); 599 int flag = evsel__intval(evsel, sample, "flags"); 600 u64 key; 601 int ret; 602 603 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid); 604 if (ret < 0) 605 return ret; 606 607 ls = lock_stat_findnew(key, name, 0); 608 if (!ls) 609 return -ENOMEM; 610 611 ts = thread_stat_findnew(sample->tid); 612 if (!ts) 613 return -ENOMEM; 614 615 seq = get_seq(ts, addr); 616 if (!seq) 617 return -ENOMEM; 618 619 switch (seq->state) { 620 case SEQ_STATE_UNINITIALIZED: 621 case SEQ_STATE_RELEASED: 622 if (!flag) { 623 seq->state = SEQ_STATE_ACQUIRING; 624 } else { 625 if (flag & TRY_LOCK) 626 ls->nr_trylock++; 627 if (flag & READ_LOCK) 628 ls->nr_readlock++; 629 seq->state = SEQ_STATE_READ_ACQUIRED; 630 seq->read_count = 1; 631 ls->nr_acquired++; 632 } 633 break; 634 case SEQ_STATE_READ_ACQUIRED: 635 if (flag & READ_LOCK) { 636 seq->read_count++; 637 ls->nr_acquired++; 638 goto end; 639 } else { 640 goto broken; 641 } 642 break; 643 case SEQ_STATE_ACQUIRED: 644 case SEQ_STATE_ACQUIRING: 645 case SEQ_STATE_CONTENDED: 646 broken: 647 /* broken lock sequence */ 648 if (!ls->broken) { 649 ls->broken = 1; 650 bad_hist[BROKEN_ACQUIRE]++; 651 } 652 list_del_init(&seq->list); 653 free(seq); 654 goto end; 655 default: 656 BUG_ON("Unknown state of lock sequence found!\n"); 657 break; 658 } 659 660 ls->nr_acquire++; 661 seq->prev_event_time = sample->time; 662 end: 663 return 0; 664 } 665 666 static int report_lock_acquired_event(struct evsel *evsel, 667 struct perf_sample *sample) 668 { 669 struct lock_stat *ls; 670 struct thread_stat *ts; 671 struct lock_seq_stat *seq; 672 u64 contended_term; 673 const char *name = evsel__strval(evsel, sample, "name"); 674 u64 addr = evsel__intval(evsel, sample, "lockdep_addr"); 675 u64 key; 676 int ret; 677 678 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid); 679 if (ret < 0) 680 return ret; 681 682 ls = lock_stat_findnew(key, name, 0); 683 if (!ls) 684 return -ENOMEM; 685 686 ts = thread_stat_findnew(sample->tid); 687 if (!ts) 688 return -ENOMEM; 689 690 seq = get_seq(ts, addr); 691 if (!seq) 692 return -ENOMEM; 693 694 switch (seq->state) { 695 case SEQ_STATE_UNINITIALIZED: 696 /* orphan event, do nothing */ 697 return 0; 698 case SEQ_STATE_ACQUIRING: 699 break; 700 case SEQ_STATE_CONTENDED: 701 contended_term = sample->time - seq->prev_event_time; 702 ls->wait_time_total += contended_term; 703 if (contended_term < ls->wait_time_min) 704 ls->wait_time_min = contended_term; 705 if (ls->wait_time_max < contended_term) 706 ls->wait_time_max = contended_term; 707 break; 708 case SEQ_STATE_RELEASED: 709 case SEQ_STATE_ACQUIRED: 710 case SEQ_STATE_READ_ACQUIRED: 711 /* broken lock sequence */ 712 if (!ls->broken) { 713 ls->broken = 1; 714 bad_hist[BROKEN_ACQUIRED]++; 715 } 716 list_del_init(&seq->list); 717 free(seq); 718 goto end; 719 default: 720 BUG_ON("Unknown state of lock sequence found!\n"); 721 break; 722 } 723 724 seq->state = SEQ_STATE_ACQUIRED; 725 ls->nr_acquired++; 726 ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0; 727 seq->prev_event_time = sample->time; 728 end: 729 return 0; 730 } 731 732 static int report_lock_contended_event(struct evsel *evsel, 733 struct perf_sample *sample) 734 { 735 struct lock_stat *ls; 736 struct thread_stat *ts; 737 struct lock_seq_stat *seq; 738 const char *name = evsel__strval(evsel, sample, "name"); 739 u64 addr = evsel__intval(evsel, sample, "lockdep_addr"); 740 u64 key; 741 int ret; 742 743 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid); 744 if (ret < 0) 745 return ret; 746 747 ls = lock_stat_findnew(key, name, 0); 748 if (!ls) 749 return -ENOMEM; 750 751 ts = thread_stat_findnew(sample->tid); 752 if (!ts) 753 return -ENOMEM; 754 755 seq = get_seq(ts, addr); 756 if (!seq) 757 return -ENOMEM; 758 759 switch (seq->state) { 760 case SEQ_STATE_UNINITIALIZED: 761 /* orphan event, do nothing */ 762 return 0; 763 case SEQ_STATE_ACQUIRING: 764 break; 765 case SEQ_STATE_RELEASED: 766 case SEQ_STATE_ACQUIRED: 767 case SEQ_STATE_READ_ACQUIRED: 768 case SEQ_STATE_CONTENDED: 769 /* broken lock sequence */ 770 if (!ls->broken) { 771 ls->broken = 1; 772 bad_hist[BROKEN_CONTENDED]++; 773 } 774 list_del_init(&seq->list); 775 free(seq); 776 goto end; 777 default: 778 BUG_ON("Unknown state of lock sequence found!\n"); 779 break; 780 } 781 782 seq->state = SEQ_STATE_CONTENDED; 783 ls->nr_contended++; 784 ls->avg_wait_time = ls->wait_time_total/ls->nr_contended; 785 seq->prev_event_time = sample->time; 786 end: 787 return 0; 788 } 789 790 static int report_lock_release_event(struct evsel *evsel, 791 struct perf_sample *sample) 792 { 793 struct lock_stat *ls; 794 struct thread_stat *ts; 795 struct lock_seq_stat *seq; 796 const char *name = evsel__strval(evsel, sample, "name"); 797 u64 addr = evsel__intval(evsel, sample, "lockdep_addr"); 798 u64 key; 799 int ret; 800 801 ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid); 802 if (ret < 0) 803 return ret; 804 805 ls = lock_stat_findnew(key, name, 0); 806 if (!ls) 807 return -ENOMEM; 808 809 ts = thread_stat_findnew(sample->tid); 810 if (!ts) 811 return -ENOMEM; 812 813 seq = get_seq(ts, addr); 814 if (!seq) 815 return -ENOMEM; 816 817 switch (seq->state) { 818 case SEQ_STATE_UNINITIALIZED: 819 goto end; 820 case SEQ_STATE_ACQUIRED: 821 break; 822 case SEQ_STATE_READ_ACQUIRED: 823 seq->read_count--; 824 BUG_ON(seq->read_count < 0); 825 if (seq->read_count) { 826 ls->nr_release++; 827 goto end; 828 } 829 break; 830 case SEQ_STATE_ACQUIRING: 831 case SEQ_STATE_CONTENDED: 832 case SEQ_STATE_RELEASED: 833 /* broken lock sequence */ 834 if (!ls->broken) { 835 ls->broken = 1; 836 bad_hist[BROKEN_RELEASE]++; 837 } 838 goto free_seq; 839 default: 840 BUG_ON("Unknown state of lock sequence found!\n"); 841 break; 842 } 843 844 ls->nr_release++; 845 free_seq: 846 list_del_init(&seq->list); 847 free(seq); 848 end: 849 return 0; 850 } 851 852 static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip, 853 char *buf, int size) 854 { 855 u64 offset; 856 857 if (map == NULL || sym == NULL) { 858 buf[0] = '\0'; 859 return 0; 860 } 861 862 offset = map->map_ip(map, ip) - sym->start; 863 864 if (offset) 865 return scnprintf(buf, size, "%s+%#lx", sym->name, offset); 866 else 867 return strlcpy(buf, sym->name, size); 868 } 869 static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sample, 870 char *buf, int size) 871 { 872 struct thread *thread; 873 struct callchain_cursor *cursor = &callchain_cursor; 874 struct machine *machine = &session->machines.host; 875 struct symbol *sym; 876 int skip = 0; 877 int ret; 878 879 /* lock names will be replaced to task name later */ 880 if (show_thread_stats) 881 return -1; 882 883 thread = machine__findnew_thread(machine, -1, sample->pid); 884 if (thread == NULL) 885 return -1; 886 887 /* use caller function name from the callchain */ 888 ret = thread__resolve_callchain(thread, cursor, evsel, sample, 889 NULL, NULL, max_stack_depth); 890 if (ret != 0) { 891 thread__put(thread); 892 return -1; 893 } 894 895 callchain_cursor_commit(cursor); 896 thread__put(thread); 897 898 while (true) { 899 struct callchain_cursor_node *node; 900 901 node = callchain_cursor_current(cursor); 902 if (node == NULL) 903 break; 904 905 /* skip first few entries - for lock functions */ 906 if (++skip <= stack_skip) 907 goto next; 908 909 sym = node->ms.sym; 910 if (sym && !machine__is_lock_function(machine, node->ip)) { 911 get_symbol_name_offset(node->ms.map, sym, node->ip, 912 buf, size); 913 return 0; 914 } 915 916 next: 917 callchain_cursor_advance(cursor); 918 } 919 return -1; 920 } 921 922 static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample) 923 { 924 struct callchain_cursor *cursor = &callchain_cursor; 925 struct machine *machine = &session->machines.host; 926 struct thread *thread; 927 u64 hash = 0; 928 int skip = 0; 929 int ret; 930 931 thread = machine__findnew_thread(machine, -1, sample->pid); 932 if (thread == NULL) 933 return -1; 934 935 /* use caller function name from the callchain */ 936 ret = thread__resolve_callchain(thread, cursor, evsel, sample, 937 NULL, NULL, max_stack_depth); 938 thread__put(thread); 939 940 if (ret != 0) 941 return -1; 942 943 callchain_cursor_commit(cursor); 944 945 while (true) { 946 struct callchain_cursor_node *node; 947 948 node = callchain_cursor_current(cursor); 949 if (node == NULL) 950 break; 951 952 /* skip first few entries - for lock functions */ 953 if (++skip <= stack_skip) 954 goto next; 955 956 if (node->ms.sym && machine__is_lock_function(machine, node->ip)) 957 goto next; 958 959 hash ^= hash_long((unsigned long)node->ip, 64); 960 961 next: 962 callchain_cursor_advance(cursor); 963 } 964 return hash; 965 } 966 967 static u64 *get_callstack(struct perf_sample *sample, int max_stack) 968 { 969 u64 *callstack; 970 u64 i; 971 int c; 972 973 callstack = calloc(max_stack, sizeof(*callstack)); 974 if (callstack == NULL) 975 return NULL; 976 977 for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) { 978 u64 ip = sample->callchain->ips[i]; 979 980 if (ip >= PERF_CONTEXT_MAX) 981 continue; 982 983 callstack[c++] = ip; 984 } 985 return callstack; 986 } 987 988 static int report_lock_contention_begin_event(struct evsel *evsel, 989 struct perf_sample *sample) 990 { 991 struct lock_stat *ls; 992 struct thread_stat *ts; 993 struct lock_seq_stat *seq; 994 u64 addr = evsel__intval(evsel, sample, "lock_addr"); 995 u64 key; 996 int ret; 997 998 ret = get_key_by_aggr_mode(&key, addr, evsel, sample); 999 if (ret < 0) 1000 return ret; 1001 1002 ls = lock_stat_find(key); 1003 if (!ls) { 1004 char buf[128]; 1005 const char *caller = buf; 1006 unsigned int flags = evsel__intval(evsel, sample, "flags"); 1007 1008 if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0) 1009 caller = "Unknown"; 1010 1011 ls = lock_stat_findnew(key, caller, flags); 1012 if (!ls) 1013 return -ENOMEM; 1014 1015 if (aggr_mode == LOCK_AGGR_CALLER && verbose) { 1016 ls->callstack = get_callstack(sample, max_stack_depth); 1017 if (ls->callstack == NULL) 1018 return -ENOMEM; 1019 } 1020 } 1021 1022 ts = thread_stat_findnew(sample->tid); 1023 if (!ts) 1024 return -ENOMEM; 1025 1026 seq = get_seq(ts, addr); 1027 if (!seq) 1028 return -ENOMEM; 1029 1030 switch (seq->state) { 1031 case SEQ_STATE_UNINITIALIZED: 1032 case SEQ_STATE_ACQUIRED: 1033 break; 1034 case SEQ_STATE_CONTENDED: 1035 /* 1036 * It can have nested contention begin with mutex spinning, 1037 * then we would use the original contention begin event and 1038 * ignore the second one. 1039 */ 1040 goto end; 1041 case SEQ_STATE_ACQUIRING: 1042 case SEQ_STATE_READ_ACQUIRED: 1043 case SEQ_STATE_RELEASED: 1044 /* broken lock sequence */ 1045 if (!ls->broken) { 1046 ls->broken = 1; 1047 bad_hist[BROKEN_CONTENDED]++; 1048 } 1049 list_del_init(&seq->list); 1050 free(seq); 1051 goto end; 1052 default: 1053 BUG_ON("Unknown state of lock sequence found!\n"); 1054 break; 1055 } 1056 1057 if (seq->state != SEQ_STATE_CONTENDED) { 1058 seq->state = SEQ_STATE_CONTENDED; 1059 seq->prev_event_time = sample->time; 1060 ls->nr_contended++; 1061 } 1062 end: 1063 return 0; 1064 } 1065 1066 static int report_lock_contention_end_event(struct evsel *evsel, 1067 struct perf_sample *sample) 1068 { 1069 struct lock_stat *ls; 1070 struct thread_stat *ts; 1071 struct lock_seq_stat *seq; 1072 u64 contended_term; 1073 u64 addr = evsel__intval(evsel, sample, "lock_addr"); 1074 u64 key; 1075 int ret; 1076 1077 ret = get_key_by_aggr_mode(&key, addr, evsel, sample); 1078 if (ret < 0) 1079 return ret; 1080 1081 ls = lock_stat_find(key); 1082 if (!ls) 1083 return 0; 1084 1085 ts = thread_stat_find(sample->tid); 1086 if (!ts) 1087 return 0; 1088 1089 seq = get_seq(ts, addr); 1090 if (!seq) 1091 return -ENOMEM; 1092 1093 switch (seq->state) { 1094 case SEQ_STATE_UNINITIALIZED: 1095 goto end; 1096 case SEQ_STATE_CONTENDED: 1097 contended_term = sample->time - seq->prev_event_time; 1098 ls->wait_time_total += contended_term; 1099 if (contended_term < ls->wait_time_min) 1100 ls->wait_time_min = contended_term; 1101 if (ls->wait_time_max < contended_term) 1102 ls->wait_time_max = contended_term; 1103 break; 1104 case SEQ_STATE_ACQUIRING: 1105 case SEQ_STATE_ACQUIRED: 1106 case SEQ_STATE_READ_ACQUIRED: 1107 case SEQ_STATE_RELEASED: 1108 /* broken lock sequence */ 1109 if (!ls->broken) { 1110 ls->broken = 1; 1111 bad_hist[BROKEN_ACQUIRED]++; 1112 } 1113 list_del_init(&seq->list); 1114 free(seq); 1115 goto end; 1116 default: 1117 BUG_ON("Unknown state of lock sequence found!\n"); 1118 break; 1119 } 1120 1121 seq->state = SEQ_STATE_ACQUIRED; 1122 ls->nr_acquired++; 1123 ls->avg_wait_time = ls->wait_time_total/ls->nr_acquired; 1124 end: 1125 return 0; 1126 } 1127 1128 /* lock oriented handlers */ 1129 /* TODO: handlers for CPU oriented, thread oriented */ 1130 static struct trace_lock_handler report_lock_ops = { 1131 .acquire_event = report_lock_acquire_event, 1132 .acquired_event = report_lock_acquired_event, 1133 .contended_event = report_lock_contended_event, 1134 .release_event = report_lock_release_event, 1135 .contention_begin_event = report_lock_contention_begin_event, 1136 .contention_end_event = report_lock_contention_end_event, 1137 }; 1138 1139 static struct trace_lock_handler contention_lock_ops = { 1140 .contention_begin_event = report_lock_contention_begin_event, 1141 .contention_end_event = report_lock_contention_end_event, 1142 }; 1143 1144 1145 static struct trace_lock_handler *trace_handler; 1146 1147 static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample) 1148 { 1149 if (trace_handler->acquire_event) 1150 return trace_handler->acquire_event(evsel, sample); 1151 return 0; 1152 } 1153 1154 static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample) 1155 { 1156 if (trace_handler->acquired_event) 1157 return trace_handler->acquired_event(evsel, sample); 1158 return 0; 1159 } 1160 1161 static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample) 1162 { 1163 if (trace_handler->contended_event) 1164 return trace_handler->contended_event(evsel, sample); 1165 return 0; 1166 } 1167 1168 static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample) 1169 { 1170 if (trace_handler->release_event) 1171 return trace_handler->release_event(evsel, sample); 1172 return 0; 1173 } 1174 1175 static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample) 1176 { 1177 if (trace_handler->contention_begin_event) 1178 return trace_handler->contention_begin_event(evsel, sample); 1179 return 0; 1180 } 1181 1182 static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample) 1183 { 1184 if (trace_handler->contention_end_event) 1185 return trace_handler->contention_end_event(evsel, sample); 1186 return 0; 1187 } 1188 1189 static void print_bad_events(int bad, int total) 1190 { 1191 /* Output for debug, this have to be removed */ 1192 int i; 1193 int broken = 0; 1194 const char *name[4] = 1195 { "acquire", "acquired", "contended", "release" }; 1196 1197 for (i = 0; i < BROKEN_MAX; i++) 1198 broken += bad_hist[i]; 1199 1200 if (quiet || (broken == 0 && !verbose)) 1201 return; 1202 1203 pr_info("\n=== output for debug===\n\n"); 1204 pr_info("bad: %d, total: %d\n", bad, total); 1205 pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100); 1206 pr_info("histogram of events caused bad sequence\n"); 1207 for (i = 0; i < BROKEN_MAX; i++) 1208 pr_info(" %10s: %d\n", name[i], bad_hist[i]); 1209 } 1210 1211 /* TODO: various way to print, coloring, nano or milli sec */ 1212 static void print_result(void) 1213 { 1214 struct lock_stat *st; 1215 struct lock_key *key; 1216 char cut_name[20]; 1217 int bad, total, printed; 1218 1219 if (!quiet) { 1220 pr_info("%20s ", "Name"); 1221 list_for_each_entry(key, &lock_keys, list) 1222 pr_info("%*s ", key->len, key->header); 1223 pr_info("\n\n"); 1224 } 1225 1226 bad = total = printed = 0; 1227 while ((st = pop_from_result())) { 1228 total++; 1229 if (st->broken) 1230 bad++; 1231 if (!st->nr_acquired) 1232 continue; 1233 1234 bzero(cut_name, 20); 1235 1236 if (strlen(st->name) < 20) { 1237 /* output raw name */ 1238 const char *name = st->name; 1239 1240 if (show_thread_stats) { 1241 struct thread *t; 1242 1243 /* st->addr contains tid of thread */ 1244 t = perf_session__findnew(session, st->addr); 1245 name = thread__comm_str(t); 1246 } 1247 1248 pr_info("%20s ", name); 1249 } else { 1250 strncpy(cut_name, st->name, 16); 1251 cut_name[16] = '.'; 1252 cut_name[17] = '.'; 1253 cut_name[18] = '.'; 1254 cut_name[19] = '\0'; 1255 /* cut off name for saving output style */ 1256 pr_info("%20s ", cut_name); 1257 } 1258 1259 list_for_each_entry(key, &lock_keys, list) { 1260 key->print(key, st); 1261 pr_info(" "); 1262 } 1263 pr_info("\n"); 1264 1265 if (++printed >= print_nr_entries) 1266 break; 1267 } 1268 1269 print_bad_events(bad, total); 1270 } 1271 1272 static bool info_threads, info_map; 1273 1274 static void dump_threads(void) 1275 { 1276 struct thread_stat *st; 1277 struct rb_node *node; 1278 struct thread *t; 1279 1280 pr_info("%10s: comm\n", "Thread ID"); 1281 1282 node = rb_first(&thread_stats); 1283 while (node) { 1284 st = container_of(node, struct thread_stat, rb); 1285 t = perf_session__findnew(session, st->tid); 1286 pr_info("%10d: %s\n", st->tid, thread__comm_str(t)); 1287 node = rb_next(node); 1288 thread__put(t); 1289 } 1290 } 1291 1292 static int compare_maps(struct lock_stat *a, struct lock_stat *b) 1293 { 1294 int ret; 1295 1296 if (a->name && b->name) 1297 ret = strcmp(a->name, b->name); 1298 else 1299 ret = !!a->name - !!b->name; 1300 1301 if (!ret) 1302 return a->addr < b->addr; 1303 else 1304 return ret < 0; 1305 } 1306 1307 static void dump_map(void) 1308 { 1309 unsigned int i; 1310 struct lock_stat *st; 1311 1312 pr_info("Address of instance: name of class\n"); 1313 for (i = 0; i < LOCKHASH_SIZE; i++) { 1314 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) { 1315 insert_to_result(st, compare_maps); 1316 } 1317 } 1318 1319 while ((st = pop_from_result())) 1320 pr_info(" %#llx: %s\n", (unsigned long long)st->addr, st->name); 1321 } 1322 1323 static int dump_info(void) 1324 { 1325 int rc = 0; 1326 1327 if (info_threads) 1328 dump_threads(); 1329 else if (info_map) 1330 dump_map(); 1331 else { 1332 rc = -1; 1333 pr_err("Unknown type of information\n"); 1334 } 1335 1336 return rc; 1337 } 1338 1339 static const struct evsel_str_handler lock_tracepoints[] = { 1340 { "lock:lock_acquire", evsel__process_lock_acquire, }, /* CONFIG_LOCKDEP */ 1341 { "lock:lock_acquired", evsel__process_lock_acquired, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */ 1342 { "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */ 1343 { "lock:lock_release", evsel__process_lock_release, }, /* CONFIG_LOCKDEP */ 1344 }; 1345 1346 static const struct evsel_str_handler contention_tracepoints[] = { 1347 { "lock:contention_begin", evsel__process_contention_begin, }, 1348 { "lock:contention_end", evsel__process_contention_end, }, 1349 }; 1350 1351 static int process_event_update(struct perf_tool *tool, 1352 union perf_event *event, 1353 struct evlist **pevlist) 1354 { 1355 int ret; 1356 1357 ret = perf_event__process_event_update(tool, event, pevlist); 1358 if (ret < 0) 1359 return ret; 1360 1361 /* this can return -EEXIST since we call it for each evsel */ 1362 perf_session__set_tracepoints_handlers(session, lock_tracepoints); 1363 perf_session__set_tracepoints_handlers(session, contention_tracepoints); 1364 return 0; 1365 } 1366 1367 typedef int (*tracepoint_handler)(struct evsel *evsel, 1368 struct perf_sample *sample); 1369 1370 static int process_sample_event(struct perf_tool *tool __maybe_unused, 1371 union perf_event *event, 1372 struct perf_sample *sample, 1373 struct evsel *evsel, 1374 struct machine *machine) 1375 { 1376 int err = 0; 1377 struct thread *thread = machine__findnew_thread(machine, sample->pid, 1378 sample->tid); 1379 1380 if (thread == NULL) { 1381 pr_debug("problem processing %d event, skipping it.\n", 1382 event->header.type); 1383 return -1; 1384 } 1385 1386 if (evsel->handler != NULL) { 1387 tracepoint_handler f = evsel->handler; 1388 err = f(evsel, sample); 1389 } 1390 1391 thread__put(thread); 1392 1393 return err; 1394 } 1395 1396 static void combine_result(void) 1397 { 1398 unsigned int i; 1399 struct lock_stat *st; 1400 1401 if (!combine_locks) 1402 return; 1403 1404 for (i = 0; i < LOCKHASH_SIZE; i++) { 1405 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) { 1406 combine_lock_stats(st); 1407 } 1408 } 1409 } 1410 1411 static void sort_result(void) 1412 { 1413 unsigned int i; 1414 struct lock_stat *st; 1415 1416 for (i = 0; i < LOCKHASH_SIZE; i++) { 1417 hlist_for_each_entry(st, &lockhash_table[i], hash_entry) { 1418 insert_to_result(st, compare); 1419 } 1420 } 1421 } 1422 1423 static const char *get_type_str(struct lock_stat *st) 1424 { 1425 static const struct { 1426 unsigned int flags; 1427 const char *name; 1428 } table[] = { 1429 { 0, "semaphore" }, 1430 { LCB_F_SPIN, "spinlock" }, 1431 { LCB_F_SPIN | LCB_F_READ, "rwlock:R" }, 1432 { LCB_F_SPIN | LCB_F_WRITE, "rwlock:W"}, 1433 { LCB_F_READ, "rwsem:R" }, 1434 { LCB_F_WRITE, "rwsem:W" }, 1435 { LCB_F_RT, "rtmutex" }, 1436 { LCB_F_RT | LCB_F_READ, "rwlock-rt:R" }, 1437 { LCB_F_RT | LCB_F_WRITE, "rwlock-rt:W"}, 1438 { LCB_F_PERCPU | LCB_F_READ, "pcpu-sem:R" }, 1439 { LCB_F_PERCPU | LCB_F_WRITE, "pcpu-sem:W" }, 1440 { LCB_F_MUTEX, "mutex" }, 1441 { LCB_F_MUTEX | LCB_F_SPIN, "mutex" }, 1442 }; 1443 1444 for (unsigned int i = 0; i < ARRAY_SIZE(table); i++) { 1445 if (table[i].flags == st->flags) 1446 return table[i].name; 1447 } 1448 return "unknown"; 1449 } 1450 1451 static void sort_contention_result(void) 1452 { 1453 sort_result(); 1454 } 1455 1456 static void print_contention_result(struct lock_contention *con) 1457 { 1458 struct lock_stat *st; 1459 struct lock_key *key; 1460 int bad, total, printed; 1461 1462 if (!quiet) { 1463 list_for_each_entry(key, &lock_keys, list) 1464 pr_info("%*s ", key->len, key->header); 1465 1466 if (show_thread_stats) 1467 pr_info(" %10s %s\n\n", "pid", "comm"); 1468 else 1469 pr_info(" %10s %s\n\n", "type", "caller"); 1470 } 1471 1472 bad = total = printed = 0; 1473 if (use_bpf) 1474 bad = bad_hist[BROKEN_CONTENDED]; 1475 1476 while ((st = pop_from_result())) { 1477 total += use_bpf ? st->nr_contended : 1; 1478 if (st->broken) 1479 bad++; 1480 1481 list_for_each_entry(key, &lock_keys, list) { 1482 key->print(key, st); 1483 pr_info(" "); 1484 } 1485 1486 if (show_thread_stats) { 1487 struct thread *t; 1488 int pid = st->addr; 1489 1490 /* st->addr contains tid of thread */ 1491 t = perf_session__findnew(session, pid); 1492 pr_info(" %10d %s\n", pid, thread__comm_str(t)); 1493 goto next; 1494 } 1495 1496 pr_info(" %10s %s\n", get_type_str(st), st->name); 1497 if (verbose) { 1498 struct map *kmap; 1499 struct symbol *sym; 1500 char buf[128]; 1501 u64 ip; 1502 1503 for (int i = 0; i < max_stack_depth; i++) { 1504 if (!st->callstack || !st->callstack[i]) 1505 break; 1506 1507 ip = st->callstack[i]; 1508 sym = machine__find_kernel_symbol(con->machine, ip, &kmap); 1509 get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf)); 1510 pr_info("\t\t\t%#lx %s\n", (unsigned long)ip, buf); 1511 } 1512 } 1513 1514 next: 1515 if (++printed >= print_nr_entries) 1516 break; 1517 } 1518 1519 print_bad_events(bad, total); 1520 } 1521 1522 static bool force; 1523 1524 static int __cmd_report(bool display_info) 1525 { 1526 int err = -EINVAL; 1527 struct perf_tool eops = { 1528 .attr = perf_event__process_attr, 1529 .event_update = process_event_update, 1530 .sample = process_sample_event, 1531 .comm = perf_event__process_comm, 1532 .mmap = perf_event__process_mmap, 1533 .namespaces = perf_event__process_namespaces, 1534 .tracing_data = perf_event__process_tracing_data, 1535 .ordered_events = true, 1536 }; 1537 struct perf_data data = { 1538 .path = input_name, 1539 .mode = PERF_DATA_MODE_READ, 1540 .force = force, 1541 }; 1542 1543 session = perf_session__new(&data, &eops); 1544 if (IS_ERR(session)) { 1545 pr_err("Initializing perf session failed\n"); 1546 return PTR_ERR(session); 1547 } 1548 1549 /* for lock function check */ 1550 symbol_conf.sort_by_name = true; 1551 symbol__init(&session->header.env); 1552 1553 if (!data.is_pipe) { 1554 if (!perf_session__has_traces(session, "lock record")) 1555 goto out_delete; 1556 1557 if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) { 1558 pr_err("Initializing perf session tracepoint handlers failed\n"); 1559 goto out_delete; 1560 } 1561 1562 if (perf_session__set_tracepoints_handlers(session, contention_tracepoints)) { 1563 pr_err("Initializing perf session tracepoint handlers failed\n"); 1564 goto out_delete; 1565 } 1566 } 1567 1568 if (setup_output_field(false, output_fields)) 1569 goto out_delete; 1570 1571 if (select_key(false)) 1572 goto out_delete; 1573 1574 if (show_thread_stats) 1575 aggr_mode = LOCK_AGGR_TASK; 1576 1577 err = perf_session__process_events(session); 1578 if (err) 1579 goto out_delete; 1580 1581 setup_pager(); 1582 if (display_info) /* used for info subcommand */ 1583 err = dump_info(); 1584 else { 1585 combine_result(); 1586 sort_result(); 1587 print_result(); 1588 } 1589 1590 out_delete: 1591 perf_session__delete(session); 1592 return err; 1593 } 1594 1595 static void sighandler(int sig __maybe_unused) 1596 { 1597 } 1598 1599 static int __cmd_contention(int argc, const char **argv) 1600 { 1601 int err = -EINVAL; 1602 struct perf_tool eops = { 1603 .attr = perf_event__process_attr, 1604 .event_update = process_event_update, 1605 .sample = process_sample_event, 1606 .comm = perf_event__process_comm, 1607 .mmap = perf_event__process_mmap, 1608 .tracing_data = perf_event__process_tracing_data, 1609 .ordered_events = true, 1610 }; 1611 struct perf_data data = { 1612 .path = input_name, 1613 .mode = PERF_DATA_MODE_READ, 1614 .force = force, 1615 }; 1616 struct lock_contention con = { 1617 .target = &target, 1618 .result = &lockhash_table[0], 1619 .map_nr_entries = bpf_map_entries, 1620 .max_stack = max_stack_depth, 1621 .stack_skip = stack_skip, 1622 }; 1623 1624 session = perf_session__new(use_bpf ? NULL : &data, &eops); 1625 if (IS_ERR(session)) { 1626 pr_err("Initializing perf session failed\n"); 1627 return PTR_ERR(session); 1628 } 1629 1630 con.machine = &session->machines.host; 1631 1632 /* for lock function check */ 1633 symbol_conf.sort_by_name = true; 1634 symbol__init(&session->header.env); 1635 1636 if (use_bpf) { 1637 err = target__validate(&target); 1638 if (err) { 1639 char errbuf[512]; 1640 1641 target__strerror(&target, err, errbuf, 512); 1642 pr_err("%s\n", errbuf); 1643 goto out_delete; 1644 } 1645 1646 signal(SIGINT, sighandler); 1647 signal(SIGCHLD, sighandler); 1648 signal(SIGTERM, sighandler); 1649 1650 con.evlist = evlist__new(); 1651 if (con.evlist == NULL) { 1652 err = -ENOMEM; 1653 goto out_delete; 1654 } 1655 1656 err = evlist__create_maps(con.evlist, &target); 1657 if (err < 0) 1658 goto out_delete; 1659 1660 if (argc) { 1661 err = evlist__prepare_workload(con.evlist, &target, 1662 argv, false, NULL); 1663 if (err < 0) 1664 goto out_delete; 1665 } 1666 1667 if (lock_contention_prepare(&con) < 0) { 1668 pr_err("lock contention BPF setup failed\n"); 1669 goto out_delete; 1670 } 1671 } else if (!data.is_pipe) { 1672 if (!perf_session__has_traces(session, "lock record")) 1673 goto out_delete; 1674 1675 if (!evlist__find_evsel_by_str(session->evlist, 1676 "lock:contention_begin")) { 1677 pr_err("lock contention evsel not found\n"); 1678 goto out_delete; 1679 } 1680 1681 if (perf_session__set_tracepoints_handlers(session, 1682 contention_tracepoints)) { 1683 pr_err("Initializing perf session tracepoint handlers failed\n"); 1684 goto out_delete; 1685 } 1686 } 1687 1688 if (setup_output_field(true, output_fields)) 1689 goto out_delete; 1690 1691 if (select_key(true)) 1692 goto out_delete; 1693 1694 if (show_thread_stats) 1695 aggr_mode = LOCK_AGGR_TASK; 1696 else 1697 aggr_mode = LOCK_AGGR_CALLER; 1698 1699 if (use_bpf) { 1700 lock_contention_start(); 1701 if (argc) 1702 evlist__start_workload(con.evlist); 1703 1704 /* wait for signal */ 1705 pause(); 1706 1707 lock_contention_stop(); 1708 lock_contention_read(&con); 1709 1710 /* abuse bad hist stats for lost entries */ 1711 bad_hist[BROKEN_CONTENDED] = con.lost; 1712 } else { 1713 err = perf_session__process_events(session); 1714 if (err) 1715 goto out_delete; 1716 } 1717 1718 setup_pager(); 1719 1720 sort_contention_result(); 1721 print_contention_result(&con); 1722 1723 out_delete: 1724 evlist__delete(con.evlist); 1725 lock_contention_finish(); 1726 perf_session__delete(session); 1727 return err; 1728 } 1729 1730 1731 static int __cmd_record(int argc, const char **argv) 1732 { 1733 const char *record_args[] = { 1734 "record", "-R", "-m", "1024", "-c", "1", "--synth", "task", 1735 }; 1736 const char *callgraph_args[] = { 1737 "--call-graph", "fp," __stringify(CONTENTION_STACK_DEPTH), 1738 }; 1739 unsigned int rec_argc, i, j, ret; 1740 unsigned int nr_tracepoints; 1741 unsigned int nr_callgraph_args = 0; 1742 const char **rec_argv; 1743 bool has_lock_stat = true; 1744 1745 for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) { 1746 if (!is_valid_tracepoint(lock_tracepoints[i].name)) { 1747 pr_debug("tracepoint %s is not enabled. " 1748 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n", 1749 lock_tracepoints[i].name); 1750 has_lock_stat = false; 1751 break; 1752 } 1753 } 1754 1755 if (has_lock_stat) 1756 goto setup_args; 1757 1758 for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) { 1759 if (!is_valid_tracepoint(contention_tracepoints[i].name)) { 1760 pr_err("tracepoint %s is not enabled.\n", 1761 contention_tracepoints[i].name); 1762 return 1; 1763 } 1764 } 1765 1766 nr_callgraph_args = ARRAY_SIZE(callgraph_args); 1767 1768 setup_args: 1769 rec_argc = ARRAY_SIZE(record_args) + nr_callgraph_args + argc - 1; 1770 1771 if (has_lock_stat) 1772 nr_tracepoints = ARRAY_SIZE(lock_tracepoints); 1773 else 1774 nr_tracepoints = ARRAY_SIZE(contention_tracepoints); 1775 1776 /* factor of 2 is for -e in front of each tracepoint */ 1777 rec_argc += 2 * nr_tracepoints; 1778 1779 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 1780 if (!rec_argv) 1781 return -ENOMEM; 1782 1783 for (i = 0; i < ARRAY_SIZE(record_args); i++) 1784 rec_argv[i] = strdup(record_args[i]); 1785 1786 for (j = 0; j < nr_tracepoints; j++) { 1787 const char *ev_name; 1788 1789 if (has_lock_stat) 1790 ev_name = strdup(lock_tracepoints[j].name); 1791 else 1792 ev_name = strdup(contention_tracepoints[j].name); 1793 1794 if (!ev_name) 1795 return -ENOMEM; 1796 1797 rec_argv[i++] = "-e"; 1798 rec_argv[i++] = ev_name; 1799 } 1800 1801 for (j = 0; j < nr_callgraph_args; j++, i++) 1802 rec_argv[i] = callgraph_args[j]; 1803 1804 for (j = 1; j < (unsigned int)argc; j++, i++) 1805 rec_argv[i] = argv[j]; 1806 1807 BUG_ON(i != rec_argc); 1808 1809 ret = cmd_record(i, rec_argv); 1810 free(rec_argv); 1811 return ret; 1812 } 1813 1814 static int parse_map_entry(const struct option *opt, const char *str, 1815 int unset __maybe_unused) 1816 { 1817 unsigned long *len = (unsigned long *)opt->value; 1818 unsigned long val; 1819 char *endptr; 1820 1821 errno = 0; 1822 val = strtoul(str, &endptr, 0); 1823 if (*endptr != '\0' || errno != 0) { 1824 pr_err("invalid BPF map length: %s\n", str); 1825 return -1; 1826 } 1827 1828 *len = val; 1829 return 0; 1830 } 1831 1832 static int parse_max_stack(const struct option *opt, const char *str, 1833 int unset __maybe_unused) 1834 { 1835 unsigned long *len = (unsigned long *)opt->value; 1836 long val; 1837 char *endptr; 1838 1839 errno = 0; 1840 val = strtol(str, &endptr, 0); 1841 if (*endptr != '\0' || errno != 0) { 1842 pr_err("invalid max stack depth: %s\n", str); 1843 return -1; 1844 } 1845 1846 if (val < 0 || val > sysctl__max_stack()) { 1847 pr_err("invalid max stack depth: %ld\n", val); 1848 return -1; 1849 } 1850 1851 *len = val; 1852 return 0; 1853 } 1854 1855 int cmd_lock(int argc, const char **argv) 1856 { 1857 const struct option lock_options[] = { 1858 OPT_STRING('i', "input", &input_name, "file", "input file name"), 1859 OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"), 1860 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"), 1861 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 1862 OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 1863 "file", "vmlinux pathname"), 1864 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, 1865 "file", "kallsyms pathname"), 1866 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"), 1867 OPT_END() 1868 }; 1869 1870 const struct option info_options[] = { 1871 OPT_BOOLEAN('t', "threads", &info_threads, 1872 "dump thread list in perf.data"), 1873 OPT_BOOLEAN('m', "map", &info_map, 1874 "map of lock instances (address:name table)"), 1875 OPT_PARENT(lock_options) 1876 }; 1877 1878 const struct option report_options[] = { 1879 OPT_STRING('k', "key", &sort_key, "acquired", 1880 "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"), 1881 OPT_STRING('F', "field", &output_fields, NULL, 1882 "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"), 1883 /* TODO: type */ 1884 OPT_BOOLEAN('c', "combine-locks", &combine_locks, 1885 "combine locks in the same class"), 1886 OPT_BOOLEAN('t', "threads", &show_thread_stats, 1887 "show per-thread lock stats"), 1888 OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"), 1889 OPT_PARENT(lock_options) 1890 }; 1891 1892 struct option contention_options[] = { 1893 OPT_STRING('k', "key", &sort_key, "wait_total", 1894 "key for sorting (contended / wait_total / wait_max / wait_min / avg_wait)"), 1895 OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,avg_wait", 1896 "output fields (contended / wait_total / wait_max / wait_min / avg_wait)"), 1897 OPT_BOOLEAN('t', "threads", &show_thread_stats, 1898 "show per-thread lock stats"), 1899 OPT_BOOLEAN('b', "use-bpf", &use_bpf, "use BPF program to collect lock contention stats"), 1900 OPT_BOOLEAN('a', "all-cpus", &target.system_wide, 1901 "System-wide collection from all CPUs"), 1902 OPT_STRING('C', "cpu", &target.cpu_list, "cpu", 1903 "List of cpus to monitor"), 1904 OPT_STRING('p', "pid", &target.pid, "pid", 1905 "Trace on existing process id"), 1906 OPT_STRING(0, "tid", &target.tid, "tid", 1907 "Trace on existing thread id (exclusive to --pid)"), 1908 OPT_CALLBACK(0, "map-nr-entries", &bpf_map_entries, "num", 1909 "Max number of BPF map entries", parse_map_entry), 1910 OPT_CALLBACK(0, "max-stack", &max_stack_depth, "num", 1911 "Set the maximum stack depth when collecting lopck contention, " 1912 "Default: " __stringify(CONTENTION_STACK_DEPTH), parse_max_stack), 1913 OPT_INTEGER(0, "stack-skip", &stack_skip, 1914 "Set the number of stack depth to skip when finding a lock caller, " 1915 "Default: " __stringify(CONTENTION_STACK_SKIP)), 1916 OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"), 1917 OPT_PARENT(lock_options) 1918 }; 1919 1920 const char * const info_usage[] = { 1921 "perf lock info [<options>]", 1922 NULL 1923 }; 1924 const char *const lock_subcommands[] = { "record", "report", "script", 1925 "info", "contention", NULL }; 1926 const char *lock_usage[] = { 1927 NULL, 1928 NULL 1929 }; 1930 const char * const report_usage[] = { 1931 "perf lock report [<options>]", 1932 NULL 1933 }; 1934 const char * const contention_usage[] = { 1935 "perf lock contention [<options>]", 1936 NULL 1937 }; 1938 unsigned int i; 1939 int rc = 0; 1940 1941 for (i = 0; i < LOCKHASH_SIZE; i++) 1942 INIT_HLIST_HEAD(lockhash_table + i); 1943 1944 argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands, 1945 lock_usage, PARSE_OPT_STOP_AT_NON_OPTION); 1946 if (!argc) 1947 usage_with_options(lock_usage, lock_options); 1948 1949 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 1950 return __cmd_record(argc, argv); 1951 } else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) { 1952 trace_handler = &report_lock_ops; 1953 if (argc) { 1954 argc = parse_options(argc, argv, 1955 report_options, report_usage, 0); 1956 if (argc) 1957 usage_with_options(report_usage, report_options); 1958 } 1959 rc = __cmd_report(false); 1960 } else if (!strcmp(argv[0], "script")) { 1961 /* Aliased to 'perf script' */ 1962 return cmd_script(argc, argv); 1963 } else if (!strcmp(argv[0], "info")) { 1964 if (argc) { 1965 argc = parse_options(argc, argv, 1966 info_options, info_usage, 0); 1967 if (argc) 1968 usage_with_options(info_usage, info_options); 1969 } 1970 /* recycling report_lock_ops */ 1971 trace_handler = &report_lock_ops; 1972 rc = __cmd_report(true); 1973 } else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) { 1974 trace_handler = &contention_lock_ops; 1975 sort_key = "wait_total"; 1976 output_fields = "contended,wait_total,wait_max,avg_wait"; 1977 1978 #ifndef HAVE_BPF_SKEL 1979 set_option_nobuild(contention_options, 'b', "use-bpf", 1980 "no BUILD_BPF_SKEL=1", false); 1981 #endif 1982 if (argc) { 1983 argc = parse_options(argc, argv, contention_options, 1984 contention_usage, 0); 1985 } 1986 rc = __cmd_contention(argc, argv); 1987 } else { 1988 usage_with_options(lock_usage, lock_options); 1989 } 1990 1991 return rc; 1992 } 1993