Merge pull request #1210 from boydgreenfield/nimprof-lock-fix

Fix nimprof import error when --threads:on.
This commit is contained in:
Varriount 2014-05-21 17:55:07 -04:00
commit f05ba51391

View file

@ -58,8 +58,9 @@ when not defined(memProfiler):
## instruction count measure instead then. ## instruction count measure instead then.
if intervalInUs <= 0: interval = 0 if intervalInUs <= 0: interval = 0
else: interval = intervalInUs * 1000 - tickCountCorrection else: interval = intervalInUs * 1000 - tickCountCorrection
when withThreads: when withThreads:
import locks
var var
profilingLock: TLock profilingLock: TLock
@ -72,7 +73,7 @@ proc hookAux(st: TStackTrace, costs: int) =
var last = high(st) var last = high(st)
while last > 0 and isNil(st[last]): dec last while last > 0 and isNil(st[last]): dec last
var h = hash(pointer(st[last])) and high(profileData) var h = hash(pointer(st[last])) and high(profileData)
# we use probing for maxChainLen entries and replace the encountered entry # we use probing for maxChainLen entries and replace the encountered entry
# with the minimal 'total' value: # with the minimal 'total' value:
if emptySlots == 0: if emptySlots == 0:
@ -133,7 +134,7 @@ else:
hookAux(st, 1) hookAux(st, 1)
elif getticks() - t0 > interval: elif getticks() - t0 > interval:
hookAux(st, 1) hookAux(st, 1)
t0 = getticks() t0 = getticks()
proc getTotal(x: ptr TProfileEntry): int = proc getTotal(x: ptr TProfileEntry): int =
result = if isNil(x): 0 else: x.total result = if isNil(x): 0 else: x.total
@ -145,7 +146,7 @@ proc `//`(a, b: int): string =
result = format("$1/$2 = $3%", a, b, formatFloat(a / b * 100.0, ffDefault, 2)) result = format("$1/$2 = $3%", a, b, formatFloat(a / b * 100.0, ffDefault, 2))
proc writeProfile() {.noconv.} = proc writeProfile() {.noconv.} =
when defined(system.TStackTrace): when defined(system.TStackTrace):
system.profilerHook = nil system.profilerHook = nil
const filename = "profile_results.txt" const filename = "profile_results.txt"
echo "writing " & filename & "..." echo "writing " & filename & "..."
@ -156,7 +157,7 @@ proc writeProfile() {.noconv.} =
var entries = 0 var entries = 0
for i in 0..high(profileData): for i in 0..high(profileData):
if profileData[i] != nil: inc entries if profileData[i] != nil: inc entries
var perProc = initCountTable[string]() var perProc = initCountTable[string]()
for i in 0..entries-1: for i in 0..entries-1:
var dups = initSet[string]() var dups = initSet[string]()
@ -166,7 +167,7 @@ proc writeProfile() {.noconv.} =
let p = $procname let p = $procname
if not containsOrIncl(dups, p): if not containsOrIncl(dups, p):
perProc.inc(p, profileData[i].total) perProc.inc(p, profileData[i].total)
var sum = 0 var sum = 0
# only write the first 100 entries: # only write the first 100 entries:
for i in 0..min(100, entries-1): for i in 0..min(100, entries-1):