mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-22 00:41:28 +00:00
implemented a stack trace profiler
This commit is contained in:
@@ -265,6 +265,10 @@ proc genWhileStmt(p: BProc, t: PNode) =
|
||||
let label = assignLabel(p.blocks[p.breakIdx])
|
||||
lineF(p, cpsStmts, "if (!$1) goto $2;$n", [rdLoc(a), label])
|
||||
genStmts(p, t.sons[1])
|
||||
|
||||
if optProfiler in p.options:
|
||||
# invoke at loop body exit:
|
||||
lineCg(p, cpsStmts, "#nimProfile();$n")
|
||||
endBlock(p)
|
||||
|
||||
dec(p.withinLoop)
|
||||
|
||||
@@ -680,27 +680,14 @@ proc genProcAux(m: BModule, prc: PSym) =
|
||||
app(generatedProc, initFrame(p, procname, filename))
|
||||
else:
|
||||
app(generatedProc, p.s(cpsLocals))
|
||||
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
||||
if gProcProfile >= 64 * 1024:
|
||||
InternalError(prc.info, "too many procedures for profiling")
|
||||
discard cgsym(m, "profileData")
|
||||
appf(p.s(cpsLocals), "\tticks NIM_profilingStart;$n")
|
||||
if prc.loc.a < 0:
|
||||
appf(m.s[cfsDebugInit], "\tprofileData[$1].procname = $2;$n", [
|
||||
toRope(gProcProfile),
|
||||
makeCString(prc.name.s)])
|
||||
prc.loc.a = gProcProfile
|
||||
inc(gProcProfile)
|
||||
prepend(p.s(cpsInit), ropef("\tNIM_profilingStart = getticks();$n"))
|
||||
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
||||
# invoke at proc entry for recursion:
|
||||
appcg(p, cpsInit, "\t#nimProfile();$n", [])
|
||||
app(generatedProc, p.s(cpsInit))
|
||||
app(generatedProc, p.s(cpsStmts))
|
||||
if p.beforeRetNeeded: appf(generatedProc, "\tBeforeRet: ;$n")
|
||||
app(generatedProc, deinitGCFrame(p))
|
||||
if optStackTrace in prc.options: app(generatedProc, deinitFrame(p))
|
||||
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
||||
appf(generatedProc,
|
||||
"\tprofileData[$1].total += elapsed(getticks(), NIM_profilingStart);$n",
|
||||
[toRope(prc.loc.a)])
|
||||
app(generatedProc, returnStmt)
|
||||
appf(generatedProc, "}$N")
|
||||
app(m.s[cfsProcs], generatedProc)
|
||||
|
||||
@@ -115,7 +115,6 @@ type
|
||||
var
|
||||
mainModProcs*, mainModInit*, mainDatInit*: PRope # parts of the main module
|
||||
gMapping*: PRope # the generated mapping file (if requested)
|
||||
gProcProfile*: Natural # proc profile counter
|
||||
gPendingModules*: seq[BModule] = @[] # list of modules that are not
|
||||
# finished with code generation
|
||||
gModules*: seq[BModule] = @[] # list of all compiled modules
|
||||
|
||||
@@ -20,6 +20,10 @@ import
|
||||
when hasTinyCBackend:
|
||||
import tccgen
|
||||
|
||||
when defined(profiler):
|
||||
{.hint: "Profiling support is turned on!".}
|
||||
import nimprof
|
||||
|
||||
var
|
||||
arguments: string = "" # the arguments to be passed to the program that
|
||||
# should be run
|
||||
|
||||
@@ -294,6 +294,7 @@ proc crcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 =
|
||||
result = crcFromRopeAux(r.right, result)
|
||||
|
||||
proc newCrcFromRopeAux(r: PRope, startVal: TCrc32): TCrc32 =
|
||||
# XXX profiling shows this is actually expensive
|
||||
var stack: TRopeSeq = @[r]
|
||||
result = startVal
|
||||
while len(stack) > 0:
|
||||
|
||||
Reference in New Issue
Block a user