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] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250313075126.547881-4-gautam@linux.ibm.com>
Date: Thu, 13 Mar 2025 13:21:24 +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>, linux-perf-users@...r.kernel.org,
        linux-kernel@...r.kernel.org
Subject: [RFC PATCH 3/3] libperf: Add counting.py example to demonstrate libperf usage from python

Add a counting.py example to demonstrate usage of libperf from python
using the C extension module support.

Example usage:
$ sudo ./counting.py
count 7903256, enabled 7903670, run 7903670
count 7902787, enabled 7902787, run 7902787

Signed-off-by: Gautam Menghani <gautam@...ux.ibm.com>
---
 .../perf/Documentation/examples/counting.py   | 74 +++++++++++++++++++
 1 file changed, 74 insertions(+)
 create mode 100755 tools/lib/perf/Documentation/examples/counting.py

diff --git a/tools/lib/perf/Documentation/examples/counting.py b/tools/lib/perf/Documentation/examples/counting.py
new file mode 100755
index 000000000000..887111bf2e04
--- /dev/null
+++ b/tools/lib/perf/Documentation/examples/counting.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python3
+
+import sys
+sys.path.append('../../')
+from libperf import *
+
+# software ids
+PERF_COUNT_SW_CPU_CLOCK = 0
+PERF_COUNT_SW_TASK_CLOCK = 1
+
+# Perf event types
+PERF_TYPE_HARDWARE = 0
+PERF_TYPE_SOFTWARE = 1
+PERF_TYPE_TRACEPOINT = 2
+PERF_TYPE_HW_CACHE = 3
+
+# perf_event_attr_read format
+PERF_FORMAT_TOTAL_TIME_ENABLED = 1 << 0
+PERF_FORMAT_TOTAL_TIME_RUNNING = 1 << 1
+PERF_FORMAT_ID = 1 << 2
+PERF_FORMAT_GROUP = 1 << 3
+PERF_FORMAT_LOST = 1 << 4
+
+# Perf sample identifier
+PERF_SAMPLE_IDENTIFIER = 1 << 16
+
+def get_attr(config):
+    attr = perf_event_attr()
+    attr.type = PERF_TYPE_SOFTWARE
+    attr.config = config
+    attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING
+    attr.disabled = 1
+    attr.size = 136
+    attr.sample_type = PERF_SAMPLE_IDENTIFIER
+    return attr
+
+libperf_init(None)
+threads = perf_thread_map__new_dummy()
+assert(threads)
+perf_thread_map__set_pid(threads, 0, 0)
+
+evlist = perf_evlist__new()
+assert(evlist)
+
+attr1 = get_attr(PERF_COUNT_SW_CPU_CLOCK)
+evsel = perf_evsel__new(attr1)
+assert(evsel)
+perf_evlist__add(evlist, evsel)
+
+attr2 = get_attr(PERF_COUNT_SW_TASK_CLOCK)
+evsel = perf_evsel__new(attr2)
+assert(evsel)
+perf_evlist__add(evlist, evsel)
+
+perf_evlist__set_maps(evlist, None, threads)
+rc = perf_evlist__open(evlist)
+if rc != 0:
+    print("failed to open evsel: ", rc)
+
+perf_evlist__enable(evlist)
+
+count = 100000
+while count >= 0:
+    count-=1
+
+perf_evlist__disable(evlist)
+c = perf_counts_values()
+for sel  in evlist:
+		perf_evsel__read(sel, 0, 0, c);
+		print("count %lu, enabled %lu, run %lu" %(c.val, c.ena, c.run))
+
+perf_evlist__close(evlist);
+perf_evlist__delete(evlist);
+perf_thread_map__put(threads);
-- 
2.47.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