twatch.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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():
  17. cpus = perf.cpu_map()
  18. threads = perf.thread_map()
  19. evsel = perf.evsel(task = 1, comm = 1, mmap = 0,
  20. wakeup_events = 1, sample_period = 1,
  21. sample_id_all = 1,
  22. sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU | perf.SAMPLE_TID)
  23. evsel.open(cpus = cpus, threads = threads);
  24. evlist = perf.evlist(cpus, threads)
  25. evlist.add(evsel)
  26. evlist.mmap()
  27. while True:
  28. evlist.poll(timeout = -1)
  29. for cpu in cpus:
  30. event = evlist.read_on_cpu(cpu)
  31. if not event:
  32. continue
  33. print "cpu: %2d, pid: %4d, tid: %4d" % (event.sample_cpu,
  34. event.sample_pid,
  35. event.sample_tid),
  36. print event
  37. if __name__ == '__main__':
  38. main()