Finn Behrens | c25ce58 | 2020-11-23 15:15:33 +0100 | [diff] [blame] | 1 | #! /usr/bin/env python |
Thomas Gleixner | 1514e85 | 2019-06-01 10:08:22 +0200 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 3 | # -*- python -*- |
| 4 | # -*- coding: utf-8 -*- |
| 5 | # twatch - Experimental use of the perf python interface |
| 6 | # Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com> |
| 7 | # |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 8 | |
| 9 | import perf |
| 10 | |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 11 | def main(context_switch = 0, thread = -1): |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 12 | cpus = perf.cpu_map() |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 13 | threads = perf.thread_map(thread) |
Arnaldo Carvalho de Melo | 58b32c1 | 2015-07-27 17:13:21 -0300 | [diff] [blame] | 14 | evsel = perf.evsel(type = perf.TYPE_SOFTWARE, |
| 15 | config = perf.COUNT_SW_DUMMY, |
| 16 | task = 1, comm = 1, mmap = 0, freq = 0, |
Arnaldo Carvalho de Melo | cfeb1d9 | 2012-01-30 14:43:21 -0200 | [diff] [blame] | 17 | wakeup_events = 1, watermark = 1, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 18 | sample_id_all = 1, context_switch = context_switch, |
Arnaldo Carvalho de Melo | 3223565c | 2013-08-01 17:00:45 -0300 | [diff] [blame] | 19 | 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] | 20 | |
| 21 | """What we want are just the PERF_RECORD_ lifetime events for threads, |
| 22 | using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1 |
| 23 | (the default), makes perf reenable irq_vectors:local_timer_entry, when |
| 24 | disabling nohz, not good for some use cases where all we want is to get |
| 25 | threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY, |
| 26 | freq=0) instead.""" |
| 27 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 28 | evsel.open(cpus = cpus, threads = threads); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 29 | evlist = perf.evlist(cpus, threads) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 30 | evlist.add(evsel) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 31 | evlist.mmap() |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 32 | while True: |
| 33 | evlist.poll(timeout = -1) |
| 34 | for cpu in cpus: |
| 35 | event = evlist.read_on_cpu(cpu) |
| 36 | if not event: |
| 37 | continue |
Arnaldo Carvalho de Melo | d2ed5d2 | 2018-02-19 12:24:13 -0300 | [diff] [blame] | 38 | print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu, |
| 39 | event.sample_pid, |
| 40 | event.sample_tid, |
| 41 | event)) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 42 | |
| 43 | if __name__ == '__main__': |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 44 | """ |
| 45 | To test the PERF_RECORD_SWITCH record, pick a pid and replace |
| 46 | in the following line. |
| 47 | |
| 48 | Example output: |
| 49 | |
| 50 | cpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 } |
| 51 | cpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 } |
| 52 | cpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 } |
| 53 | cpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 } |
| 54 | |
| 55 | It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT |
| 56 | to figure out if this is a context switch in or out of the monitored threads. |
| 57 | |
| 58 | If bored, please add command line option parsing support for these options :-) |
| 59 | """ |
| 60 | # main(context_switch = 1, thread = 31463) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 61 | main() |