implemented a stack trace profiler

This commit is contained in:
Araq
2012-09-15 00:21:10 +02:00
parent 4a435a8fb4
commit 214c7a2ebd
11 changed files with 241 additions and 65 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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: