xref: /openbmc/linux/tools/perf/python/twatch.py (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1*c25ce589SFinn Behrens#! /usr/bin/env python
21514e851SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
3877108e4SArnaldo Carvalho de Melo# -*- python -*-
4877108e4SArnaldo Carvalho de Melo# -*- coding: utf-8 -*-
5877108e4SArnaldo Carvalho de Melo#   twatch - Experimental use of the perf python interface
6877108e4SArnaldo Carvalho de Melo#   Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com>
7877108e4SArnaldo Carvalho de Melo#
8877108e4SArnaldo Carvalho de Melo
9877108e4SArnaldo Carvalho de Meloimport perf
10877108e4SArnaldo Carvalho de Melo
11ae938802SArnaldo Carvalho de Melodef main(context_switch = 0, thread = -1):
12877108e4SArnaldo Carvalho de Melo	cpus = perf.cpu_map()
13ae938802SArnaldo Carvalho de Melo	threads = perf.thread_map(thread)
1458b32c1bSArnaldo Carvalho de Melo	evsel = perf.evsel(type	  = perf.TYPE_SOFTWARE,
1558b32c1bSArnaldo Carvalho de Melo			   config = perf.COUNT_SW_DUMMY,
1658b32c1bSArnaldo Carvalho de Melo			   task = 1, comm = 1, mmap = 0, freq = 0,
17cfeb1d90SArnaldo Carvalho de Melo			   wakeup_events = 1, watermark = 1,
18ae938802SArnaldo Carvalho de Melo			   sample_id_all = 1, context_switch = context_switch,
193223565cSArnaldo Carvalho de Melo			   sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU)
2058b32c1bSArnaldo Carvalho de Melo
2158b32c1bSArnaldo Carvalho de Melo	"""What we want are just the PERF_RECORD_ lifetime events for threads,
2258b32c1bSArnaldo Carvalho de Melo	 using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1
2358b32c1bSArnaldo Carvalho de Melo	 (the default), makes perf reenable irq_vectors:local_timer_entry, when
2458b32c1bSArnaldo Carvalho de Melo	 disabling nohz, not good for some use cases where all we want is to get
2558b32c1bSArnaldo Carvalho de Melo	 threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY,
2658b32c1bSArnaldo Carvalho de Melo	 freq=0) instead."""
2758b32c1bSArnaldo Carvalho de Melo
28877108e4SArnaldo Carvalho de Melo	evsel.open(cpus = cpus, threads = threads);
297e2ed097SArnaldo Carvalho de Melo	evlist = perf.evlist(cpus, threads)
30877108e4SArnaldo Carvalho de Melo	evlist.add(evsel)
317e2ed097SArnaldo Carvalho de Melo	evlist.mmap()
32877108e4SArnaldo Carvalho de Melo	while True:
33877108e4SArnaldo Carvalho de Melo		evlist.poll(timeout = -1)
34877108e4SArnaldo Carvalho de Melo		for cpu in cpus:
35877108e4SArnaldo Carvalho de Melo			event = evlist.read_on_cpu(cpu)
36877108e4SArnaldo Carvalho de Melo			if not event:
37877108e4SArnaldo Carvalho de Melo				continue
38d2ed5d2bSArnaldo Carvalho de Melo			print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu,
39877108e4SArnaldo Carvalho de Melo                                                                        event.sample_pid,
40d2ed5d2bSArnaldo Carvalho de Melo                                                                        event.sample_tid,
41d2ed5d2bSArnaldo Carvalho de Melo                                                                        event))
42877108e4SArnaldo Carvalho de Melo
43877108e4SArnaldo Carvalho de Meloif __name__ == '__main__':
44ae938802SArnaldo Carvalho de Melo    """
45ae938802SArnaldo Carvalho de Melo	To test the PERF_RECORD_SWITCH record, pick a pid and replace
46ae938802SArnaldo Carvalho de Melo	in the following line.
47ae938802SArnaldo Carvalho de Melo
48ae938802SArnaldo Carvalho de Melo	Example output:
49ae938802SArnaldo Carvalho de Melo
50ae938802SArnaldo Carvalho de Melocpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 }
51ae938802SArnaldo Carvalho de Melocpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 }
52ae938802SArnaldo Carvalho de Melocpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 }
53ae938802SArnaldo Carvalho de Melocpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 }
54ae938802SArnaldo Carvalho de Melo
55ae938802SArnaldo Carvalho de Melo	It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT
56ae938802SArnaldo Carvalho de Melo	to figure out if this is a context switch in or out of the monitored threads.
57ae938802SArnaldo Carvalho de Melo
58ae938802SArnaldo Carvalho de Melo	If bored, please add command line option parsing support for these options :-)
59ae938802SArnaldo Carvalho de Melo    """
60ae938802SArnaldo Carvalho de Melo    # main(context_switch = 1, thread = 31463)
61877108e4SArnaldo Carvalho de Melo    main()
62