Finn Behrens | 2d1f7c9 | 2020-11-23 15:15:33 +0100 | [diff] [blame] | 1 | #! /usr/bin/env python |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 2 | # -*- python -*- |
| 3 | # -*- coding: utf-8 -*- |
| 4 | # twatch - Experimental use of the perf python interface |
| 5 | # Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 6 | # |
| 7 | # This application is free software; you can redistribute it and/or |
| 8 | # modify it under the terms of the GNU General Public License |
| 9 | # as published by the Free Software Foundation; version 2. |
| 10 | # |
| 11 | # This application is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | # General Public License for more details. |
| 15 | |
| 16 | import perf |
| 17 | |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 18 | def main(context_switch = 0, thread = -1): |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 19 | cpus = perf.cpu_map() |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 20 | threads = perf.thread_map(thread) |
Arnaldo Carvalho de Melo | 58b32c1 | 2015-07-27 17:13:21 -0300 | [diff] [blame] | 21 | evsel = perf.evsel(type = perf.TYPE_SOFTWARE, |
| 22 | config = perf.COUNT_SW_DUMMY, |
| 23 | task = 1, comm = 1, mmap = 0, freq = 0, |
Arnaldo Carvalho de Melo | cfeb1d9 | 2012-01-30 14:43:21 -0200 | [diff] [blame] | 24 | wakeup_events = 1, watermark = 1, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 25 | sample_id_all = 1, context_switch = context_switch, |
Arnaldo Carvalho de Melo | 3223565 | 2013-08-01 17:00:45 -0300 | [diff] [blame] | 26 | sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU) |
Arnaldo Carvalho de Melo | 58b32c1 | 2015-07-27 17:13:21 -0300 | [diff] [blame] | 27 | |
| 28 | """What we want are just the PERF_RECORD_ lifetime events for threads, |
| 29 | using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1 |
| 30 | (the default), makes perf reenable irq_vectors:local_timer_entry, when |
| 31 | disabling nohz, not good for some use cases where all we want is to get |
| 32 | threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY, |
| 33 | freq=0) instead.""" |
| 34 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 35 | evsel.open(cpus = cpus, threads = threads); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 36 | evlist = perf.evlist(cpus, threads) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 37 | evlist.add(evsel) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 38 | evlist.mmap() |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 39 | while True: |
| 40 | evlist.poll(timeout = -1) |
| 41 | for cpu in cpus: |
| 42 | event = evlist.read_on_cpu(cpu) |
| 43 | if not event: |
| 44 | continue |
| 45 | print "cpu: %2d, pid: %4d, tid: %4d" % (event.sample_cpu, |
| 46 | event.sample_pid, |
| 47 | event.sample_tid), |
| 48 | print event |
| 49 | |
| 50 | if __name__ == '__main__': |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 51 | """ |
| 52 | To test the PERF_RECORD_SWITCH record, pick a pid and replace |
| 53 | in the following line. |
| 54 | |
| 55 | Example output: |
| 56 | |
| 57 | cpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 } |
| 58 | cpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 } |
| 59 | cpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 } |
| 60 | cpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 } |
| 61 | |
| 62 | It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT |
| 63 | to figure out if this is a context switch in or out of the monitored threads. |
| 64 | |
| 65 | If bored, please add command line option parsing support for these options :-) |
| 66 | """ |
| 67 | # main(context_switch = 1, thread = 31463) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 68 | main() |