From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EBADECD5BB3 for ; Fri, 22 May 2026 22:05:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=w8zoT8NtEq4Wo2jdLV0BHRgdjeNd/pAmkexwraDI86U=; b=hNtpqcogPzVm+TYy7JZOTI9zDt O1xWwH58vN04cM1bSaEfcj5WjOTW6ELz6PTOChQvJgd5OtrkoO2YBeCy7+KOMu+sJ0OzlBnfqwyLf bwQzCpugfrvb9BQ52EaQjrwXEQzoKFnP7AayJtTy9PxJnF0ELH8PduI6Vgd+3Z398Rh9wGLr0TEgd D3es5ZPoO21FwTw7k0Ay7jTnWyRO3UgUs172+QCUUMy/G+zAomO6rDFtYLFka03mvSnMyyHehjOyd vzwvTrmM8JCqNAEsbNeBUwlOIjRLqkftriT6OcW+MCZOYE+CWxw7D7o1KqRuvnFnJqcnix0Js2HBq VfxnZViw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wQXzx-0000000C3N4-3LEe; Fri, 22 May 2026 22:05:41 +0000 Received: from mail-dl1-x1249.google.com ([2607:f8b0:4864:20::1249]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wQXze-0000000C2zB-1hS8 for linux-arm-kernel@lists.infradead.org; Fri, 22 May 2026 22:05:24 +0000 Received: by mail-dl1-x1249.google.com with SMTP id a92af1059eb24-135f2aa4742so6169142c88.1 for ; Fri, 22 May 2026 15:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779487521; x=1780092321; darn=lists.infradead.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=w8zoT8NtEq4Wo2jdLV0BHRgdjeNd/pAmkexwraDI86U=; b=s6NQpazV7/egG8GT8SZ257l7+/GfidVnwQi8KNw1UcPbtMKuWTdFQpiMAjR/kJyzwo V7veJE8Dbm00RR9JJPkXqwhpRnGVajyKf5N55vFNVNOPAD8651uSWBWYfjTb+PO+Jk41 SHP60fwgDTujpggFWcyJFwSD0ejIXUcVNoQcsrRszh8QxgprWZJF/xLPgkOeRFDt/Vfl /IUibO9FrCPk4WnwTrDspLysGV790qNiB3FeOTUz3GMu8wsdxhYSkBNQ7kMLLhMkz4ll +G66Bmk4NtVprCkSy+FwdJyskjCSlHCpwZahKJ6HIbFNaeA5u8iqM1K8+K9/VPJM2db+ 85Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779487521; x=1780092321; 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=w8zoT8NtEq4Wo2jdLV0BHRgdjeNd/pAmkexwraDI86U=; b=Ed5Dw+A0X6R6X3GK3vEwRYyntVy7OfUbs2CUEEcrNSjYRNXQpdRmHpYhrwKscoU+Vf M1Vf/ngF+yjiCyo3S543k+rs6F9YvHD05zcaQEUKW5fEkwaxeKonZ+/qaMYC6ii/Drfo sXkVmmf4IJasCIjxw8B6Sb6yr5ncuTcNn0Hz+ZZJ9KxfX7BvuqM+N+K8+TAsmxmdEkFF oDUHXdEEOijxg74IoYRGJ+5efUbIw42h/PGeZhEBnGW1r3KdkZsTc7uRhvesknbsNiEI qu6OxHrz1d64Oe/HPgBbgpQIfBwhOQv0sEf+GtW01M2ZZVrBb5iz69gxkoVrrcnIs4aC rXEQ== X-Forwarded-Encrypted: i=1; AFNElJ+z4yc8twt7RmxNEenecgx6pGZixM3k8rC1Fx6ZNsCaKPTAAyj6EZDBfeaiGdcNIkHjqiTjSbXZn3VSbbVCG0++@lists.infradead.org X-Gm-Message-State: AOJu0Yw8v0yb6dpLXA8mOdPwRkqDl6PV9vUYuEbHfoBbQmnSKNYcNIb2 ybHDyYrWZyJMZQsfpkTlxtAC2MWJkk8qMHQE18CKlBaGjW3dekMDce0SHG4dESBbP9NweMsdzbN tqgr9kSRSEA== X-Received: from dlbvc2.prod.google.com ([2002:a05:7022:fe02:b0:133:3081:bb33]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:128a:b0:135:dd7c:7 with SMTP id a92af1059eb24-1365fc6db3dmr2056201c88.38.1779487520955; Fri, 22 May 2026 15:05:20 -0700 (PDT) Date: Fri, 22 May 2026 15:04:29 -0700 In-Reply-To: <20260522220435.2378363-1-irogers@google.com> Mime-Version: 1.0 References: <20260428071903.1886173-1-irogers@google.com> <20260522220435.2378363-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.794.g4f17f83d09-goog Message-ID: <20260522220435.2378363-19-irogers@google.com> Subject: [PATCH v9 18/23] perf python: Extend API for stat events in python.c From: Ian Rogers To: irogers@google.com, 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 Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260522_150522_481739_2F33D289 X-CRM114-Status: GOOD ( 20.64 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add stat information to the session. Add call backs for stat events. Assisted-by: Gemini:gemini-3.1-pro-preview Signed-off-by: Ian Rogers --- v5: 1. Fix Memory Corruption: Corrected the memory offset for `stat_round_type` in `pyrf_stat_round_event__members` by adding the base offset of `struct pyrf_event`. 2. Fix Memory Leak: Added `Py_XDECREF()` to free the unused return value of the Python callback in `pyrf_session_tool__stat_round()`. --- v7: - Added comprehensive size checks in pyrf_event__new for all event types. - Fixed line length warning. --- tools/perf/util/python.c | 180 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 173 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index de7a51389572..4ee3fdbf35de 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -343,6 +343,77 @@ static PyTypeObject pyrf_lost_event__type = { .tp_repr = (reprfunc)pyrf_lost_event__repr, }; +static const char pyrf_stat_event__doc[] = PyDoc_STR("perf stat event object."); + +static PyMemberDef pyrf_stat_event__members[] = { + sample_members + member_def(perf_event_header, type, T_UINT, "event type"), + member_def(perf_record_stat, id, T_ULONGLONG, "event id"), + member_def(perf_record_stat, cpu, T_UINT, "event cpu"), + member_def(perf_record_stat, thread, T_UINT, "event thread"), + member_def(perf_record_stat, val, T_ULONGLONG, "counter value"), + member_def(perf_record_stat, ena, T_ULONGLONG, "enabled time"), + member_def(perf_record_stat, run, T_ULONGLONG, "running time"), + { .name = NULL, }, +}; + +static PyObject *pyrf_stat_event__repr(const struct pyrf_event *pevent) +{ + return PyUnicode_FromFormat( + "{ type: stat, id: %llu, cpu: %u, thread: %u, val: %llu, ena: %llu, run: %llu }", + pevent->event.stat.id, + pevent->event.stat.cpu, + pevent->event.stat.thread, + pevent->event.stat.val, + pevent->event.stat.ena, + pevent->event.stat.run); +} + +static PyTypeObject pyrf_stat_event__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.stat_event", + .tp_basicsize = sizeof(struct pyrf_event), + .tp_new = PyType_GenericNew, + .tp_dealloc = (destructor)pyrf_event__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_stat_event__doc, + .tp_members = pyrf_stat_event__members, + .tp_getset = pyrf_event__getset, + .tp_repr = (reprfunc)pyrf_stat_event__repr, +}; + +static const char pyrf_stat_round_event__doc[] = PyDoc_STR("perf stat round event object."); + +static PyMemberDef pyrf_stat_round_event__members[] = { + sample_members + member_def(perf_event_header, type, T_UINT, "event type"), + { .name = "stat_round_type", .type = T_ULONGLONG, + .offset = offsetof(struct pyrf_event, event) + offsetof(struct perf_record_stat_round, type), + .doc = "round type" }, + member_def(perf_record_stat_round, time, T_ULONGLONG, "round time"), + { .name = NULL, }, +}; + +static PyObject *pyrf_stat_round_event__repr(const struct pyrf_event *pevent) +{ + return PyUnicode_FromFormat("{ type: stat_round, type: %llu, time: %llu }", + pevent->event.stat_round.type, + pevent->event.stat_round.time); +} + +static PyTypeObject pyrf_stat_round_event__type = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "perf.stat_round_event", + .tp_basicsize = sizeof(struct pyrf_event), + .tp_new = PyType_GenericNew, + .tp_dealloc = (destructor)pyrf_event__delete, + .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc = pyrf_stat_round_event__doc, + .tp_members = pyrf_stat_round_event__members, + .tp_getset = pyrf_event__getset, + .tp_repr = (reprfunc)pyrf_stat_round_event__repr, +}; + static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); static PyMemberDef pyrf_read_event__members[] = { @@ -1034,6 +1105,12 @@ static int pyrf_event__setup_types(void) if (err < 0) goto out; err = PyType_Ready(&pyrf_context_switch_event__type); + if (err < 0) + goto out; + err = PyType_Ready(&pyrf_stat_event__type); + if (err < 0) + goto out; + err = PyType_Ready(&pyrf_stat_round_event__type); if (err < 0) goto out; err = PyType_Ready(&pyrf_callchain_node__type); @@ -1059,6 +1136,8 @@ static PyTypeObject *pyrf_event__type[] = { [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, + [PERF_RECORD_STAT] = &pyrf_stat_event__type, + [PERF_RECORD_STAT_ROUND] = &pyrf_stat_round_event__type, }; static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *evsel, @@ -1091,13 +1170,13 @@ static PyObject *pyrf_event__new(const union perf_event *event, struct evsel *ev case PERF_RECORD_EXIT: min_size = sizeof(struct perf_record_fork); break; + case PERF_RECORD_LOST: + min_size = sizeof(struct perf_record_lost); + break; case PERF_RECORD_THROTTLE: case PERF_RECORD_UNTHROTTLE: min_size = sizeof(struct perf_record_throttle); break; - case PERF_RECORD_LOST: - min_size = sizeof(struct perf_record_lost); - break; case PERF_RECORD_READ: min_size = sizeof(struct perf_record_read); break; @@ -2119,7 +2198,40 @@ static PyObject *pyrf_evsel__get_attr_wakeup_events(PyObject *self, void */*clos return PyLong_FromUnsignedLong(pevsel->evsel->core.attr.wakeup_events); } +static PyObject *pyrf_evsel__get_ids(struct pyrf_evsel *pevsel, void *closure __maybe_unused) +{ + struct evsel *evsel = pevsel->evsel; + PyObject *list = PyList_New(0); + + if (!list) + return NULL; + + for (u32 i = 0; i < evsel->core.ids; i++) { + PyObject *id = PyLong_FromUnsignedLongLong(evsel->core.id[i]); + int ret; + + if (!id) { + Py_DECREF(list); + return NULL; + } + ret = PyList_Append(list, id); + Py_DECREF(id); + if (ret < 0) { + Py_DECREF(list); + return NULL; + } + } + + return list; +} + static PyGetSetDef pyrf_evsel__getset[] = { + { + .name = "ids", + .get = (getter)pyrf_evsel__get_ids, + .set = NULL, + .doc = "event IDs.", + }, { .name = "tracking", .get = pyrf_evsel__get_tracking, @@ -2894,6 +3006,8 @@ static const struct perf_constant perf__constants[] = { PERF_CONST(RECORD_LOST_SAMPLES), PERF_CONST(RECORD_SWITCH), PERF_CONST(RECORD_SWITCH_CPU_WIDE), + PERF_CONST(RECORD_STAT), + PERF_CONST(RECORD_STAT_ROUND), PERF_CONST(RECORD_MISC_SWITCH_OUT), { .name = NULL, }, @@ -3270,6 +3384,7 @@ struct pyrf_session { struct perf_tool tool; struct pyrf_data *pdata; PyObject *sample; + PyObject *stat; }; static int pyrf_session_tool__sample(const struct perf_tool *tool, @@ -3294,6 +3409,50 @@ static int pyrf_session_tool__sample(const struct perf_tool *tool, return 0; } +static int pyrf_session_tool__stat(const struct perf_tool *tool, + struct perf_session *session, + union perf_event *event) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); + struct evsel *evsel = evlist__id2evsel(session->evlist, event->stat.id); + PyObject *pyevent = pyrf_event__new(event, evsel, psession->session); + const char *name = evsel ? evsel__name(evsel) : "unknown"; + PyObject *ret; + + if (pyevent == NULL) + return -ENOMEM; + + ret = PyObject_CallFunction(psession->stat, "Os", pyevent, name); + if (!ret) { + Py_DECREF(pyevent); + return -1; + } + Py_DECREF(ret); + Py_DECREF(pyevent); + return 0; +} + +static int pyrf_session_tool__stat_round(const struct perf_tool *tool, + struct perf_session *session __maybe_unused, + union perf_event *event) +{ + struct pyrf_session *psession = container_of(tool, struct pyrf_session, tool); + PyObject *pyevent = pyrf_event__new(event, /*evsel=*/NULL, psession->session); + PyObject *ret; + + if (pyevent == NULL) + return -ENOMEM; + + ret = PyObject_CallFunction(psession->stat, "O", pyevent); + if (!ret) { + Py_DECREF(pyevent); + return -1; + } + Py_DECREF(ret); + Py_DECREF(pyevent); + return 0; +} + static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObject *args) { struct machine *machine; @@ -3325,13 +3484,13 @@ static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObje static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { struct pyrf_data *pdata; - PyObject *sample = NULL; - static char *kwlist[] = { "data", "sample", NULL }; + PyObject *sample = NULL, *stat = NULL; + static char *kwlist[] = { "data", "sample", "stat", NULL }; struct pyrf_session *psession; struct perf_session *session; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O", kwlist, &pyrf_data__type, &pdata, - &sample)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|OO", kwlist, &pyrf_data__type, &pdata, + &sample, &stat)) return NULL; psession = PyObject_New(struct pyrf_session, type); @@ -3340,6 +3499,7 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject psession->session = NULL; psession->sample = NULL; + psession->stat = NULL; psession->pdata = NULL; Py_INCREF(pdata); @@ -3362,8 +3522,13 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject } while (0) ADD_TOOL(sample); + ADD_TOOL(stat); #undef ADD_TOOL + if (stat) + psession->tool.stat_round = pyrf_session_tool__stat_round; + + psession->tool.comm = perf_event__process_comm; psession->tool.mmap = perf_event__process_mmap; psession->tool.mmap2 = perf_event__process_mmap2; @@ -3403,6 +3568,7 @@ static void pyrf_session__delete(struct pyrf_session *psession) perf_session__delete(psession->session); Py_XDECREF(psession->pdata); Py_XDECREF(psession->sample); + Py_XDECREF(psession->stat); Py_TYPE(psession)->tp_free((PyObject *)psession); } -- 2.54.0.794.g4f17f83d09-goog