Files
Nim/compiler/vmprofiler.nim
Andreas Rumpf f608e109c9 massive refactoring for IC (#25282)
TODO:

- [ ] test writing of .nif files
- [x] implement loading of fields in PType/PSym that might not have been
loaded
- [ ] implement interface logic
- [ ] implement pragma "replays"
- [ ] implement special logic for `converter`
- [ ] implement special logic for `method`
- [ ] test the logic holds up for `export`
- [ ] implement logic to free the memory of PSym/PType if memory
pressure is high
- [ ] implement logic to close memory mapped files if too many are open.

---------

Co-authored-by: demotomohiro <gpuppur@gmail.com>
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
2025-11-13 21:31:24 +01:00

46 lines
1.3 KiB
Nim

import ast, options, vmdef, lineinfos, msgs
import std/[times, strutils, tables]
proc enter*(prof: var Profiler, c: PCtx, tos: PStackFrame) {.inline.} =
if optProfileVM in c.config.globalOptions:
prof.tEnter = cpuTime()
prof.tos = tos
proc leaveImpl(prof: var Profiler, c: PCtx) {.noinline.} =
let tLeave = cpuTime()
var tos = prof.tos
var data = c.config.vmProfileData.data
while tos != nil:
if tos.prc != nil:
let li = tos.prc.info
if li notin data:
data[li] = ProfileInfo()
data[li].time += tLeave - prof.tEnter
if tos == prof.tos:
inc data[li].count
tos = tos.next
proc leave*(prof: var Profiler, c: PCtx) {.inline.} =
if optProfileVM in c.config.globalOptions:
leaveImpl(prof, c)
proc dump*(conf: ConfigRef, pd: ProfileData): string =
var data = pd.data
result = "\nprof: µs #instr location"
for i in 0..<32:
var tMax: float
var infoMax: ProfileInfo = default(ProfileInfo)
var flMax: TLineInfo = default(TLineInfo)
for fl, info in data:
if info.time > infoMax.time:
infoMax = info
flMax = fl
if infoMax.count == 0:
break
result.add " " & align($int(infoMax.time * 1e6), 10) &
align($int(infoMax.count), 10) & " " &
conf.toFileLineCol(flMax) & "\n"
data.del flMax