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