Linux Audio

Check our new training course

Loading...
v6.2
   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#include "util/bpf_skel/lock_data.h"
  16
  17#include <subcmd/pager.h>
  18#include <subcmd/parse-options.h>
  19#include "util/trace-event.h"
  20#include "util/tracepoint.h"
  21
  22#include "util/debug.h"
  23#include "util/session.h"
  24#include "util/tool.h"
  25#include "util/data.h"
  26#include "util/string2.h"
  27#include "util/map.h"
  28#include "util/util.h"
  29
 
  30#include <sys/types.h>
  31#include <sys/prctl.h>
  32#include <semaphore.h>
  33#include <math.h>
  34#include <limits.h>
  35#include <ctype.h>
  36
  37#include <linux/list.h>
  38#include <linux/hash.h>
  39#include <linux/kernel.h>
  40#include <linux/zalloc.h>
  41#include <linux/err.h>
  42#include <linux/stringify.h>
  43
  44static struct perf_session *session;
  45static struct target target;
  46
  47/* based on kernel/lockdep.c */
  48#define LOCKHASH_BITS		12
  49#define LOCKHASH_SIZE		(1UL << LOCKHASH_BITS)
  50
  51static struct hlist_head lockhash_table[LOCKHASH_SIZE];
  52
  53#define __lockhashfn(key)	hash_long((unsigned long)key, LOCKHASH_BITS)
  54#define lockhashentry(key)	(lockhash_table + __lockhashfn((key)))
  55
  56static struct rb_root		thread_stats;
  57
  58static bool combine_locks;
  59static bool show_thread_stats;
  60static bool show_lock_addrs;
 
 
  61static bool use_bpf;
  62static unsigned long bpf_map_entries = 10240;
  63static int max_stack_depth = CONTENTION_STACK_DEPTH;
  64static int stack_skip = CONTENTION_STACK_SKIP;
  65static int print_nr_entries = INT_MAX / 2;
 
 
 
 
 
 
 
 
  66
  67static struct lock_filter filters;
  68
  69static enum lock_aggr_mode aggr_mode = LOCK_AGGR_ADDR;
  70
 
 
 
 
 
  71static struct thread_stat *thread_stat_find(u32 tid)
  72{
  73	struct rb_node *node;
  74	struct thread_stat *st;
  75
  76	node = thread_stats.rb_node;
  77	while (node) {
  78		st = container_of(node, struct thread_stat, rb);
  79		if (st->tid == tid)
  80			return st;
  81		else if (tid < st->tid)
  82			node = node->rb_left;
  83		else
  84			node = node->rb_right;
  85	}
  86
  87	return NULL;
  88}
  89
  90static void thread_stat_insert(struct thread_stat *new)
  91{
  92	struct rb_node **rb = &thread_stats.rb_node;
  93	struct rb_node *parent = NULL;
  94	struct thread_stat *p;
  95
  96	while (*rb) {
  97		p = container_of(*rb, struct thread_stat, rb);
  98		parent = *rb;
  99
 100		if (new->tid < p->tid)
 101			rb = &(*rb)->rb_left;
 102		else if (new->tid > p->tid)
 103			rb = &(*rb)->rb_right;
 104		else
 105			BUG_ON("inserting invalid thread_stat\n");
 106	}
 107
 108	rb_link_node(&new->rb, parent, rb);
 109	rb_insert_color(&new->rb, &thread_stats);
 110}
 111
 112static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
 113{
 114	struct thread_stat *st;
 115
 116	st = thread_stat_find(tid);
 117	if (st)
 118		return st;
 119
 120	st = zalloc(sizeof(struct thread_stat));
 121	if (!st) {
 122		pr_err("memory allocation failed\n");
 123		return NULL;
 124	}
 125
 126	st->tid = tid;
 127	INIT_LIST_HEAD(&st->seq_list);
 128
 129	thread_stat_insert(st);
 130
 131	return st;
 132}
 133
 134static struct thread_stat *thread_stat_findnew_first(u32 tid);
 135static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
 136	thread_stat_findnew_first;
 137
 138static struct thread_stat *thread_stat_findnew_first(u32 tid)
 139{
 140	struct thread_stat *st;
 141
 142	st = zalloc(sizeof(struct thread_stat));
 143	if (!st) {
 144		pr_err("memory allocation failed\n");
 145		return NULL;
 146	}
 147	st->tid = tid;
 148	INIT_LIST_HEAD(&st->seq_list);
 149
 150	rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
 151	rb_insert_color(&st->rb, &thread_stats);
 152
 153	thread_stat_findnew = thread_stat_findnew_after_first;
 154	return st;
 155}
 156
 157/* build simple key function one is bigger than two */
 158#define SINGLE_KEY(member)						\
 159	static int lock_stat_key_ ## member(struct lock_stat *one,	\
 160					 struct lock_stat *two)		\
 161	{								\
 162		return one->member > two->member;			\
 163	}
 164
 165SINGLE_KEY(nr_acquired)
 166SINGLE_KEY(nr_contended)
 167SINGLE_KEY(avg_wait_time)
 168SINGLE_KEY(wait_time_total)
 169SINGLE_KEY(wait_time_max)
 170
 171static int lock_stat_key_wait_time_min(struct lock_stat *one,
 172					struct lock_stat *two)
 173{
 174	u64 s1 = one->wait_time_min;
 175	u64 s2 = two->wait_time_min;
 176	if (s1 == ULLONG_MAX)
 177		s1 = 0;
 178	if (s2 == ULLONG_MAX)
 179		s2 = 0;
 180	return s1 > s2;
 181}
 182
 183struct lock_key {
 184	/*
 185	 * name: the value for specify by user
 186	 * this should be simpler than raw name of member
 187	 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
 188	 */
 189	const char		*name;
 190	/* header: the string printed on the header line */
 191	const char		*header;
 192	/* len: the printing width of the field */
 193	int			len;
 194	/* key: a pointer to function to compare two lock stats for sorting */
 195	int			(*key)(struct lock_stat*, struct lock_stat*);
 196	/* print: a pointer to function to print a given lock stats */
 197	void			(*print)(struct lock_key*, struct lock_stat*);
 198	/* list: list entry to link this */
 199	struct list_head	list;
 200};
 201
 202static void lock_stat_key_print_time(unsigned long long nsec, int len)
 203{
 204	static const struct {
 205		float base;
 206		const char *unit;
 207	} table[] = {
 208		{ 1e9 * 3600, "h " },
 209		{ 1e9 * 60, "m " },
 210		{ 1e9, "s " },
 211		{ 1e6, "ms" },
 212		{ 1e3, "us" },
 213		{ 0, NULL },
 214	};
 215
 
 
 
 
 
 
 216	for (int i = 0; table[i].unit; i++) {
 217		if (nsec < table[i].base)
 218			continue;
 219
 220		pr_info("%*.2f %s", len - 3, nsec / table[i].base, table[i].unit);
 221		return;
 222	}
 223
 224	pr_info("%*llu %s", len - 3, nsec, "ns");
 225}
 226
 227#define PRINT_KEY(member)						\
 228static void lock_stat_key_print_ ## member(struct lock_key *key,	\
 229					   struct lock_stat *ls)	\
 230{									\
 231	pr_info("%*llu", key->len, (unsigned long long)ls->member);	\
 232}
 233
 234#define PRINT_TIME(member)						\
 235static void lock_stat_key_print_ ## member(struct lock_key *key,	\
 236					   struct lock_stat *ls)	\
 237{									\
 238	lock_stat_key_print_time((unsigned long long)ls->member, key->len);	\
 239}
 240
 241PRINT_KEY(nr_acquired)
 242PRINT_KEY(nr_contended)
 243PRINT_TIME(avg_wait_time)
 244PRINT_TIME(wait_time_total)
 245PRINT_TIME(wait_time_max)
 246
 247static void lock_stat_key_print_wait_time_min(struct lock_key *key,
 248					      struct lock_stat *ls)
 249{
 250	u64 wait_time = ls->wait_time_min;
 251
 252	if (wait_time == ULLONG_MAX)
 253		wait_time = 0;
 254
 255	lock_stat_key_print_time(wait_time, key->len);
 256}
 257
 258
 259static const char		*sort_key = "acquired";
 260
 261static int			(*compare)(struct lock_stat *, struct lock_stat *);
 262
 263static struct rb_root		sorted; /* place to store intermediate data */
 264static struct rb_root		result;	/* place to store sorted data */
 265
 266static LIST_HEAD(lock_keys);
 267static const char		*output_fields;
 268
 269#define DEF_KEY_LOCK(name, header, fn_suffix, len)			\
 270	{ #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
 271static struct lock_key report_keys[] = {
 272	DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
 273	DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
 274	DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
 275	DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
 276	DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
 277	DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
 278
 279	/* extra comparisons much complicated should be here */
 280	{ }
 281};
 282
 283static struct lock_key contention_keys[] = {
 284	DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
 285	DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
 286	DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
 287	DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
 288	DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
 289
 290	/* extra comparisons much complicated should be here */
 291	{ }
 292};
 293
 294static int select_key(bool contention)
 295{
 296	int i;
 297	struct lock_key *keys = report_keys;
 298
 299	if (contention)
 300		keys = contention_keys;
 301
 302	for (i = 0; keys[i].name; i++) {
 303		if (!strcmp(keys[i].name, sort_key)) {
 304			compare = keys[i].key;
 305
 306			/* selected key should be in the output fields */
 307			if (list_empty(&keys[i].list))
 308				list_add_tail(&keys[i].list, &lock_keys);
 309
 310			return 0;
 311		}
 312	}
 313
 314	pr_err("Unknown compare key: %s\n", sort_key);
 315	return -1;
 316}
 317
 318static int add_output_field(bool contention, char *name)
 319{
 320	int i;
 321	struct lock_key *keys = report_keys;
 322
 323	if (contention)
 324		keys = contention_keys;
 325
 326	for (i = 0; keys[i].name; i++) {
 327		if (strcmp(keys[i].name, name))
 328			continue;
 329
 330		/* prevent double link */
 331		if (list_empty(&keys[i].list))
 332			list_add_tail(&keys[i].list, &lock_keys);
 333
 334		return 0;
 335	}
 336
 337	pr_err("Unknown output field: %s\n", name);
 338	return -1;
 339}
 340
 341static int setup_output_field(bool contention, const char *str)
 342{
 343	char *tok, *tmp, *orig;
 344	int i, ret = 0;
 345	struct lock_key *keys = report_keys;
 346
 347	if (contention)
 348		keys = contention_keys;
 349
 350	/* no output field given: use all of them */
 351	if (str == NULL) {
 352		for (i = 0; keys[i].name; i++)
 353			list_add_tail(&keys[i].list, &lock_keys);
 354		return 0;
 355	}
 356
 357	for (i = 0; keys[i].name; i++)
 358		INIT_LIST_HEAD(&keys[i].list);
 359
 360	orig = tmp = strdup(str);
 361	if (orig == NULL)
 362		return -ENOMEM;
 363
 364	while ((tok = strsep(&tmp, ",")) != NULL){
 365		ret = add_output_field(contention, tok);
 366		if (ret < 0)
 367			break;
 368	}
 369	free(orig);
 370
 371	return ret;
 372}
 373
 374static void combine_lock_stats(struct lock_stat *st)
 375{
 376	struct rb_node **rb = &sorted.rb_node;
 377	struct rb_node *parent = NULL;
 378	struct lock_stat *p;
 379	int ret;
 380
 381	while (*rb) {
 382		p = container_of(*rb, struct lock_stat, rb);
 383		parent = *rb;
 384
 385		if (st->name && p->name)
 386			ret = strcmp(st->name, p->name);
 387		else
 388			ret = !!st->name - !!p->name;
 389
 390		if (ret == 0) {
 391			p->nr_acquired += st->nr_acquired;
 392			p->nr_contended += st->nr_contended;
 393			p->wait_time_total += st->wait_time_total;
 394
 395			if (p->nr_contended)
 396				p->avg_wait_time = p->wait_time_total / p->nr_contended;
 397
 398			if (p->wait_time_min > st->wait_time_min)
 399				p->wait_time_min = st->wait_time_min;
 400			if (p->wait_time_max < st->wait_time_max)
 401				p->wait_time_max = st->wait_time_max;
 402
 403			p->broken |= st->broken;
 404			st->combined = 1;
 405			return;
 406		}
 407
 408		if (ret < 0)
 409			rb = &(*rb)->rb_left;
 410		else
 411			rb = &(*rb)->rb_right;
 412	}
 413
 414	rb_link_node(&st->rb, parent, rb);
 415	rb_insert_color(&st->rb, &sorted);
 416}
 417
 418static void insert_to_result(struct lock_stat *st,
 419			     int (*bigger)(struct lock_stat *, struct lock_stat *))
 420{
 421	struct rb_node **rb = &result.rb_node;
 422	struct rb_node *parent = NULL;
 423	struct lock_stat *p;
 424
 425	if (combine_locks && st->combined)
 426		return;
 427
 428	while (*rb) {
 429		p = container_of(*rb, struct lock_stat, rb);
 430		parent = *rb;
 431
 432		if (bigger(st, p))
 433			rb = &(*rb)->rb_left;
 434		else
 435			rb = &(*rb)->rb_right;
 436	}
 437
 438	rb_link_node(&st->rb, parent, rb);
 439	rb_insert_color(&st->rb, &result);
 440}
 441
 442/* returns left most element of result, and erase it */
 443static struct lock_stat *pop_from_result(void)
 444{
 445	struct rb_node *node = result.rb_node;
 446
 447	if (!node)
 448		return NULL;
 449
 450	while (node->rb_left)
 451		node = node->rb_left;
 452
 453	rb_erase(node, &result);
 454	return container_of(node, struct lock_stat, rb);
 455}
 456
 457static struct lock_stat *lock_stat_find(u64 addr)
 458{
 459	struct hlist_head *entry = lockhashentry(addr);
 460	struct lock_stat *ret;
 461
 462	hlist_for_each_entry(ret, entry, hash_entry) {
 463		if (ret->addr == addr)
 464			return ret;
 465	}
 466	return NULL;
 467}
 468
 469static struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags)
 470{
 471	struct hlist_head *entry = lockhashentry(addr);
 472	struct lock_stat *ret, *new;
 473
 474	hlist_for_each_entry(ret, entry, hash_entry) {
 475		if (ret->addr == addr)
 476			return ret;
 477	}
 478
 479	new = zalloc(sizeof(struct lock_stat));
 480	if (!new)
 481		goto alloc_failed;
 482
 483	new->addr = addr;
 484	new->name = strdup(name);
 485	if (!new->name) {
 486		free(new);
 487		goto alloc_failed;
 488	}
 489
 490	new->flags = flags;
 491	new->wait_time_min = ULLONG_MAX;
 492
 493	hlist_add_head(&new->hash_entry, entry);
 494	return new;
 495
 496alloc_failed:
 497	pr_err("memory allocation failed\n");
 498	return NULL;
 499}
 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 501struct trace_lock_handler {
 502	/* it's used on CONFIG_LOCKDEP */
 503	int (*acquire_event)(struct evsel *evsel,
 504			     struct perf_sample *sample);
 505
 506	/* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
 507	int (*acquired_event)(struct evsel *evsel,
 508			      struct perf_sample *sample);
 509
 510	/* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
 511	int (*contended_event)(struct evsel *evsel,
 512			       struct perf_sample *sample);
 513
 514	/* it's used on CONFIG_LOCKDEP */
 515	int (*release_event)(struct evsel *evsel,
 516			     struct perf_sample *sample);
 517
 518	/* it's used when CONFIG_LOCKDEP is off */
 519	int (*contention_begin_event)(struct evsel *evsel,
 520				      struct perf_sample *sample);
 521
 522	/* it's used when CONFIG_LOCKDEP is off */
 523	int (*contention_end_event)(struct evsel *evsel,
 524				    struct perf_sample *sample);
 525};
 526
 527static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
 528{
 529	struct lock_seq_stat *seq;
 530
 531	list_for_each_entry(seq, &ts->seq_list, list) {
 532		if (seq->addr == addr)
 533			return seq;
 534	}
 535
 536	seq = zalloc(sizeof(struct lock_seq_stat));
 537	if (!seq) {
 538		pr_err("memory allocation failed\n");
 539		return NULL;
 540	}
 541	seq->state = SEQ_STATE_UNINITIALIZED;
 542	seq->addr = addr;
 543
 544	list_add(&seq->list, &ts->seq_list);
 545	return seq;
 546}
 547
 548enum broken_state {
 549	BROKEN_ACQUIRE,
 550	BROKEN_ACQUIRED,
 551	BROKEN_CONTENDED,
 552	BROKEN_RELEASE,
 553	BROKEN_MAX,
 554};
 555
 556static int bad_hist[BROKEN_MAX];
 557
 558enum acquire_flags {
 559	TRY_LOCK = 1,
 560	READ_LOCK = 2,
 561};
 562
 563static int get_key_by_aggr_mode_simple(u64 *key, u64 addr, u32 tid)
 564{
 565	switch (aggr_mode) {
 566	case LOCK_AGGR_ADDR:
 567		*key = addr;
 568		break;
 569	case LOCK_AGGR_TASK:
 570		*key = tid;
 571		break;
 572	case LOCK_AGGR_CALLER:
 
 573	default:
 574		pr_err("Invalid aggregation mode: %d\n", aggr_mode);
 575		return -EINVAL;
 576	}
 577	return 0;
 578}
 579
 580static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample);
 581
 582static int get_key_by_aggr_mode(u64 *key, u64 addr, struct evsel *evsel,
 583				 struct perf_sample *sample)
 584{
 585	if (aggr_mode == LOCK_AGGR_CALLER) {
 586		*key = callchain_id(evsel, sample);
 587		return 0;
 588	}
 589	return get_key_by_aggr_mode_simple(key, addr, sample->tid);
 590}
 591
 592static int report_lock_acquire_event(struct evsel *evsel,
 593				     struct perf_sample *sample)
 594{
 595	struct lock_stat *ls;
 596	struct thread_stat *ts;
 597	struct lock_seq_stat *seq;
 598	const char *name = evsel__strval(evsel, sample, "name");
 599	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 600	int flag = evsel__intval(evsel, sample, "flags");
 601	u64 key;
 602	int ret;
 603
 604	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 605	if (ret < 0)
 606		return ret;
 607
 608	ls = lock_stat_findnew(key, name, 0);
 609	if (!ls)
 610		return -ENOMEM;
 611
 612	ts = thread_stat_findnew(sample->tid);
 613	if (!ts)
 614		return -ENOMEM;
 615
 616	seq = get_seq(ts, addr);
 617	if (!seq)
 618		return -ENOMEM;
 619
 620	switch (seq->state) {
 621	case SEQ_STATE_UNINITIALIZED:
 622	case SEQ_STATE_RELEASED:
 623		if (!flag) {
 624			seq->state = SEQ_STATE_ACQUIRING;
 625		} else {
 626			if (flag & TRY_LOCK)
 627				ls->nr_trylock++;
 628			if (flag & READ_LOCK)
 629				ls->nr_readlock++;
 630			seq->state = SEQ_STATE_READ_ACQUIRED;
 631			seq->read_count = 1;
 632			ls->nr_acquired++;
 633		}
 634		break;
 635	case SEQ_STATE_READ_ACQUIRED:
 636		if (flag & READ_LOCK) {
 637			seq->read_count++;
 638			ls->nr_acquired++;
 639			goto end;
 640		} else {
 641			goto broken;
 642		}
 643		break;
 644	case SEQ_STATE_ACQUIRED:
 645	case SEQ_STATE_ACQUIRING:
 646	case SEQ_STATE_CONTENDED:
 647broken:
 648		/* broken lock sequence */
 649		if (!ls->broken) {
 650			ls->broken = 1;
 651			bad_hist[BROKEN_ACQUIRE]++;
 652		}
 653		list_del_init(&seq->list);
 654		free(seq);
 655		goto end;
 656	default:
 657		BUG_ON("Unknown state of lock sequence found!\n");
 658		break;
 659	}
 660
 661	ls->nr_acquire++;
 662	seq->prev_event_time = sample->time;
 663end:
 664	return 0;
 665}
 666
 667static int report_lock_acquired_event(struct evsel *evsel,
 668				      struct perf_sample *sample)
 669{
 670	struct lock_stat *ls;
 671	struct thread_stat *ts;
 672	struct lock_seq_stat *seq;
 673	u64 contended_term;
 674	const char *name = evsel__strval(evsel, sample, "name");
 675	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 676	u64 key;
 677	int ret;
 678
 679	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 680	if (ret < 0)
 681		return ret;
 682
 683	ls = lock_stat_findnew(key, name, 0);
 684	if (!ls)
 685		return -ENOMEM;
 686
 687	ts = thread_stat_findnew(sample->tid);
 688	if (!ts)
 689		return -ENOMEM;
 690
 691	seq = get_seq(ts, addr);
 692	if (!seq)
 693		return -ENOMEM;
 694
 695	switch (seq->state) {
 696	case SEQ_STATE_UNINITIALIZED:
 697		/* orphan event, do nothing */
 698		return 0;
 699	case SEQ_STATE_ACQUIRING:
 700		break;
 701	case SEQ_STATE_CONTENDED:
 702		contended_term = sample->time - seq->prev_event_time;
 703		ls->wait_time_total += contended_term;
 704		if (contended_term < ls->wait_time_min)
 705			ls->wait_time_min = contended_term;
 706		if (ls->wait_time_max < contended_term)
 707			ls->wait_time_max = contended_term;
 708		break;
 709	case SEQ_STATE_RELEASED:
 710	case SEQ_STATE_ACQUIRED:
 711	case SEQ_STATE_READ_ACQUIRED:
 712		/* broken lock sequence */
 713		if (!ls->broken) {
 714			ls->broken = 1;
 715			bad_hist[BROKEN_ACQUIRED]++;
 716		}
 717		list_del_init(&seq->list);
 718		free(seq);
 719		goto end;
 720	default:
 721		BUG_ON("Unknown state of lock sequence found!\n");
 722		break;
 723	}
 724
 725	seq->state = SEQ_STATE_ACQUIRED;
 726	ls->nr_acquired++;
 727	ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
 728	seq->prev_event_time = sample->time;
 729end:
 730	return 0;
 731}
 732
 733static int report_lock_contended_event(struct evsel *evsel,
 734				       struct perf_sample *sample)
 735{
 736	struct lock_stat *ls;
 737	struct thread_stat *ts;
 738	struct lock_seq_stat *seq;
 739	const char *name = evsel__strval(evsel, sample, "name");
 740	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 741	u64 key;
 742	int ret;
 743
 744	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 745	if (ret < 0)
 746		return ret;
 747
 748	ls = lock_stat_findnew(key, name, 0);
 749	if (!ls)
 750		return -ENOMEM;
 751
 752	ts = thread_stat_findnew(sample->tid);
 753	if (!ts)
 754		return -ENOMEM;
 755
 756	seq = get_seq(ts, addr);
 757	if (!seq)
 758		return -ENOMEM;
 759
 760	switch (seq->state) {
 761	case SEQ_STATE_UNINITIALIZED:
 762		/* orphan event, do nothing */
 763		return 0;
 764	case SEQ_STATE_ACQUIRING:
 765		break;
 766	case SEQ_STATE_RELEASED:
 767	case SEQ_STATE_ACQUIRED:
 768	case SEQ_STATE_READ_ACQUIRED:
 769	case SEQ_STATE_CONTENDED:
 770		/* broken lock sequence */
 771		if (!ls->broken) {
 772			ls->broken = 1;
 773			bad_hist[BROKEN_CONTENDED]++;
 774		}
 775		list_del_init(&seq->list);
 776		free(seq);
 777		goto end;
 778	default:
 779		BUG_ON("Unknown state of lock sequence found!\n");
 780		break;
 781	}
 782
 783	seq->state = SEQ_STATE_CONTENDED;
 784	ls->nr_contended++;
 785	ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
 786	seq->prev_event_time = sample->time;
 787end:
 788	return 0;
 789}
 790
 791static int report_lock_release_event(struct evsel *evsel,
 792				     struct perf_sample *sample)
 793{
 794	struct lock_stat *ls;
 795	struct thread_stat *ts;
 796	struct lock_seq_stat *seq;
 797	const char *name = evsel__strval(evsel, sample, "name");
 798	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 799	u64 key;
 800	int ret;
 801
 802	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 803	if (ret < 0)
 804		return ret;
 805
 806	ls = lock_stat_findnew(key, name, 0);
 807	if (!ls)
 808		return -ENOMEM;
 809
 810	ts = thread_stat_findnew(sample->tid);
 811	if (!ts)
 812		return -ENOMEM;
 813
 814	seq = get_seq(ts, addr);
 815	if (!seq)
 816		return -ENOMEM;
 817
 818	switch (seq->state) {
 819	case SEQ_STATE_UNINITIALIZED:
 820		goto end;
 821	case SEQ_STATE_ACQUIRED:
 822		break;
 823	case SEQ_STATE_READ_ACQUIRED:
 824		seq->read_count--;
 825		BUG_ON(seq->read_count < 0);
 826		if (seq->read_count) {
 827			ls->nr_release++;
 828			goto end;
 829		}
 830		break;
 831	case SEQ_STATE_ACQUIRING:
 832	case SEQ_STATE_CONTENDED:
 833	case SEQ_STATE_RELEASED:
 834		/* broken lock sequence */
 835		if (!ls->broken) {
 836			ls->broken = 1;
 837			bad_hist[BROKEN_RELEASE]++;
 838		}
 839		goto free_seq;
 840	default:
 841		BUG_ON("Unknown state of lock sequence found!\n");
 842		break;
 843	}
 844
 845	ls->nr_release++;
 846free_seq:
 847	list_del_init(&seq->list);
 848	free(seq);
 849end:
 850	return 0;
 851}
 852
 853static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,
 854				  char *buf, int size)
 855{
 856	u64 offset;
 857
 858	if (map == NULL || sym == NULL) {
 859		buf[0] = '\0';
 860		return 0;
 861	}
 862
 863	offset = map->map_ip(map, ip) - sym->start;
 864
 865	if (offset)
 866		return scnprintf(buf, size, "%s+%#lx", sym->name, offset);
 867	else
 868		return strlcpy(buf, sym->name, size);
 869}
 870static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sample,
 871				  char *buf, int size)
 872{
 873	struct thread *thread;
 874	struct callchain_cursor *cursor = &callchain_cursor;
 875	struct machine *machine = &session->machines.host;
 876	struct symbol *sym;
 877	int skip = 0;
 878	int ret;
 879
 880	/* lock names will be replaced to task name later */
 881	if (show_thread_stats)
 882		return -1;
 883
 884	thread = machine__findnew_thread(machine, -1, sample->pid);
 885	if (thread == NULL)
 886		return -1;
 887
 
 
 888	/* use caller function name from the callchain */
 889	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
 890					NULL, NULL, max_stack_depth);
 891	if (ret != 0) {
 892		thread__put(thread);
 893		return -1;
 894	}
 895
 896	callchain_cursor_commit(cursor);
 897	thread__put(thread);
 898
 899	while (true) {
 900		struct callchain_cursor_node *node;
 901
 902		node = callchain_cursor_current(cursor);
 903		if (node == NULL)
 904			break;
 905
 906		/* skip first few entries - for lock functions */
 907		if (++skip <= stack_skip)
 908			goto next;
 909
 910		sym = node->ms.sym;
 911		if (sym && !machine__is_lock_function(machine, node->ip)) {
 912			get_symbol_name_offset(node->ms.map, sym, node->ip,
 913					       buf, size);
 914			return 0;
 915		}
 916
 917next:
 918		callchain_cursor_advance(cursor);
 919	}
 920	return -1;
 921}
 922
 923static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
 924{
 925	struct callchain_cursor *cursor = &callchain_cursor;
 926	struct machine *machine = &session->machines.host;
 927	struct thread *thread;
 928	u64 hash = 0;
 929	int skip = 0;
 930	int ret;
 931
 932	thread = machine__findnew_thread(machine, -1, sample->pid);
 933	if (thread == NULL)
 934		return -1;
 935
 
 936	/* use caller function name from the callchain */
 937	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
 938					NULL, NULL, max_stack_depth);
 939	thread__put(thread);
 940
 941	if (ret != 0)
 942		return -1;
 943
 944	callchain_cursor_commit(cursor);
 945
 946	while (true) {
 947		struct callchain_cursor_node *node;
 948
 949		node = callchain_cursor_current(cursor);
 950		if (node == NULL)
 951			break;
 952
 953		/* skip first few entries - for lock functions */
 954		if (++skip <= stack_skip)
 955			goto next;
 956
 957		if (node->ms.sym && machine__is_lock_function(machine, node->ip))
 958			goto next;
 959
 960		hash ^= hash_long((unsigned long)node->ip, 64);
 961
 962next:
 963		callchain_cursor_advance(cursor);
 964	}
 965	return hash;
 966}
 967
 968static u64 *get_callstack(struct perf_sample *sample, int max_stack)
 969{
 970	u64 *callstack;
 971	u64 i;
 972	int c;
 973
 974	callstack = calloc(max_stack, sizeof(*callstack));
 975	if (callstack == NULL)
 976		return NULL;
 977
 978	for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) {
 979		u64 ip = sample->callchain->ips[i];
 980
 981		if (ip >= PERF_CONTEXT_MAX)
 982			continue;
 983
 984		callstack[c++] = ip;
 985	}
 986	return callstack;
 987}
 988
 989static int report_lock_contention_begin_event(struct evsel *evsel,
 990					      struct perf_sample *sample)
 991{
 992	struct lock_stat *ls;
 993	struct thread_stat *ts;
 994	struct lock_seq_stat *seq;
 995	u64 addr = evsel__intval(evsel, sample, "lock_addr");
 996	unsigned int flags = evsel__intval(evsel, sample, "flags");
 997	u64 key;
 998	int i, ret;
 999	static bool kmap_loaded;
1000	struct machine *machine = &session->machines.host;
1001	struct map *kmap;
1002	struct symbol *sym;
1003
1004	ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1005	if (ret < 0)
1006		return ret;
1007
1008	if (!kmap_loaded) {
1009		unsigned long *addrs;
1010
1011		/* make sure it loads the kernel map to find lock symbols */
1012		map__load(machine__kernel_map(machine));
1013		kmap_loaded = true;
1014
1015		/* convert (kernel) symbols to addresses */
1016		for (i = 0; i < filters.nr_syms; i++) {
1017			sym = machine__find_kernel_symbol_by_name(machine,
1018								  filters.syms[i],
1019								  &kmap);
1020			if (sym == NULL) {
1021				pr_warning("ignore unknown symbol: %s\n",
1022					   filters.syms[i]);
1023				continue;
1024			}
1025
1026			addrs = realloc(filters.addrs,
1027					(filters.nr_addrs + 1) * sizeof(*addrs));
1028			if (addrs == NULL) {
1029				pr_warning("memory allocation failure\n");
1030				return -ENOMEM;
1031			}
1032
1033			addrs[filters.nr_addrs++] = kmap->unmap_ip(kmap, sym->start);
1034			filters.addrs = addrs;
1035		}
1036	}
1037
1038	ls = lock_stat_find(key);
1039	if (!ls) {
1040		char buf[128];
1041		const char *name = "";
1042
1043		switch (aggr_mode) {
1044		case LOCK_AGGR_ADDR:
1045			sym = machine__find_kernel_symbol(machine, key, &kmap);
1046			if (sym)
1047				name = sym->name;
1048			break;
1049		case LOCK_AGGR_CALLER:
1050			name = buf;
1051			if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0)
1052				name = "Unknown";
1053			break;
 
1054		case LOCK_AGGR_TASK:
1055		default:
1056			break;
1057		}
1058
1059		ls = lock_stat_findnew(key, name, flags);
1060		if (!ls)
1061			return -ENOMEM;
1062
1063		if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1064			ls->callstack = get_callstack(sample, max_stack_depth);
1065			if (ls->callstack == NULL)
1066				return -ENOMEM;
1067		}
1068	}
1069
1070	if (filters.nr_types) {
1071		bool found = false;
1072
1073		for (i = 0; i < filters.nr_types; i++) {
1074			if (flags == filters.types[i]) {
1075				found = true;
1076				break;
1077			}
1078		}
1079
1080		if (!found)
1081			return 0;
1082	}
1083
1084	if (filters.nr_addrs) {
1085		bool found = false;
1086
1087		for (i = 0; i < filters.nr_addrs; i++) {
1088			if (addr == filters.addrs[i]) {
1089				found = true;
1090				break;
1091			}
1092		}
1093
1094		if (!found)
1095			return 0;
1096	}
1097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1098	ts = thread_stat_findnew(sample->tid);
1099	if (!ts)
1100		return -ENOMEM;
1101
1102	seq = get_seq(ts, addr);
1103	if (!seq)
1104		return -ENOMEM;
1105
1106	switch (seq->state) {
1107	case SEQ_STATE_UNINITIALIZED:
1108	case SEQ_STATE_ACQUIRED:
1109		break;
1110	case SEQ_STATE_CONTENDED:
1111		/*
1112		 * It can have nested contention begin with mutex spinning,
1113		 * then we would use the original contention begin event and
1114		 * ignore the second one.
1115		 */
1116		goto end;
1117	case SEQ_STATE_ACQUIRING:
1118	case SEQ_STATE_READ_ACQUIRED:
1119	case SEQ_STATE_RELEASED:
1120		/* broken lock sequence */
1121		if (!ls->broken) {
1122			ls->broken = 1;
1123			bad_hist[BROKEN_CONTENDED]++;
1124		}
1125		list_del_init(&seq->list);
1126		free(seq);
1127		goto end;
1128	default:
1129		BUG_ON("Unknown state of lock sequence found!\n");
1130		break;
1131	}
1132
1133	if (seq->state != SEQ_STATE_CONTENDED) {
1134		seq->state = SEQ_STATE_CONTENDED;
1135		seq->prev_event_time = sample->time;
1136		ls->nr_contended++;
1137	}
1138end:
1139	return 0;
1140}
1141
1142static int report_lock_contention_end_event(struct evsel *evsel,
1143					    struct perf_sample *sample)
1144{
1145	struct lock_stat *ls;
1146	struct thread_stat *ts;
1147	struct lock_seq_stat *seq;
1148	u64 contended_term;
1149	u64 addr = evsel__intval(evsel, sample, "lock_addr");
1150	u64 key;
1151	int ret;
1152
1153	ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1154	if (ret < 0)
1155		return ret;
1156
1157	ls = lock_stat_find(key);
1158	if (!ls)
1159		return 0;
1160
1161	ts = thread_stat_find(sample->tid);
1162	if (!ts)
1163		return 0;
1164
1165	seq = get_seq(ts, addr);
1166	if (!seq)
1167		return -ENOMEM;
1168
1169	switch (seq->state) {
1170	case SEQ_STATE_UNINITIALIZED:
1171		goto end;
1172	case SEQ_STATE_CONTENDED:
1173		contended_term = sample->time - seq->prev_event_time;
1174		ls->wait_time_total += contended_term;
1175		if (contended_term < ls->wait_time_min)
1176			ls->wait_time_min = contended_term;
1177		if (ls->wait_time_max < contended_term)
1178			ls->wait_time_max = contended_term;
1179		break;
1180	case SEQ_STATE_ACQUIRING:
1181	case SEQ_STATE_ACQUIRED:
1182	case SEQ_STATE_READ_ACQUIRED:
1183	case SEQ_STATE_RELEASED:
1184		/* broken lock sequence */
1185		if (!ls->broken) {
1186			ls->broken = 1;
1187			bad_hist[BROKEN_ACQUIRED]++;
1188		}
1189		list_del_init(&seq->list);
1190		free(seq);
1191		goto end;
1192	default:
1193		BUG_ON("Unknown state of lock sequence found!\n");
1194		break;
1195	}
1196
1197	seq->state = SEQ_STATE_ACQUIRED;
1198	ls->nr_acquired++;
1199	ls->avg_wait_time = ls->wait_time_total/ls->nr_acquired;
1200end:
1201	return 0;
1202}
1203
1204/* lock oriented handlers */
1205/* TODO: handlers for CPU oriented, thread oriented */
1206static struct trace_lock_handler report_lock_ops  = {
1207	.acquire_event		= report_lock_acquire_event,
1208	.acquired_event		= report_lock_acquired_event,
1209	.contended_event	= report_lock_contended_event,
1210	.release_event		= report_lock_release_event,
1211	.contention_begin_event	= report_lock_contention_begin_event,
1212	.contention_end_event	= report_lock_contention_end_event,
1213};
1214
1215static struct trace_lock_handler contention_lock_ops  = {
1216	.contention_begin_event	= report_lock_contention_begin_event,
1217	.contention_end_event	= report_lock_contention_end_event,
1218};
1219
1220
1221static struct trace_lock_handler *trace_handler;
1222
1223static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
1224{
1225	if (trace_handler->acquire_event)
1226		return trace_handler->acquire_event(evsel, sample);
1227	return 0;
1228}
1229
1230static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
1231{
1232	if (trace_handler->acquired_event)
1233		return trace_handler->acquired_event(evsel, sample);
1234	return 0;
1235}
1236
1237static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
1238{
1239	if (trace_handler->contended_event)
1240		return trace_handler->contended_event(evsel, sample);
1241	return 0;
1242}
1243
1244static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
1245{
1246	if (trace_handler->release_event)
1247		return trace_handler->release_event(evsel, sample);
1248	return 0;
1249}
1250
1251static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample)
1252{
1253	if (trace_handler->contention_begin_event)
1254		return trace_handler->contention_begin_event(evsel, sample);
1255	return 0;
1256}
1257
1258static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample)
1259{
1260	if (trace_handler->contention_end_event)
1261		return trace_handler->contention_end_event(evsel, sample);
1262	return 0;
1263}
1264
1265static void print_bad_events(int bad, int total)
1266{
1267	/* Output for debug, this have to be removed */
1268	int i;
1269	int broken = 0;
1270	const char *name[4] =
1271		{ "acquire", "acquired", "contended", "release" };
1272
1273	for (i = 0; i < BROKEN_MAX; i++)
1274		broken += bad_hist[i];
1275
1276	if (quiet || (broken == 0 && verbose <= 0))
1277		return;
1278
1279	pr_info("\n=== output for debug===\n\n");
1280	pr_info("bad: %d, total: %d\n", bad, total);
1281	pr_info("bad rate: %.2f %%\n", (double)bad / (double)total * 100);
1282	pr_info("histogram of events caused bad sequence\n");
1283	for (i = 0; i < BROKEN_MAX; i++)
1284		pr_info(" %10s: %d\n", name[i], bad_hist[i]);
1285}
1286
1287/* TODO: various way to print, coloring, nano or milli sec */
1288static void print_result(void)
1289{
1290	struct lock_stat *st;
1291	struct lock_key *key;
1292	char cut_name[20];
1293	int bad, total, printed;
1294
1295	if (!quiet) {
1296		pr_info("%20s ", "Name");
1297		list_for_each_entry(key, &lock_keys, list)
1298			pr_info("%*s ", key->len, key->header);
1299		pr_info("\n\n");
1300	}
1301
1302	bad = total = printed = 0;
1303	while ((st = pop_from_result())) {
1304		total++;
1305		if (st->broken)
1306			bad++;
1307		if (!st->nr_acquired)
1308			continue;
1309
1310		bzero(cut_name, 20);
1311
1312		if (strlen(st->name) < 20) {
1313			/* output raw name */
1314			const char *name = st->name;
1315
1316			if (show_thread_stats) {
1317				struct thread *t;
1318
1319				/* st->addr contains tid of thread */
1320				t = perf_session__findnew(session, st->addr);
1321				name = thread__comm_str(t);
1322			}
1323
1324			pr_info("%20s ", name);
1325		} else {
1326			strncpy(cut_name, st->name, 16);
1327			cut_name[16] = '.';
1328			cut_name[17] = '.';
1329			cut_name[18] = '.';
1330			cut_name[19] = '\0';
1331			/* cut off name for saving output style */
1332			pr_info("%20s ", cut_name);
1333		}
1334
1335		list_for_each_entry(key, &lock_keys, list) {
1336			key->print(key, st);
1337			pr_info(" ");
1338		}
1339		pr_info("\n");
1340
1341		if (++printed >= print_nr_entries)
1342			break;
1343	}
1344
1345	print_bad_events(bad, total);
1346}
1347
1348static bool info_threads, info_map;
1349
1350static void dump_threads(void)
1351{
1352	struct thread_stat *st;
1353	struct rb_node *node;
1354	struct thread *t;
1355
1356	pr_info("%10s: comm\n", "Thread ID");
1357
1358	node = rb_first(&thread_stats);
1359	while (node) {
1360		st = container_of(node, struct thread_stat, rb);
1361		t = perf_session__findnew(session, st->tid);
1362		pr_info("%10d: %s\n", st->tid, thread__comm_str(t));
1363		node = rb_next(node);
1364		thread__put(t);
1365	}
1366}
1367
1368static int compare_maps(struct lock_stat *a, struct lock_stat *b)
1369{
1370	int ret;
1371
1372	if (a->name && b->name)
1373		ret = strcmp(a->name, b->name);
1374	else
1375		ret = !!a->name - !!b->name;
1376
1377	if (!ret)
1378		return a->addr < b->addr;
1379	else
1380		return ret < 0;
1381}
1382
1383static void dump_map(void)
1384{
1385	unsigned int i;
1386	struct lock_stat *st;
1387
1388	pr_info("Address of instance: name of class\n");
1389	for (i = 0; i < LOCKHASH_SIZE; i++) {
1390		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1391			insert_to_result(st, compare_maps);
1392		}
1393	}
1394
1395	while ((st = pop_from_result()))
1396		pr_info(" %#llx: %s\n", (unsigned long long)st->addr, st->name);
1397}
1398
1399static int dump_info(void)
1400{
1401	int rc = 0;
1402
1403	if (info_threads)
1404		dump_threads();
1405	else if (info_map)
1406		dump_map();
1407	else {
1408		rc = -1;
1409		pr_err("Unknown type of information\n");
1410	}
1411
1412	return rc;
1413}
1414
1415static const struct evsel_str_handler lock_tracepoints[] = {
1416	{ "lock:lock_acquire",	 evsel__process_lock_acquire,   }, /* CONFIG_LOCKDEP */
1417	{ "lock:lock_acquired",	 evsel__process_lock_acquired,  }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1418	{ "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1419	{ "lock:lock_release",	 evsel__process_lock_release,   }, /* CONFIG_LOCKDEP */
1420};
1421
1422static const struct evsel_str_handler contention_tracepoints[] = {
1423	{ "lock:contention_begin", evsel__process_contention_begin, },
1424	{ "lock:contention_end",   evsel__process_contention_end,   },
1425};
1426
1427static int process_event_update(struct perf_tool *tool,
1428				union perf_event *event,
1429				struct evlist **pevlist)
1430{
1431	int ret;
1432
1433	ret = perf_event__process_event_update(tool, event, pevlist);
1434	if (ret < 0)
1435		return ret;
1436
1437	/* this can return -EEXIST since we call it for each evsel */
1438	perf_session__set_tracepoints_handlers(session, lock_tracepoints);
1439	perf_session__set_tracepoints_handlers(session, contention_tracepoints);
1440	return 0;
1441}
1442
1443typedef int (*tracepoint_handler)(struct evsel *evsel,
1444				  struct perf_sample *sample);
1445
1446static int process_sample_event(struct perf_tool *tool __maybe_unused,
1447				union perf_event *event,
1448				struct perf_sample *sample,
1449				struct evsel *evsel,
1450				struct machine *machine)
1451{
1452	int err = 0;
1453	struct thread *thread = machine__findnew_thread(machine, sample->pid,
1454							sample->tid);
1455
1456	if (thread == NULL) {
1457		pr_debug("problem processing %d event, skipping it.\n",
1458			event->header.type);
1459		return -1;
1460	}
1461
1462	if (evsel->handler != NULL) {
1463		tracepoint_handler f = evsel->handler;
1464		err = f(evsel, sample);
1465	}
1466
1467	thread__put(thread);
1468
1469	return err;
1470}
1471
1472static void combine_result(void)
1473{
1474	unsigned int i;
1475	struct lock_stat *st;
1476
1477	if (!combine_locks)
1478		return;
1479
1480	for (i = 0; i < LOCKHASH_SIZE; i++) {
1481		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1482			combine_lock_stats(st);
1483		}
1484	}
1485}
1486
1487static void sort_result(void)
1488{
1489	unsigned int i;
1490	struct lock_stat *st;
1491
1492	for (i = 0; i < LOCKHASH_SIZE; i++) {
1493		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1494			insert_to_result(st, compare);
1495		}
1496	}
1497}
1498
1499static const struct {
1500	unsigned int flags;
 
1501	const char *name;
1502} lock_type_table[] = {
1503	{ 0,				"semaphore" },
1504	{ LCB_F_SPIN,			"spinlock" },
1505	{ LCB_F_SPIN | LCB_F_READ,	"rwlock:R" },
1506	{ LCB_F_SPIN | LCB_F_WRITE,	"rwlock:W"},
1507	{ LCB_F_READ,			"rwsem:R" },
1508	{ LCB_F_WRITE,			"rwsem:W" },
1509	{ LCB_F_RT,			"rtmutex" },
1510	{ LCB_F_RT | LCB_F_READ,	"rwlock-rt:R" },
1511	{ LCB_F_RT | LCB_F_WRITE,	"rwlock-rt:W"},
1512	{ LCB_F_PERCPU | LCB_F_READ,	"pcpu-sem:R" },
1513	{ LCB_F_PERCPU | LCB_F_WRITE,	"pcpu-sem:W" },
1514	{ LCB_F_MUTEX,			"mutex" },
1515	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex" },
1516	/* alias for get_type_flag() */
1517	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex-spin" },
1518};
1519
1520static const char *get_type_str(unsigned int flags)
1521{
 
 
 
 
 
 
 
 
 
 
 
 
 
1522	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1523		if (lock_type_table[i].flags == flags)
1524			return lock_type_table[i].name;
1525	}
1526	return "unknown";
1527}
1528
1529static unsigned int get_type_flag(const char *str)
1530{
1531	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1532		if (!strcmp(lock_type_table[i].name, str))
1533			return lock_type_table[i].flags;
1534	}
 
 
 
 
1535	return UINT_MAX;
1536}
1537
1538static void lock_filter_finish(void)
1539{
1540	zfree(&filters.types);
1541	filters.nr_types = 0;
1542
1543	zfree(&filters.addrs);
1544	filters.nr_addrs = 0;
1545
1546	for (int i = 0; i < filters.nr_syms; i++)
1547		free(filters.syms[i]);
1548
1549	zfree(&filters.syms);
1550	filters.nr_syms = 0;
 
 
 
1551}
1552
1553static void sort_contention_result(void)
1554{
1555	sort_result();
1556}
1557
1558static void print_contention_result(struct lock_contention *con)
1559{
1560	struct lock_stat *st;
1561	struct lock_key *key;
1562	int bad, total, printed;
1563
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1564	if (!quiet) {
1565		list_for_each_entry(key, &lock_keys, list)
1566			pr_info("%*s ", key->len, key->header);
 
 
 
 
1567
1568		switch (aggr_mode) {
1569		case LOCK_AGGR_TASK:
1570			pr_info("  %10s   %s\n\n", "pid", "comm");
1571			break;
1572		case LOCK_AGGR_CALLER:
1573			pr_info("  %10s   %s\n\n", "type", "caller");
1574			break;
1575		case LOCK_AGGR_ADDR:
1576			pr_info("  %16s   %s\n\n", "address", "symbol");
1577			break;
1578		default:
1579			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1580		}
1581	}
 
1582
1583	bad = total = printed = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1584	if (use_bpf)
1585		bad = bad_hist[BROKEN_CONTENDED];
1586
1587	while ((st = pop_from_result())) {
1588		struct thread *t;
1589		int pid;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1590
 
1591		total += use_bpf ? st->nr_contended : 1;
1592		if (st->broken)
1593			bad++;
1594
1595		if (!st->wait_time_total)
1596			continue;
1597
1598		list_for_each_entry(key, &lock_keys, list) {
1599			key->print(key, st);
1600			pr_info(" ");
1601		}
1602
1603		switch (aggr_mode) {
1604		case LOCK_AGGR_CALLER:
1605			pr_info("  %10s   %s\n", get_type_str(st->flags), st->name);
1606			break;
1607		case LOCK_AGGR_TASK:
1608			pid = st->addr;
1609			t = perf_session__findnew(session, pid);
1610			pr_info("  %10d   %s\n", pid, thread__comm_str(t));
1611			break;
1612		case LOCK_AGGR_ADDR:
1613			pr_info("  %016llx   %s\n", (unsigned long long)st->addr,
1614				st->name ? : "");
1615			break;
1616		default:
1617			break;
1618		}
1619
1620		if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1621			struct map *kmap;
1622			struct symbol *sym;
1623			char buf[128];
1624			u64 ip;
1625
1626			for (int i = 0; i < max_stack_depth; i++) {
1627				if (!st->callstack || !st->callstack[i])
1628					break;
1629
1630				ip = st->callstack[i];
1631				sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
1632				get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
1633				pr_info("\t\t\t%#lx  %s\n", (unsigned long)ip, buf);
1634			}
1635		}
1636
1637		if (++printed >= print_nr_entries)
1638			break;
1639	}
 
 
1640
1641	print_bad_events(bad, total);
1642}
1643
1644static bool force;
1645
1646static int __cmd_report(bool display_info)
1647{
1648	int err = -EINVAL;
1649	struct perf_tool eops = {
1650		.attr		 = perf_event__process_attr,
1651		.event_update	 = process_event_update,
1652		.sample		 = process_sample_event,
1653		.comm		 = perf_event__process_comm,
1654		.mmap		 = perf_event__process_mmap,
1655		.namespaces	 = perf_event__process_namespaces,
1656		.tracing_data	 = perf_event__process_tracing_data,
1657		.ordered_events	 = true,
1658	};
1659	struct perf_data data = {
1660		.path  = input_name,
1661		.mode  = PERF_DATA_MODE_READ,
1662		.force = force,
1663	};
1664
1665	session = perf_session__new(&data, &eops);
1666	if (IS_ERR(session)) {
1667		pr_err("Initializing perf session failed\n");
1668		return PTR_ERR(session);
1669	}
1670
1671	/* for lock function check */
1672	symbol_conf.sort_by_name = true;
1673	symbol_conf.allow_aliases = true;
1674	symbol__init(&session->header.env);
1675
1676	if (!data.is_pipe) {
1677		if (!perf_session__has_traces(session, "lock record"))
1678			goto out_delete;
1679
1680		if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
1681			pr_err("Initializing perf session tracepoint handlers failed\n");
1682			goto out_delete;
1683		}
1684
1685		if (perf_session__set_tracepoints_handlers(session, contention_tracepoints)) {
1686			pr_err("Initializing perf session tracepoint handlers failed\n");
1687			goto out_delete;
1688		}
1689	}
1690
1691	if (setup_output_field(false, output_fields))
1692		goto out_delete;
1693
1694	if (select_key(false))
1695		goto out_delete;
1696
1697	if (show_thread_stats)
1698		aggr_mode = LOCK_AGGR_TASK;
1699
1700	err = perf_session__process_events(session);
1701	if (err)
1702		goto out_delete;
1703
1704	setup_pager();
1705	if (display_info) /* used for info subcommand */
1706		err = dump_info();
1707	else {
1708		combine_result();
1709		sort_result();
1710		print_result();
1711	}
1712
1713out_delete:
1714	perf_session__delete(session);
1715	return err;
1716}
1717
1718static void sighandler(int sig __maybe_unused)
1719{
1720}
1721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1722static int __cmd_contention(int argc, const char **argv)
1723{
1724	int err = -EINVAL;
1725	struct perf_tool eops = {
1726		.attr		 = perf_event__process_attr,
1727		.event_update	 = process_event_update,
1728		.sample		 = process_sample_event,
1729		.comm		 = perf_event__process_comm,
1730		.mmap		 = perf_event__process_mmap,
1731		.tracing_data	 = perf_event__process_tracing_data,
1732		.ordered_events	 = true,
1733	};
1734	struct perf_data data = {
1735		.path  = input_name,
1736		.mode  = PERF_DATA_MODE_READ,
1737		.force = force,
1738	};
1739	struct lock_contention con = {
1740		.target = &target,
1741		.result = &lockhash_table[0],
1742		.map_nr_entries = bpf_map_entries,
1743		.max_stack = max_stack_depth,
1744		.stack_skip = stack_skip,
1745		.filters = &filters,
 
 
 
1746	};
1747
 
 
 
 
 
 
1748	session = perf_session__new(use_bpf ? NULL : &data, &eops);
1749	if (IS_ERR(session)) {
1750		pr_err("Initializing perf session failed\n");
1751		return PTR_ERR(session);
 
 
1752	}
1753
1754	con.machine = &session->machines.host;
1755
1756	con.aggr_mode = aggr_mode = show_thread_stats ? LOCK_AGGR_TASK :
1757		show_lock_addrs ? LOCK_AGGR_ADDR : LOCK_AGGR_CALLER;
 
 
 
 
1758
1759	/* for lock function check */
1760	symbol_conf.sort_by_name = true;
1761	symbol_conf.allow_aliases = true;
1762	symbol__init(&session->header.env);
1763
1764	if (use_bpf) {
1765		err = target__validate(&target);
1766		if (err) {
1767			char errbuf[512];
1768
1769			target__strerror(&target, err, errbuf, 512);
1770			pr_err("%s\n", errbuf);
1771			goto out_delete;
1772		}
1773
1774		signal(SIGINT, sighandler);
1775		signal(SIGCHLD, sighandler);
1776		signal(SIGTERM, sighandler);
1777
1778		con.evlist = evlist__new();
1779		if (con.evlist == NULL) {
1780			err = -ENOMEM;
1781			goto out_delete;
1782		}
1783
1784		err = evlist__create_maps(con.evlist, &target);
1785		if (err < 0)
1786			goto out_delete;
1787
1788		if (argc) {
1789			err = evlist__prepare_workload(con.evlist, &target,
1790						       argv, false, NULL);
1791			if (err < 0)
1792				goto out_delete;
1793		}
1794
1795		if (lock_contention_prepare(&con) < 0) {
1796			pr_err("lock contention BPF setup failed\n");
1797			goto out_delete;
1798		}
1799	} else if (!data.is_pipe) {
1800		if (!perf_session__has_traces(session, "lock record"))
1801			goto out_delete;
1802
1803		if (!evlist__find_evsel_by_str(session->evlist,
1804					       "lock:contention_begin")) {
1805			pr_err("lock contention evsel not found\n");
1806			goto out_delete;
1807		}
1808
1809		if (perf_session__set_tracepoints_handlers(session,
1810						contention_tracepoints)) {
1811			pr_err("Initializing perf session tracepoint handlers failed\n");
1812			goto out_delete;
1813		}
1814	}
1815
1816	if (setup_output_field(true, output_fields))
1817		goto out_delete;
1818
1819	if (select_key(true))
1820		goto out_delete;
1821
 
 
 
 
 
 
 
 
 
1822	if (use_bpf) {
1823		lock_contention_start();
1824		if (argc)
1825			evlist__start_workload(con.evlist);
1826
1827		/* wait for signal */
1828		pause();
1829
1830		lock_contention_stop();
1831		lock_contention_read(&con);
1832
1833		/* abuse bad hist stats for lost entries */
1834		bad_hist[BROKEN_CONTENDED] = con.lost;
1835	} else {
1836		err = perf_session__process_events(session);
1837		if (err)
1838			goto out_delete;
1839	}
1840
1841	setup_pager();
1842
1843	sort_contention_result();
1844	print_contention_result(&con);
1845
1846out_delete:
1847	lock_filter_finish();
1848	evlist__delete(con.evlist);
1849	lock_contention_finish();
1850	perf_session__delete(session);
 
1851	return err;
1852}
1853
1854
1855static int __cmd_record(int argc, const char **argv)
1856{
1857	const char *record_args[] = {
1858		"record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
1859	};
1860	const char *callgraph_args[] = {
1861		"--call-graph", "fp," __stringify(CONTENTION_STACK_DEPTH),
1862	};
1863	unsigned int rec_argc, i, j, ret;
1864	unsigned int nr_tracepoints;
1865	unsigned int nr_callgraph_args = 0;
1866	const char **rec_argv;
1867	bool has_lock_stat = true;
1868
1869	for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
1870		if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
1871			pr_debug("tracepoint %s is not enabled. "
1872				 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
1873				 lock_tracepoints[i].name);
1874			has_lock_stat = false;
1875			break;
1876		}
1877	}
1878
1879	if (has_lock_stat)
1880		goto setup_args;
1881
1882	for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) {
1883		if (!is_valid_tracepoint(contention_tracepoints[i].name)) {
1884			pr_err("tracepoint %s is not enabled.\n",
1885			       contention_tracepoints[i].name);
1886			return 1;
1887		}
1888	}
1889
1890	nr_callgraph_args = ARRAY_SIZE(callgraph_args);
1891
1892setup_args:
1893	rec_argc = ARRAY_SIZE(record_args) + nr_callgraph_args + argc - 1;
1894
1895	if (has_lock_stat)
1896		nr_tracepoints = ARRAY_SIZE(lock_tracepoints);
1897	else
1898		nr_tracepoints = ARRAY_SIZE(contention_tracepoints);
1899
1900	/* factor of 2 is for -e in front of each tracepoint */
1901	rec_argc += 2 * nr_tracepoints;
1902
1903	rec_argv = calloc(rec_argc + 1, sizeof(char *));
1904	if (!rec_argv)
1905		return -ENOMEM;
1906
1907	for (i = 0; i < ARRAY_SIZE(record_args); i++)
1908		rec_argv[i] = strdup(record_args[i]);
1909
1910	for (j = 0; j < nr_tracepoints; j++) {
1911		const char *ev_name;
1912
1913		if (has_lock_stat)
1914			ev_name = strdup(lock_tracepoints[j].name);
1915		else
1916			ev_name = strdup(contention_tracepoints[j].name);
1917
1918		if (!ev_name)
 
1919			return -ENOMEM;
 
1920
1921		rec_argv[i++] = "-e";
1922		rec_argv[i++] = ev_name;
1923	}
1924
1925	for (j = 0; j < nr_callgraph_args; j++, i++)
1926		rec_argv[i] = callgraph_args[j];
1927
1928	for (j = 1; j < (unsigned int)argc; j++, i++)
1929		rec_argv[i] = argv[j];
1930
1931	BUG_ON(i != rec_argc);
1932
1933	ret = cmd_record(i, rec_argv);
1934	free(rec_argv);
1935	return ret;
1936}
1937
1938static int parse_map_entry(const struct option *opt, const char *str,
1939			    int unset __maybe_unused)
1940{
1941	unsigned long *len = (unsigned long *)opt->value;
1942	unsigned long val;
1943	char *endptr;
1944
1945	errno = 0;
1946	val = strtoul(str, &endptr, 0);
1947	if (*endptr != '\0' || errno != 0) {
1948		pr_err("invalid BPF map length: %s\n", str);
1949		return -1;
1950	}
1951
1952	*len = val;
1953	return 0;
1954}
1955
1956static int parse_max_stack(const struct option *opt, const char *str,
1957			   int unset __maybe_unused)
1958{
1959	unsigned long *len = (unsigned long *)opt->value;
1960	long val;
1961	char *endptr;
1962
1963	errno = 0;
1964	val = strtol(str, &endptr, 0);
1965	if (*endptr != '\0' || errno != 0) {
1966		pr_err("invalid max stack depth: %s\n", str);
1967		return -1;
1968	}
1969
1970	if (val < 0 || val > sysctl__max_stack()) {
1971		pr_err("invalid max stack depth: %ld\n", val);
1972		return -1;
1973	}
1974
1975	*len = val;
1976	return 0;
1977}
1978
1979static bool add_lock_type(unsigned int flags)
1980{
1981	unsigned int *tmp;
1982
1983	tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
1984	if (tmp == NULL)
1985		return false;
1986
1987	tmp[filters.nr_types++] = flags;
1988	filters.types = tmp;
1989	return true;
1990}
1991
1992static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
1993			   int unset __maybe_unused)
1994{
1995	char *s, *tmp, *tok;
1996	int ret = 0;
1997
1998	s = strdup(str);
1999	if (s == NULL)
2000		return -1;
2001
2002	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2003		unsigned int flags = get_type_flag(tok);
2004
2005		if (flags == -1U) {
2006			char buf[32];
2007
2008			if (strchr(tok, ':'))
2009			    continue;
2010
2011			/* try :R and :W suffixes for rwlock, rwsem, ... */
2012			scnprintf(buf, sizeof(buf), "%s:R", tok);
2013			flags = get_type_flag(buf);
2014			if (flags != UINT_MAX) {
2015				if (!add_lock_type(flags)) {
2016					ret = -1;
2017					break;
2018				}
2019			}
2020
2021			scnprintf(buf, sizeof(buf), "%s:W", tok);
2022			flags = get_type_flag(buf);
2023			if (flags != UINT_MAX) {
2024				if (!add_lock_type(flags)) {
2025					ret = -1;
2026					break;
2027				}
2028			}
2029			continue;
2030		}
2031
2032		if (!add_lock_type(flags)) {
2033			ret = -1;
2034			break;
2035		}
2036
2037		if (!strcmp(tok, "mutex")) {
2038			flags = get_type_flag("mutex-spin");
2039			if (flags != UINT_MAX) {
2040				if (!add_lock_type(flags)) {
2041					ret = -1;
2042					break;
2043				}
2044			}
2045		}
2046	}
2047
2048	free(s);
2049	return ret;
2050}
2051
2052static bool add_lock_addr(unsigned long addr)
2053{
2054	unsigned long *tmp;
2055
2056	tmp = realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.addrs));
2057	if (tmp == NULL) {
2058		pr_err("Memory allocation failure\n");
2059		return false;
2060	}
2061
2062	tmp[filters.nr_addrs++] = addr;
2063	filters.addrs = tmp;
2064	return true;
2065}
2066
2067static bool add_lock_sym(char *name)
2068{
2069	char **tmp;
2070	char *sym = strdup(name);
2071
2072	if (sym == NULL) {
2073		pr_err("Memory allocation failure\n");
2074		return false;
2075	}
2076
2077	tmp = realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.syms));
2078	if (tmp == NULL) {
2079		pr_err("Memory allocation failure\n");
2080		free(sym);
2081		return false;
2082	}
2083
2084	tmp[filters.nr_syms++] = sym;
2085	filters.syms = tmp;
2086	return true;
2087}
2088
2089static int parse_lock_addr(const struct option *opt __maybe_unused, const char *str,
2090			   int unset __maybe_unused)
2091{
2092	char *s, *tmp, *tok;
2093	int ret = 0;
2094	u64 addr;
2095
2096	s = strdup(str);
2097	if (s == NULL)
2098		return -1;
2099
2100	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2101		char *end;
2102
2103		addr = strtoul(tok, &end, 16);
2104		if (*end == '\0') {
2105			if (!add_lock_addr(addr)) {
2106				ret = -1;
2107				break;
2108			}
2109			continue;
2110		}
2111
2112		/*
2113		 * At this moment, we don't have kernel symbols.  Save the symbols
2114		 * in a separate list and resolve them to addresses later.
2115		 */
2116		if (!add_lock_sym(tok)) {
2117			ret = -1;
2118			break;
2119		}
2120	}
2121
2122	free(s);
2123	return ret;
2124}
2125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2126int cmd_lock(int argc, const char **argv)
2127{
2128	const struct option lock_options[] = {
2129	OPT_STRING('i', "input", &input_name, "file", "input file name"),
 
2130	OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
2131	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
2132	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
2133	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
2134		   "file", "vmlinux pathname"),
2135	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2136		   "file", "kallsyms pathname"),
2137	OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
2138	OPT_END()
2139	};
2140
2141	const struct option info_options[] = {
2142	OPT_BOOLEAN('t', "threads", &info_threads,
2143		    "dump thread list in perf.data"),
2144	OPT_BOOLEAN('m', "map", &info_map,
2145		    "map of lock instances (address:name table)"),
2146	OPT_PARENT(lock_options)
2147	};
2148
2149	const struct option report_options[] = {
2150	OPT_STRING('k', "key", &sort_key, "acquired",
2151		    "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2152	OPT_STRING('F', "field", &output_fields, NULL,
2153		    "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2154	/* TODO: type */
2155	OPT_BOOLEAN('c', "combine-locks", &combine_locks,
2156		    "combine locks in the same class"),
2157	OPT_BOOLEAN('t', "threads", &show_thread_stats,
2158		    "show per-thread lock stats"),
2159	OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2160	OPT_PARENT(lock_options)
2161	};
2162
2163	struct option contention_options[] = {
2164	OPT_STRING('k', "key", &sort_key, "wait_total",
2165		    "key for sorting (contended / wait_total / wait_max / wait_min / avg_wait)"),
2166	OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,avg_wait",
2167		    "output fields (contended / wait_total / wait_max / wait_min / avg_wait)"),
2168	OPT_BOOLEAN('t', "threads", &show_thread_stats,
2169		    "show per-thread lock stats"),
2170	OPT_BOOLEAN('b', "use-bpf", &use_bpf, "use BPF program to collect lock contention stats"),
2171	OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
2172		    "System-wide collection from all CPUs"),
2173	OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2174		    "List of cpus to monitor"),
2175	OPT_STRING('p', "pid", &target.pid, "pid",
2176		   "Trace on existing process id"),
2177	OPT_STRING(0, "tid", &target.tid, "tid",
2178		   "Trace on existing thread id (exclusive to --pid)"),
2179	OPT_CALLBACK(0, "map-nr-entries", &bpf_map_entries, "num",
2180		     "Max number of BPF map entries", parse_map_entry),
2181	OPT_CALLBACK(0, "max-stack", &max_stack_depth, "num",
2182		     "Set the maximum stack depth when collecting lopck contention, "
2183		     "Default: " __stringify(CONTENTION_STACK_DEPTH), parse_max_stack),
2184	OPT_INTEGER(0, "stack-skip", &stack_skip,
2185		    "Set the number of stack depth to skip when finding a lock caller, "
2186		    "Default: " __stringify(CONTENTION_STACK_SKIP)),
2187	OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2188	OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"),
2189	OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS",
2190		     "Filter specific type of locks", parse_lock_type),
2191	OPT_CALLBACK('L', "lock-filter", NULL, "ADDRS/NAMES",
2192		     "Filter specific address/symbol of locks", parse_lock_addr),
 
 
 
 
 
 
 
 
2193	OPT_PARENT(lock_options)
2194	};
2195
2196	const char * const info_usage[] = {
2197		"perf lock info [<options>]",
2198		NULL
2199	};
2200	const char *const lock_subcommands[] = { "record", "report", "script",
2201						 "info", "contention", NULL };
2202	const char *lock_usage[] = {
2203		NULL,
2204		NULL
2205	};
2206	const char * const report_usage[] = {
2207		"perf lock report [<options>]",
2208		NULL
2209	};
2210	const char * const contention_usage[] = {
2211		"perf lock contention [<options>]",
2212		NULL
2213	};
2214	unsigned int i;
2215	int rc = 0;
2216
 
 
 
 
2217	for (i = 0; i < LOCKHASH_SIZE; i++)
2218		INIT_HLIST_HEAD(lockhash_table + i);
2219
 
