xref: /linux/tools/perf/python/counting.py (revision 0939bd2fcf337243133b0271335a2838857c319f)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3# -*- python -*-
4# -*- coding: utf-8 -*-
5
6import argparse
7import perf
8
9def main(event: str):
10    evlist = perf.parse_events(event)
11
12    for evsel in evlist:
13        evsel.read_format = perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING
14
15    evlist.open()
16    evlist.enable()
17
18    count = 100000
19    while count > 0:
20        count -= 1
21
22    evlist.disable()
23
24    for evsel in evlist:
25        for cpu in evsel.cpus():
26            for thread in evsel.threads():
27                counts = evsel.read(cpu, thread)
28                print(f"For {evsel} val: {counts.val} enable: {counts.ena} run: {counts.run}")
29
30    evlist.close()
31
32if __name__ == '__main__':
33    ap = argparse.ArgumentParser()
34    ap.add_argument('-e', '--event', help="Events to open", default="cpu-clock,task-clock")
35    args = ap.parse_args()
36    main(args.event)
37