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