1877108e4SArnaldo Carvalho de Melo#! /usr/bin/python 2877108e4SArnaldo Carvalho de Melo# -*- python -*- 3877108e4SArnaldo Carvalho de Melo# -*- coding: utf-8 -*- 4877108e4SArnaldo Carvalho de Melo# twatch - Experimental use of the perf python interface 5877108e4SArnaldo Carvalho de Melo# Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com> 6877108e4SArnaldo Carvalho de Melo# 7877108e4SArnaldo Carvalho de Melo# This application is free software; you can redistribute it and/or 8877108e4SArnaldo Carvalho de Melo# modify it under the terms of the GNU General Public License 9877108e4SArnaldo Carvalho de Melo# as published by the Free Software Foundation; version 2. 10877108e4SArnaldo Carvalho de Melo# 11877108e4SArnaldo Carvalho de Melo# This application is distributed in the hope that it will be useful, 12877108e4SArnaldo Carvalho de Melo# but WITHOUT ANY WARRANTY; without even the implied warranty of 13877108e4SArnaldo Carvalho de Melo# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14877108e4SArnaldo Carvalho de Melo# General Public License for more details. 15877108e4SArnaldo Carvalho de Melo 16877108e4SArnaldo Carvalho de Meloimport perf 17877108e4SArnaldo Carvalho de Melo 18*ae938802SArnaldo Carvalho de Melodef main(context_switch = 0, thread = -1): 19877108e4SArnaldo Carvalho de Melo cpus = perf.cpu_map() 20*ae938802SArnaldo Carvalho de Melo threads = perf.thread_map(thread) 2158b32c1bSArnaldo Carvalho de Melo evsel = perf.evsel(type = perf.TYPE_SOFTWARE, 2258b32c1bSArnaldo Carvalho de Melo config = perf.COUNT_SW_DUMMY, 2358b32c1bSArnaldo Carvalho de Melo task = 1, comm = 1, mmap = 0, freq = 0, 24cfeb1d90SArnaldo Carvalho de Melo wakeup_events = 1, watermark = 1, 25*ae938802SArnaldo Carvalho de Melo sample_id_all = 1, context_switch = context_switch, 263223565cSArnaldo Carvalho de Melo sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU) 2758b32c1bSArnaldo Carvalho de Melo 2858b32c1bSArnaldo Carvalho de Melo """What we want are just the PERF_RECORD_ lifetime events for threads, 2958b32c1bSArnaldo Carvalho de Melo using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1 3058b32c1bSArnaldo Carvalho de Melo (the default), makes perf reenable irq_vectors:local_timer_entry, when 3158b32c1bSArnaldo Carvalho de Melo disabling nohz, not good for some use cases where all we want is to get 3258b32c1bSArnaldo Carvalho de Melo threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY, 3358b32c1bSArnaldo Carvalho de Melo freq=0) instead.""" 3458b32c1bSArnaldo Carvalho de Melo 35877108e4SArnaldo Carvalho de Melo evsel.open(cpus = cpus, threads = threads); 367e2ed097SArnaldo Carvalho de Melo evlist = perf.evlist(cpus, threads) 37877108e4SArnaldo Carvalho de Melo evlist.add(evsel) 387e2ed097SArnaldo Carvalho de Melo evlist.mmap() 39877108e4SArnaldo Carvalho de Melo while True: 40877108e4SArnaldo Carvalho de Melo evlist.poll(timeout = -1) 41877108e4SArnaldo Carvalho de Melo for cpu in cpus: 42877108e4SArnaldo Carvalho de Melo event = evlist.read_on_cpu(cpu) 43877108e4SArnaldo Carvalho de Melo if not event: 44877108e4SArnaldo Carvalho de Melo continue 45877108e4SArnaldo Carvalho de Melo print "cpu: %2d, pid: %4d, tid: %4d" % (event.sample_cpu, 46877108e4SArnaldo Carvalho de Melo event.sample_pid, 47877108e4SArnaldo Carvalho de Melo event.sample_tid), 48877108e4SArnaldo Carvalho de Melo print event 49877108e4SArnaldo Carvalho de Melo 50877108e4SArnaldo Carvalho de Meloif __name__ == '__main__': 51*ae938802SArnaldo Carvalho de Melo """ 52*ae938802SArnaldo Carvalho de Melo To test the PERF_RECORD_SWITCH record, pick a pid and replace 53*ae938802SArnaldo Carvalho de Melo in the following line. 54*ae938802SArnaldo Carvalho de Melo 55*ae938802SArnaldo Carvalho de Melo Example output: 56*ae938802SArnaldo Carvalho de Melo 57*ae938802SArnaldo Carvalho de Melocpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 } 58*ae938802SArnaldo Carvalho de Melocpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 } 59*ae938802SArnaldo Carvalho de Melocpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 } 60*ae938802SArnaldo Carvalho de Melocpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 } 61*ae938802SArnaldo Carvalho de Melo 62*ae938802SArnaldo Carvalho de Melo It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT 63*ae938802SArnaldo Carvalho de Melo to figure out if this is a context switch in or out of the monitored threads. 64*ae938802SArnaldo Carvalho de Melo 65*ae938802SArnaldo Carvalho de Melo If bored, please add command line option parsing support for these options :-) 66*ae938802SArnaldo Carvalho de Melo """ 67*ae938802SArnaldo Carvalho de Melo # main(context_switch = 1, thread = 31463) 68877108e4SArnaldo Carvalho de Melo main() 69