leak detector: sort type based entries by total used bytes

This commit is contained in:
Andreas Rumpf
2018-01-18 11:11:16 +01:00
parent 03b7df74a9
commit 6f6551d77d

View File

@@ -18,12 +18,38 @@ proc protect*(x: pointer): ForeignCell =
result.owner = addr(gch)
when defined(nimTypeNames):
type InstancesInfo = array[400, (cstring, int, int)]
proc sortInstances(a: var InstancesInfo; n: int) =
# we use shellsort here; fast and simple
var h = 1
while true:
h = 3 * h + 1
if h > n: break
while true:
h = h div 3
for i in countup(h, n - 1):
var v = a[i]
var j = i
while a[j - h][2] < v[2]:
a[j] = a[j - h]
j = j - h
if j < h: break
a[j] = v
if h == 1: break
proc dumpNumberOfInstances* =
var a: InstancesInfo
var n = 0
var it = nimTypeRoot
while it != nil:
if it.instances > 0:
c_fprintf(stdout, "[Heap] %s: #%ld; bytes: %ld\n", it.name, it.instances, it.sizes)
if it.instances > 0 and n < a.len:
a[n] = (it.name, it.instances, it.sizes)
inc n
it = it.nextType
sortInstances(a, n)
for i in 0 .. n-1:
c_fprintf(stdout, "[Heap] %s: #%ld; bytes: %ld\n", a[i][0], a[i][1], a[i][2])
when defined(nimGcRefLeak):
proc oomhandler() =