Commit 8ab12a20 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf callchain: Use pthread keys for tls callchain_cursor



Pthread keys are more portable than __thread and allow the association
of a destructor with the key. Use the destructor to clean up TLS
callchain cursors to aid understanding memory leaks.

Committer notes:

Had to fixup a series of unconverted places and also check for the
return of get_tls_callchain_cursor() as it may fail and return NULL.

In that unlikely case we now either print something to a file, if the
caller was expecting to print a callchain, or return an error code to
state that resolving the callchain isn't possible.

In some cases this was made easier because thread__resolve_callchain()
already can fail for other reasons, so this new one (cursor == NULL) can
be added and the callers don't have to explicitely check for this new
condition.

Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Brian Robbins <brianrob@linux.microsoft.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: Fangrui Song <maskray@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Ye Xingchen <ye.xingchen@zte.com.cn>
Cc: Yuan Can <yuancan@huawei.com>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230608232823.4027869-25-irogers@google.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent d7ba60a4
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -284,6 +284,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
	struct hist_entry *he;
	struct addr_location al;
	struct mem_info *mi, *mi_dup;
	struct callchain_cursor *cursor;
	int ret;

	addr_location__init(&al);
@@ -297,7 +298,8 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
	if (c2c.stitch_lbr)
		thread__set_lbr_stitch_enable(al.thread, true);

	ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
	cursor = get_tls_callchain_cursor();
	ret = sample__resolve_callchain(sample, cursor, NULL,
					evsel, &al, sysctl_perf_event_max_stack);
	if (ret)
		goto out;
+10 −4
Original line number Diff line number Diff line
@@ -399,6 +399,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
	struct addr_location al;
	struct machine *machine = &kmem_session->machines.host;
	struct callchain_cursor_node *node;
	struct callchain_cursor *cursor;
	u64 result = sample->ip;

	addr_location__init(&al);
@@ -408,14 +409,19 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
	}

	al.thread = machine__findnew_thread(machine, sample->pid, sample->tid);
	sample__resolve_callchain(sample, &callchain_cursor, NULL, evsel, &al, 16);

	callchain_cursor_commit(&callchain_cursor);
	cursor = get_tls_callchain_cursor();
	if (cursor == NULL)
		goto out;

	sample__resolve_callchain(sample, cursor, NULL, evsel, &al, 16);

	callchain_cursor_commit(cursor);
	while (true) {
		struct alloc_func key, *caller;
		u64 addr;

		node = callchain_cursor_current(&callchain_cursor);
		node = callchain_cursor_current(cursor);
		if (node == NULL)
			break;

@@ -434,7 +440,7 @@ static u64 find_callsite(struct evsel *evsel, struct perf_sample *sample)
		} else
			pr_debug3("skipping alloc function: %s\n", caller->name);

		callchain_cursor_advance(&callchain_cursor);
		callchain_cursor_advance(cursor);
	}

	pr_debug2("unknown callsite: %"PRIx64 "\n", sample->ip);
+10 −2
Original line number Diff line number Diff line
@@ -589,7 +589,7 @@ static void timehist_save_callchain(struct perf_kwork *kwork,
	struct symbol *sym;
	struct thread *thread;
	struct callchain_cursor_node *node;
	struct callchain_cursor *cursor = &callchain_cursor;
	struct callchain_cursor *cursor;

	if (!kwork->show_callchain || sample->callchain == NULL)
		return;
@@ -601,6 +601,8 @@ static void timehist_save_callchain(struct perf_kwork *kwork,
		return;
	}

	cursor = get_tls_callchain_cursor();

	if (thread__resolve_callchain(thread, cursor, evsel, sample,
				      NULL, NULL, kwork->max_stack + 2) != 0) {
		pr_debug("Failed to resolve callchain, skipping\n");
@@ -686,12 +688,18 @@ static void timehist_print_event(struct perf_kwork *kwork,
	 * callchain
	 */
	if (kwork->show_callchain) {
		struct callchain_cursor *cursor = get_tls_callchain_cursor();

		if (cursor == NULL)
			return;

		printf(" ");

		sample__fprintf_sym(sample, al, 0,
				    EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
				    EVSEL__PRINT_CALLCHAIN_ARROW |
				    EVSEL__PRINT_SKIP_IGNORED,
				    &callchain_cursor, symbol_conf.bt_stop_list,
				    cursor, symbol_conf.bt_stop_list,
				    stdout);
	}

+5 −2
Original line number Diff line number Diff line
@@ -911,7 +911,7 @@ static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sampl
				  char *buf, int size)
{
	struct thread *thread;
	struct callchain_cursor *cursor = &callchain_cursor;
	struct callchain_cursor *cursor;
	struct machine *machine = &session->machines.host;
	struct symbol *sym;
	int skip = 0;
@@ -925,6 +925,8 @@ static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sampl
	if (thread == NULL)
		return -1;

	cursor = get_tls_callchain_cursor();

	/* use caller function name from the callchain */
	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
					NULL, NULL, max_stack_depth);
@@ -962,7 +964,7 @@ static int lock_contention_caller(struct evsel *evsel, struct perf_sample *sampl

static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
{
	struct callchain_cursor *cursor = &callchain_cursor;
	struct callchain_cursor *cursor;
	struct machine *machine = &session->machines.host;
	struct thread *thread;
	u64 hash = 0;
@@ -973,6 +975,7 @@ static u64 callchain_id(struct evsel *evsel, struct perf_sample *sample)
	if (thread == NULL)
		return -1;

	cursor = get_tls_callchain_cursor();
	/* use caller function name from the callchain */
	ret = thread__resolve_callchain(thread, cursor, evsel, sample,
					NULL, NULL, max_stack_depth);
+11 −3
Original line number Diff line number Diff line
@@ -2111,7 +2111,7 @@ static void timehist_print_sample(struct perf_sched *sched,
			    EVSEL__PRINT_SYM | EVSEL__PRINT_ONELINE |
			    EVSEL__PRINT_CALLCHAIN_ARROW |
			    EVSEL__PRINT_SKIP_IGNORED,
			    &callchain_cursor, symbol_conf.bt_stop_list,  stdout);
			    get_tls_callchain_cursor(), symbol_conf.bt_stop_list,  stdout);

out:
	printf("\n");
@@ -2196,7 +2196,7 @@ static void save_task_callchain(struct perf_sched *sched,
				struct evsel *evsel,
				struct machine *machine)
{
	struct callchain_cursor *cursor = &callchain_cursor;
	struct callchain_cursor *cursor;
	struct thread *thread;

	/* want main thread for process - has maps */
@@ -2209,6 +2209,8 @@ static void save_task_callchain(struct perf_sched *sched,
	if (!sched->show_callchain || sample->callchain == NULL)
		return;

	cursor = get_tls_callchain_cursor();

	if (thread__resolve_callchain(thread, cursor, evsel, sample,
				      NULL, NULL, sched->max_stack + 2) != 0) {
		if (verbose > 0)
@@ -2338,10 +2340,16 @@ static void save_idle_callchain(struct perf_sched *sched,
				struct idle_thread_runtime *itr,
				struct perf_sample *sample)
{
	struct callchain_cursor *cursor;

	if (!sched->show_callchain || sample->callchain == NULL)
		return;

	callchain_cursor__copy(&itr->cursor, &callchain_cursor);
	cursor = get_tls_callchain_cursor();
	if (cursor == NULL)
		return;

	callchain_cursor__copy(&itr->cursor, cursor);
}

static struct thread *timehist_get_thread(struct perf_sched *sched,
Loading