summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorEmery Hemingway <ehmry@posteo.net>2018-08-07 08:48:52 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-08-07 08:48:52 +0200
commitbe4816f5097ce72e124e5e087ffa4d386fa17d4d (patch)
tree41e304ea67847a9a598e08196114d2dd457c9d52
parentbbc404d80d77365a1c402a0862a3d26958b16e7b (diff)
downloadNim-be4816f5097ce72e124e5e087ffa4d386fa17d4d.tar.gz
Iterator over heap instances (#8548)
Provide "dumpHeapInstances" for iterating over type statistics
of heaps. This can be used to present structured heap information
as an alternative to "dumpNumberOfInstances".
-rw-r--r--lib/system/gc_common.nim22
1 files changed, 14 insertions, 8 deletions
diff --git a/lib/system/gc_common.nim b/lib/system/gc_common.nim
index dcea0c4cc..88e150cd1 100644
--- a/lib/system/gc_common.nim
+++ b/lib/system/gc_common.nim
@@ -37,22 +37,28 @@ when defined(nimTypeNames):
         a[j] = v
       if h == 1: break
 
-  proc dumpNumberOfInstances* =
-    # also add the allocated strings to the list of known types:
+  iterator dumpHeapInstances*(): tuple[name: cstring; count: int; sizes: int] =
+    ## Iterate over summaries of types on heaps.
+    ## This data may be inaccurate if allocations
+    ## are made by the iterator body.
     if strDesc.nextType == nil:
       strDesc.nextType = nimTypeRoot
       strDesc.name = "string"
       nimTypeRoot = addr strDesc
+    var it = nimTypeRoot
+    while it != nil:
+      if (it.instances > 0 or it.sizes != 0):
+        yield (it.name, it.instances, it.sizes)
+      it = it.nextType
+
+  proc dumpNumberOfInstances* =
     var a: InstancesInfo
     var n = 0
-    var it = nimTypeRoot
     var totalAllocated = 0
-    while it != nil:
-      if (it.instances > 0 or it.sizes != 0) and n < a.len:
-        a[n] = (it.name, it.instances, it.sizes)
-        inc n
+    for it in dumpHeapInstances():
+      a[n] = it
+      inc n
       inc totalAllocated, it.sizes
-      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])