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>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH 3/3] libperf: Add counting.py example to demonstrate libperf usage from python
Date: Thu, 13 Mar 2025 13:21:24 +0530	[thread overview]
Message-ID: <20250313075126.547881-4-gautam@linux.ibm.com> (raw)
In-Reply-To: <20250313075126.547881-1-gautam@linux.ibm.com>

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


  parent reply	other threads:[~2025-03-13  7:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13  7:51 [RFC PATCH 0/3] Introduce a C extension module to allow libperf usage from python Gautam Menghani
2025-03-13  7:51 ` [RFC PATCH 1/3] libperf: Introduce wrappers for perf structs to be exposed to python Gautam Menghani
2025-03-13  7:51 ` [RFC PATCH 2/3] libperf: Introduce a C extension module for python Gautam Menghani
2025-03-13  7:51 ` Gautam Menghani [this message]
2025-03-13 14:12 ` [RFC PATCH 0/3] Introduce a C extension module to allow libperf usage from python John B. Wyatt IV
2025-03-13 16:01   ` Ian Rogers
2025-03-14  8:20     ` 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=20250313075126.547881-4-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=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).