diff --git a/src/core/heap.c b/src/core/heap.c index 8e9805e7..55d27116 100644 --- a/src/core/heap.c +++ b/src/core/heap.c @@ -42,3 +42,35 @@ void heapVerify() { } #endif + +static u64 heap_PICounts[t_COUNT]; +static u64 heap_PISizes[t_COUNT]; + +void heap_PIFn(Value* v) { + heap_PICounts[v->type]++; + heap_PISizes[v->type]+= mm_size(v); +} + +void heap_printInfo(bool sizes, bool types) { + u64 total = mm_heapAlloc; + u64 used = mm_heapUsed(); + printf("RAM allocated: "N64u"\n", total); + printf("heap in use: "N64u"\n", used); + #if MM!=0 + if (sizes) { + i32 count = sizeof(mm_ctrs)/8; + for (i32 i = 0; i < count; i++) { + u64 count = mm_ctrs[i]; + if (count>0) printf("size %llu: "N64u"\n", i>64? 1ULL<<(i*3) : 1ULL<0) printf("type %d/%s: count "N64u", total size "N64u"\n", i, type_repr(i), count, heap_PISizes[i]); + } + } + #endif +} diff --git a/src/core/heap.h b/src/core/heap.h index 88047f6a..d82528f9 100644 --- a/src/core/heap.h +++ b/src/core/heap.h @@ -21,3 +21,5 @@ static bool heapVerify_visitP(void* x) { void heapVerify(void); #endif + +void heap_printInfo(bool sizes, bool types); diff --git a/src/main.c b/src/main.c index 71b2b987..184f2322 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ static B gsc_exec_inline(B src, B path, B args) { static bool isCmd(char* s, char** e, const char* cmd) { while (*cmd) { + if (*cmd==' ' && *s==0) { *e = s; return true; } if (*s!=*cmd || *s==0) return false; s++; cmd++; } @@ -191,6 +192,21 @@ int main(int argc, char* argv[]) { code = fromUTF8l(repE); time = am; output = false; + } else if (isCmd(cmdS, &cmdE, "mem ")) { + bool sizes = 0; + bool types = 0; + char c; + while ((c=*(cmdE++)) != 0) { + if (c=='t') types=1; + if (c=='s') sizes=1; + } + heap_printInfo(sizes, types); + goto cont; + } else if (isCmd(cmdS, &cmdE, "gc ")) { + if (gc_depth!=0) printf("Cannot GC currently\n"); + else if (ENABLE_GC) gc_forceGC(); + else printf("Macro ENABLE_GC was false at compile-time, cannot GC\n"); + goto cont; } else { printf("Unknown REPL command\n"); goto cont;