xref: /linux-6.15/tools/perf/python/tracepoint.py (revision f7cffbab)
1#! /usr/bin/env python
2# SPDX-License-Identifier: GPL-2.0
3# -*- python -*-
4# -*- coding: utf-8 -*-
5
6import perf
7
8def main():
9    cpus    = perf.cpu_map()
10    threads = perf.thread_map(-1)
11    evlist = perf.parse_events("sched:sched_switch", cpus, threads)
12    # Disable tracking of mmaps and similar that are unnecessary.
13    for ev in evlist:
14        ev.tracking = False
15    # Configure evsels with default record options.
16    evlist.config()
17    # Simplify the sample_type and read_format of evsels
18    for ev in evlist:
19        ev.sample_type = ev.sample_type & ~perf.SAMPLE_IP
20        ev.read_format = 0
21
22    evlist.open()
23    evlist.mmap()
24    evlist.enable();
25
26    while True:
27        evlist.poll(timeout = -1)
28        for cpu in cpus:
29            event = evlist.read_on_cpu(cpu)
30            if not event:
31                continue
32
33            if not isinstance(event, perf.sample_event):
34                continue
35
36            print("time %u prev_comm=%s prev_pid=%d prev_prio=%d prev_state=0x%x ==> next_comm=%s next_pid=%d next_prio=%d" % (
37                   event.sample_time,
38                   event.prev_comm,
39                   event.prev_pid,
40                   event.prev_prio,
41                   event.prev_state,
42                   event.next_comm,
43                   event.next_pid,
44                   event.next_prio))
45
46if __name__ == '__main__':
47    main()
48