lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250728055937.58531-3-gautam@linux.ibm.com>
Date: Mon, 28 Jul 2025 11:29:28 +0530
From: Gautam Menghani <gautam@...ux.ibm.com>
To: peterz@...radead.org, mingo@...hat.com, acme@...nel.org,
        namhyung@...nel.org, mark.rutland@....com,
        alexander.shishkin@...ux.intel.com, jolsa@...nel.org,
        irogers@...gle.com, adrian.hunter@...el.com, kan.liang@...ux.intel.com
Cc: Gautam Menghani <gautam@...ux.ibm.com>, maddy@...ux.ibm.com,
        linux-perf-users@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: [PATCH 2/2] perf python: Add sampling.py as example for sampling

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@...ux.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


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