162306a36Sopenharmony_ci#! /usr/bin/env python
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
362306a36Sopenharmony_ci# -*- python -*-
462306a36Sopenharmony_ci# -*- coding: utf-8 -*-
562306a36Sopenharmony_ci#   twatch - Experimental use of the perf python interface
662306a36Sopenharmony_ci#   Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com>
762306a36Sopenharmony_ci#
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciimport perf
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cidef main(context_switch = 0, thread = -1):
1262306a36Sopenharmony_ci	cpus = perf.cpu_map()
1362306a36Sopenharmony_ci	threads = perf.thread_map(thread)
1462306a36Sopenharmony_ci	evsel = perf.evsel(type	  = perf.TYPE_SOFTWARE,
1562306a36Sopenharmony_ci			   config = perf.COUNT_SW_DUMMY,
1662306a36Sopenharmony_ci			   task = 1, comm = 1, mmap = 0, freq = 0,
1762306a36Sopenharmony_ci			   wakeup_events = 1, watermark = 1,
1862306a36Sopenharmony_ci			   sample_id_all = 1, context_switch = context_switch,
1962306a36Sopenharmony_ci			   sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci	"""What we want are just the PERF_RECORD_ lifetime events for threads,
2262306a36Sopenharmony_ci	 using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1
2362306a36Sopenharmony_ci	 (the default), makes perf reenable irq_vectors:local_timer_entry, when
2462306a36Sopenharmony_ci	 disabling nohz, not good for some use cases where all we want is to get
2562306a36Sopenharmony_ci	 threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY,
2662306a36Sopenharmony_ci	 freq=0) instead."""
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	evsel.open(cpus = cpus, threads = threads);
2962306a36Sopenharmony_ci	evlist = perf.evlist(cpus, threads)
3062306a36Sopenharmony_ci	evlist.add(evsel)
3162306a36Sopenharmony_ci	evlist.mmap()
3262306a36Sopenharmony_ci	while True:
3362306a36Sopenharmony_ci		evlist.poll(timeout = -1)
3462306a36Sopenharmony_ci		for cpu in cpus:
3562306a36Sopenharmony_ci			event = evlist.read_on_cpu(cpu)
3662306a36Sopenharmony_ci			if not event:
3762306a36Sopenharmony_ci				continue
3862306a36Sopenharmony_ci			print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu,
3962306a36Sopenharmony_ci                                                                        event.sample_pid,
4062306a36Sopenharmony_ci                                                                        event.sample_tid,
4162306a36Sopenharmony_ci                                                                        event))
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciif __name__ == '__main__':
4462306a36Sopenharmony_ci    """
4562306a36Sopenharmony_ci	To test the PERF_RECORD_SWITCH record, pick a pid and replace
4662306a36Sopenharmony_ci	in the following line.
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	Example output:
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cicpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 }
5162306a36Sopenharmony_cicpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 }
5262306a36Sopenharmony_cicpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 }
5362306a36Sopenharmony_cicpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 }
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT
5662306a36Sopenharmony_ci	to figure out if this is a context switch in or out of the monitored threads.
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	If bored, please add command line option parsing support for these options :-)
5962306a36Sopenharmony_ci    """
6062306a36Sopenharmony_ci    # main(context_switch = 1, thread = 31463)
6162306a36Sopenharmony_ci    main()
62