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: Ian 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: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
committed by
Arnaldo Carvalho de Melo
parent
d7ba60a4e5
commit
8ab12a2038
@@ -58,7 +58,8 @@ struct callchain_param callchain_param_default = {
|
||||
CALLCHAIN_PARAM_DEFAULT
|
||||
};
|
||||
|
||||
__thread struct callchain_cursor callchain_cursor;
|
||||
/* Used for thread-local struct callchain_cursor. */
|
||||
static pthread_key_t callchain_cursor;
|
||||
|
||||
int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
|
||||
{
|
||||
@@ -986,6 +987,9 @@ int callchain_append(struct callchain_root *root,
|
||||
struct callchain_cursor *cursor,
|
||||
u64 period)
|
||||
{
|
||||
if (cursor == NULL)
|
||||
return -1;
|
||||
|
||||
if (!cursor->nr)
|
||||
return 0;
|
||||
|
||||
@@ -1116,7 +1120,7 @@ int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *samp
|
||||
if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
|
||||
!symbol_conf.show_branchflag_count)
|
||||
return 0;
|
||||
return callchain_append(he->callchain, &callchain_cursor, sample->period);
|
||||
return callchain_append(he->callchain, get_tls_callchain_cursor(), sample->period);
|
||||
}
|
||||
|
||||
int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
|
||||
@@ -1570,6 +1574,43 @@ out:
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
static void callchain_cursor__delete(void *vcursor)
|
||||
{
|
||||
struct callchain_cursor *cursor = vcursor;
|
||||
struct callchain_cursor_node *node, *next;
|
||||
|
||||
callchain_cursor_reset(cursor);
|
||||
for (node = cursor->first; node != NULL; node = next) {
|
||||
next = node->next;
|
||||
free(node);
|
||||
}
|
||||
free(cursor);
|
||||
}
|
||||
|
||||
static void init_callchain_cursor_key(void)
|
||||
{
|
||||
if (pthread_key_create(&callchain_cursor, callchain_cursor__delete)) {
|
||||
pr_err("callchain cursor creation failed");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
struct callchain_cursor *get_tls_callchain_cursor(void)
|
||||
{
|
||||
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
|
||||
struct callchain_cursor *cursor;
|
||||
|
||||
pthread_once(&once_control, init_callchain_cursor_key);
|
||||
cursor = pthread_getspecific(callchain_cursor);
|
||||
if (!cursor) {
|
||||
cursor = zalloc(sizeof(*cursor));
|
||||
if (!cursor)
|
||||
pr_debug3("%s: not enough memory\n", __func__);
|
||||
pthread_setspecific(callchain_cursor, cursor);
|
||||
}
|
||||
return cursor;
|
||||
}
|
||||
|
||||
int callchain_cursor__copy(struct callchain_cursor *dst,
|
||||
struct callchain_cursor *src)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user