stat-cpi.py 2.4 KB

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