tracepoint.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /usr/bin/env python
  2. # SPDX-License-Identifier: GPL-2.0
  3. # -*- python -*-
  4. # -*- coding: utf-8 -*-
  5. import perf
  6. def change_proctitle():
  7. try:
  8. import setproctitle
  9. setproctitle.setproctitle("tracepoint.py")
  10. except:
  11. print("Install the setproctitle python package to help with top and friends")
  12. def main():
  13. change_proctitle()
  14. cpus = perf.cpu_map()
  15. threads = perf.thread_map(-1)
  16. evlist = perf.parse_events("sched:sched_switch", cpus, threads)
  17. # Disable tracking of mmaps and similar that are unnecessary.
  18. for ev in evlist:
  19. ev.tracking = False
  20. # Configure evsels with default record options.
  21. evlist.config()
  22. # Simplify the sample_type and read_format of evsels
  23. for ev in evlist:
  24. ev.sample_type = ev.sample_type & ~perf.SAMPLE_IP
  25. ev.read_format = 0
  26. evlist.open()
  27. evlist.mmap()
  28. evlist.enable();
  29. while True:
  30. evlist.poll(timeout = -1)
  31. for cpu in cpus:
  32. event = evlist.read_on_cpu(cpu)
  33. if not event:
  34. continue
  35. if not isinstance(event, perf.sample_event):
  36. continue
  37. print("time %u prev_comm=%s prev_pid=%d prev_prio=%d prev_state=0x%x ==> next_comm=%s next_pid=%d next_prio=%d" % (
  38. event.sample_time,
  39. event.prev_comm,
  40. event.prev_pid,
  41. event.prev_prio,
  42. event.prev_state,
  43. event.next_comm,
  44. event.next_pid,
  45. event.next_prio))
  46. if __name__ == '__main__':
  47. main()