Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TODO] profiler improvements #10119

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/pure/nimprof.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ const
withThreads = compileOption("threads")
tickCountCorrection = 50_000

when not declared(system.StackTrace):
type StackTrace = object
lines: array[0..20, cstring]
files: array[0..20, cstring]
proc `[]`*(st: StackTrace, i: int): cstring = st.lines[i]

# We use a simple hash table of bounded size to keep track of the stack traces:
type
ProfileEntry = object
total: int
st: StackTrace
ProfileData = array[0..64*1024-1, ptr ProfileEntry]

proc `==`(x, y: cstring): bool {.noSideEffect, inline.} =
## Checks for equality between two `cstring` variables.
# copied over from system.nim as workaround for #10106
proc strcmp(a, b: cstring): cint {.noSideEffect,
importc, header: "<string.h>".}
if pointer(x) == pointer(y): result = true
elif x.isNil or y.isNil: result = false
else: result = strcmp(x, y) == 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still a letfover, I don't see you use it anywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the fix for #10106 ; without it it fails:

nim c --profiler:on compiler/nim.nim
compiler/nim c -o:/tmp/z01  compiler/nim.nim

fails with error msg here #10106 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a fix, it's workaround and I don't like it.

proc `==`(a, b: StackTrace): bool =
for i in 0 .. high(a.lines):
if a[i] != b[i]: return false
Expand Down Expand Up @@ -146,7 +149,7 @@ else:
dec gTicker

proc hook(st: StackTrace) {.nimcall, locks: 0.} =
#echo "profiling! ", interval
# echo "profiling! ", interval
if interval == 0:
hookAux(st, 1)
else:
Expand All @@ -167,7 +170,7 @@ proc writeProfile() {.noconv.} =
when declared(system.StackTrace):
system.profilerHook = nil
const filename = "profile_results.txt"
echo "writing " & filename & "..."
echo "writing " & filename & " ..."
var f: File
if open(f, filename, fmWrite):
sort(profileData, cmpEntries)
Expand All @@ -188,7 +191,7 @@ proc writeProfile() {.noconv.} =

var sum = 0
# only write the first 100 entries:
for i in 0..min(100, entries-1):
for i in 0..<min(100, entries):
if profileData[i].total > 1:
inc sum, profileData[i].total
writeLine(f, "Entry: ", i+1, "/", entries, " Calls: ",
Expand All @@ -197,8 +200,9 @@ proc writeProfile() {.noconv.} =
for ii in 0..high(StackTrace.lines):
let procname = profileData[i].st[ii]
let filename = profileData[i].st.files[ii]
let line = profileData[i].st.locLines[ii]
if isNil(procname): break
writeLine(f, " ", $filename & ": " & $procname, " ", perProc[$procname] // totalCalls)
writeLine(f, " ", $filename & ":" & $line & " " & $procname, " ", perProc[$procname] // totalCalls)
close(f)
echo "... done"
else:
Expand Down
12 changes: 11 additions & 1 deletion lib/system/profiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# code generator. The idea is to inject the instruction stream
# with 'nimProfile()' calls. These calls are injected at every loop end
# (except perhaps loops that have no side-effects). At every Nth call a
# stack trace is taken. A stack tace is a list of cstrings.
# stack trace is taken. A stack trace is a list of cstrings.

when defined(profiler) and defined(memProfiler):
{.error: "profiler and memProfiler cannot be defined at the same time (See Embedded Stack Trace Profiler (ESTP) User Guide) for more details".}
Expand All @@ -25,6 +25,13 @@ type
StackTrace* = object
lines*: array[0..MaxTraceLen-1, cstring]
files*: array[0..MaxTraceLen-1, cstring]
#[
todo(minor):
* rename to loc after renaming `lines` to procnames
* make StackTrace = object: entry: array[0..MaxTraceLen-1, StackTraceEntry]
with some StackTraceEntry object
]#
locLines*: array[0..MaxTraceLen-1, int]
ProfilerHook* = proc (st: StackTrace) {.nimcall.}

proc `[]`*(st: StackTrace, i: int): cstring = st.lines[i]
Expand All @@ -40,6 +47,7 @@ proc captureStackTrace(f: PFrame, st: var StackTrace) =
# the (-1) is for the "..." entry
st.lines[i] = it.procname
st.files[i] = it.filename
st.locLines[i] = it.line
inc(i)
inc(total)
it = it.prev
Expand All @@ -52,10 +60,12 @@ proc captureStackTrace(f: PFrame, st: var StackTrace) =
if total != i:
st.lines[i] = "..."
st.files[i] = "..."
st.locLines[i] = 0
inc(i)
while b != nil and i <= high(st.lines):
st.lines[i] = b.procname
st.files[i] = b.filename
st.locLines[i] = b.line
inc(i)
b = b.prev

Expand Down