From nobody Sun Oct 5 23:38:27 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE86E64A8F; Mon, 28 Jul 2025 06:00:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753682426; cv=none; b=fLQk3DGTsTt6vJMZRDALSbFvCZCFJTk04BFUtI5AN6q8+Q0TcRBd1uknXXdq3DIktyQGl9/AiD2kpi4tq3qI62lFz9cR4LzRCzuFCCd3B6wTk9aBJxI7pLWduXBDfoaO+Njcfv+SWkgCqSupW+sXgj2bT31zzyLah7jdyaNO+Iw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753682426; c=relaxed/simple; bh=vTmlu+c7Zoz2xMIdAcpul6HT5IPGBfnHlNr4WyYp5Q0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kab7QeH5n1NJg9fgMHBBaGJpnNNT/vtHC5T8g27cMc0XlTznhw1KBSi1EMH8dbHxdzlM86SkcljybZcQ8D+HCqY61zh0gACkCzLWv3wmEi6LNsonq2R5odH+TT9iQSDEmNabmDM+MW0WasuQTExmac7uLq/fxPqbGHkvY/aJkhs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=n8ioa6GT; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="n8ioa6GT" Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56RLGVDp002686; Mon, 28 Jul 2025 06:00:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=FOh8XU1nn2zievokx Bo2RdnYJ7gCVX88ugqivTVtqLw=; b=n8ioa6GTDZ0rzbwmGAcRjfqGnB4Ha84Ep XxynFORuRaiIUbRM2ptJO65LDCxRpNIeAFXmoN9FBLJhkqoPWGNYAde7dnyiuwvv u6gFkcWqE+GvzBMkxFg23uwWJYPw9Bma/oklRmM5nWcZO4np88I4Lmyyl5SGcY6Y u8PQy0CxYKtfc63K/fWbxZBewLLlp2lOel5xmHcaJYx8hsHwOWYQ0Rhw4IjTOeXF w+R61i9dItIlCtwjiEXuw6QEC8+LXo8AMEd8+NCng8MND1ByoWJcmVpa9PqY1XiE DTBqwc7Rgc4/19JencnsiCYHUAo6EDTpcaLiD5bR9FY0xmZWA5w8w== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 484qaty8q5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:07 +0000 (GMT) Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 56S5pt53014355; Mon, 28 Jul 2025 06:00:06 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 484qaty8pw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:06 +0000 (GMT) Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56S5dXg2018312; Mon, 28 Jul 2025 06:00:05 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 485abnve6u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:05 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56S6013r48365930 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 28 Jul 2025 06:00:01 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 398D620043; Mon, 28 Jul 2025 06:00:01 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9EFAE20040; Mon, 28 Jul 2025 05:59:57 +0000 (GMT) Received: from li-c6426e4c-27cf-11b2-a85c-95d65bc0de0e.ibm.com.com (unknown [9.124.209.252]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 28 Jul 2025 05:59:57 +0000 (GMT) From: Gautam Menghani To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com Cc: Gautam Menghani , maddy@linux.ibm.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] perf python: Add support for record_opts struct Date: Mon, 28 Jul 2025 11:29:27 +0530 Message-ID: <20250728055937.58531-2-gautam@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250728055937.58531-1-gautam@linux.ibm.com> References: <20250728055937.58531-1-gautam@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=Xvz6OUF9 c=1 sm=1 tr=0 ts=688711e7 cx=c_pps a=GFwsV6G8L6GxiO2Y/PsHdQ==:117 a=GFwsV6G8L6GxiO2Y/PsHdQ==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=xbiQ1PCPNy82S5ANkocA:9 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzI4MDA0MiBTYWx0ZWRfXwn/i6VS1UF17 rXHsgnK4Tz1mYT2wR2CaDa+fVhVumjtAWk4q04ZSA+zWDpo2YMqJNMoy4OTKMS5u8zlZBB6ETLs S4+ec4n8LF20jRSRX6jhbjK4N/5Xcm0qFtaTMF6OwXt3grIsBmhTtE9KjLReOp91FaQ8dsxNOy2 jOn8qN+K1VcOGHyy6i1HxlwZS0a43z94iJgBLjljEur1Y+PfI4ZubZ4nUNHQXDJ/5cTammvtMNT 7D08WoM4uPUCQhwMEOjEABK8Htl6vwlbBHai1rjIMY4QGUw6Jbf3EqoyC6SBRvhnvGcgBtk6e4Z pwxqTavIvIY5R8BiQQujUyJUFrZ5ERQwhuGp2fYalMvgsct8sgYH6GrFjsG5+MFKF3ewHvYDl1g pjKMu+3zlqDFP+Flts8qmO0mgZ8qGwea9KLYqv6jlXSsQc5qZWXSBKtTg39n2dIQmPICImUf X-Proofpoint-GUID: Gk5b-BKSbOFE72l_ebJuIvE0VgTmgRRt X-Proofpoint-ORIG-GUID: WJn9SsajVC8DO_yaIyfOCKBVgvcyIZNC X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-28_02,2025-07-24_01,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 spamscore=0 priorityscore=1501 adultscore=0 lowpriorityscore=0 bulkscore=0 malwarescore=0 suspectscore=0 impostorscore=0 clxscore=1015 phishscore=0 mlxlogscore=999 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507280042 Content-Type: text/plain; charset="utf-8" Add support for record_opts struct to allow python to control the initialization of the evsels instead of using the current default options. Signed-off-by: Gautam Menghani --- tools/perf/util/python.c | 186 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 183 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 2f28f71325a8..8537edbb3b0b 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -1045,6 +1045,169 @@ static int pyrf_evsel__setup_types(void) return PyType_Ready(&pyrf_evsel__type); } =20 +struct pyrf_target { + PyObject_HEAD + + struct target target; +}; + +static int pyrf_target__init(struct pyrf_target *target, + PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] =3D { "pid", "tid", "cpu_list", "bpf_str", "system_= wide", "uses_mmap", + "default_per_cpu", "per_thread", "use_bpf", "inherit", + "initial_delay", "attr_map", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssppppppis", + kwlist, &(target->target.pid), &(target->target.tid), + &(target->target.cpu_list), &(target->target.bpf_str), + &(target->target.system_wide), + &(target->target.uses_mmap), + &(target->target.default_per_cpu), + &(target->target.per_thread), &(target->target.use_bpf), + &(target->target.inherit), &(target->target.initial_delay), + &(target->target.attr_map))) + return -1; + + return 0; +} + +static void pyrf_target__delete(struct pyrf_target *target) +{ + Py_TYPE(target)->tp_free((PyObject *)target); +} + +static const char pyrf_target__doc[] =3D PyDoc_STR("target object."); + +static PyTypeObject pyrf_target__type =3D { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name =3D "perf.target", + .tp_basicsize =3D sizeof(struct pyrf_target), + .tp_dealloc =3D (destructor)pyrf_target__delete, + .tp_flags =3D Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc =3D pyrf_target__doc, + .tp_init =3D (initproc)pyrf_target__init, +}; + +static int pyrf_target__setup_types(void) +{ + pyrf_target__type.tp_new =3D PyType_GenericNew; + return PyType_Ready(&pyrf_target__type); +} + +struct pyrf_record_opts { + PyObject_HEAD + + struct record_opts opts; +}; + +static int pyrf_record_opts__init(struct pyrf_record_opts *popts, + PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] =3D { "target", "inherit_stat", "no_buffering", "no= _inherit", + "no_inherit_set", "no_samples", "raw_samples", + "sample_address", "sample_phys_addr", "sample_data_page_size", + "sample_code_page_size", "sample_weight", "sample_time", + "sample_time_set", "sample_cpu", "sample_identifier", + "sample_data_src", "period", "period_set", "running_time", + "full_auxtrace", "auxtrace_snapshot_mode", + "auxtrace_snapshot_on_exit", "auxtrace_sample_mode", + "record_namespaces", "record_cgroup", "record_switch_events", + "record_switch_events_set", "all_kernel", "all_user", + "kernel_callchains", "user_callchains", "tail_synthesize", + "overwrite", "ignore_missing_thread", "strict_freq", "sample_id", + "no_bpf_event", "kcore", "text_poke", "build_id", "freq", + "mmap_pages", "auxtrace_mmap_pages", "user_freq", "branch_stack", + "sample_intr_regs", "sample_user_regs", "default_interval", + "user_interval", "auxtrace_snapshot_size", "auxtrace_snapshot_opts", + "auxtrace_sample_opts", "sample_transaction", "use_clockid", + "clockid", "clockid_res_ns", "nr_cblocks", "affinity", "mmap_flush", + "comp_level", "nr_threads_synthesize", "ctl_fd", "ctl_fd_ack", + "ctl_fd_close", "synth", "threads_spec", "threads_user_spec", + "off_cpu_thresh_ns", NULL }; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|OppppppppppppppppppppppppppppppppppppppppIIIIIIIIIIssppiIiiiIIiipii= sI", + kwlist, &(popts->opts.target), &(popts->opts.inherit_stat), + &(popts->opts.no_buffering), &(popts->opts.no_inherit), + &(popts->opts.no_inherit_set), &(popts->opts.no_samples), + &(popts->opts.raw_samples), &(popts->opts.sample_address), + &(popts->opts.sample_phys_addr), + &(popts->opts.sample_data_page_size), + &(popts->opts.sample_code_page_size), + &(popts->opts.sample_weight), + &(popts->opts.sample_time), &(popts->opts.sample_time_set), + &(popts->opts.sample_cpu), + &(popts->opts.sample_identifier), + &(popts->opts.sample_data_src), &(popts->opts.period), + &(popts->opts.period_set), &(popts->opts.running_time), + &(popts->opts.full_auxtrace), + &(popts->opts.auxtrace_snapshot_mode), + &(popts->opts.auxtrace_snapshot_on_exit), + &(popts->opts.auxtrace_sample_mode), + &(popts->opts.record_namespaces), + &(popts->opts.record_cgroup), + &(popts->opts.record_switch_events), + &(popts->opts.record_switch_events_set), + &(popts->opts.all_kernel), &(popts->opts.all_user), + &(popts->opts.kernel_callchains), + &(popts->opts.user_callchains), + &(popts->opts.tail_synthesize), + &(popts->opts.overwrite), + &(popts->opts.ignore_missing_thread), + &(popts->opts.strict_freq), &(popts->opts.sample_id), + &(popts->opts.no_bpf_event), &(popts->opts.kcore), + &(popts->opts.text_poke), &(popts->opts.build_id), + &(popts->opts.freq), &(popts->opts.mmap_pages), + &(popts->opts.auxtrace_mmap_pages), + &(popts->opts.user_freq), + &(popts->opts.branch_stack), + &(popts->opts.sample_intr_regs), + &(popts->opts.sample_user_regs), + &(popts->opts.default_interval), + &(popts->opts.user_interval), + &(popts->opts.auxtrace_snapshot_size), + &(popts->opts.auxtrace_snapshot_opts), + &(popts->opts.auxtrace_sample_opts), + &(popts->opts.sample_transaction), + &(popts->opts.use_clockid), + &(popts->opts.clockid), &(popts->opts.clockid_res_ns), + &(popts->opts.nr_cblocks), &(popts->opts.affinity), + &(popts->opts.mmap_flush), &(popts->opts.comp_level), + &(popts->opts.nr_threads_synthesize), + &(popts->opts.ctl_fd), + &(popts->opts.ctl_fd_ack), &(popts->opts.ctl_fd_close), + &(popts->opts.synth), &(popts->opts.threads_spec), + &(popts->opts.threads_user_spec), + &(popts->opts.off_cpu_thresh_ns))) + return -1; + + return 0; +} + +static const char pyrf_record_opts__doc[] =3D PyDoc_STR("perf record_opts = object."); + +static void pyrf_record_opts__delete(struct pyrf_record_opts *perf_record_= opts) +{ + Py_TYPE(perf_record_opts)->tp_free((PyObject *)perf_record_opts); +} + +static PyTypeObject pyrf_record_opts__type =3D { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name =3D "perf.record_opts", + .tp_basicsize =3D sizeof(struct pyrf_record_opts), + .tp_dealloc =3D (destructor)pyrf_record_opts__delete, + .tp_flags =3D Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + .tp_doc =3D pyrf_record_opts__doc, + .tp_init =3D (initproc)pyrf_record_opts__init, +}; + +static int pyrf_record_opts__setup_types(void) +{ + pyrf_record_opts__type.tp_new =3D PyType_GenericNew; + return PyType_Ready(&pyrf_record_opts__type); +} + struct pyrf_evlist { PyObject_HEAD =20 @@ -1263,7 +1426,7 @@ static PyObject *pyrf_evlist__close(struct pyrf_evlis= t *pevlist) return Py_None; } =20 -static PyObject *pyrf_evlist__config(struct pyrf_evlist *pevlist) +static PyObject *pyrf_evlist__config(struct pyrf_evlist *pevlist, PyObject= *args, PyObject *kwargs) { struct record_opts opts =3D { .sample_time =3D true, @@ -1281,8 +1444,17 @@ static PyObject *pyrf_evlist__config(struct pyrf_evl= ist *pevlist) .no_buffering =3D true, .no_inherit =3D true, }; + PyObject *popts =3D NULL; + static char *kwlist[] =3D { "record_opts", NULL }; struct evlist *evlist =3D &pevlist->evlist; =20 + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, + &popts)) + return NULL; + + if (popts) + opts =3D ((struct pyrf_record_opts *) popts)->opts; + evlist__config(evlist, &opts, &callchain_param); Py_INCREF(Py_None); return Py_None; @@ -1354,7 +1526,7 @@ static PyMethodDef pyrf_evlist__methods[] =3D { { .ml_name =3D "config", .ml_meth =3D (PyCFunction)pyrf_evlist__config, - .ml_flags =3D METH_NOARGS, + .ml_flags =3D METH_VARARGS | METH_KEYWORDS, .ml_doc =3D PyDoc_STR("Apply default record options to the evlist.") }, { @@ -1718,7 +1890,9 @@ PyMODINIT_FUNC PyInit_perf(void) pyrf_evsel__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 || pyrf_cpu_map__setup_types() < 0 || - pyrf_counts_values__setup_types() < 0) + pyrf_counts_values__setup_types() < 0 || + pyrf_target__setup_types() < 0 || + pyrf_record_opts__setup_types() < 0) return module; =20 /* The page_size is placed in util object. */ @@ -1766,6 +1940,12 @@ PyMODINIT_FUNC PyInit_perf(void) Py_INCREF(&pyrf_counts_values__type); PyModule_AddObject(module, "counts_values", (PyObject *)&pyrf_counts_valu= es__type); =20 + Py_INCREF(&pyrf_target__type); + PyModule_AddObject(module, "target", (PyObject *)&pyrf_target__type); + + Py_INCREF(&pyrf_record_opts__type); + PyModule_AddObject(module, "record_opts", (PyObject *)&pyrf_record_opts__= type); + dict =3D PyModule_GetDict(module); if (dict =3D=3D NULL) goto error; --=20 2.49.0 From nobody Sun Oct 5 23:38:27 2025 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B175F216E26; Mon, 28 Jul 2025 06:00:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753682434; cv=none; b=RcHlP3Ov8Oh71C5sj4zWWdHpZRSGx6aM8e6veaQJVM+Nxeccb/sfes2xWEe97YuaVKfGmpuiBkqmT7d6RUScRriTEdVzinfqfGKznn5dou+Gvm092nHvU8fse9KKJRFYoCW5wwxHFS65bBvbYrgTiuhq1NEBfp2ciefX7HBJriQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753682434; c=relaxed/simple; bh=Ah3R0v2rYJ487XEMoZp7KO92f0egugJX8Gzr7UGw6Bo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ibsmG7TEQjXZMc44KQdE0TfOwh0W8E4KZbgHoIilQgq4Wz5LZrblqJmLzx6FDqf2SMRoS5g9dkkZNiYX7jV2SDAl8aDUzOSKMUXBiOs1hb1rfc7RioGfBYqCuL5H30beEYbCtHqY8woUn6EAVtsN2AHal45SZnDYUw5gheJavyk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=FYQLNztU; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="FYQLNztU" Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 56S4kjKQ025384; Mon, 28 Jul 2025 06:00:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=r8k5c8iqL9I/tfdQ1 FuVzFOAu7J/hlKtF00ZSuzsfbM=; b=FYQLNztUYaw2UDspktLsvL9xbmdVlOf5y TGyaoHYar47SrlzDdymkOTtxtmx03CcCx1lG1lnhxfVv/3W1kLghuRERCB44X26s a3jWyTxxt2xdG+z/LPZLFoPLsT8uRyRp4Y4Zz+BJ2LUBFLah5QVEEhHIkohh1ESU spBGZJAACCzbtIBcvU8gurXal3xkkt3Yh7lEFsOJFpbIoCXHSRKySJrye1NKnAme yCKP7cfz8nuU0IdTnxkvbgqf9MMkUHMW9sqxRJ0nt217+WDmZKIEWEpV/c8+wUC5 mZqwwk7Nw84BySNeYqCLrT5xDukg8r2pAJ+JAr4YQJ2qSahkvlZUw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 484qfqf7yy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:22 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.1.12/8.18.0.8) with ESMTP id 56S5ux9I013615; Mon, 28 Jul 2025 06:00:21 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 484qfqf7yt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:21 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.2/8.18.1.2) with ESMTP id 56S4iE6P017275; Mon, 28 Jul 2025 06:00:20 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4859qyvhry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 28 Jul 2025 06:00:20 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 56S60Gej58917174 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 28 Jul 2025 06:00:16 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0BF4520040; Mon, 28 Jul 2025 06:00:16 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7A62220043; Mon, 28 Jul 2025 06:00:12 +0000 (GMT) Received: from li-c6426e4c-27cf-11b2-a85c-95d65bc0de0e.ibm.com.com (unknown [9.124.209.252]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 28 Jul 2025 06:00:12 +0000 (GMT) From: Gautam Menghani To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com Cc: Gautam Menghani , maddy@linux.ibm.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] perf python: Add sampling.py as example for sampling Date: Mon, 28 Jul 2025 11:29:28 +0530 Message-ID: <20250728055937.58531-3-gautam@linux.ibm.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250728055937.58531-1-gautam@linux.ibm.com> References: <20250728055937.58531-1-gautam@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNzI4MDA0MiBTYWx0ZWRfX/lguueOOtgfM Wh7mzELm9hp9A23ZxkM+xfBT9QBQGLzxnRYQafLigDAdJClFtRvxo6gza7EWWKGfosF0ZVV8NVX Ffi+f0WA+jIDviodcGRVq7Yb29FWlPFbk2oOqAfSUyZIfszIdpBZWOPC9+qGUBnRPqh1si735U4 NJgJqczbADFe717U+Djq2pa4jvcqvU6VXecNZN0bvmM2q9WmoCsuvsmcUigRxuatk/loqVXwa8c t2u/+At8Pmse54Z/IsbdJhlkV0LCXVhIWfr35+JusxG+LDXWAmXdxqb2I3uy0thKVU4kpS35Xn7 hW/zFo11WVMebphkfCMHLCkuxGl5N0op8KDRAfm6EuDRZ/DCHBgxPwG0A8UWEXZ50PlHUd7FLn6 q2jLOLDNXOX0Q4J/wMBVbY62rbjhM+lAyNufJHU7HQGu+1Ep1MS1WkKqqyegvp8hYg5WCn9o X-Authority-Analysis: v=2.4 cv=Je28rVKV c=1 sm=1 tr=0 ts=688711f6 cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=Wb1JkmetP80A:10 a=VnNF1IyMAAAA:8 a=BcAa_kQfSTLkdWV7I_sA:9 X-Proofpoint-GUID: 7scDrOH8zODaMPoVWUNU5haszRnReSlp X-Proofpoint-ORIG-GUID: Fipa9eN_SySQPQOVzDWZXk-TX3sy7xnd X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-07-28_02,2025-07-24_01,2025-03-28_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 clxscore=1015 lowpriorityscore=0 spamscore=0 adultscore=0 suspectscore=0 bulkscore=0 malwarescore=0 priorityscore=1501 phishscore=0 mlxscore=0 impostorscore=0 classifier=spam authscore=0 authtc=n/a authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.19.0-2505280000 definitions=main-2507280042 Content-Type: text/plain; charset="utf-8" Add sampling.py - a python version of sampling.c to demonstrate sampling events. Sample output: ``` $ sudo ./sampling.py libperf: mmap_per_cpu: nr cpu values 8 nr threads 1 libperf: idx 0: mmapping fd 3 libperf: idx 1: mmapping fd 4 libperf: idx 2: mmapping fd 5 libperf: idx 3: mmapping fd 6 libperf: idx 4: mmapping fd 7 libperf: idx 5: mmapping fd 8 libperf: idx 6: mmapping fd 9 libperf: idx 7: mmapping fd 10 cpu: 0 pid: 4168 tid: 4168 ip: 0x7fb274150c15 period: 345012 cpu: 1 pid: 57137 tid: 57137 ip: 0xffffffffc0745a2c period: 286666 cpu: 2 pid: 6247 tid: 6247 ip: 0x7fe10a883988 period: 391180 cpu: 3 pid: 0 tid: 0 ip: 0xffffffffa5413e67 period: 245301 cpu: 4 pid: 4168 tid: 4194 ip: 0xffffffffc086a1cc period: 269605 cpu: 5 pid: 0 tid: 0 ip: 0xffffffffa53bfcb1 period: 90978 cpu: 6 pid: 0 tid: 0 ip: 0xffffffffa6469333 period: 309112 ``` Signed-off-by: Gautam Menghani --- tools/perf/python/sampling.py | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 tools/perf/python/sampling.py diff --git a/tools/perf/python/sampling.py b/tools/perf/python/sampling.py new file mode 100755 index 000000000000..b5f4e1362c63 --- /dev/null +++ b/tools/perf/python/sampling.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +# -*- python -*- +# -*- coding: utf-8 -*- + +import perf +import time + +def main(): + cpus =3D perf.cpu_map() + threads =3D perf.thread_map(-1) + + evlist =3D perf.parse_events('cpu-cycles', cpus, threads) + + tgt =3D perf.target(uses_mmap =3D True, default_per_cpu =3D True) + opts =3D perf.record_opts(freq=3D1000, target=3Dtgt, sample_time=3DTru= e, + sample_cpu=3DTrue, no_buffering=3DTrue, no_inh= erit=3DTrue) + for ev in evlist: + ev.tracking =3D False + ev.read_format =3D 0 + ev.sample_type =3D perf.SAMPLE_IP|perf.SAMPLE_TID|perf.SAMPLE_CPU|= perf.SAMPLE_PERIOD + evlist.config(opts) + + evlist.open() + evlist.mmap() + + evlist.enable() + time.sleep(2) + evlist.disable() + + done =3D False + while done is False: + for cpu in cpus: + event =3D evlist.read_on_cpu(cpu) + if event is None: + done =3D True + break + + if not isinstance(event, perf.sample_event): + continue + + print(f"cpu: {event.sample_cpu:<3} pid: {event.sample_pid:<6} " + f"tid: {event.sample_tid:<6} ip: {hex(event.sample_ip)= :<20} " + f"period: {event.sample_period:<20}") + + evlist.close() + +if __name__ =3D=3D '__main__': + main() --=20 2.49.0