vmprofiler.nim 1.3 KB

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