Back to home page

OSCL-LXR

 
 

    


0001 #! /usr/bin/env python
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 # -*- python -*-
0004 # -*- coding: utf-8 -*-
0005 #   twatch - Experimental use of the perf python interface
0006 #   Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com>
0007 #
0008 
0009 import perf
0010 
0011 def main(context_switch = 0, thread = -1):
0012     cpus = perf.cpu_map()
0013     threads = perf.thread_map(thread)
0014     evsel = perf.evsel(type   = perf.TYPE_SOFTWARE,
0015                config = perf.COUNT_SW_DUMMY,
0016                task = 1, comm = 1, mmap = 0, freq = 0,
0017                wakeup_events = 1, watermark = 1,
0018                sample_id_all = 1, context_switch = context_switch,
0019                sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU)
0020 
0021     """What we want are just the PERF_RECORD_ lifetime events for threads,
0022      using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1
0023      (the default), makes perf reenable irq_vectors:local_timer_entry, when
0024      disabling nohz, not good for some use cases where all we want is to get
0025      threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY,
0026      freq=0) instead."""
0027 
0028     evsel.open(cpus = cpus, threads = threads);
0029     evlist = perf.evlist(cpus, threads)
0030     evlist.add(evsel)
0031     evlist.mmap()
0032     while True:
0033         evlist.poll(timeout = -1)
0034         for cpu in cpus:
0035             event = evlist.read_on_cpu(cpu)
0036             if not event:
0037                 continue
0038             print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu,
0039                                                                         event.sample_pid,
0040                                                                         event.sample_tid,
0041                                                                         event))
0042 
0043 if __name__ == '__main__':
0044     """
0045     To test the PERF_RECORD_SWITCH record, pick a pid and replace
0046     in the following line.
0047 
0048     Example output:
0049 
0050 cpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 }
0051 cpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 }
0052 cpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 }
0053 cpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 }
0054 
0055     It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT
0056     to figure out if this is a context switch in or out of the monitored threads.
0057 
0058     If bored, please add command line option parsing support for these options :-)
0059     """
0060     # main(context_switch = 1, thread = 31463)
0061     main()