xref: /OK3568_Linux_fs/kernel/tools/perf/python/tracepoint.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#! /usr/bin/env python
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0
3*4882a593Smuzhiyun# -*- python -*-
4*4882a593Smuzhiyun# -*- coding: utf-8 -*-
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunimport perf
7*4882a593Smuzhiyun
8*4882a593Smuzhiyunclass tracepoint(perf.evsel):
9*4882a593Smuzhiyun    def __init__(self, sys, name):
10*4882a593Smuzhiyun        config = perf.tracepoint(sys, name)
11*4882a593Smuzhiyun        perf.evsel.__init__(self,
12*4882a593Smuzhiyun                            type   = perf.TYPE_TRACEPOINT,
13*4882a593Smuzhiyun                            config = config,
14*4882a593Smuzhiyun                            freq = 0, sample_period = 1, wakeup_events = 1,
15*4882a593Smuzhiyun                            sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU | perf.SAMPLE_RAW | perf.SAMPLE_TIME)
16*4882a593Smuzhiyun
17*4882a593Smuzhiyundef main():
18*4882a593Smuzhiyun    tp      = tracepoint("sched", "sched_switch")
19*4882a593Smuzhiyun    cpus    = perf.cpu_map()
20*4882a593Smuzhiyun    threads = perf.thread_map(-1)
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun    evlist = perf.evlist(cpus, threads)
23*4882a593Smuzhiyun    evlist.add(tp)
24*4882a593Smuzhiyun    evlist.open()
25*4882a593Smuzhiyun    evlist.mmap()
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun    while True:
28*4882a593Smuzhiyun        evlist.poll(timeout = -1)
29*4882a593Smuzhiyun        for cpu in cpus:
30*4882a593Smuzhiyun            event = evlist.read_on_cpu(cpu)
31*4882a593Smuzhiyun            if not event:
32*4882a593Smuzhiyun                continue
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun            if not isinstance(event, perf.sample_event):
35*4882a593Smuzhiyun                continue
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun            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" % (
38*4882a593Smuzhiyun                   event.sample_time,
39*4882a593Smuzhiyun                   event.prev_comm,
40*4882a593Smuzhiyun                   event.prev_pid,
41*4882a593Smuzhiyun                   event.prev_prio,
42*4882a593Smuzhiyun                   event.prev_state,
43*4882a593Smuzhiyun                   event.next_comm,
44*4882a593Smuzhiyun                   event.next_pid,
45*4882a593Smuzhiyun                   event.next_prio)
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunif __name__ == '__main__':
48*4882a593Smuzhiyun    main()
49