vmprofiler.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import
  2. options, vmdef, times, lineinfos, strutils, tables,
  3. msgs
  4. proc enter*(prof: var Profiler, c: PCtx, tos: PStackFrame) {.inline.} =
  5. if optProfileVM in c.config.globalOptions:
  6. prof.tEnter = cpuTime()
  7. prof.tos = tos
  8. proc leaveImpl(prof: var Profiler, c: PCtx) {.noinline.} =
  9. let tLeave = cpuTime()
  10. var tos = prof.tos
  11. var data = c.config.vmProfileData.data
  12. while tos != nil:
  13. if tos.prc != nil:
  14. let li = tos.prc.info
  15. if li notin data:
  16. data[li] = ProfileInfo()
  17. data[li].time += tLeave - prof.tEnter
  18. if tos == prof.tos:
  19. inc data[li].count
  20. tos = tos.next
  21. proc leave*(prof: var Profiler, c: PCtx) {.inline.} =
  22. if optProfileVM in c.config.globalOptions:
  23. leaveImpl(prof, c)
  24. proc dump*(conf: ConfigRef, pd: ProfileData): string =
  25. var data = pd.data
  26. echo "\nprof: µs #instr location"
  27. for i in 0..<32:
  28. var tMax: float
  29. var infoMax: ProfileInfo
  30. var flMax: TLineInfo
  31. for fl, info in data:
  32. if info.time > infoMax.time:
  33. infoMax = info
  34. flMax = fl
  35. if infoMax.count == 0:
  36. break
  37. result.add " " & align($int(infoMax.time * 1e6), 10) &
  38. align($int(infoMax.count), 10) & " " &
  39. conf.toFileLineCol(flMax) & "\n"
  40. data.del flMax