From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AF673D1CCA for ; Tue, 28 Apr 2026 07:19:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777360795; cv=none; b=kGty3qlGu2kEi+hHi3ySeZy9qsR9IeX5GlfOYbM80W75h7tnEi+MdGgZshlmjOSWeWCVld9x/lV7Zqv1/RvxUd5cc8Feas6HJl5FeDRPpN60cPvLBg75aGx8hgqIhNvqvB/pRwLXa1kzETsSZfAuMqdFUWJqPRm0Y0rDLe30pTc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777360795; c=relaxed/simple; bh=8XnAv8ccixqUgCl4eTovV40T1SKiPp/ERmPb9Jrq4ps=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DJBC2l+5iHDJe3IiKiSPnUOI5raJn87LeGnfz5vPUTYX9rqDXbRdfPJ4ofIVLvx5qBhfejxTj7RVUPdjyZJvT75MQNKqV5Z19wr4EVOG2ivF0KTBjuF3CGcCIx1gJYZUf32GZwAlbcQb4YQVVE7J1Acjk8SNVjRHT+w0eN2v32Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tfx29FuB; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tfx29FuB" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2bdf6fe90a9so17674694eec.1 for ; Tue, 28 Apr 2026 00:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777360792; x=1777965592; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RLkF3ieT3Z4VoZlnrzpuq1NoYuxCcqNkN8XbQ6IDzmQ=; b=tfx29FuBIY9VZSDeUGwNJIUvc7SsQ4nMw4fUkxOFMRCYP27NzAxQWXP4xJp/j+KT2p Duh0pxo3N0bBpPNAXH1qo9SZj5uU4eZKNhD/MzS37arR+UARvXQnjweHtE8OVeOsvpUM nVJCRhHCE36YtI1IGjAH9YBWDAUyc55NoTrQm55l06Ic9bv0h8pA234YrS+Vo20teMVi s2A6bHcvyxbF0+fWMH2BwnvzSXoKd5PivKW9+nk5/wCvYzOx8vQxOHt8CrXcAicEpSVi zO2IKBJhjzx08RpWzGHyup+GeEhF87FqM6Zia4z8r2N4kbuolZk2jWEs+/pz/JDTgu3D 5FMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777360792; x=1777965592; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RLkF3ieT3Z4VoZlnrzpuq1NoYuxCcqNkN8XbQ6IDzmQ=; b=p5FFRPZySO40bKCc4qR3YwILqA6pIWDfHpSqs5OKQBaf4RtiN3LqgPNIfOJrrhI5aM XlP+sZyOJ19K6Bvm4RP7M6wyz0XNRceGOfGdb3+MBFr/QzOWWvk4X3uJGTXkM+kEdIAB KywoNx1HBESpP5qXL9u6CRucchUA0t6o/4vVU8SFPo4nsUu7ohTlsb+LdyzOTzjpmpTL nhaXljTnYz7u5T83agETimXES1FPld6HOgzFNn0sC27JP/FUkRuVE3HAx/S25f1dO+8Z LUYvN/sjCGkgDUYX1o7kIfMybqX0+IvfYZiX8xp+GuK2+aY/KCLAKk4CGUitIPxZkfVO FkkA== X-Forwarded-Encrypted: i=1; AFNElJ89+vmuEyGFzxGbPnTnFpMFZiBvLeXLR0mof4qDO//qIGoy3yoxFIDIrCQpWVpaua8/wUXY91HYr0AJX9e7veTO@vger.kernel.org X-Gm-Message-State: AOJu0Yx3xGM8sRnJ7Sp02v5agKkMigC0UHk1ePFH5FeFDASTlin4a9zf aaeR4Ou0cSVhyWdgKsT9Rwizuze33gpevulMw3RlfMW+tTqxedDnxEZbZLYmpuej5xoMPdVcXVF oEuvgSvUlIQ== X-Received: from dybmx9.prod.google.com ([2002:a05:7300:d409:b0:2d7:d4ce:8994]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:f496:b0:2ed:e14:7f58 with SMTP id 5a478bee46e88-2ed0e1484ecmr323732eec.34.1777360792023; Tue, 28 Apr 2026 00:19:52 -0700 (PDT) Date: Tue, 28 Apr 2026 00:18:22 -0700 In-Reply-To: <20260428071903.1886173-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260425224951.174663-1-irogers@google.com> <20260428071903.1886173-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260428071903.1886173-18-irogers@google.com> Subject: [PATCH v8 17/58] perf python: Add callchain support From: Ian Rogers To: acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com, Ian Rogers Content-Type: text/plain; charset="UTF-8" Implement pyrf_callchain_node and pyrf_callchain types for lazy iteration over callchain frames. Add callchain property to sample_event. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v2: 1. Eager Callchain Resolution: Moved the callchain resolution from deferred iteration to eager processing in pyrf_session_tool__sample() . This avoids risks of reading from unmapped memory or following dangling pointers to closed sessions. 2. Cached Callchain: Added a callchain field to struct pyrf_event to store the resolved object. 3. Simplified Access: pyrf_sample_event__get_callchain() now just returns the cached object if available. 4. Avoided Double Free: Handled lazy cleanups properly. v6: - Moved callchain resolution from `session_tool__sample` to `pyrf_event__new`. --- tools/perf/util/python.c | 241 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index ae009bc9a0d7..2ff588517c29 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -64,6 +64,8 @@ struct pyrf_event { struct addr_location al; /** @al_resolved: True when machine__resolve been called. */ bool al_resolved; + /** @callchain: Resolved callchain, eagerly computed if requested. */ + PyObject *callchain; /** @event: The underlying perf_event that may be in a file or ring buffer. */ union perf_event event; }; @@ -101,6 +103,7 @@ static void pyrf_event__delete(struct pyrf_event *pevent) { if (pevent->al_resolved) addr_location__exit(&pevent->al); + Py_XDECREF(pevent->callchain); perf_sample__exit(&pevent->sample); Py_TYPE(pevent)->tp_free((PyObject *)pevent); } @@ -667,6 +670,181 @@ static PyObject *pyrf_sample_event__insn(PyObject *self, PyObject *args __maybe_ pevent->sample.insn_len); } +struct pyrf_callchain_node { + PyObject_HEAD + u64 ip; + struct map *map; + struct symbol *sym; +}; + +static void pyrf_callchain_node__delete(struct pyrf_callchain_node *pnode) +{ + map__put(pnode->map); + Py_TYPE(pnode)->tp_free((PyObject*)pnode); +} + +static PyObject *pyrf_callchain_node__get_ip(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + return PyLong_FromUnsignedLongLong(pnode->ip); +} + +static PyObject *pyrf_callchain_node__get_symbol(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + if (pnode->sym) + return PyUnicode_FromString(pnode->sym->name); + return PyUnicode_FromString("[unknown]"); +} + +static PyObject *pyrf_callchain_node__get_dso(struct pyrf_callchain_node *pnode, + void *closure __maybe_unused) +{ + const char *dsoname = "[unknown]"; + + if (pnode->map) { + struct dso *dso = map__dso(pnode->map); + if (dso) { + if (symbol_conf.show_kernel_path && dso__long_name(dso)) + dsoname = dso__long_name(dso); + else + dsoname = dso__name(dso); + } + } + return PyUnicode_FromString(dsoname); +} + +static PyGetSetDef pyrf_callchain_node__getset[] = { + { .name = "ip", .get = (getter)pyrf_callchain_node__get_ip, }, + { .name = "symbol", .get = (getter)pyrf_callchain_node__get_symbol, }, + { .name = "dso", .get = (getter)pyrf_callchain_node__get_dso, }, + { .name = NULL, }, +}; + +static PyTypeObject pyrf_callchain_node__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.callchain_node", + .tp_basicsize = sizeof(struct pyrf_callchain_node), + .tp_dealloc = (destructor)pyrf_callchain_node__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = "perf callchain node object.", + .tp_getset = pyrf_callchain_node__getset, +}; + +struct pyrf_callchain_frame { + u64 ip; + struct map *map; + struct symbol *sym; +}; + +struct pyrf_callchain { + PyObject_HEAD + struct pyrf_event *pevent; + struct pyrf_callchain_frame *frames; + u64 nr_frames; + u64 pos; + bool resolved; +}; + +static void pyrf_callchain__delete(struct pyrf_callchain *pchain) +{ + Py_XDECREF(pchain->pevent); + if (pchain->frames) { + for (u64 i = 0; i < pchain->nr_frames; i++) + map__put(pchain->frames[i].map); + free(pchain->frames); + } + Py_TYPE(pchain)->tp_free((PyObject*)pchain); +} + +static PyObject *pyrf_callchain__next(struct pyrf_callchain *pchain) +{ + struct pyrf_callchain_node *pnode; + + if (!pchain->resolved) { + struct evsel *evsel = pchain->pevent->sample.evsel; + struct evlist *evlist = evsel->evlist; + struct perf_session *session = evlist ? evlist__session(evlist) : NULL; + struct addr_location al; + struct callchain_cursor *cursor; + struct callchain_cursor_node *node; + u64 i; + + if (!session || !pchain->pevent->sample.callchain) + return NULL; + + addr_location__init(&al); + if (machine__resolve(&session->machines.host, &al, &pchain->pevent->sample) < 0) { + addr_location__exit(&al); + return NULL; + } + + cursor = get_tls_callchain_cursor(); + if (thread__resolve_callchain(al.thread, cursor, evsel, + &pchain->pevent->sample, NULL, NULL, + PERF_MAX_STACK_DEPTH) != 0) { + addr_location__exit(&al); + return NULL; + } + callchain_cursor_commit(cursor); + + pchain->nr_frames = cursor->nr; + if (pchain->nr_frames > 0) { + pchain->frames = calloc(pchain->nr_frames, sizeof(*pchain->frames)); + if (!pchain->frames) { + addr_location__exit(&al); + return PyErr_NoMemory(); + } + + for (i = 0; i < pchain->nr_frames; i++) { + node = callchain_cursor_current(cursor); + pchain->frames[i].ip = node->ip; + pchain->frames[i].map = map__get(node->ms.map); + pchain->frames[i].sym = node->ms.sym; + callchain_cursor_advance(cursor); + } + } + pchain->resolved = true; + addr_location__exit(&al); + } + + if (pchain->pos >= pchain->nr_frames) + return NULL; + + pnode = PyObject_New(struct pyrf_callchain_node, &pyrf_callchain_node__type); + if (!pnode) + return NULL; + + pnode->ip = pchain->frames[pchain->pos].ip; + pnode->map = map__get(pchain->frames[pchain->pos].map); + pnode->sym = pchain->frames[pchain->pos].sym; + + pchain->pos++; + return (PyObject *)pnode; +} + +static PyTypeObject pyrf_callchain__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.callchain", + .tp_basicsize = sizeof(struct pyrf_callchain), + .tp_dealloc = (destructor)pyrf_callchain__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = "perf callchain object.", + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)pyrf_callchain__next, +}; + +static PyObject *pyrf_sample_event__get_callchain(PyObject *self, void *closure __maybe_unused) +{ + struct pyrf_event *pevent = (void *)self; + + if (!pevent->callchain) + Py_RETURN_NONE; + + Py_INCREF(pevent->callchain); + return pevent->callchain; +} + static PyObject* pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) { @@ -681,6 +859,12 @@ pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) } static PyGetSetDef pyrf_sample_event__getset[] = { + { + .name = "callchain", + .get = pyrf_sample_event__get_callchain, + .set = NULL, + .doc = "event callchain.", + }, { .name = "raw_buf", .get = (getter)pyrf_sample_event__get_raw_buf, @@ -850,6 +1034,12 @@ static int pyrf_event__setup_types(void) err = PyType_Ready(&pyrf_context_switch_event__type); if (err < 0) goto out; + err = PyType_Ready(&pyrf_callchain_node__type); + if (err < 0) + goto out; + err = PyType_Ready(&pyrf_callchain__type); + if (err < 0) + goto out; out: return err; } @@ -870,9 +1060,11 @@ static PyTypeObject *pyrf_event__type[] = { }; static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel, - struct perf_session *session __maybe_unused) + struct perf_session *session) { struct pyrf_event *pevent; + struct perf_sample *sample; + struct machine *machine = session ? &session->machines.host : NULL; size_t size; int err; size_t min_size = sizeof(struct perf_event_header); @@ -1031,6 +1223,7 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev } perf_sample__init(&pevent->sample, /*all=*/true); + pevent->callchain = NULL; pevent->al_resolved = false; addr_location__init(&pevent->al); @@ -1044,6 +1237,49 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev return PyErr_Format(PyExc_OSError, "perf: can't parse sample, err=%d", err); } + sample = &pevent->sample; + if (machine && sample->callchain) { + struct addr_location al; + struct callchain_cursor *cursor; + u64 i; + struct pyrf_callchain *pchain; + + addr_location__init(&al); + if (machine__resolve(machine, &al, sample) >= 0) { + cursor = get_tls_callchain_cursor(); + if (thread__resolve_callchain(al.thread, cursor, evsel, sample, + NULL, NULL, PERF_MAX_STACK_DEPTH) == 0) { + callchain_cursor_commit(cursor); + + pchain = PyObject_New(struct pyrf_callchain, &pyrf_callchain__type); + if (pchain) { + pchain->pevent = pevent; + Py_INCREF(pevent); + pchain->nr_frames = cursor->nr; + pchain->pos = 0; + pchain->resolved = true; + pchain->frames = calloc(pchain->nr_frames, + sizeof(*pchain->frames)); + if (pchain->frames) { + struct callchain_cursor_node *node; + + for (i = 0; i < pchain->nr_frames; i++) { + node = callchain_cursor_current(cursor); + pchain->frames[i].ip = node->ip; + pchain->frames[i].map = + map__get(node->ms.map); + pchain->frames[i].sym = node->ms.sym; + callchain_cursor_advance(cursor); + } + pevent->callchain = (PyObject *)pchain; + } else { + Py_DECREF(pchain); + } + } + } + addr_location__exit(&al); + } + } return (PyObject *)pevent; } @@ -3141,6 +3377,9 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject } psession->session = session; + symbol_conf.use_callchain = true; + symbol_conf.show_kernel_path = true; + symbol_conf.inline_name = false; if (symbol__init(perf_session__env(session)) < 0) { PyErr_SetString(PyExc_OSError, "perf: symbol__init failed"); goto err_out; -- 2.54.0.545.g6539524ca2-goog