1#! /usr/bin/python 2# -*- python -*- 3# -*- coding: utf-8 -*- 4 5import perf 6 7class tracepoint(perf.evsel): 8 def __init__(self, sys, name): 9 config = perf.tracepoint(sys, name) 10 perf.evsel.__init__(self, 11 type = perf.TYPE_TRACEPOINT, 12 config = config, 13 freq = 0, sample_period = 1, wakeup_events = 1, 14 sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU | perf.SAMPLE_RAW | perf.SAMPLE_TIME) 15 16def main(): 17 tp = tracepoint("sched", "sched_switch") 18 cpus = perf.cpu_map() 19 threads = perf.thread_map(-1) 20 21 evlist = perf.evlist(cpus, threads) 22 evlist.add(tp) 23 evlist.open() 24 evlist.mmap() 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