2220	argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
2221					lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
2222	if (!argc)
2223		usage_with_options(lock_usage, lock_options);
2224
2225	if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
2226		return __cmd_record(argc, argv);
2227	} else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
2228		trace_handler = &report_lock_ops;
2229		if (argc) {
2230			argc = parse_options(argc, argv,
2231					     report_options, report_usage, 0);
2232			if (argc)
2233				usage_with_options(report_usage, report_options);
2234		}
2235		rc = __cmd_report(false);
2236	} else if (!strcmp(argv[0], "script")) {
2237		/* Aliased to 'perf script' */
2238		return cmd_script(argc, argv);
2239	} else if (!strcmp(argv[0], "info")) {
2240		if (argc) {
2241			argc = parse_options(argc, argv,
2242					     info_options, info_usage, 0);
2243			if (argc)
2244				usage_with_options(info_usage, info_options);
2245		}
2246		/* recycling report_lock_ops */
2247		trace_handler = &report_lock_ops;
2248		rc = __cmd_report(true);
2249	} else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) {
2250		trace_handler = &contention_lock_ops;
2251		sort_key = "wait_total";
2252		output_fields = "contended,wait_total,wait_max,avg_wait";
2253
2254#ifndef HAVE_BPF_SKEL
2255		set_option_nobuild(contention_options, 'b', "use-bpf",
2256				   "no BUILD_BPF_SKEL=1", false);
2257#endif
2258		if (argc) {
2259			argc = parse_options(argc, argv, contention_options,
2260					     contention_usage, 0);
2261		}
2262
2263		if (show_thread_stats && show_lock_addrs) {
2264			pr_err("Cannot use thread and addr mode together\n");
2265			parse_options_usage(contention_usage, contention_options,
2266					    "threads", 0);
2267			parse_options_usage(NULL, contention_options,
2268					    "lock-addr", 0);
2269			return -1;
2270		}
2271
2272		rc = __cmd_contention(argc, argv);
2273	} else {
2274		usage_with_options(lock_usage, lock_options);
2275	}
2276
 
