stat-cpi.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: GPL-2.0
  3. from __future__ import print_function
  4. data = {}
  5. times = []
  6. threads = []
  7. cpus = []
  8. def get_key(time, event, cpu, thread):
  9. return "%d-%s-%d-%d" % (time, event, cpu, thread)
  10. def store_key(time, cpu, thread):
  11. if (time not in times):
  12. times.append(time)
  13. if (cpu not in cpus):
  14. cpus.append(cpu)
  15. if (thread not in threads):
  16. threads.append(thread)
  17. def store(time, event, cpu, thread, val, ena, run):
  18. #print("event %s cpu %d, thread %d, time %d, val %d, ena %d, run %d" %
  19. # (event, cpu, thread, time, val, ena, run))
  20. store_key(time, cpu, thread)
  21. key = get_key(time, event, cpu, thread)
  22. data[key] = [ val, ena, run]
  23. def get(time, event, cpu, thread):
  24. key = get_key(time, event, cpu, thread)
  25. return data[key][0]
  26. def stat__cycles_k(cpu, thread, time, val, ena, run):
  27. store(time, "cycles", cpu, thread, val, ena, run);
  28. def stat__instructions_k(cpu, thread, time, val, ena, run):
  29. store(time, "instructions", cpu, thread, val, ena, run);
  30. def stat__cycles_u(cpu, thread, time, val, ena, run):
  31. store(time, "cycles", cpu, thread, val, ena, run);
  32. def stat__instructions_u(cpu, thread, time, val, ena, run):
  33. store(time, "instructions", cpu, thread, val, ena, run);
  34. def stat__cycles(cpu, thread, time, val, ena, run):
  35. store(time, "cycles", cpu, thread, val, ena, run);
  36. def stat__instructions(cpu, thread, time, val, ena, run):
  37. store(time, "instructions", cpu, thread, val, ena, run);
  38. def stat__interval(time):
  39. for cpu in cpus:
  40. for thread in threads:
  41. cyc = get(time, "cycles", cpu, thread)
  42. ins = get(time, "instructions", cpu, thread)
  43. cpi = 0
  44. if ins != 0:
  45. cpi = cyc/float(ins)
  46. print("%15f: cpu %d, thread %d -> cpi %f (%d/%d)" % (time/(float(1000000000)), cpu, thread, cpi, cyc, ins))
  47. def trace_end():
  48. pass
  49. # XXX trace_end callback could be used as an alternative place
  50. # to compute same values as in the script above:
  51. #
  52. # for time in times:
  53. # for cpu in cpus:
  54. # for thread in threads:
  55. # cyc = get(time, "cycles", cpu, thread)
  56. # ins = get(time, "instructions", cpu, thread)
  57. #
  58. # if ins != 0:
  59. # cpi = cyc/float(ins)
  60. #
  61. # print("time %.9f, cpu %d, thread %d -> cpi %f" % (time/(float(1000000000)), cpu, thread, cpi))