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 41A163A7F60 for ; Thu, 23 Apr 2026 16:34:57 +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=1776962098; cv=none; b=fPWVOloS62CQepqo5FtdrEbX1vZvWYgzLu9exab6l4/HFkUZjC5AyrgBxDmS6aZNcKcmy4CMiXHrwlPgVOvRDS0NjHGQd2B34VATtgd+33/Mdyi5p3UMYJQ0iXW8AwnrLA1YnCpCJVs511PIjZVefkVz8lG7Wb9GQAPNkOnVews= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776962098; c=relaxed/simple; bh=Q9A6Fy/T5nITzrEIcbVTJsrcRKhWo07ZTbbHva8YMA8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oZ/aUyLbZiw2AmsYnAx4PSNWKNBh/+LdSbJr/FBb5ev4lkgdxlo2/jHUTnIl87rSh6W4R98tk60g1/2bFOc3M7srZWf1hdi6qOa/psHWRiBQybNn0saH5VsumFfZ2ikTIil9zJi+JPHQVq0q+Pguu1V42aeA3mpmM2zkfKBEWZM= 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=Cr/hG8x1; 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="Cr/hG8x1" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2c0f6593ef5so8468712eec.1 for ; Thu, 23 Apr 2026 09:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776962096; x=1777566896; 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=VZwGVsQ9oOkj3162dFlhg6DsvT8t476do7f7ssi8lO8=; b=Cr/hG8x14hvMmzT2XLe0HaroGg0qGyLEwLbuyUND+mDZnoyABYt3Evw6Lye5JO2RWn MB79VR9STK+GJh5Fyb7P0/XCS/13GEhx7xg/JZwTsIqelX0Mtkmwnq3e0MVvssvATgXD SN7PttRjAYCDPhC6MNgrbZVvu6CsDMk+D6LkWwZvVfj2cdUHGvUKdQEZ29zV37s8pIuU eGNJyzpi8f0n2UBam+VuEQwtbUKpnEGRyRrLW1w6v+JO7tqGd0PJSuaKQhoaz9QA4zQ/ ttnknMIs4b2PmFYmWUJ82jqTPe/BiPKoJOqBc+3jy81a+NzZ+6Oodro+W2Q+Sa47tELy FhDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776962096; x=1777566896; 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=VZwGVsQ9oOkj3162dFlhg6DsvT8t476do7f7ssi8lO8=; b=JMG97BPRTlzf4P8FPn8qX2CT+oHzXLb0qxHo4MaIW6599QkI+3BLfModS5tNxARoYd M3W33lJjt+5WPihmvyz6y5ptc5jfgAdm+0ckSSxqvTUELfw4NguvMNJAjuf7/4vxUAFh TwEa920EEH8Zey5piKLPL5e2yVyFdzB67xrf470vCbpHobDh00m9zWcxkX94vSm5JWbZ 4wbiiDw3sFRlOZQcGX0YRU66/CFxDGd/D1PJy0gHVms5zBNtmeOV72ec8FFYSjnc1yJS QlnvwareIFLQA5hP6ShXVy1rppmuSppCB6f527sjIuiYh/emz8YduDdCcZuKBOrfJnQ0 8nfQ== X-Forwarded-Encrypted: i=1; AFNElJ8sN6R9lHrZxUD8cM5WrHvW3Q0sCe3KDKziU2rKKGOihkRyTr9J07RhaGrSSp+703gP611cJsBFPs83DcM=@vger.kernel.org X-Gm-Message-State: AOJu0YxpDFxT0huaiIeJLQD3lb59rDQyy4f6Z8+5Wzy/7GHiaF8a153e fuyc0Lu133hd2HG7LdtO/re19hMkg98jF4vdBlrdDWM+DSmgIWWM3MSZ0BpAbg9I2bwROEzhprx d88dgXSNUnA== X-Received: from dybgp27.prod.google.com ([2002:a05:7301:211b:b0:2e3:c168:2677]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:5784:b0:2dd:c066:c02 with SMTP id 5a478bee46e88-2e47a10653dmr15324185eec.22.1776962095992; Thu, 23 Apr 2026 09:34:55 -0700 (PDT) Date: Thu, 23 Apr 2026 09:33:26 -0700 In-Reply-To: <20260423163406.1779809-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260423161006.1762700-1-irogers@google.com> <20260423163406.1779809-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260423163406.1779809-19-irogers@google.com> Subject: [PATCH v4 18/58] perf python: Add callchain support From: Ian Rogers To: irogers@google.com, acme@kernel.org, adrian.hunter@intel.com, james.clark@linaro.org, leo.yan@linux.dev, namhyung@kernel.org, tmricht@linux.ibm.com Cc: 9erthalion6@gmail.com, adityab1@linux.ibm.com, alexandre.chartre@oracle.com, alice.mei.rogers@gmail.com, ankur.a.arora@oracle.com, ashelat@redhat.com, atrajeev@linux.ibm.com, blakejones@google.com, changbin.du@huawei.com, chuck.lever@oracle.com, collin.funk1@gmail.com, coresight@lists.linaro.org, ctshao@google.com, dapeng1.mi@linux.intel.com, derek.foreman@collabora.com, dsterba@suse.com, gautam@linux.ibm.com, howardchu95@gmail.com, john.g.garry@oracle.com, jolsa@kernel.org, jonathan.cameron@huawei.com, justinstitt@google.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mike.leach@arm.com, mingo@redhat.com, morbo@google.com, nathan@kernel.org, nichen@iscas.ac.cn, nick.desaulniers+lkml@gmail.com, pan.deng@intel.com, peterz@infradead.org, ravi.bangoria@amd.com, ricky.ringler@proton.me, stephen.s.brennan@oracle.com, sun.jian.kdev@gmail.com, suzuki.poulose@arm.com, swapnil.sapkal@amd.com, tanze@kylinos.cn, terrelln@fb.com, thomas.falcon@intel.com, tianyou.li@intel.com, tycho@kernel.org, wangyang.guo@intel.com, xiaqinxin@huawei.com, yang.lee@linux.alibaba.com, yuzhuo@google.com, zhiguo.zhou@intel.com, zli94@ncsu.edu 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. --- tools/perf/util/python.c | 237 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 63ee9bc65721..28961ad47010 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -66,6 +66,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; }; @@ -103,6 +105,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); } @@ -621,6 +624,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) { @@ -635,6 +813,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, @@ -803,6 +987,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; } @@ -848,6 +1038,7 @@ static PyObject *pyrf_event__new(const union perf_event *event) if (pevent != NULL) { memcpy(&pevent->event, event, event->header.size); pevent->sample.evsel = NULL; + pevent->callchain = NULL; pevent->al_resolved = false; addr_location__init(&pevent->al); } @@ -2810,6 +3001,49 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool, if (pevent->sample.merged_callchain) pevent->sample.callchain = NULL; + if (sample->callchain) { + struct addr_location al; + struct callchain_cursor *cursor; + u64 i; + struct pyrf_callchain *pchain; + + addr_location__init(&al); + if (machine__resolve(&psession->session->machines.host, &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); + } + } + ret = PyObject_CallFunction(psession->sample, "O", pyevent); if (!ret) { PyErr_Print(); @@ -2900,6 +3134,9 @@ static int pyrf_session__init(struct pyrf_session *psession, PyObject *args, PyO return -1; } + symbol_conf.use_callchain = true; + symbol_conf.show_kernel_path = true; + symbol_conf.inline_name = false; if (symbol__init(perf_session__env(psession->session)) < 0) { perf_session__delete(psession->session); psession->session = NULL; -- 2.54.0.rc2.533.g4f5dca5207-goog