2277	return rc;
2278}
v6.8
   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/cgroup.h"
  14#include "util/callchain.h"
  15#include "util/lock-contention.h"
  16#include "util/bpf_skel/lock_data.h"
  17
  18#include <subcmd/pager.h>
  19#include <subcmd/parse-options.h>
  20#include "util/trace-event.h"
  21#include "util/tracepoint.h"
  22
  23#include "util/debug.h"
  24#include "util/session.h"
  25#include "util/tool.h"
  26#include "util/data.h"
  27#include "util/string2.h"
  28#include "util/map.h"
  29#include "util/util.h"
  30
  31#include <stdio.h>
  32#include <sys/types.h>
  33#include <sys/prctl.h>
  34#include <semaphore.h>
  35#include <math.h>
  36#include <limits.h>
  37#include <ctype.h>
  38
  39#include <linux/list.h>
  40#include <linux/hash.h>
  41#include <linux/kernel.h>
  42#include <linux/zalloc.h>
  43#include <linux/err.h>
  44#include <linux/stringify.h>
  45
  46static struct perf_session *session;
  47static struct target target;
  48
  49/* based on kernel/lockdep.c */
  50#define LOCKHASH_BITS		12
  51#define LOCKHASH_SIZE		(1UL << LOCKHASH_BITS)
  52
  53static struct hlist_head *lockhash_table;
  54
  55#define __lockhashfn(key)	hash_long((unsigned long)key, LOCKHASH_BITS)
  56#define lockhashentry(key)	(lockhash_table + __lockhashfn((key)))
  57
  58static struct rb_root		thread_stats;
  59
  60static bool combine_locks;
  61static bool show_thread_stats;
  62static bool show_lock_addrs;
  63static bool show_lock_owner;
  64static bool show_lock_cgroups;
  65static bool use_bpf;
  66static unsigned long bpf_map_entries = MAX_ENTRIES;
  67static int max_stack_depth = CONTENTION_STACK_DEPTH;
  68static int stack_skip = CONTENTION_STACK_SKIP;
  69static int print_nr_entries = INT_MAX / 2;
  70static LIST_HEAD(callstack_filters);
  71static const char *output_name = NULL;
  72static FILE *lock_output;
  73
  74struct callstack_filter {
  75	struct list_head list;
  76	char name[];
  77};
  78
  79static struct lock_filter filters;
  80
  81static enum lock_aggr_mode aggr_mode = LOCK_AGGR_ADDR;
  82
  83static bool needs_callstack(void)
  84{
  85	return !list_empty(&callstack_filters);
  86}
  87
  88static struct thread_stat *thread_stat_find(u32 tid)
  89{
  90	struct rb_node *node;
  91	struct thread_stat *st;
  92
  93	node = thread_stats.rb_node;
  94	while (node) {
  95		st = container_of(node, struct thread_stat, rb);
  96		if (st->tid == tid)
  97			return st;
  98		else if (tid < st->tid)
  99			node = node->rb_left;
 100		else
 101			node = node->rb_right;
 102	}
 103
 104	return NULL;
 105}
 106
 107static void thread_stat_insert(struct thread_stat *new)
 108{
 109	struct rb_node **rb = &thread_stats.rb_node;
 110	struct rb_node *parent = NULL;
 111	struct thread_stat *p;
 112
 113	while (*rb) {
 114		p = container_of(*rb, struct thread_stat, rb);
 115		parent = *rb;
 116
 117		if (new->tid < p->tid)
 118			rb = &(*rb)->rb_left;
 119		else if (new->tid > p->tid)
 120			rb = &(*rb)->rb_right;
 121		else
 122			BUG_ON("inserting invalid thread_stat\n");
 123	}
 124
 125	rb_link_node(&new->rb, parent, rb);
 126	rb_insert_color(&new->rb, &thread_stats);
 127}
 128
 129static struct thread_stat *thread_stat_findnew_after_first(u32 tid)
 130{
 131	struct thread_stat *st;
 132
 133	st = thread_stat_find(tid);
 134	if (st)
 135		return st;
 136
 137	st = zalloc(sizeof(struct thread_stat));
 138	if (!st) {
 139		pr_err("memory allocation failed\n");
 140		return NULL;
 141	}
 142
 143	st->tid = tid;
 144	INIT_LIST_HEAD(&st->seq_list);
 145
 146	thread_stat_insert(st);
 147
 148	return st;
 149}
 150
 151static struct thread_stat *thread_stat_findnew_first(u32 tid);
 152static struct thread_stat *(*thread_stat_findnew)(u32 tid) =
 153	thread_stat_findnew_first;
 154
 155static struct thread_stat *thread_stat_findnew_first(u32 tid)
 156{
 157	struct thread_stat *st;
 158
 159	st = zalloc(sizeof(struct thread_stat));
 160	if (!st) {
 161		pr_err("memory allocation failed\n");
 162		return NULL;
 163	}
 164	st->tid = tid;
 165	INIT_LIST_HEAD(&st->seq_list);
 166
 167	rb_link_node(&st->rb, NULL, &thread_stats.rb_node);
 168	rb_insert_color(&st->rb, &thread_stats);
 169
 170	thread_stat_findnew = thread_stat_findnew_after_first;
 171	return st;
 172}
 173
 174/* build simple key function one is bigger than two */
 175#define SINGLE_KEY(member)						\
 176	static int lock_stat_key_ ## member(struct lock_stat *one,	\
 177					 struct lock_stat *two)		\
 178	{								\
 179		return one->member > two->member;			\
 180	}
 181
 182SINGLE_KEY(nr_acquired)
 183SINGLE_KEY(nr_contended)
 184SINGLE_KEY(avg_wait_time)
 185SINGLE_KEY(wait_time_total)
 186SINGLE_KEY(wait_time_max)
 187
 188static int lock_stat_key_wait_time_min(struct lock_stat *one,
 189					struct lock_stat *two)
 190{
 191	u64 s1 = one->wait_time_min;
 192	u64 s2 = two->wait_time_min;
 193	if (s1 == ULLONG_MAX)
 194		s1 = 0;
 195	if (s2 == ULLONG_MAX)
 196		s2 = 0;
 197	return s1 > s2;
 198}
 199
 200struct lock_key {
 201	/*
 202	 * name: the value for specify by user
 203	 * this should be simpler than raw name of member
 204	 * e.g. nr_acquired -> acquired, wait_time_total -> wait_total
 205	 */
 206	const char		*name;
 207	/* header: the string printed on the header line */
 208	const char		*header;
 209	/* len: the printing width of the field */
 210	int			len;
 211	/* key: a pointer to function to compare two lock stats for sorting */
 212	int			(*key)(struct lock_stat*, struct lock_stat*);
 213	/* print: a pointer to function to print a given lock stats */
 214	void			(*print)(struct lock_key*, struct lock_stat*);
 215	/* list: list entry to link this */
 216	struct list_head	list;
 217};
 218
 219static void lock_stat_key_print_time(unsigned long long nsec, int len)
 220{
 221	static const struct {
 222		float base;
 223		const char *unit;
 224	} table[] = {
 225		{ 1e9 * 3600, "h " },
 226		{ 1e9 * 60, "m " },
 227		{ 1e9, "s " },
 228		{ 1e6, "ms" },
 229		{ 1e3, "us" },
 230		{ 0, NULL },
 231	};
 232
 233	/* for CSV output */
 234	if (len == 0) {
 235		fprintf(lock_output, "%llu", nsec);
 236		return;
 237	}
 238
 239	for (int i = 0; table[i].unit; i++) {
 240		if (nsec < table[i].base)
 241			continue;
 242
 243		fprintf(lock_output, "%*.2f %s", len - 3, nsec / table[i].base, table[i].unit);
 244		return;
 245	}
 246
 247	fprintf(lock_output, "%*llu %s", len - 3, nsec, "ns");
 248}
 249
 250#define PRINT_KEY(member)						\
 251static void lock_stat_key_print_ ## member(struct lock_key *key,	\
 252					   struct lock_stat *ls)	\
 253{									\
 254	fprintf(lock_output, "%*llu", key->len, (unsigned long long)ls->member);\
 255}
 256
 257#define PRINT_TIME(member)						\
 258static void lock_stat_key_print_ ## member(struct lock_key *key,	\
 259					   struct lock_stat *ls)	\
 260{									\
 261	lock_stat_key_print_time((unsigned long long)ls->member, key->len);	\
 262}
 263
 264PRINT_KEY(nr_acquired)
 265PRINT_KEY(nr_contended)
 266PRINT_TIME(avg_wait_time)
 267PRINT_TIME(wait_time_total)
 268PRINT_TIME(wait_time_max)
 269
 270static void lock_stat_key_print_wait_time_min(struct lock_key *key,
 271					      struct lock_stat *ls)
 272{
 273	u64 wait_time = ls->wait_time_min;
 274
 275	if (wait_time == ULLONG_MAX)
 276		wait_time = 0;
 277
 278	lock_stat_key_print_time(wait_time, key->len);
 279}
 280
 281
 282static const char		*sort_key = "acquired";
 283
 284static int			(*compare)(struct lock_stat *, struct lock_stat *);
 285
 286static struct rb_root		sorted; /* place to store intermediate data */
 287static struct rb_root		result;	/* place to store sorted data */
 288
 289static LIST_HEAD(lock_keys);
 290static const char		*output_fields;
 291
 292#define DEF_KEY_LOCK(name, header, fn_suffix, len)			\
 293	{ #name, header, len, lock_stat_key_ ## fn_suffix, lock_stat_key_print_ ## fn_suffix, {} }
 294static struct lock_key report_keys[] = {
 295	DEF_KEY_LOCK(acquired, "acquired", nr_acquired, 10),
 296	DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
 297	DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
 298	DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
 299	DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
 300	DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
 301
 302	/* extra comparisons much complicated should be here */
 303	{ }
 304};
 305
 306static struct lock_key contention_keys[] = {
 307	DEF_KEY_LOCK(contended, "contended", nr_contended, 10),
 308	DEF_KEY_LOCK(wait_total, "total wait", wait_time_total, 12),
 309	DEF_KEY_LOCK(wait_max, "max wait", wait_time_max, 12),
 310	DEF_KEY_LOCK(wait_min, "min wait", wait_time_min, 12),
 311	DEF_KEY_LOCK(avg_wait, "avg wait", avg_wait_time, 12),
 312
 313	/* extra comparisons much complicated should be here */
 314	{ }
 315};
 316
 317static int select_key(bool contention)
 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, sort_key)) {
 327			compare = keys[i].key;
 328
 329			/* selected key should be in the output fields */
 330			if (list_empty(&keys[i].list))
 331				list_add_tail(&keys[i].list, &lock_keys);
 332
 333			return 0;
 334		}
 335	}
 336
 337	pr_err("Unknown compare key: %s\n", sort_key);
 338	return -1;
 339}
 340
 341static int add_output_field(bool contention, char *name)
 342{
 343	int i;
 344	struct lock_key *keys = report_keys;
 345
 346	if (contention)
 347		keys = contention_keys;
 348
 349	for (i = 0; keys[i].name; i++) {
 350		if (strcmp(keys[i].name, name))
 351			continue;
 352
 353		/* prevent double link */
 354		if (list_empty(&keys[i].list))
 355			list_add_tail(&keys[i].list, &lock_keys);
 356
 357		return 0;
 358	}
 359
 360	pr_err("Unknown output field: %s\n", name);
 361	return -1;
 362}
 363
 364static int setup_output_field(bool contention, const char *str)
 365{
 366	char *tok, *tmp, *orig;
 367	int i, ret = 0;
 368	struct lock_key *keys = report_keys;
 369
 370	if (contention)
 371		keys = contention_keys;
 372
 373	/* no output field given: use all of them */
 374	if (str == NULL) {
 375		for (i = 0; keys[i].name; i++)
 376			list_add_tail(&keys[i].list, &lock_keys);
 377		return 0;
 378	}
 379
 380	for (i = 0; keys[i].name; i++)
 381		INIT_LIST_HEAD(&keys[i].list);
 382
 383	orig = tmp = strdup(str);
 384	if (orig == NULL)
 385		return -ENOMEM;
 386
 387	while ((tok = strsep(&tmp, ",")) != NULL){
 388		ret = add_output_field(contention, tok);
 389		if (ret < 0)
 390			break;
 391	}
 392	free(orig);
 393
 394	return ret;
 395}
 396
 397static void combine_lock_stats(struct lock_stat *st)
 398{
 399	struct rb_node **rb = &sorted.rb_node;
 400	struct rb_node *parent = NULL;
 401	struct lock_stat *p;
 402	int ret;
 403
 404	while (*rb) {
 405		p = container_of(*rb, struct lock_stat, rb);
 406		parent = *rb;
 407
 408		if (st->name && p->name)
 409			ret = strcmp(st->name, p->name);
 410		else
 411			ret = !!st->name - !!p->name;
 412
 413		if (ret == 0) {
 414			p->nr_acquired += st->nr_acquired;
 415			p->nr_contended += st->nr_contended;
 416			p->wait_time_total += st->wait_time_total;
 417
 418			if (p->nr_contended)
 419				p->avg_wait_time = p->wait_time_total / p->nr_contended;
 420
 421			if (p->wait_time_min > st->wait_time_min)
 422				p->wait_time_min = st->wait_time_min;
 423			if (p->wait_time_max < st->wait_time_max)
 424				p->wait_time_max = st->wait_time_max;
 425
 426			p->broken |= st->broken;
 427			st->combined = 1;
 428			return;
 429		}
 430
 431		if (ret < 0)
 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, &sorted);
 439}
 440
 441static void insert_to_result(struct lock_stat *st,
 442			     int (*bigger)(struct lock_stat *, struct lock_stat *))
 443{
 444	struct rb_node **rb = &result.rb_node;
 445	struct rb_node *parent = NULL;
 446	struct lock_stat *p;
 447
 448	if (combine_locks && st->combined)
 449		return;
 450
 451	while (*rb) {
 452		p = container_of(*rb, struct lock_stat, rb);
 453		parent = *rb;
 454
 455		if (bigger(st, p))
 456			rb = &(*rb)->rb_left;
 457		else
 458			rb = &(*rb)->rb_right;
 459	}
 460
 461	rb_link_node(&st->rb, parent, rb);
 462	rb_insert_color(&st->rb, &result);
 463}
 464
 465/* returns left most element of result, and erase it */
 466static struct lock_stat *pop_from_result(void)
 467{
 468	struct rb_node *node = result.rb_node;
 469
 470	if (!node)
 471		return NULL;
 472
 473	while (node->rb_left)
 474		node = node->rb_left;
 475
 476	rb_erase(node, &result);
 477	return container_of(node, struct lock_stat, rb);
 478}
 479
 480struct lock_stat *lock_stat_find(u64 addr)
 481{
 482	struct hlist_head *entry = lockhashentry(addr);
 483	struct lock_stat *ret;
 484
 485	hlist_for_each_entry(ret, entry, hash_entry) {
 486		if (ret->addr == addr)
 487			return ret;
 488	}
 489	return NULL;
 490}
 491
 492struct lock_stat *lock_stat_findnew(u64 addr, const char *name, int flags)
 493{
 494	struct hlist_head *entry = lockhashentry(addr);
 495	struct lock_stat *ret, *new;
 496
 497	hlist_for_each_entry(ret, entry, hash_entry) {
 498		if (ret->addr == addr)
 499			return ret;
 500	}
 501
 502	new = zalloc(sizeof(struct lock_stat));
 503	if (!new)
 504		goto alloc_failed;
 505
 506	new->addr = addr;
 507	new->name = strdup(name);
 508	if (!new->name) {
 509		free(new);
 510		goto alloc_failed;
 511	}
 512
 513	new->flags = flags;
 514	new->wait_time_min = ULLONG_MAX;
 515
 516	hlist_add_head(&new->hash_entry, entry);
 517	return new;
 518
 519alloc_failed:
 520	pr_err("memory allocation failed\n");
 521	return NULL;
 522}
 523
 524bool match_callstack_filter(struct machine *machine, u64 *callstack)
 525{
 526	struct map *kmap;
 527	struct symbol *sym;
 528	u64 ip;
 529	const char *arch = perf_env__arch(machine->env);
 530
 531	if (list_empty(&callstack_filters))
 532		return true;
 533
 534	for (int i = 0; i < max_stack_depth; i++) {
 535		struct callstack_filter *filter;
 536
 537		/*
 538		 * In powerpc, the callchain saved by kernel always includes
 539		 * first three entries as the NIP (next instruction pointer),
 540		 * LR (link register), and the contents of LR save area in the
 541		 * second stack frame. In certain scenarios its possible to have
 542		 * invalid kernel instruction addresses in either LR or the second
 543		 * stack frame's LR. In that case, kernel will store that address as
 544		 * zero.
 545		 *
 546		 * The below check will continue to look into callstack,
 547		 * incase first or second callstack index entry has 0
 548		 * address for powerpc.
 549		 */
 550		if (!callstack || (!callstack[i] && (strcmp(arch, "powerpc") ||
 551						(i != 1 && i != 2))))
 552			break;
 553
 554		ip = callstack[i];
 555		sym = machine__find_kernel_symbol(machine, ip, &kmap);
 556		if (sym == NULL)
 557			continue;
 558
 559		list_for_each_entry(filter, &callstack_filters, list) {
 560			if (strstr(sym->name, filter->name))
 561				return true;
 562		}
 563	}
 564	return false;
 565}
 566
 567struct trace_lock_handler {
 568	/* it's used on CONFIG_LOCKDEP */
 569	int (*acquire_event)(struct evsel *evsel,
 570			     struct perf_sample *sample);
 571
 572	/* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
 573	int (*acquired_event)(struct evsel *evsel,
 574			      struct perf_sample *sample);
 575
 576	/* it's used on CONFIG_LOCKDEP && CONFIG_LOCK_STAT */
 577	int (*contended_event)(struct evsel *evsel,
 578			       struct perf_sample *sample);
 579
 580	/* it's used on CONFIG_LOCKDEP */
 581	int (*release_event)(struct evsel *evsel,
 582			     struct perf_sample *sample);
 583
 584	/* it's used when CONFIG_LOCKDEP is off */
 585	int (*contention_begin_event)(struct evsel *evsel,
 586				      struct perf_sample *sample);
 587
 588	/* it's used when CONFIG_LOCKDEP is off */
 589	int (*contention_end_event)(struct evsel *evsel,
 590				    struct perf_sample *sample);
 591};
 592
 593static struct lock_seq_stat *get_seq(struct thread_stat *ts, u64 addr)
 594{
 595	struct lock_seq_stat *seq;
 596
 597	list_for_each_entry(seq, &ts->seq_list, list) {
 598		if (seq->addr == addr)
 599			return seq;
 600	}
 601
 602	seq = zalloc(sizeof(struct lock_seq_stat));
 603	if (!seq) {
 604		pr_err("memory allocation failed\n");
 605		return NULL;
 606	}
 607	seq->state = SEQ_STATE_UNINITIALIZED;
 608	seq->addr = addr;
 609
 610	list_add(&seq->list, &ts->seq_list);
 611	return seq;
 612}
 613
 614enum broken_state {
 615	BROKEN_ACQUIRE,
 616	BROKEN_ACQUIRED,
 617	BROKEN_CONTENDED,
 618	BROKEN_RELEASE,
 619	BROKEN_MAX,
 620};
 621
 622static int bad_hist[BROKEN_MAX];
 623
 624enum acquire_flags {
 625	TRY_LOCK = 1,
 626	READ_LOCK = 2,
 627};
 628
 629static int get_key_by_aggr_mode_simple(u64 *key, u64 addr, u32 tid)
 630{
 631	switch (aggr_mode) {
 632	case LOCK_AGGR_ADDR:
 633		*key = addr;
 634		break;
 635	case LOCK_AGGR_TASK:
 636		*key = tid;
 637		break;
 638	case LOCK_AGGR_CALLER:
 639	case LOCK_AGGR_CGROUP:
 640	default:
 641		pr_err("Invalid aggregation mode: %d\n", aggr_mode);
 642		return -EINVAL;
 643	}
 644	return 0;
 645}
 646
 647static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample);
 648
 649static int get_key_by_aggr_mode(u64 *key, u64 addr, struct evsel *evsel,
 650				 struct perf_sample *sample)
 651{
 652	if (aggr_mode == LOCK_AGGR_CALLER) {
 653		*key = callchain_id(evsel, sample);
 654		return 0;
 655	}
 656	return get_key_by_aggr_mode_simple(key, addr, sample->tid);
 657}
 658
 659static int report_lock_acquire_event(struct evsel *evsel,
 660				     struct perf_sample *sample)
 661{
 662	struct lock_stat *ls;
 663	struct thread_stat *ts;
 664	struct lock_seq_stat *seq;
 665	const char *name = evsel__strval(evsel, sample, "name");
 666	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 667	int flag = evsel__intval(evsel, sample, "flags");
 668	u64 key;
 669	int ret;
 670
 671	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 672	if (ret < 0)
 673		return ret;
 674
 675	ls = lock_stat_findnew(key, name, 0);
 676	if (!ls)
 677		return -ENOMEM;
 678
 679	ts = thread_stat_findnew(sample->tid);
 680	if (!ts)
 681		return -ENOMEM;
 682
 683	seq = get_seq(ts, addr);
 684	if (!seq)
 685		return -ENOMEM;
 686
 687	switch (seq->state) {
 688	case SEQ_STATE_UNINITIALIZED:
 689	case SEQ_STATE_RELEASED:
 690		if (!flag) {
 691			seq->state = SEQ_STATE_ACQUIRING;
 692		} else {
 693			if (flag & TRY_LOCK)
 694				ls->nr_trylock++;
 695			if (flag & READ_LOCK)
 696				ls->nr_readlock++;
 697			seq->state = SEQ_STATE_READ_ACQUIRED;
 698			seq->read_count = 1;
 699			ls->nr_acquired++;
 700		}
 701		break;
 702	case SEQ_STATE_READ_ACQUIRED:
 703		if (flag & READ_LOCK) {
 704			seq->read_count++;
 705			ls->nr_acquired++;
 706			goto end;
 707		} else {
 708			goto broken;
 709		}
 710		break;
 711	case SEQ_STATE_ACQUIRED:
 712	case SEQ_STATE_ACQUIRING:
 713	case SEQ_STATE_CONTENDED:
 714broken:
 715		/* broken lock sequence */
 716		if (!ls->broken) {
 717			ls->broken = 1;
 718			bad_hist[BROKEN_ACQUIRE]++;
 719		}
 720		list_del_init(&seq->list);
 721		free(seq);
 722		goto end;
 723	default:
 724		BUG_ON("Unknown state of lock sequence found!\n");
 725		break;
 726	}
 727
 728	ls->nr_acquire++;
 729	seq->prev_event_time = sample->time;
 730end:
 731	return 0;
 732}
 733
 734static int report_lock_acquired_event(struct evsel *evsel,
 735				      struct perf_sample *sample)
 736{
 737	struct lock_stat *ls;
 738	struct thread_stat *ts;
 739	struct lock_seq_stat *seq;
 740	u64 contended_term;
 741	const char *name = evsel__strval(evsel, sample, "name");
 742	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 743	u64 key;
 744	int ret;
 745
 746	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 747	if (ret < 0)
 748		return ret;
 749
 750	ls = lock_stat_findnew(key, name, 0);
 751	if (!ls)
 752		return -ENOMEM;
 753
 754	ts = thread_stat_findnew(sample->tid);
 755	if (!ts)
 756		return -ENOMEM;
 757
 758	seq = get_seq(ts, addr);
 759	if (!seq)
 760		return -ENOMEM;
 761
 762	switch (seq->state) {
 763	case SEQ_STATE_UNINITIALIZED:
 764		/* orphan event, do nothing */
 765		return 0;
 766	case SEQ_STATE_ACQUIRING:
 767		break;
 768	case SEQ_STATE_CONTENDED:
 769		contended_term = sample->time - seq->prev_event_time;
 770		ls->wait_time_total += contended_term;
 771		if (contended_term < ls->wait_time_min)
 772			ls->wait_time_min = contended_term;
 773		if (ls->wait_time_max < contended_term)
 774			ls->wait_time_max = contended_term;
 775		break;
 776	case SEQ_STATE_RELEASED:
 777	case SEQ_STATE_ACQUIRED:
 778	case SEQ_STATE_READ_ACQUIRED:
 779		/* broken lock sequence */
 780		if (!ls->broken) {
 781			ls->broken = 1;
 782			bad_hist[BROKEN_ACQUIRED]++;
 783		}
 784		list_del_init(&seq->list);
 785		free(seq);
 786		goto end;
 787	default:
 788		BUG_ON("Unknown state of lock sequence found!\n");
 789		break;
 790	}
 791
 792	seq->state = SEQ_STATE_ACQUIRED;
 793	ls->nr_acquired++;
 794	ls->avg_wait_time = ls->nr_contended ? ls->wait_time_total/ls->nr_contended : 0;
 795	seq->prev_event_time = sample->time;
 796end:
 797	return 0;
 798}
 799
 800static int report_lock_contended_event(struct evsel *evsel,
 801				       struct perf_sample *sample)
 802{
 803	struct lock_stat *ls;
 804	struct thread_stat *ts;
 805	struct lock_seq_stat *seq;
 806	const char *name = evsel__strval(evsel, sample, "name");
 807	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 808	u64 key;
 809	int ret;
 810
 811	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 812	if (ret < 0)
 813		return ret;
 814
 815	ls = lock_stat_findnew(key, name, 0);
 816	if (!ls)
 817		return -ENOMEM;
 818
 819	ts = thread_stat_findnew(sample->tid);
 820	if (!ts)
 821		return -ENOMEM;
 822
 823	seq = get_seq(ts, addr);
 824	if (!seq)
 825		return -ENOMEM;
 826
 827	switch (seq->state) {
 828	case SEQ_STATE_UNINITIALIZED:
 829		/* orphan event, do nothing */
 830		return 0;
 831	case SEQ_STATE_ACQUIRING:
 832		break;
 833	case SEQ_STATE_RELEASED:
 834	case SEQ_STATE_ACQUIRED:
 835	case SEQ_STATE_READ_ACQUIRED:
 836	case SEQ_STATE_CONTENDED:
 837		/* broken lock sequence */
 838		if (!ls->broken) {
 839			ls->broken = 1;
 840			bad_hist[BROKEN_CONTENDED]++;
 841		}
 842		list_del_init(&seq->list);
 843		free(seq);
 844		goto end;
 845	default:
 846		BUG_ON("Unknown state of lock sequence found!\n");
 847		break;
 848	}
 849
 850	seq->state = SEQ_STATE_CONTENDED;
 851	ls->nr_contended++;
 852	ls->avg_wait_time = ls->wait_time_total/ls->nr_contended;
 853	seq->prev_event_time = sample->time;
 854end:
 855	return 0;
 856}
 857
 858static int report_lock_release_event(struct evsel *evsel,
 859				     struct perf_sample *sample)
 860{
 861	struct lock_stat *ls;
 862	struct thread_stat *ts;
 863	struct lock_seq_stat *seq;
 864	const char *name = evsel__strval(evsel, sample, "name");
 865	u64 addr = evsel__intval(evsel, sample, "lockdep_addr");
 866	u64 key;
 867	int ret;
 868
 869	ret = get_key_by_aggr_mode_simple(&key, addr, sample->tid);
 870	if (ret < 0)
 871		return ret;
 872
 873	ls = lock_stat_findnew(key, name, 0);
 874	if (!ls)
 875		return -ENOMEM;
 876
 877	ts = thread_stat_findnew(sample->tid);
 878	if (!ts)
 879		return -ENOMEM;
 880
 881	seq = get_seq(ts, addr);
 882	if (!seq)
 883		return -ENOMEM;
 884
 885	switch (seq->state) {
 886	case SEQ_STATE_UNINITIALIZED:
 887		goto end;
 888	case SEQ_STATE_ACQUIRED:
 889		break;
 890	case SEQ_STATE_READ_ACQUIRED:
 891		seq->read_count--;
 892		BUG_ON(seq->read_count < 0);
 893		if (seq->read_count) {
 894			ls->nr_release++;
 895			goto end;
 896		}
 897		break;
 898	case SEQ_STATE_ACQUIRING:
 899	case SEQ_STATE_CONTENDED:
 900	case SEQ_STATE_RELEASED:
 901		/* broken lock sequence */
 902		if (!ls->broken) {
 903			ls->broken = 1;
 904			bad_hist[BROKEN_RELEASE]++;
 905		}
 906		goto free_seq;
 907	default:
 908		BUG_ON("Unknown state of lock sequence found!\n");
 909		break;
 910	}
 911
 912	ls->nr_release++;
 913free_seq:
 914	list_del_init(&seq->list);
 915	free(seq);
 916end:
 917	return 0;
 918}
 919
 920static int get_symbol_name_offset(struct map *map, struct symbol *sym, u64 ip,
 921				  char *buf, int size)
 922{
 923	u64 offset;
 924
 925	if (map == NULL || sym == NULL) {
 926		buf[0] = '\0';
 927		return 0;
 928	}
 929
 930	offset = map__map_ip(map, ip) - sym->start;
 931
 932	if (offset)
 933		return scnprintf(buf, size, "%s+%#lx", sym->name, offset);
 934	else
 935		return strlcpy(buf, sym->name, size);
 936}
 937static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sample,
 938				  char *buf, int size)
 939{
 940	struct thread *thread;
 941	struct callchain_cursor *cursor;
 942	struct machine *machine = &session->machines.host;
 943	struct symbol *sym;
 944	int skip = 0;
 945	int ret;
 946
 947	/* lock names will be replaced to task name later */
 948	if (show_thread_stats)
 949		return -1;
 950
 951	thread = machine__findnew_thread(machine, -1, sample->pid);
 952	if (thread == NULL)
 953		return -1;
 954
 955	cursor = get_tls_callchain_cursor();
 956
 957	/* use caller function name from the callchain */
 958	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
 959					NULL, NULL, max_stack_depth);
 960	if (ret != 0) {
 961		thread__put(thread);
 962		return -1;
 963	}
 964
 965	callchain_cursor_commit(cursor);
 966	thread__put(thread);
 967
 968	while (true) {
 969		struct callchain_cursor_node *node;
 970
 971		node = callchain_cursor_current(cursor);
 972		if (node == NULL)
 973			break;
 974
 975		/* skip first few entries - for lock functions */
 976		if (++skip <= stack_skip)
 977			goto next;
 978
 979		sym = node->ms.sym;
 980		if (sym && !machine__is_lock_function(machine, node->ip)) {
 981			get_symbol_name_offset(node->ms.map, sym, node->ip,
 982					       buf, size);
 983			return 0;
 984		}
 985
 986next:
 987		callchain_cursor_advance(cursor);
 988	}
 989	return -1;
 990}
 991
 992static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
 993{
 994	struct callchain_cursor *cursor;
 995	struct machine *machine = &session->machines.host;
 996	struct thread *thread;
 997	u64 hash = 0;
 998	int skip = 0;
 999	int ret;
1000
1001	thread = machine__findnew_thread(machine, -1, sample->pid);
1002	if (thread == NULL)
1003		return -1;
1004
1005	cursor = get_tls_callchain_cursor();
1006	/* use caller function name from the callchain */
1007	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
1008					NULL, NULL, max_stack_depth);
1009	thread__put(thread);
1010
1011	if (ret != 0)
1012		return -1;
1013
1014	callchain_cursor_commit(cursor);
1015
1016	while (true) {
1017		struct callchain_cursor_node *node;
1018
1019		node = callchain_cursor_current(cursor);
1020		if (node == NULL)
1021			break;
1022
1023		/* skip first few entries - for lock functions */
1024		if (++skip <= stack_skip)
1025			goto next;
1026
1027		if (node->ms.sym && machine__is_lock_function(machine, node->ip))
1028			goto next;
1029
1030		hash ^= hash_long((unsigned long)node->ip, 64);
1031
1032next:
1033		callchain_cursor_advance(cursor);
1034	}
1035	return hash;
1036}
1037
1038static u64 *get_callstack(struct perf_sample *sample, int max_stack)
1039{
1040	u64 *callstack;
1041	u64 i;
1042	int c;
1043
1044	callstack = calloc(max_stack, sizeof(*callstack));
1045	if (callstack == NULL)
1046		return NULL;
1047
1048	for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) {
1049		u64 ip = sample->callchain->ips[i];
1050
1051		if (ip >= PERF_CONTEXT_MAX)
1052			continue;
1053
1054		callstack[c++] = ip;
1055	}
1056	return callstack;
1057}
1058
1059static int report_lock_contention_begin_event(struct evsel *evsel,
1060					      struct perf_sample *sample)
1061{
1062	struct lock_stat *ls;
1063	struct thread_stat *ts;
1064	struct lock_seq_stat *seq;
1065	u64 addr = evsel__intval(evsel, sample, "lock_addr");
1066	unsigned int flags = evsel__intval(evsel, sample, "flags");
1067	u64 key;
1068	int i, ret;
1069	static bool kmap_loaded;
1070	struct machine *machine = &session->machines.host;
1071	struct map *kmap;
1072	struct symbol *sym;
1073
1074	ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1075	if (ret < 0)
1076		return ret;
1077
1078	if (!kmap_loaded) {
1079		unsigned long *addrs;
1080
1081		/* make sure it loads the kernel map to find lock symbols */
1082		map__load(machine__kernel_map(machine));
1083		kmap_loaded = true;
1084
1085		/* convert (kernel) symbols to addresses */
1086		for (i = 0; i < filters.nr_syms; i++) {
1087			sym = machine__find_kernel_symbol_by_name(machine,
1088								  filters.syms[i],
1089								  &kmap);
1090			if (sym == NULL) {
1091				pr_warning("ignore unknown symbol: %s\n",
1092					   filters.syms[i]);
1093				continue;
1094			}
1095
1096			addrs = realloc(filters.addrs,
1097					(filters.nr_addrs + 1) * sizeof(*addrs));
1098			if (addrs == NULL) {
1099				pr_warning("memory allocation failure\n");
1100				return -ENOMEM;
1101			}
1102
1103			addrs[filters.nr_addrs++] = map__unmap_ip(kmap, sym->start);
1104			filters.addrs = addrs;
1105		}
1106	}
1107
1108	ls = lock_stat_find(key);
1109	if (!ls) {
1110		char buf[128];
1111		const char *name = "";
1112
1113		switch (aggr_mode) {
1114		case LOCK_AGGR_ADDR:
1115			sym = machine__find_kernel_symbol(machine, key, &kmap);
1116			if (sym)
1117				name = sym->name;
1118			break;
1119		case LOCK_AGGR_CALLER:
1120			name = buf;
1121			if (lock_contention_caller(evsel, sample, buf, sizeof(buf)) < 0)
1122				name = "Unknown";
1123			break;
1124		case LOCK_AGGR_CGROUP:
1125		case LOCK_AGGR_TASK:
1126		default:
1127			break;
1128		}
1129
1130		ls = lock_stat_findnew(key, name, flags);
1131		if (!ls)
1132			return -ENOMEM;
 
 
 
 
 
 
1133	}
1134
1135	if (filters.nr_types) {
1136		bool found = false;
1137
1138		for (i = 0; i < filters.nr_types; i++) {
1139			if (flags == filters.types[i]) {
1140				found = true;
1141				break;
1142			}
1143		}
1144
1145		if (!found)
1146			return 0;
1147	}
1148
1149	if (filters.nr_addrs) {
1150		bool found = false;
1151
1152		for (i = 0; i < filters.nr_addrs; i++) {
1153			if (addr == filters.addrs[i]) {
1154				found = true;
1155				break;
1156			}
1157		}
1158
1159		if (!found)
1160			return 0;
1161	}
1162
1163	if (needs_callstack()) {
1164		u64 *callstack = get_callstack(sample, max_stack_depth);
1165		if (callstack == NULL)
1166			return -ENOMEM;
1167
1168		if (!match_callstack_filter(machine, callstack)) {
1169			free(callstack);
1170			return 0;
1171		}
1172
1173		if (ls->callstack == NULL)
1174			ls->callstack = callstack;
1175		else
1176			free(callstack);
1177	}
1178
1179	ts = thread_stat_findnew(sample->tid);
1180	if (!ts)
1181		return -ENOMEM;
1182
1183	seq = get_seq(ts, addr);
1184	if (!seq)
1185		return -ENOMEM;
1186
1187	switch (seq->state) {
1188	case SEQ_STATE_UNINITIALIZED:
1189	case SEQ_STATE_ACQUIRED:
1190		break;
1191	case SEQ_STATE_CONTENDED:
1192		/*
1193		 * It can have nested contention begin with mutex spinning,
1194		 * then we would use the original contention begin event and
1195		 * ignore the second one.
1196		 */
1197		goto end;
1198	case SEQ_STATE_ACQUIRING:
1199	case SEQ_STATE_READ_ACQUIRED:
1200	case SEQ_STATE_RELEASED:
1201		/* broken lock sequence */
1202		if (!ls->broken) {
1203			ls->broken = 1;
1204			bad_hist[BROKEN_CONTENDED]++;
1205		}
1206		list_del_init(&seq->list);
1207		free(seq);
1208		goto end;
1209	default:
1210		BUG_ON("Unknown state of lock sequence found!\n");
1211		break;
1212	}
1213
1214	if (seq->state != SEQ_STATE_CONTENDED) {
1215		seq->state = SEQ_STATE_CONTENDED;
1216		seq->prev_event_time = sample->time;
1217		ls->nr_contended++;
1218	}
1219end:
1220	return 0;
1221}
1222
1223static int report_lock_contention_end_event(struct evsel *evsel,
1224					    struct perf_sample *sample)
1225{
1226	struct lock_stat *ls;
1227	struct thread_stat *ts;
1228	struct lock_seq_stat *seq;
1229	u64 contended_term;
1230	u64 addr = evsel__intval(evsel, sample, "lock_addr");
1231	u64 key;
1232	int ret;
1233
1234	ret = get_key_by_aggr_mode(&key, addr, evsel, sample);
1235	if (ret < 0)
1236		return ret;
1237
1238	ls = lock_stat_find(key);
1239	if (!ls)
1240		return 0;
1241
1242	ts = thread_stat_find(sample->tid);
1243	if (!ts)
1244		return 0;
1245
1246	seq = get_seq(ts, addr);
1247	if (!seq)
1248		return -ENOMEM;
1249
1250	switch (seq->state) {
1251	case SEQ_STATE_UNINITIALIZED:
1252		goto end;
1253	case SEQ_STATE_CONTENDED:
1254		contended_term = sample->time - seq->prev_event_time;
1255		ls->wait_time_total += contended_term;
1256		if (contended_term < ls->wait_time_min)
1257			ls->wait_time_min = contended_term;
1258		if (ls->wait_time_max < contended_term)
1259			ls->wait_time_max = contended_term;
1260		break;
1261	case SEQ_STATE_ACQUIRING:
1262	case SEQ_STATE_ACQUIRED:
1263	case SEQ_STATE_READ_ACQUIRED:
1264	case SEQ_STATE_RELEASED:
1265		/* broken lock sequence */
1266		if (!ls->broken) {
1267			ls->broken = 1;
1268			bad_hist[BROKEN_ACQUIRED]++;
1269		}
1270		list_del_init(&seq->list);
1271		free(seq);
1272		goto end;
1273	default:
1274		BUG_ON("Unknown state of lock sequence found!\n");
1275		break;
1276	}
1277
1278	seq->state = SEQ_STATE_ACQUIRED;
1279	ls->nr_acquired++;
1280	ls->avg_wait_time = ls->wait_time_total/ls->nr_acquired;
1281end:
1282	return 0;
1283}
1284
1285/* lock oriented handlers */
1286/* TODO: handlers for CPU oriented, thread oriented */
1287static struct trace_lock_handler report_lock_ops  = {
1288	.acquire_event		= report_lock_acquire_event,
1289	.acquired_event		= report_lock_acquired_event,
1290	.contended_event	= report_lock_contended_event,
1291	.release_event		= report_lock_release_event,
1292	.contention_begin_event	= report_lock_contention_begin_event,
1293	.contention_end_event	= report_lock_contention_end_event,
1294};
1295
1296static struct trace_lock_handler contention_lock_ops  = {
1297	.contention_begin_event	= report_lock_contention_begin_event,
1298	.contention_end_event	= report_lock_contention_end_event,
1299};
1300
1301
1302static struct trace_lock_handler *trace_handler;
1303
1304static int evsel__process_lock_acquire(struct evsel *evsel, struct perf_sample *sample)
1305{
1306	if (trace_handler->acquire_event)
1307		return trace_handler->acquire_event(evsel, sample);
1308	return 0;
1309}
1310
1311static int evsel__process_lock_acquired(struct evsel *evsel, struct perf_sample *sample)
1312{
1313	if (trace_handler->acquired_event)
1314		return trace_handler->acquired_event(evsel, sample);
1315	return 0;
1316}
1317
1318static int evsel__process_lock_contended(struct evsel *evsel, struct perf_sample *sample)
1319{
1320	if (trace_handler->contended_event)
1321		return trace_handler->contended_event(evsel, sample);
1322	return 0;
1323}
1324
1325static int evsel__process_lock_release(struct evsel *evsel, struct perf_sample *sample)
1326{
1327	if (trace_handler->release_event)
1328		return trace_handler->release_event(evsel, sample);
1329	return 0;
1330}
1331
1332static int evsel__process_contention_begin(struct evsel *evsel, struct perf_sample *sample)
1333{
1334	if (trace_handler->contention_begin_event)
1335		return trace_handler->contention_begin_event(evsel, sample);
1336	return 0;
1337}
1338
1339static int evsel__process_contention_end(struct evsel *evsel, struct perf_sample *sample)
1340{
1341	if (trace_handler->contention_end_event)
1342		return trace_handler->contention_end_event(evsel, sample);
1343	return 0;
1344}
1345
1346static void print_bad_events(int bad, int total)
1347{
1348	/* Output for debug, this have to be removed */
1349	int i;
1350	int broken = 0;
1351	const char *name[4] =
1352		{ "acquire", "acquired", "contended", "release" };
1353
1354	for (i = 0; i < BROKEN_MAX; i++)
1355		broken += bad_hist[i];
1356
1357	if (quiet || total == 0 || (broken == 0 && verbose <= 0))
1358		return;
1359
1360	fprintf(lock_output, "\n=== output for debug ===\n\n");
1361	fprintf(lock_output, "bad: %d, total: %d\n", bad, total);
1362	fprintf(lock_output, "bad rate: %.2f %%\n", (double)bad / (double)total * 100);
1363	fprintf(lock_output, "histogram of events caused bad sequence\n");
1364	for (i = 0; i < BROKEN_MAX; i++)
1365		fprintf(lock_output, " %10s: %d\n", name[i], bad_hist[i]);
1366}
1367
1368/* TODO: various way to print, coloring, nano or milli sec */
1369static void print_result(void)
1370{
1371	struct lock_stat *st;
1372	struct lock_key *key;
1373	char cut_name[20];
1374	int bad, total, printed;
1375
1376	if (!quiet) {
1377		fprintf(lock_output, "%20s ", "Name");
1378		list_for_each_entry(key, &lock_keys, list)
1379			fprintf(lock_output, "%*s ", key->len, key->header);
1380		fprintf(lock_output, "\n\n");
1381	}
1382
1383	bad = total = printed = 0;
1384	while ((st = pop_from_result())) {
1385		total++;
1386		if (st->broken)
1387			bad++;
1388		if (!st->nr_acquired)
1389			continue;
1390
1391		bzero(cut_name, 20);
1392
1393		if (strlen(st->name) < 20) {
1394			/* output raw name */
1395			const char *name = st->name;
1396
1397			if (show_thread_stats) {
1398				struct thread *t;
1399
1400				/* st->addr contains tid of thread */
1401				t = perf_session__findnew(session, st->addr);
1402				name = thread__comm_str(t);
1403			}
1404
1405			fprintf(lock_output, "%20s ", name);
1406		} else {
1407			strncpy(cut_name, st->name, 16);
1408			cut_name[16] = '.';
1409			cut_name[17] = '.';
1410			cut_name[18] = '.';
1411			cut_name[19] = '\0';
1412			/* cut off name for saving output style */
1413			fprintf(lock_output, "%20s ", cut_name);
1414		}
1415
1416		list_for_each_entry(key, &lock_keys, list) {
1417			key->print(key, st);
1418			fprintf(lock_output, " ");
1419		}
1420		fprintf(lock_output, "\n");
1421
1422		if (++printed >= print_nr_entries)
1423			break;
1424	}
1425
1426	print_bad_events(bad, total);
1427}
1428
1429static bool info_threads, info_map;
1430
1431static void dump_threads(void)
1432{
1433	struct thread_stat *st;
1434	struct rb_node *node;
1435	struct thread *t;
1436
1437	fprintf(lock_output, "%10s: comm\n", "Thread ID");
1438
1439	node = rb_first(&thread_stats);
1440	while (node) {
1441		st = container_of(node, struct thread_stat, rb);
1442		t = perf_session__findnew(session, st->tid);
1443		fprintf(lock_output, "%10d: %s\n", st->tid, thread__comm_str(t));
1444		node = rb_next(node);
1445		thread__put(t);
1446	}
1447}
1448
1449static int compare_maps(struct lock_stat *a, struct lock_stat *b)
1450{
1451	int ret;
1452
1453	if (a->name && b->name)
1454		ret = strcmp(a->name, b->name);
1455	else
1456		ret = !!a->name - !!b->name;
1457
1458	if (!ret)
1459		return a->addr < b->addr;
1460	else
1461		return ret < 0;
1462}
1463
1464static void dump_map(void)
1465{
1466	unsigned int i;
1467	struct lock_stat *st;
1468
1469	fprintf(lock_output, "Address of instance: name of class\n");
1470	for (i = 0; i < LOCKHASH_SIZE; i++) {
1471		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1472			insert_to_result(st, compare_maps);
1473		}
1474	}
1475
1476	while ((st = pop_from_result()))
1477		fprintf(lock_output, " %#llx: %s\n", (unsigned long long)st->addr, st->name);
1478}
1479
1480static int dump_info(void)
1481{
1482	int rc = 0;
1483
1484	if (info_threads)
1485		dump_threads();
1486	else if (info_map)
1487		dump_map();
1488	else {
1489		rc = -1;
1490		pr_err("Unknown type of information\n");
1491	}
1492
1493	return rc;
1494}
1495
1496static const struct evsel_str_handler lock_tracepoints[] = {
1497	{ "lock:lock_acquire",	 evsel__process_lock_acquire,   }, /* CONFIG_LOCKDEP */
1498	{ "lock:lock_acquired",	 evsel__process_lock_acquired,  }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1499	{ "lock:lock_contended", evsel__process_lock_contended, }, /* CONFIG_LOCKDEP, CONFIG_LOCK_STAT */
1500	{ "lock:lock_release",	 evsel__process_lock_release,   }, /* CONFIG_LOCKDEP */
1501};
1502
1503static const struct evsel_str_handler contention_tracepoints[] = {
1504	{ "lock:contention_begin", evsel__process_contention_begin, },
1505	{ "lock:contention_end",   evsel__process_contention_end,   },
1506};
1507
1508static int process_event_update(struct perf_tool *tool,
1509				union perf_event *event,
1510				struct evlist **pevlist)
1511{
1512	int ret;
1513
1514	ret = perf_event__process_event_update(tool, event, pevlist);
1515	if (ret < 0)
1516		return ret;
1517
1518	/* this can return -EEXIST since we call it for each evsel */
1519	perf_session__set_tracepoints_handlers(session, lock_tracepoints);
1520	perf_session__set_tracepoints_handlers(session, contention_tracepoints);
1521	return 0;
1522}
1523
1524typedef int (*tracepoint_handler)(struct evsel *evsel,
1525				  struct perf_sample *sample);
1526
1527static int process_sample_event(struct perf_tool *tool __maybe_unused,
1528				union perf_event *event,
1529				struct perf_sample *sample,
1530				struct evsel *evsel,
1531				struct machine *machine)
1532{
1533	int err = 0;
1534	struct thread *thread = machine__findnew_thread(machine, sample->pid,
1535							sample->tid);
1536
1537	if (thread == NULL) {
1538		pr_debug("problem processing %d event, skipping it.\n",
1539			event->header.type);
1540		return -1;
1541	}
1542
1543	if (evsel->handler != NULL) {
1544		tracepoint_handler f = evsel->handler;
1545		err = f(evsel, sample);
1546	}
1547
1548	thread__put(thread);
1549
1550	return err;
1551}
1552
1553static void combine_result(void)
1554{
1555	unsigned int i;
1556	struct lock_stat *st;
1557
1558	if (!combine_locks)
1559		return;
1560
1561	for (i = 0; i < LOCKHASH_SIZE; i++) {
1562		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1563			combine_lock_stats(st);
1564		}
1565	}
1566}
1567
1568static void sort_result(void)
1569{
1570	unsigned int i;
1571	struct lock_stat *st;
1572
1573	for (i = 0; i < LOCKHASH_SIZE; i++) {
1574		hlist_for_each_entry(st, &lockhash_table[i], hash_entry) {
1575			insert_to_result(st, compare);
1576		}
1577	}
1578}
1579
1580static const struct {
1581	unsigned int flags;
1582	const char *str;
1583	const char *name;
1584} lock_type_table[] = {
1585	{ 0,				"semaphore",	"semaphore" },
1586	{ LCB_F_SPIN,			"spinlock",	"spinlock" },
1587	{ LCB_F_SPIN | LCB_F_READ,	"rwlock:R",	"rwlock" },
1588	{ LCB_F_SPIN | LCB_F_WRITE,	"rwlock:W",	"rwlock" },
1589	{ LCB_F_READ,			"rwsem:R",	"rwsem" },
1590	{ LCB_F_WRITE,			"rwsem:W",	"rwsem" },
1591	{ LCB_F_RT,			"rt-mutex",	"rt-mutex" },
1592	{ LCB_F_RT | LCB_F_READ,	"rwlock-rt:R",	"rwlock-rt" },
1593	{ LCB_F_RT | LCB_F_WRITE,	"rwlock-rt:W",	"rwlock-rt" },
1594	{ LCB_F_PERCPU | LCB_F_READ,	"pcpu-sem:R",	"percpu-rwsem" },
1595	{ LCB_F_PERCPU | LCB_F_WRITE,	"pcpu-sem:W",	"percpu-rwsem" },
1596	{ LCB_F_MUTEX,			"mutex",	"mutex" },
1597	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex",	"mutex" },
1598	/* alias for get_type_flag() */
1599	{ LCB_F_MUTEX | LCB_F_SPIN,	"mutex-spin",	"mutex" },
1600};
1601
1602static const char *get_type_str(unsigned int flags)
1603{
1604	flags &= LCB_F_MAX_FLAGS - 1;
1605
1606	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1607		if (lock_type_table[i].flags == flags)
1608			return lock_type_table[i].str;
1609	}
1610	return "unknown";
1611}
1612
1613static const char *get_type_name(unsigned int flags)
1614{
1615	flags &= LCB_F_MAX_FLAGS - 1;
1616
1617	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1618		if (lock_type_table[i].flags == flags)
1619			return lock_type_table[i].name;
1620	}
1621	return "unknown";
1622}
1623
1624static unsigned int get_type_flag(const char *str)
1625{
1626	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1627		if (!strcmp(lock_type_table[i].name, str))
1628			return lock_type_table[i].flags;
1629	}
1630	for (unsigned int i = 0; i < ARRAY_SIZE(lock_type_table); i++) {
1631		if (!strcmp(lock_type_table[i].str, str))
1632			return lock_type_table[i].flags;
1633	}
1634	return UINT_MAX;
1635}
1636
1637static void lock_filter_finish(void)
1638{
1639	zfree(&filters.types);
1640	filters.nr_types = 0;
1641
1642	zfree(&filters.addrs);
1643	filters.nr_addrs = 0;
1644
1645	for (int i = 0; i < filters.nr_syms; i++)
1646		free(filters.syms[i]);
1647
1648	zfree(&filters.syms);
1649	filters.nr_syms = 0;
1650
1651	zfree(&filters.cgrps);
1652	filters.nr_cgrps = 0;
1653}
1654
1655static void sort_contention_result(void)
1656{
1657	sort_result();
1658}
1659
1660static void print_header_stdio(void)
1661{
 
1662	struct lock_key *key;
 
1663
1664	list_for_each_entry(key, &lock_keys, list)
1665		fprintf(lock_output, "%*s ", key->len, key->header);
1666
1667	switch (aggr_mode) {
1668	case LOCK_AGGR_TASK:
1669		fprintf(lock_output, "  %10s   %s\n\n", "pid",
1670			show_lock_owner ? "owner" : "comm");
1671		break;
1672	case LOCK_AGGR_CALLER:
1673		fprintf(lock_output, "  %10s   %s\n\n", "type", "caller");
1674		break;
1675	case LOCK_AGGR_ADDR:
1676		fprintf(lock_output, "  %16s   %s\n\n", "address", "symbol");
1677		break;
1678	case LOCK_AGGR_CGROUP:
1679		fprintf(lock_output, "  %s\n\n", "cgroup");
1680		break;
1681	default:
1682		break;
1683	}
1684}
1685
1686static void print_header_csv(const char *sep)
1687{
1688	struct lock_key *key;
1689
1690	fprintf(lock_output, "# output: ");
1691	list_for_each_entry(key, &lock_keys, list)
1692		fprintf(lock_output, "%s%s ", key->header, sep);
1693
1694	switch (aggr_mode) {
1695	case LOCK_AGGR_TASK:
1696		fprintf(lock_output, "%s%s %s\n", "pid", sep,
1697			show_lock_owner ? "owner" : "comm");
1698		break;
1699	case LOCK_AGGR_CALLER:
1700		fprintf(lock_output, "%s%s %s", "type", sep, "caller");
1701		if (verbose > 0)
1702			fprintf(lock_output, "%s %s", sep, "stacktrace");
1703		fprintf(lock_output, "\n");
1704		break;
1705	case LOCK_AGGR_ADDR:
1706		fprintf(lock_output, "%s%s %s%s %s\n", "address", sep, "symbol", sep, "type");
1707		break;
1708	case LOCK_AGGR_CGROUP:
1709		fprintf(lock_output, "%s\n", "cgroup");
1710		break;
1711	default:
1712		break;
1713	}
1714}
1715
1716static void print_header(void)
1717{
1718	if (!quiet) {
1719		if (symbol_conf.field_sep)
1720			print_header_csv(symbol_conf.field_sep);
1721		else
1722			print_header_stdio();
1723	}
1724}
1725
1726static void print_lock_stat_stdio(struct lock_contention *con, struct lock_stat *st)
1727{
1728	struct lock_key *key;
1729	struct thread *t;
1730	int pid;
1731
1732	list_for_each_entry(key, &lock_keys, list) {
1733		key->print(key, st);
1734		fprintf(lock_output, " ");
1735	}
1736
1737	switch (aggr_mode) {
1738	case LOCK_AGGR_CALLER:
1739		fprintf(lock_output, "  %10s   %s\n", get_type_str(st->flags), st->name);
1740		break;
1741	case LOCK_AGGR_TASK:
1742		pid = st->addr;
1743		t = perf_session__findnew(session, pid);
1744		fprintf(lock_output, "  %10d   %s\n",
1745			pid, pid == -1 ? "Unknown" : thread__comm_str(t));
1746		break;
1747	case LOCK_AGGR_ADDR:
1748		fprintf(lock_output, "  %016llx   %s (%s)\n", (unsigned long long)st->addr,
1749			st->name, get_type_name(st->flags));
1750		break;
1751	case LOCK_AGGR_CGROUP:
1752		fprintf(lock_output, "  %s\n", st->name);
1753		break;
1754	default:
1755		break;
1756	}
1757
1758	if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1759		struct map *kmap;
1760		struct symbol *sym;
1761		char buf[128];
1762		u64 ip;
1763
1764		for (int i = 0; i < max_stack_depth; i++) {
1765			if (!st->callstack || !st->callstack[i])
1766				break;
1767
1768			ip = st->callstack[i];
1769			sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
1770			get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
1771			fprintf(lock_output, "\t\t\t%#lx  %s\n", (unsigned long)ip, buf);
1772		}
1773	}
1774}
1775
1776static void print_lock_stat_csv(struct lock_contention *con, struct lock_stat *st,
1777				const char *sep)
1778{
1779	struct lock_key *key;
1780	struct thread *t;
1781	int pid;
1782
1783	list_for_each_entry(key, &lock_keys, list) {
1784		key->print(key, st);
1785		fprintf(lock_output, "%s ", sep);
1786	}
1787
1788	switch (aggr_mode) {
1789	case LOCK_AGGR_CALLER:
1790		fprintf(lock_output, "%s%s %s", get_type_str(st->flags), sep, st->name);
1791		if (verbose <= 0)
1792			fprintf(lock_output, "\n");
1793		break;
1794	case LOCK_AGGR_TASK:
1795		pid = st->addr;
1796		t = perf_session__findnew(session, pid);
1797		fprintf(lock_output, "%d%s %s\n", pid, sep,
1798			pid == -1 ? "Unknown" : thread__comm_str(t));
1799		break;
1800	case LOCK_AGGR_ADDR:
1801		fprintf(lock_output, "%llx%s %s%s %s\n", (unsigned long long)st->addr, sep,
1802			st->name, sep, get_type_name(st->flags));
1803		break;
1804	case LOCK_AGGR_CGROUP:
1805		fprintf(lock_output, "%s\n",st->name);
1806		break;
1807	default:
1808		break;
1809	}
1810
1811	if (aggr_mode == LOCK_AGGR_CALLER && verbose > 0) {
1812		struct map *kmap;
1813		struct symbol *sym;
1814		char buf[128];
1815		u64 ip;
1816
1817		for (int i = 0; i < max_stack_depth; i++) {
1818			if (!st->callstack || !st->callstack[i])
1819				break;
1820
1821			ip = st->callstack[i];
1822			sym = machine__find_kernel_symbol(con->machine, ip, &kmap);
1823			get_symbol_name_offset(kmap, sym, ip, buf, sizeof(buf));
1824			fprintf(lock_output, "%s %#lx %s", i ? ":" : sep, (unsigned long) ip, buf);
1825		}
1826		fprintf(lock_output, "\n");
1827	}
1828}
1829
1830static void print_lock_stat(struct lock_contention *con, struct lock_stat *st)
1831{
1832	if (symbol_conf.field_sep)
1833		print_lock_stat_csv(con, st, symbol_conf.field_sep);
1834	else
1835		print_lock_stat_stdio(con, st);
1836}
1837
1838static void print_footer_stdio(int total, int bad, struct lock_contention_fails *fails)
1839{
1840	/* Output for debug, this have to be removed */
1841	int broken = fails->task + fails->stack + fails->time + fails->data;
1842
1843	if (!use_bpf)
1844		print_bad_events(bad, total);
1845
1846	if (quiet || total == 0 || (broken == 0 && verbose <= 0))
1847		return;
1848
1849	total += broken;
1850	fprintf(lock_output, "\n=== output for debug ===\n\n");
1851	fprintf(lock_output, "bad: %d, total: %d\n", broken, total);
1852	fprintf(lock_output, "bad rate: %.2f %%\n", 100.0 * broken / total);
1853
1854	fprintf(lock_output, "histogram of failure reasons\n");
1855	fprintf(lock_output, " %10s: %d\n", "task", fails->task);
1856	fprintf(lock_output, " %10s: %d\n", "stack", fails->stack);
1857	fprintf(lock_output, " %10s: %d\n", "time", fails->time);
1858	fprintf(lock_output, " %10s: %d\n", "data", fails->data);
1859}
1860
1861static void print_footer_csv(int total, int bad, struct lock_contention_fails *fails,
1862			     const char *sep)
1863{
1864	/* Output for debug, this have to be removed */
1865	if (use_bpf)
1866		bad = fails->task + fails->stack + fails->time + fails->data;
1867
1868	if (quiet || total == 0 || (bad == 0 && verbose <= 0))
1869		return;
1870
1871	total += bad;
1872	fprintf(lock_output, "# debug: total=%d%s bad=%d", total, sep, bad);
1873
1874	if (use_bpf) {
1875		fprintf(lock_output, "%s bad_%s=%d", sep, "task", fails->task);
1876		fprintf(lock_output, "%s bad_%s=%d", sep, "stack", fails->stack);
1877		fprintf(lock_output, "%s bad_%s=%d", sep, "time", fails->time);
1878		fprintf(lock_output, "%s bad_%s=%d", sep, "data", fails->data);
1879	} else {
1880		int i;
1881		const char *name[4] = { "acquire", "acquired", "contended", "release" };
1882
1883		for (i = 0; i < BROKEN_MAX; i++)
1884			fprintf(lock_output, "%s bad_%s=%d", sep, name[i], bad_hist[i]);
1885	}
1886	fprintf(lock_output, "\n");
1887}
1888
1889static void print_footer(int total, int bad, struct lock_contention_fails *fails)
1890{
1891	if (symbol_conf.field_sep)
1892		print_footer_csv(total, bad, fails, symbol_conf.field_sep);
1893	else
1894		print_footer_stdio(total, bad, fails);
1895}
1896
1897static void print_contention_result(struct lock_contention *con)
1898{
1899	struct lock_stat *st;
1900	int bad, total, printed;
1901
1902	if (!quiet)
1903		print_header();
1904
1905	bad = total = printed = 0;
1906
1907	while ((st = pop_from_result())) {
1908		total += use_bpf ? st->nr_contended : 1;
1909		if (st->broken)
1910			bad++;
1911
1912		if (!st->wait_time_total)
1913			continue;
1914
1915		print_lock_stat(con, st);
 
 
 
1916
1917		if (++printed >= print_nr_entries)
 
 
 
 
 
 
 
 
 
 
 
 
 
1918			break;
1919	}
1920
1921	if (print_nr_entries) {
1922		/* update the total/bad stats */
1923		while ((st = pop_from_result())) {
1924			total += use_bpf ? st->nr_contended : 1;
1925			if (st->broken)
1926				bad++;
 
 
 
 
 
 
 
 
 
1927		}
 
 
 
1928	}
1929	/* some entries are collected but hidden by the callstack filter */
1930	total += con->nr_filtered;
1931
1932	print_footer(total, bad, &con->fails);
1933}
1934
1935static bool force;
1936
1937static int __cmd_report(bool display_info)
1938{
1939	int err = -EINVAL;
1940	struct perf_tool eops = {
1941		.attr		 = perf_event__process_attr,
1942		.event_update	 = process_event_update,
1943		.sample		 = process_sample_event,
1944		.comm		 = perf_event__process_comm,
1945		.mmap		 = perf_event__process_mmap,
1946		.namespaces	 = perf_event__process_namespaces,
1947		.tracing_data	 = perf_event__process_tracing_data,
1948		.ordered_events	 = true,
1949	};
1950	struct perf_data data = {
1951		.path  = input_name,
1952		.mode  = PERF_DATA_MODE_READ,
1953		.force = force,
1954	};
1955
1956	session = perf_session__new(&data, &eops);
1957	if (IS_ERR(session)) {
1958		pr_err("Initializing perf session failed\n");
1959		return PTR_ERR(session);
1960	}
1961
 
 
1962	symbol_conf.allow_aliases = true;
1963	symbol__init(&session->header.env);
1964
1965	if (!data.is_pipe) {
1966		if (!perf_session__has_traces(session, "lock record"))
1967			goto out_delete;
1968
1969		if (perf_session__set_tracepoints_handlers(session, lock_tracepoints)) {
1970			pr_err("Initializing perf session tracepoint handlers failed\n");
1971			goto out_delete;
1972		}
1973
1974		if (perf_session__set_tracepoints_handlers(session, contention_tracepoints)) {
1975			pr_err("Initializing perf session tracepoint handlers failed\n");
1976			goto out_delete;
1977		}
1978	}
1979
1980	if (setup_output_field(false, output_fields))
1981		goto out_delete;
1982
1983	if (select_key(false))
1984		goto out_delete;
1985
1986	if (show_thread_stats)
1987		aggr_mode = LOCK_AGGR_TASK;
1988
1989	err = perf_session__process_events(session);
1990	if (err)
1991		goto out_delete;
1992
1993	setup_pager();
1994	if (display_info) /* used for info subcommand */
1995		err = dump_info();
1996	else {
1997		combine_result();
1998		sort_result();
1999		print_result();
2000	}
2001
2002out_delete:
2003	perf_session__delete(session);
2004	return err;
2005}
2006
2007static void sighandler(int sig __maybe_unused)
2008{
2009}
2010
2011static int check_lock_contention_options(const struct option *options,
2012					 const char * const *usage)
2013
2014{
2015	if (show_thread_stats && show_lock_addrs) {
2016		pr_err("Cannot use thread and addr mode together\n");
2017		parse_options_usage(usage, options, "threads", 0);
2018		parse_options_usage(NULL, options, "lock-addr", 0);
2019		return -1;
2020	}
2021
2022	if (show_lock_owner && !use_bpf) {
2023		pr_err("Lock owners are available only with BPF\n");
2024		parse_options_usage(usage, options, "lock-owner", 0);
2025		parse_options_usage(NULL, options, "use-bpf", 0);
2026		return -1;
2027	}
2028
2029	if (show_lock_owner && show_lock_addrs) {
2030		pr_err("Cannot use owner and addr mode together\n");
2031		parse_options_usage(usage, options, "lock-owner", 0);
2032		parse_options_usage(NULL, options, "lock-addr", 0);
2033		return -1;
2034	}
2035
2036	if (show_lock_cgroups && !use_bpf) {
2037		pr_err("Cgroups are available only with BPF\n");
2038		parse_options_usage(usage, options, "lock-cgroup", 0);
2039		parse_options_usage(NULL, options, "use-bpf", 0);
2040		return -1;
2041	}
2042
2043	if (show_lock_cgroups && show_lock_addrs) {
2044		pr_err("Cannot use cgroup and addr mode together\n");
2045		parse_options_usage(usage, options, "lock-cgroup", 0);
2046		parse_options_usage(NULL, options, "lock-addr", 0);
2047		return -1;
2048	}
2049
2050	if (show_lock_cgroups && show_thread_stats) {
2051		pr_err("Cannot use cgroup and thread mode together\n");
2052		parse_options_usage(usage, options, "lock-cgroup", 0);
2053		parse_options_usage(NULL, options, "threads", 0);
2054		return -1;
2055	}
2056
2057	if (symbol_conf.field_sep) {
2058		if (strstr(symbol_conf.field_sep, ":") || /* part of type flags */
2059		    strstr(symbol_conf.field_sep, "+") || /* part of caller offset */
2060		    strstr(symbol_conf.field_sep, ".")) { /* can be in a symbol name */
2061			pr_err("Cannot use the separator that is already used\n");
2062			parse_options_usage(usage, options, "x", 1);
2063			return -1;
2064		}
2065	}
2066
2067	if (show_lock_owner)
2068		show_thread_stats = true;
2069
2070	return 0;
2071}
2072
2073static int __cmd_contention(int argc, const char **argv)
2074{
2075	int err = -EINVAL;
2076	struct perf_tool eops = {
2077		.attr		 = perf_event__process_attr,
2078		.event_update	 = process_event_update,
2079		.sample		 = process_sample_event,
2080		.comm		 = perf_event__process_comm,
2081		.mmap		 = perf_event__process_mmap,
2082		.tracing_data	 = perf_event__process_tracing_data,
2083		.ordered_events	 = true,
2084	};
2085	struct perf_data data = {
2086		.path  = input_name,
2087		.mode  = PERF_DATA_MODE_READ,
2088		.force = force,
2089	};
2090	struct lock_contention con = {
2091		.target = &target,
 
2092		.map_nr_entries = bpf_map_entries,
2093		.max_stack = max_stack_depth,
2094		.stack_skip = stack_skip,
2095		.filters = &filters,
2096		.save_callstack = needs_callstack(),
2097		.owner = show_lock_owner,
2098		.cgroups = RB_ROOT,
2099	};
2100
2101	lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
2102	if (!lockhash_table)
2103		return -ENOMEM;
2104
2105	con.result = &lockhash_table[0];
2106
2107	session = perf_session__new(use_bpf ? NULL : &data, &eops);
2108	if (IS_ERR(session)) {
2109		pr_err("Initializing perf session failed\n");
2110		err = PTR_ERR(session);
2111		session = NULL;
2112		goto out_delete;
2113	}
2114
2115	con.machine = &session->machines.host;
2116
2117	con.aggr_mode = aggr_mode = show_thread_stats ? LOCK_AGGR_TASK :
2118		show_lock_addrs ? LOCK_AGGR_ADDR :
2119		show_lock_cgroups ? LOCK_AGGR_CGROUP : LOCK_AGGR_CALLER;
2120
2121	if (con.aggr_mode == LOCK_AGGR_CALLER)
2122		con.save_callstack = true;
2123
 
 
2124	symbol_conf.allow_aliases = true;
2125	symbol__init(&session->header.env);
2126
2127	if (use_bpf) {
2128		err = target__validate(&target);
2129		if (err) {
2130			char errbuf[512];
2131
2132			target__strerror(&target, err, errbuf, 512);
2133			pr_err("%s\n", errbuf);
2134			goto out_delete;
2135		}
2136
2137		signal(SIGINT, sighandler);
2138		signal(SIGCHLD, sighandler);
2139		signal(SIGTERM, sighandler);
2140
2141		con.evlist = evlist__new();
2142		if (con.evlist == NULL) {
2143			err = -ENOMEM;
2144			goto out_delete;
2145		}
2146
2147		err = evlist__create_maps(con.evlist, &target);
2148		if (err < 0)
2149			goto out_delete;
2150
2151		if (argc) {
2152			err = evlist__prepare_workload(con.evlist, &target,
2153						       argv, false, NULL);
2154			if (err < 0)
2155				goto out_delete;
2156		}
2157
2158		if (lock_contention_prepare(&con) < 0) {
2159			pr_err("lock contention BPF setup failed\n");
2160			goto out_delete;
2161		}
2162	} else if (!data.is_pipe) {
2163		if (!perf_session__has_traces(session, "lock record"))
2164			goto out_delete;
2165
2166		if (!evlist__find_evsel_by_str(session->evlist,
2167					       "lock:contention_begin")) {
2168			pr_err("lock contention evsel not found\n");
2169			goto out_delete;
2170		}
2171
2172		if (perf_session__set_tracepoints_handlers(session,
2173						contention_tracepoints)) {
2174			pr_err("Initializing perf session tracepoint handlers failed\n");
2175			goto out_delete;
2176		}
2177	}
2178
2179	if (setup_output_field(true, output_fields))
2180		goto out_delete;
2181
2182	if (select_key(true))
2183		goto out_delete;
2184
2185	if (symbol_conf.field_sep) {
2186		int i;
2187		struct lock_key *keys = contention_keys;
2188
2189		/* do not align output in CSV format */
2190		for (i = 0; keys[i].name; i++)
2191			keys[i].len = 0;
2192	}
2193
2194	if (use_bpf) {
2195		lock_contention_start();
2196		if (argc)
2197			evlist__start_workload(con.evlist);
2198
2199		/* wait for signal */
2200		pause();
2201
2202		lock_contention_stop();
2203		lock_contention_read(&con);
 
 
 
2204	} else {
2205		err = perf_session__process_events(session);
2206		if (err)
2207			goto out_delete;
2208	}
2209
2210	setup_pager();
2211
2212	sort_contention_result();
2213	print_contention_result(&con);
2214
2215out_delete:
2216	lock_filter_finish();
2217	evlist__delete(con.evlist);
2218	lock_contention_finish(&con);
2219	perf_session__delete(session);
2220	zfree(&lockhash_table);
2221	return err;
2222}
2223
2224
2225static int __cmd_record(int argc, const char **argv)
2226{
2227	const char *record_args[] = {
2228		"record", "-R", "-m", "1024", "-c", "1", "--synth", "task",
2229	};
2230	const char *callgraph_args[] = {
2231		"--call-graph", "fp," __stringify(CONTENTION_STACK_DEPTH),
2232	};
2233	unsigned int rec_argc, i, j, ret;
2234	unsigned int nr_tracepoints;
2235	unsigned int nr_callgraph_args = 0;
2236	const char **rec_argv;
2237	bool has_lock_stat = true;
2238
2239	for (i = 0; i < ARRAY_SIZE(lock_tracepoints); i++) {
2240		if (!is_valid_tracepoint(lock_tracepoints[i].name)) {
2241			pr_debug("tracepoint %s is not enabled. "
2242				 "Are CONFIG_LOCKDEP and CONFIG_LOCK_STAT enabled?\n",
2243				 lock_tracepoints[i].name);
2244			has_lock_stat = false;
2245			break;
2246		}
2247	}
2248
2249	if (has_lock_stat)
2250		goto setup_args;
2251
2252	for (i = 0; i < ARRAY_SIZE(contention_tracepoints); i++) {
2253		if (!is_valid_tracepoint(contention_tracepoints[i].name)) {
2254			pr_err("tracepoint %s is not enabled.\n",
2255			       contention_tracepoints[i].name);
2256			return 1;
2257		}
2258	}
2259
2260	nr_callgraph_args = ARRAY_SIZE(callgraph_args);
2261
2262setup_args:
2263	rec_argc = ARRAY_SIZE(record_args) + nr_callgraph_args + argc - 1;
2264
2265	if (has_lock_stat)
2266		nr_tracepoints = ARRAY_SIZE(lock_tracepoints);
2267	else
2268		nr_tracepoints = ARRAY_SIZE(contention_tracepoints);
2269
2270	/* factor of 2 is for -e in front of each tracepoint */
2271	rec_argc += 2 * nr_tracepoints;
2272
2273	rec_argv = calloc(rec_argc + 1, sizeof(char *));
2274	if (!rec_argv)
2275		return -ENOMEM;
2276
2277	for (i = 0; i < ARRAY_SIZE(record_args); i++)
2278		rec_argv[i] = strdup(record_args[i]);
2279
2280	for (j = 0; j < nr_tracepoints; j++) {
2281		const char *ev_name;
2282
2283		if (has_lock_stat)
2284			ev_name = strdup(lock_tracepoints[j].name);
2285		else
2286			ev_name = strdup(contention_tracepoints[j].name);
2287
2288		if (!ev_name) {
2289			free(rec_argv);
2290			return -ENOMEM;
2291		}
2292
2293		rec_argv[i++] = "-e";
2294		rec_argv[i++] = ev_name;
2295	}
2296
2297	for (j = 0; j < nr_callgraph_args; j++, i++)
2298		rec_argv[i] = callgraph_args[j];
2299
2300	for (j = 1; j < (unsigned int)argc; j++, i++)
2301		rec_argv[i] = argv[j];
2302
2303	BUG_ON(i != rec_argc);
2304
2305	ret = cmd_record(i, rec_argv);
2306	free(rec_argv);
2307	return ret;
2308}
2309
2310static int parse_map_entry(const struct option *opt, const char *str,
2311			    int unset __maybe_unused)
2312{
2313	unsigned long *len = (unsigned long *)opt->value;
2314	unsigned long val;
2315	char *endptr;
2316
2317	errno = 0;
2318	val = strtoul(str, &endptr, 0);
2319	if (*endptr != '\0' || errno != 0) {
2320		pr_err("invalid BPF map length: %s\n", str);
2321		return -1;
2322	}
2323
2324	*len = val;
2325	return 0;
2326}
2327
2328static int parse_max_stack(const struct option *opt, const char *str,
2329			   int unset __maybe_unused)
2330{
2331	unsigned long *len = (unsigned long *)opt->value;
2332	long val;
2333	char *endptr;
2334
2335	errno = 0;
2336	val = strtol(str, &endptr, 0);
2337	if (*endptr != '\0' || errno != 0) {
2338		pr_err("invalid max stack depth: %s\n", str);
2339		return -1;
2340	}
2341
2342	if (val < 0 || val > sysctl__max_stack()) {
2343		pr_err("invalid max stack depth: %ld\n", val);
2344		return -1;
2345	}
2346
2347	*len = val;
2348	return 0;
2349}
2350
2351static bool add_lock_type(unsigned int flags)
2352{
2353	unsigned int *tmp;
2354
2355	tmp = realloc(filters.types, (filters.nr_types + 1) * sizeof(*filters.types));
2356	if (tmp == NULL)
2357		return false;
2358
2359	tmp[filters.nr_types++] = flags;
2360	filters.types = tmp;
2361	return true;
2362}
2363
2364static int parse_lock_type(const struct option *opt __maybe_unused, const char *str,
2365			   int unset __maybe_unused)
2366{
2367	char *s, *tmp, *tok;
2368	int ret = 0;
2369
2370	s = strdup(str);
2371	if (s == NULL)
2372		return -1;
2373
2374	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2375		unsigned int flags = get_type_flag(tok);
2376
2377		if (flags == -1U) {
2378			pr_err("Unknown lock flags: %s\n", tok);
2379			ret = -1;
2380			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2381		}
2382
2383		if (!add_lock_type(flags)) {
2384			ret = -1;
2385			break;
2386		}
 
 
 
 
 
 
 
 
 
 
2387	}
2388
2389	free(s);
2390	return ret;
2391}
2392
2393static bool add_lock_addr(unsigned long addr)
2394{
2395	unsigned long *tmp;
2396
2397	tmp = realloc(filters.addrs, (filters.nr_addrs + 1) * sizeof(*filters.addrs));
2398	if (tmp == NULL) {
2399		pr_err("Memory allocation failure\n");
2400		return false;
2401	}
2402
2403	tmp[filters.nr_addrs++] = addr;
2404	filters.addrs = tmp;
2405	return true;
2406}
2407
2408static bool add_lock_sym(char *name)
2409{
2410	char **tmp;
2411	char *sym = strdup(name);
2412
2413	if (sym == NULL) {
2414		pr_err("Memory allocation failure\n");
2415		return false;
2416	}
2417
2418	tmp = realloc(filters.syms, (filters.nr_syms + 1) * sizeof(*filters.syms));
2419	if (tmp == NULL) {
2420		pr_err("Memory allocation failure\n");
2421		free(sym);
2422		return false;
2423	}
2424
2425	tmp[filters.nr_syms++] = sym;
2426	filters.syms = tmp;
2427	return true;
2428}
2429
2430static int parse_lock_addr(const struct option *opt __maybe_unused, const char *str,
2431			   int unset __maybe_unused)
2432{
2433	char *s, *tmp, *tok;
2434	int ret = 0;
2435	u64 addr;
2436
2437	s = strdup(str);
2438	if (s == NULL)
2439		return -1;
2440
2441	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2442		char *end;
2443
2444		addr = strtoul(tok, &end, 16);
2445		if (*end == '\0') {
2446			if (!add_lock_addr(addr)) {
2447				ret = -1;
2448				break;
2449			}
2450			continue;
2451		}
2452
2453		/*
2454		 * At this moment, we don't have kernel symbols.  Save the symbols
2455		 * in a separate list and resolve them to addresses later.
2456		 */
2457		if (!add_lock_sym(tok)) {
2458			ret = -1;
2459			break;
2460		}
2461	}
2462
2463	free(s);
2464	return ret;
2465}
2466
2467static int parse_call_stack(const struct option *opt __maybe_unused, const char *str,
2468			   int unset __maybe_unused)
2469{
2470	char *s, *tmp, *tok;
2471	int ret = 0;
2472
2473	s = strdup(str);
2474	if (s == NULL)
2475		return -1;
2476
2477	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2478		struct callstack_filter *entry;
2479
2480		entry = malloc(sizeof(*entry) + strlen(tok) + 1);
2481		if (entry == NULL) {
2482			pr_err("Memory allocation failure\n");
2483			free(s);
2484			return -1;
2485		}
2486
2487		strcpy(entry->name, tok);
2488		list_add_tail(&entry->list, &callstack_filters);
2489	}
2490
2491	free(s);
2492	return ret;
2493}
2494
2495static int parse_output(const struct option *opt __maybe_unused, const char *str,
2496			int unset __maybe_unused)
2497{
2498	const char **name = (const char **)opt->value;
2499
2500	if (str == NULL)
2501		return -1;
2502
2503	lock_output = fopen(str, "w");
2504	if (lock_output == NULL) {
2505		pr_err("Cannot open %s\n", str);
2506		return -1;
2507	}
2508
2509	*name = str;
2510	return 0;
2511}
2512
2513static bool add_lock_cgroup(char *name)
2514{
2515	u64 *tmp;
2516	struct cgroup *cgrp;
2517
2518	cgrp = cgroup__new(name, /*do_open=*/false);
2519	if (cgrp == NULL) {
2520		pr_err("Failed to create cgroup: %s\n", name);
2521		return false;
2522	}
2523
2524	if (read_cgroup_id(cgrp) < 0) {
2525		pr_err("Failed to read cgroup id for %s\n", name);
2526		cgroup__put(cgrp);
2527		return false;
2528	}
2529
2530	tmp = realloc(filters.cgrps, (filters.nr_cgrps + 1) * sizeof(*filters.cgrps));
2531	if (tmp == NULL) {
2532		pr_err("Memory allocation failure\n");
2533		return false;
2534	}
2535
2536	tmp[filters.nr_cgrps++] = cgrp->id;
2537	filters.cgrps = tmp;
2538	cgroup__put(cgrp);
2539	return true;
2540}
2541
2542static int parse_cgroup_filter(const struct option *opt __maybe_unused, const char *str,
2543			       int unset __maybe_unused)
2544{
2545	char *s, *tmp, *tok;
2546	int ret = 0;
2547
2548	s = strdup(str);
2549	if (s == NULL)
2550		return -1;
2551
2552	for (tok = strtok_r(s, ", ", &tmp); tok; tok = strtok_r(NULL, ", ", &tmp)) {
2553		if (!add_lock_cgroup(tok)) {
2554			ret = -1;
2555			break;
2556		}
2557	}
2558
2559	free(s);
2560	return ret;
2561}
2562
2563int cmd_lock(int argc, const char **argv)
2564{
2565	const struct option lock_options[] = {
2566	OPT_STRING('i', "input", &input_name, "file", "input file name"),
2567	OPT_CALLBACK(0, "output", &output_name, "file", "output file name", parse_output),
2568	OPT_INCR('v', "verbose", &verbose, "be more verbose (show symbol address, etc)"),
2569	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, "dump raw trace in ASCII"),
2570	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
2571	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
2572		   "file", "vmlinux pathname"),
2573	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
2574		   "file", "kallsyms pathname"),
2575	OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
2576	OPT_END()
2577	};
2578
2579	const struct option info_options[] = {
2580	OPT_BOOLEAN('t', "threads", &info_threads,
2581		    "dump thread list in perf.data"),
2582	OPT_BOOLEAN('m', "map", &info_map,
2583		    "map of lock instances (address:name table)"),
2584	OPT_PARENT(lock_options)
2585	};
2586
2587	const struct option report_options[] = {
2588	OPT_STRING('k', "key", &sort_key, "acquired",
2589		    "key for sorting (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2590	OPT_STRING('F', "field", &output_fields, NULL,
2591		    "output fields (acquired / contended / avg_wait / wait_total / wait_max / wait_min)"),
2592	/* TODO: type */
2593	OPT_BOOLEAN('c', "combine-locks", &combine_locks,
2594		    "combine locks in the same class"),
2595	OPT_BOOLEAN('t', "threads", &show_thread_stats,
2596		    "show per-thread lock stats"),
2597	OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2598	OPT_PARENT(lock_options)
2599	};
2600
2601	struct option contention_options[] = {
2602	OPT_STRING('k', "key", &sort_key, "wait_total",
2603		    "key for sorting (contended / wait_total / wait_max / wait_min / avg_wait)"),
2604	OPT_STRING('F', "field", &output_fields, "contended,wait_total,wait_max,avg_wait",
2605		    "output fields (contended / wait_total / wait_max / wait_min / avg_wait)"),
2606	OPT_BOOLEAN('t', "threads", &show_thread_stats,
2607		    "show per-thread lock stats"),
2608	OPT_BOOLEAN('b', "use-bpf", &use_bpf, "use BPF program to collect lock contention stats"),
2609	OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
2610		    "System-wide collection from all CPUs"),
2611	OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2612		    "List of cpus to monitor"),
2613	OPT_STRING('p', "pid", &target.pid, "pid",
2614		   "Trace on existing process id"),
2615	OPT_STRING(0, "tid", &target.tid, "tid",
2616		   "Trace on existing thread id (exclusive to --pid)"),
2617	OPT_CALLBACK('M', "map-nr-entries", &bpf_map_entries, "num",
2618		     "Max number of BPF map entries", parse_map_entry),
2619	OPT_CALLBACK(0, "max-stack", &max_stack_depth, "num",
2620		     "Set the maximum stack depth when collecting lock contention, "
2621		     "Default: " __stringify(CONTENTION_STACK_DEPTH), parse_max_stack),
2622	OPT_INTEGER(0, "stack-skip", &stack_skip,
2623		    "Set the number of stack depth to skip when finding a lock caller, "
2624		    "Default: " __stringify(CONTENTION_STACK_SKIP)),
2625	OPT_INTEGER('E', "entries", &print_nr_entries, "display this many functions"),
2626	OPT_BOOLEAN('l', "lock-addr", &show_lock_addrs, "show lock stats by address"),
2627	OPT_CALLBACK('Y', "type-filter", NULL, "FLAGS",
2628		     "Filter specific type of locks", parse_lock_type),
2629	OPT_CALLBACK('L', "lock-filter", NULL, "ADDRS/NAMES",
2630		     "Filter specific address/symbol of locks", parse_lock_addr),
2631	OPT_CALLBACK('S', "callstack-filter", NULL, "NAMES",
2632		     "Filter specific function in the callstack", parse_call_stack),
2633	OPT_BOOLEAN('o', "lock-owner", &show_lock_owner, "show lock owners instead of waiters"),
2634	OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, "separator",
2635		   "print result in CSV format with custom separator"),
2636	OPT_BOOLEAN(0, "lock-cgroup", &show_lock_cgroups, "show lock stats by cgroup"),
2637	OPT_CALLBACK('G', "cgroup-filter", NULL, "CGROUPS",
2638		     "Filter specific cgroups", parse_cgroup_filter),
2639	OPT_PARENT(lock_options)
2640	};
2641
2642	const char * const info_usage[] = {
2643		"perf lock info [<options>]",
2644		NULL
2645	};
2646	const char *const lock_subcommands[] = { "record", "report", "script",
2647						 "info", "contention", NULL };
2648	const char *lock_usage[] = {
2649		NULL,
2650		NULL
2651	};
2652	const char * const report_usage[] = {
2653		"perf lock report [<options>]",
2654		NULL
2655	};
2656	const char * const contention_usage[] = {
2657		"perf lock contention [<options>]",
2658		NULL
2659	};
2660	unsigned int i;
2661	int rc = 0;
2662
2663	lockhash_table = calloc(LOCKHASH_SIZE, sizeof(*lockhash_table));
2664	if (!lockhash_table)
2665		return -ENOMEM;
2666
2667	for (i = 0; i < LOCKHASH_SIZE; i++)
2668		INIT_HLIST_HEAD(lockhash_table + i);
2669
2670	lock_output = stderr;
2671	argc = parse_options_subcommand(argc, argv, lock_options, lock_subcommands,
2672					lock_usage, PARSE_OPT_STOP_AT_NON_OPTION);
2673	if (!argc)
2674		usage_with_options(lock_usage, lock_options);
2675
2676	if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
2677		return __cmd_record(argc, argv);
2678	} else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
2679		trace_handler = &report_lock_ops;
2680		if (argc) {
2681			argc = parse_options(argc, argv,
2682					     report_options, report_usage, 0);
2683			if (argc)
2684				usage_with_options(report_usage, report_options);
2685		}
2686		rc = __cmd_report(false);
2687	} else if (!strcmp(argv[0], "script")) {
2688		/* Aliased to 'perf script' */
2689		rc = cmd_script(argc, argv);
2690	} else if (!strcmp(argv[0], "info")) {
2691		if (argc) {
2692			argc = parse_options(argc, argv,
2693					     info_options, info_usage, 0);
2694			if (argc)
2695				usage_with_options(info_usage, info_options);
2696		}
2697		/* recycling report_lock_ops */
2698		trace_handler = &report_lock_ops;
2699		rc = __cmd_report(true);
2700	} else if (strlen(argv[0]) > 2 && strstarts("contention", argv[0])) {
2701		trace_handler = &contention_lock_ops;
2702		sort_key = "wait_total";
2703		output_fields = "contended,wait_total,wait_max,avg_wait";
2704
2705#ifndef HAVE_BPF_SKEL
2706		set_option_nobuild(contention_options, 'b', "use-bpf",
2707				   "no BUILD_BPF_SKEL=1", false);
2708#endif
2709		if (argc) {
2710			argc = parse_options(argc, argv, contention_options,
2711					     contention_usage, 0);
2712		}
2713
2714		if (check_lock_contention_options(contention_options,
2715						  contention_usage) < 0)
 
 
 
 
2716			return -1;
 
2717
2718		rc = __cmd_contention(argc, argv);
2719	} else {
2720		usage_with_options(lock_usage, lock_options);
2721	}
2722
2723	zfree(&lockhash_table);
2724	return rc;
2725}