mirror of https://lore.kernel.org/lkml/
 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>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	maddy@linux.ibm.com
Subject: [PATCH 4/4] perf python: Add counting.py as example for counting perf events
Date: Thu,  1 May 2025 15:06:30 +0530	[thread overview]
Message-ID: <20250501093633.578010-5-gautam@linux.ibm.com> (raw)
In-Reply-To: <20250501093633.578010-1-gautam@linux.ibm.com>

Add counting.py - a python version of counting.c to demonstrate
measuring and reading of counts for given perf events.

Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
---
 tools/perf/python/counting.py | 41 +++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100755 tools/perf/python/counting.py

diff --git a/tools/perf/python/counting.py b/tools/perf/python/counting.py
new file mode 100755
index 000000000000..0c58907bd8bf
--- /dev/null
+++ b/tools/perf/python/counting.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+# -*- python -*-
+# -*- coding: utf-8 -*-
+
+import perf
+
+def main():
+        cpus = perf.cpu_map()
+        thread_map = perf.thread_map(-1)
+        evlist = perf.evlist(cpus, thread_map)
+
+        evsel1 = perf.evsel(type = perf.TYPE_SOFTWARE,
+                 config = perf.COUNT_SW_CPU_CLOCK,
+                 read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
+                 disabled=1)
+        evlist.add(evsel1)
+
+        evsel2 = perf.evsel(type = perf.TYPE_SOFTWARE,
+                 config = perf.COUNT_SW_TASK_CLOCK,
+                 read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING,
+                 disabled=1)
+        evlist.add(evsel2)
+
+        evlist.open()
+        evlist.enable()
+
+        count = 100000
+        while count > 0:
+            count -= 1
+
+        evlist.disable()
+        evsel = evlist.next(None)
+        while evsel != None:
+            counts = evsel.read(0, 0)
+            print(counts.val, counts.ena, counts.run)
+            evsel = evlist.next(evsel)
+        evlist.close()
+
+if __name__ == '__main__':
+    main()
-- 
2.49.0


  parent reply	other threads:[~2025-05-01  9:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-01  9:36 [PATCH 0/4] perf python: Add missing infra pieces " Gautam Menghani
2025-05-01  9:36 ` [PATCH 1/4] perf python: Add support for perf_counts_values to return counter data Gautam Menghani
2025-05-01  9:36 ` [PATCH 2/4] perf python: Add evsel read method Gautam Menghani
2025-05-01  9:36 ` [PATCH 3/4] perf python: Add evlist close and next methods Gautam Menghani
2025-05-01 15:49   ` Ian Rogers
2025-05-05 11:49     ` Gautam Menghani
2025-05-05 21:02       ` Ian Rogers
2025-05-01  9:36 ` Gautam Menghani [this message]
2025-05-01 16:04   ` [PATCH 4/4] perf python: Add counting.py as example for counting perf events Ian Rogers
2025-05-01 16:11     ` Ian Rogers
2025-05-05 11:51     ` 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=20250501093633.578010-5-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

all inboxes | Powered by JetHome®