Improved GC debugging

This commit is contained in:
Mattia Giambirtone 2022-11-05 09:23:51 +01:00
parent d09b72694e
commit 6f444582a4
2 changed files with 20 additions and 11 deletions

View File

@ -68,6 +68,7 @@ type
## this system and is not handled
## manually by the VM
bytesAllocated: tuple[total, current: int]
cycles: int
nextGC: int
pointers: HashSet[uint64]
objects: seq[ptr HeapObject]
@ -108,6 +109,7 @@ proc newPeonGC*: PeonGC =
result.bytesAllocated = (0, 0)
result.objects = @[]
result.nextGC = FirstGC
result.cycles = 0
proc collect*(self: var PeonVM)
@ -121,18 +123,18 @@ proc reallocate*(self: var PeonVM, p: pointer, oldSize: int, newSize: int): poin
if newSize == 0 and not p.isNil():
when debugMem:
if oldSize > 1:
echo &"DEBUG - Memory manager: Deallocating {oldSize} bytes of memory"
echo &"DEBUG - MM: Deallocating {oldSize} bytes of memory"
else:
echo "DEBUG - Memory manager: Deallocating 1 byte of memory"
echo "DEBUG - MM: Deallocating 1 byte of memory"
elif (oldSize > 0 and not p.isNil() and newSize > oldSize) or oldSize == 0:
when debugMem:
if oldSize == 0:
if newSize > 1:
echo &"DEBUG - Memory manager: Allocating {newSize} bytes of memory"
echo &"DEBUG - MM: Allocating {newSize} bytes of memory"
else:
echo "DEBUG - Memory manager: Allocating 1 byte of memory"
echo "DEBUG - MM: Allocating 1 byte of memory"
else:
echo &"DEBUG - Memory manager: Resizing {oldSize} bytes of memory to {newSize} bytes"
echo &"DEBUG - M: Resizing {oldSize} bytes of memory to {newSize} bytes"
self.gc.bytesAllocated.total += newSize - oldSize
when debugStressGC:
self.collect()
@ -171,19 +173,20 @@ proc allocate*(self: var PeonVM, kind: ObjectKind, size: typedesc, count: int):
result = cast[ptr HeapObject](self.reallocate(nil, 0, sizeof(HeapObject)))
setkind(result[], kind, kind)
result.marked = false
self.gc.bytesAllocated.total += sizeof(HeapObject)
self.gc.bytesAllocated.current += sizeof(HeapObject)
case kind:
of String:
result.str = cast[ptr UncheckedArray[char]](self.reallocate(nil, 0, sizeof(size) * count))
result.len = count
else:
discard # TODO
when debugAlloc:
echo &"DEBUG - GC: Allocated new object: {result[]}"
self.gc.bytesAllocated.current += sizeof(size) * count
self.gc.objects.add(result)
self.gc.pointers.incl(cast[uint64](result))
when debugAlloc:
echo &"DEBUG - GC: Allocated new object: {result[]}"
echo &"DEBUG - GC: Current heap size: {self.gc.bytesAllocated.current}"
echo &"DEBUG - GC: Total bytes allocated: {self.gc.bytesAllocated.total}"
echo &"DEBUG - GC: Tracked objects: {self.gc.pointers.len()}"
echo &"DEBUG - GC: Completed GC cycles: {self.gc.cycles}"
proc mark(self: ptr HeapObject): bool =
@ -282,6 +285,11 @@ proc free(self: var PeonVM, obj: ptr HeapObject) =
discard # TODO
self.free(HeapObject, obj)
self.gc.pointers.excl(cast[uint64](obj))
when debugAlloc:
echo &"DEBUG - GC: Current heap size: {self.gc.bytesAllocated.current}"
echo &"DEBUG - GC: Total bytes allocated: {self.gc.bytesAllocated.total}"
echo &"DEBUG - GC: Tracked objects: {self.gc.pointers.len()}"
echo &"DEBUG - GC: Completed GC cycles: {self.gc.cycles}"
proc sweep(self: var PeonVM) =
@ -329,6 +337,7 @@ proc collect(self: var PeonVM) =
let before = self.gc.bytesAllocated.current
let time = getMonoTime().ticks().float() / 1_000_000
echo &"DEBUG - GC: Starting collection cycle at heap size {self.gc.bytesAllocated.current}"
inc(self.gc.cycles)
self.trace(self.markRoots())
self.sweep()
self.gc.nextGC = self.gc.bytesAllocated.current * HeapGrowFactor

View File

@ -1,7 +1,7 @@
import std;
var x = 1000000;
var x = 1;
var y = "just a test";
print(y);
print("Starting GC torture test");