1 #! /usr/bin/env python !! 1 #! /usr/bin/python 2 # SPDX-License-Identifier: GPL-2.0-only << 3 # -*- python -*- 2 # -*- python -*- 4 # -*- coding: utf-8 -*- 3 # -*- coding: utf-8 -*- 5 # twatch - Experimental use of the perf pyth 4 # twatch - Experimental use of the perf python interface 6 # Copyright (C) 2011 Arnaldo Carvalho de Mel< 5 # Copyright (C) 2011 Arnaldo Carvalho de Melo <acme@redhat.com> 7 # 6 # >> 7 # This application is free software; you can redistribute it and/or >> 8 # modify it under the terms of the GNU General Public License >> 9 # as published by the Free Software Foundation; version 2. >> 10 # >> 11 # This application is distributed in the hope that it will be useful, >> 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of >> 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> 14 # General Public License for more details. 8 15 9 import perf 16 import perf 10 17 11 def main(context_switch = 0, thread = -1): !! 18 def main(): 12 cpus = perf.cpu_map() 19 cpus = perf.cpu_map() 13 threads = perf.thread_map(thread) !! 20 threads = perf.thread_map() 14 evsel = perf.evsel(type = perf.TYPE_ !! 21 evsel = perf.evsel(task = 1, comm = 1, mmap = 0, 15 config = perf.COUNT << 16 task = 1, comm = 1, << 17 wakeup_events = 1, 22 wakeup_events = 1, watermark = 1, 18 sample_id_all = 1, !! 23 sample_id_all = 1, 19 sample_type = perf. !! 24 sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU | perf.SAMPLE_TID) 20 << 21 """What we want are just the PERF_RECO << 22 using the default, PERF_TYPE_HARDWARE << 23 (the default), makes perf reenable ir << 24 disabling nohz, not good for some use << 25 threads comes and goes... So use (per << 26 freq=0) instead.""" << 27 << 28 evsel.open(cpus = cpus, threads = thre 25 evsel.open(cpus = cpus, threads = threads); 29 evlist = perf.evlist(cpus, threads) 26 evlist = perf.evlist(cpus, threads) 30 evlist.add(evsel) 27 evlist.add(evsel) 31 evlist.mmap() 28 evlist.mmap() 32 while True: 29 while True: 33 evlist.poll(timeout = -1) 30 evlist.poll(timeout = -1) 34 for cpu in cpus: 31 for cpu in cpus: 35 event = evlist.read_on 32 event = evlist.read_on_cpu(cpu) 36 if not event: 33 if not event: 37 continue 34 continue 38 print("cpu: {0}, pid: !! 35 print "cpu: %2d, pid: %4d, tid: %4d" % (event.sample_cpu, 39 !! 36 event.sample_pid, 40 !! 37 event.sample_tid), 41 !! 38 print event 42 39 43 if __name__ == '__main__': 40 if __name__ == '__main__': 44 """ << 45 To test the PERF_RECORD_SWITCH record, << 46 in the following line. << 47 << 48 Example output: << 49 << 50 cpu: 3, pid: 31463, tid: 31593 { type: context << 51 cpu: 1, pid: 31463, tid: 31489 { type: context << 52 cpu: 2, pid: 31463, tid: 31496 { type: context << 53 cpu: 3, pid: 31463, tid: 31491 { type: context << 54 << 55 It is possible as well to use event.mi << 56 to figure out if this is a context swi << 57 << 58 If bored, please add command line opti << 59 """ << 60 # main(context_switch = 1, thread = 31463) << 61 main() 41 main()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.