twatch.py 2.7 KB

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