linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gautam Menghani <gautam@linux.ibm.com>
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 <gautam@linux.ibm.com>,
	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	[thread overview]
Message-ID: <20250728055937.58531-3-gautam@linux.ibm.com> (raw)
In-Reply-To: <20250728055937.58531-1-gautam@linux.ibm.com>

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 <gautam@linux.ibm.com>
---
 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    = perf.cpu_map()
+    threads = perf.thread_map(-1)
+
+    evlist = perf.parse_events('cpu-cycles', cpus, threads)
+
+    tgt = perf.target(uses_mmap = True, default_per_cpu = True)
+    opts = perf.record_opts(freq=1000, target=tgt, sample_time=True,
+                            sample_cpu=True, no_buffering=True, no_inherit=True)
+    for ev in evlist:
+        ev.tracking = False
+        ev.read_format = 0
+        ev.sample_type = 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 = False
+    while done is False:
+        for cpu in cpus:
+            event = evlist.read_on_cpu(cpu)
+            if event is None:
+                done = 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__ == '__main__':
+    main()
-- 
2.49.0


  parent reply	other threads:[~2025-07-28  6:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28  5:59 [PATCH 0/2] perf python: Add an example for sampling Gautam Menghani
2025-07-28  5:59 ` [PATCH 1/2] perf python: Add support for record_opts struct Gautam Menghani
2025-07-28  5:59 ` Gautam Menghani [this message]
2025-08-12  5:22 ` [PATCH 0/2] perf python: Add an example for sampling Gautam Menghani
2025-08-26  7:07 ` Gautam Menghani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250728055937.58531-3-gautam@linux.ibm.com \
    --to=gautam@linux.ibm.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=maddy@linux.ibm.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).