From nobody Mon Oct 6 03:14:00 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 6639E25CC4B for ; Fri, 25 Jul 2025 18:52:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753469552; cv=none; b=HXkP69vyJByf46fEW5i2iiSxd8JRWRbdpqwsAXEsEN+2FqvgOUItz0zqSK3brUczMJ04illhczT7t+EM73Jf5T8e7+0xd0lhdWOZh5wyMPtMnrPuqm1/4a8e/xhR4wd+CoQrTTpd7c7P8YLH3X6TmXPUVTiBt8CbwJ4H7yakYns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753469552; c=relaxed/simple; bh=WH5p4e07F4j/C2grpvaVClwmENW7p3AYQnbpEGMwEwM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E9XxPUWYrSToHlqgCIpz9k27kV0zyMYS/nGmDIdWNYdQAHT0O3ZatA0jDaFJJ5sSeHU78VQFdaLB6JtAeRlHDzp1O4U66yoHToFldzQ2+kL6+psArFoy5jQN1aw9ZPsShBOBVLaK9vj5soOgVp82tLuZQJv7ba18oOCB3m388gs= 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=kFb+O97a; arc=none smtp.client-ip=209.85.214.202 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="kFb+O97a" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-234906c5e29so29890935ad.0 for ; Fri, 25 Jul 2025 11:52:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753469551; x=1754074351; 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=36jzqX45ozM1W5UExySfyBKC/IdxpLPD3I1cgg1xnIc=; b=kFb+O97auJ/9pL8k5QPw/srQGAtbwsxq/css43HRrV6tIBjb8lY49DoSimTmQREnjy s4tFhlRR143DPqOSO1atouWCl4b1ZGf0CxQPrRXWsbqWEcIKBX5SQJ4ZpYfyjRJmWbLy RmYm7NhJG1PSzDxRCJGM3AoRhE/NRDUXSFBe+6x+bEVXuxT7TAZiIVgdQosl23EnfOys kDW4DnZyJRbBURo1qj2NI3WhLUteG93MX36wKZgrh+u6Hvk/wZ58jZa+nnk6Pzmc/g1e M22cBnka4FqcNUPiJxD4zOCUpl2IRQNJ+UxlpmeofX3L64YVdzK0FC6dw+98lyxtRhpJ EwRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753469551; x=1754074351; 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=36jzqX45ozM1W5UExySfyBKC/IdxpLPD3I1cgg1xnIc=; b=ggBqve1yWs06Rm64lTHg20aw7a1HX1Ld0R+ON1jx+5qUD80536kBtyUjFvRExuiKa+ ayJM18zsDN525VR1QFv+bi2QecrQzJ9Uv32yjUfd/KG2RQ9hreuH+rGcvmXl89HBwofP lsmt46ljbWL0tkfdzHQbjaX2ZCLSqkqU6P86sQTdD51fXf1wvzBxixJMq9VWLcs1ICfN 4vK9J6boiixg7VlWGMQAdAvJKxAn4V3TdmHOxhLou6UO2qa9Bz1j6vWe4DDKSD/0ylJD hcBfuK34rgsKQsxIukHW2ytyz7mwlKqoab/uf/v9XFWS9MYprBTmeeKiULaZSc+c1t5W CG2A== X-Forwarded-Encrypted: i=1; AJvYcCUaqUloGXSDLl944hsxt8yMWeFRlC6ZtK2W2eLXJXPvsj8kBMuBTtvRhq2zV9WjaXPwmdA97GOWPMJNDVc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1jEVh4P5hXOIkoZrvlnt5LkdVEt5stR6pouz2IXQSgVbDZNxI gnQxDef7TYQeWzpJ1t/iT/HOHuqY9Ujxb/hk6a/9jJWHoaAtjGOvro2hHYGvVyde+Fuk/mMOlhU uEzse1PMGrQ== X-Google-Smtp-Source: AGHT+IFGweU6cmOkhYOD4Op5Ao3pioLcfS1ktxTpDhwRZ4rXL+7ZFxYMYEvTsxGoi+V91kmvV7lJYLdPbbVi X-Received: from plbmq11.prod.google.com ([2002:a17:902:fd4b:b0:23f:b1ee:3457]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1111:b0:234:d7b2:2ac3 with SMTP id d9443c01a7336-23fb3029b76mr43134105ad.20.1753469550702; Fri, 25 Jul 2025 11:52:30 -0700 (PDT) Date: Fri, 25 Jul 2025 11:51:55 -0700 In-Reply-To: <20250725185202.68671-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: <20250725185202.68671-1-irogers@google.com> X-Mailer: git-send-email 2.50.1.552.g942d659e1b-goog Message-ID: <20250725185202.68671-10-irogers@google.com> Subject: [PATCH v9 09/16] perf python: Add basic PMU abstraction and pmus sequence From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Xu Yang , "Masami Hiramatsu (Google)" , Collin Funk , Howard Chu , Weilin Wang , Andi Kleen , "Dr. David Alan Gilbert" , Thomas Richter , Tiezhu Yang , Gautam Menghani , Thomas Falcon , Chun-Tse Shao , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Arnaldo Carvalho de Melo Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an ability to iterate over PMUs and a basic PMU type then can just show the PMU's name. An example usage: ``` $ python Python 3.12.9 (main, Feb 5 2025, 01:31:18) [GCC 14.2.0] on linux >>> import perf >>> list(perf.pmus()) [pmu(cpu), pmu(breakpoint), pmu(cstate_core), pmu(cstate_pkg), pmu(hwmon_acpitz), pmu(hwmon_ac), pmu(hwmon_bat0), pmu(hwmon_coretemp), pmu(hwmon_iwlwifi_1), pmu(hwmon_nvme), pmu(hwmon_thinkpad), pmu(hwmon_ucsi_source_psy_usbc000_0), pmu(hwmon_ucsi_source_psy_usbc000_0), pmu(i915), pmu(intel_bts), pmu(intel_pt), pmu(kprobe), pmu(msr), pmu(power), pmu(software), pmu(tool), pmu(tracepoint), pmu(uncore_arb), pmu(uncore_cbox_0), pmu(uncore_cbox_1), pmu(uncore_cbox_2), pmu(uncore_cbox_3), pmu(uncore_cbox_4), pmu(uncore_cbox_5), pmu(uncore_cbox_6), pmu(uncore_cbox_7), pmu(uncore_clock), pmu(uncore_imc_free_running_0), pmu(uncore_imc_free_running_1), pmu(uprobe)] ``` Signed-off-by: Ian Rogers Tested-by: Arnaldo Carvalho de Melo --- tools/perf/util/python.c | 140 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 9c9972d77d1e..ddcb5d40ff14 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -639,6 +639,138 @@ static int pyrf_thread_map__setup_types(void) return PyType_Ready(&pyrf_thread_map__type); } =20 +/** + * A python wrapper for perf_pmus that are globally owned by the pmus.c co= de. + */ +struct pyrf_pmu { + PyObject_HEAD + + struct perf_pmu *pmu; +}; + +static void pyrf_pmu__delete(struct pyrf_pmu *ppmu) +{ + Py_TYPE(ppmu)->tp_free((PyObject *)ppmu); +} + +static PyObject *pyrf_pmu__name(PyObject *self) +{ + struct pyrf_pmu *ppmu =3D (void *)self; + + return PyUnicode_FromString(ppmu->pmu->name); +} + +static PyObject *pyrf_pmu__repr(PyObject *self) +{ + struct pyrf_pmu *ppmu =3D (void *)self; + + return PyUnicode_FromFormat("pmu(%s)", ppmu->pmu->name); +} + +static const char pyrf_pmu__doc[] =3D PyDoc_STR("perf Performance Monitori= ng Unit (PMU) object."); + +static PyMethodDef pyrf_pmu__methods[] =3D { + { + .ml_name =3D "name", + .ml_meth =3D (PyCFunction)pyrf_pmu__name, + .ml_flags =3D METH_NOARGS, + .ml_doc =3D PyDoc_STR("Name of the PMU including suffixes.") + }, + { .ml_name =3D NULL, } +}; + +/** The python type for a perf.pmu. */ +static PyTypeObject pyrf_pmu__type =3D { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name =3D "perf.pmu", + .tp_basicsize =3D sizeof(struct pyrf_pmu), + .tp_dealloc =3D (destructor)pyrf_pmu__delete, + .tp_flags =3D Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc =3D pyrf_pmu__doc, + .tp_methods =3D pyrf_pmu__methods, + .tp_str =3D pyrf_pmu__name, + .tp_repr =3D pyrf_pmu__repr, +}; + +static int pyrf_pmu__setup_types(void) +{ + pyrf_pmu__type.tp_new =3D PyType_GenericNew; + return PyType_Ready(&pyrf_pmu__type); +} + + +/** A python iterator for pmus that has no equivalent in the C code. */ +struct pyrf_pmu_iterator { + PyObject_HEAD + struct perf_pmu *pmu; +}; + +static void pyrf_pmu_iterator__dealloc(struct pyrf_pmu_iterator *self) +{ + Py_TYPE(self)->tp_free((PyObject *) self); +} + +static PyObject *pyrf_pmu_iterator__new(PyTypeObject *type, PyObject *args= __maybe_unused, + PyObject *kwds __maybe_unused) +{ + struct pyrf_pmu_iterator *itr =3D (void *)type->tp_alloc(type, 0); + + if (itr !=3D NULL) + itr->pmu =3D perf_pmus__scan(/*pmu=3D*/NULL); + + return (PyObject *) itr; +} + +static PyObject *pyrf_pmu_iterator__iter(PyObject *self) +{ + Py_INCREF(self); + return self; +} + +static PyObject *pyrf_pmu_iterator__iternext(PyObject *self) +{ + struct pyrf_pmu_iterator *itr =3D (void *)self; + struct pyrf_pmu *ppmu; + + if (itr->pmu =3D=3D NULL) { + PyErr_SetNone(PyExc_StopIteration); + return NULL; + } + // Create object to return. + ppmu =3D PyObject_New(struct pyrf_pmu, &pyrf_pmu__type); + if (ppmu) { + ppmu->pmu =3D itr->pmu; + // Advance iterator. + itr->pmu =3D perf_pmus__scan(itr->pmu); + } + return (PyObject *)ppmu; +} + +/** The python type for the PMU iterator. */ +static PyTypeObject pyrf_pmu_iterator__type =3D { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name =3D "pmus.iterator", + .tp_doc =3D "Iterator for the pmus string sequence.", + .tp_basicsize =3D sizeof(struct pyrf_pmu_iterator), + .tp_itemsize =3D 0, + .tp_flags =3D Py_TPFLAGS_DEFAULT, + .tp_new =3D pyrf_pmu_iterator__new, + .tp_dealloc =3D (destructor) pyrf_pmu_iterator__dealloc, + .tp_iter =3D pyrf_pmu_iterator__iter, + .tp_iternext =3D pyrf_pmu_iterator__iternext, +}; + +static int pyrf_pmu_iterator__setup_types(void) +{ + return PyType_Ready(&pyrf_pmu_iterator__type); +} + +static PyObject *pyrf__pmus(PyObject *self, PyObject *args) +{ + // Calling the class creates an instance of the iterator. + return PyObject_CallObject((PyObject *) &pyrf_pmu_iterator__type, /*args= =3D*/NULL); +} + struct pyrf_counts_values { PyObject_HEAD =20 @@ -1691,6 +1823,12 @@ static PyMethodDef perf__methods[] =3D { .ml_flags =3D METH_VARARGS, .ml_doc =3D PyDoc_STR("Parse a string of events and return an evlist.") }, + { + .ml_name =3D "pmus", + .ml_meth =3D (PyCFunction) pyrf__pmus, + .ml_flags =3D METH_NOARGS, + .ml_doc =3D PyDoc_STR("Returns a sequence of pmus.") + }, { .ml_name =3D NULL, } }; =20 @@ -1718,6 +1856,8 @@ PyMODINIT_FUNC PyInit_perf(void) pyrf_evsel__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 || pyrf_cpu_map__setup_types() < 0 || + pyrf_pmu_iterator__setup_types() < 0 || + pyrf_pmu__setup_types() < 0 || pyrf_counts_values__setup_types() < 0) return module; =20 --=20 2.50.1.552.g942d659e1b-goog