heap dump

This commit is contained in:
dzaima 2022-03-06 15:38:30 +02:00
parent 03978f333c
commit d2674db956
12 changed files with 67 additions and 8 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ perf.*
/asm_* /asm_*
/src/singeli/gen /src/singeli/gen
/Singeli /Singeli
CBQNHeapDump

View File

@ -14,7 +14,7 @@
/* sysfn.c*/M(fName,"•file.Name") \ /* sysfn.c*/M(fName,"•file.Name") \
/* sysfn.c*/M(tRaw,"•term.Raw") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") M(tOutRaw,"•term.OutRaw") M(tErrRaw,"•term.ErrRaw") \ /* sysfn.c*/M(tRaw,"•term.Raw") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") M(tOutRaw,"•term.OutRaw") M(tErrRaw,"•term.ErrRaw") \
/* inverse.c*/M(setInvReg, "(SetInvReg)") M(setInvSwap, "(SetInvSwap)") M(nativeInvReg, "(NativeInvReg)") M(nativeInvSwap, "(NativeInvSwap)") \ /* inverse.c*/M(setInvReg, "(SetInvReg)") M(setInvSwap, "(SetInvSwap)") M(nativeInvReg, "(NativeInvReg)") M(nativeInvSwap, "(NativeInvSwap)") \
/*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \ /*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") M(heapDump,"•internal.HeapDump") \
/*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \ /*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \
/*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \ /*internal.c*/D(variation,"•internal.Variation") A(listVariations,"•internal.ListVariations") M(clearRefs,"•internal.ClearRefs") M(unshare,"•internal.Unshare") \
/* arithm.c*/M(sin,"•math.Sin") M(cos,"•math.Cos") M(tan,"•math.Tan") M(asin,"•math.Asin") M(acos,"•math.Acos") M(atan,"•math.Atan") /* arithm.c*/M(sin,"•math.Sin") M(cos,"•math.Cos") M(tan,"•math.Tan") M(asin,"•math.Asin") M(acos,"•math.Acos") M(atan,"•math.Atan")

View File

@ -1,5 +1,6 @@
#include "../core.h" #include "../core.h"
#include "../utils/mut.h" #include "../utils/mut.h"
#include "../utils/file.h"
#include "../builtins.h" #include "../builtins.h"
#include "../ns.h" #include "../ns.h"
@ -257,6 +258,11 @@ static B unshare(B x) {
} }
} }
B heapDump_c1(B t, B x) {
cbqn_heapDump();
return m_c32(0);
}
B unshare_c1(B t, B x) { B unshare_c1(B t, B x) {
if (!isArr(x)) thrM("•internal.Unshare: Argument must be an array"); if (!isArr(x)) thrM("•internal.Unshare: Argument must be an array");
B r = unshare(x); B r = unshare(x);
@ -272,8 +278,8 @@ B getInternalNS() {
listVariations_def = m_str8l("if"); listVariations_def = m_str8l("if");
gc_addFn(variation_gcRoot); gc_addFn(variation_gcRoot);
#define F(X) inc(bi_##X), #define F(X) inc(bi_##X),
Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info","listvariations","variation","clearrefs","unshare","deepsqueeze"); Body* d = m_nnsDesc("type","eltype","refc","squeeze","ispure","info","listvariations","variation","clearrefs","unshare","deepsqueeze","heapdump");
internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(listVariations)F(variation)F(clearRefs)F(unshare)F(deepSqueeze)); internalNS = m_nns(d,F(itype)F(elType)F(refc)F(squeeze)F(isPure)F(info)F(listVariations)F(variation)F(clearRefs)F(unshare)F(deepSqueeze)F(heapDump));
#undef F #undef F
gc_add(internalNS); gc_add(internalNS);
} }

View File

@ -213,10 +213,8 @@ typedef union B {
/*32*/ F(comp) F(block) F(body) F(scope) F(scopeExt) F(blBlocks) \ /*32*/ F(comp) F(block) F(body) F(scope) F(scopeExt) F(blBlocks) \
/*38*/ F(ns) F(nsDesc) F(fldAlias) F(vfyObj) F(hashmap) F(temp) F(nfn) F(nfnDesc) \ /*38*/ F(ns) F(nsDesc) F(fldAlias) F(vfyObj) F(hashmap) F(temp) F(nfn) F(nfnDesc) \
/*46*/ F(freed) F(harrPartial) F(customObj) \ /*46*/ F(freed) F(harrPartial) F(customObj) \
/*49*/ F(fun_invReg ) F(md1_invReg ) F(md2_invReg ) \
/*52*/ F(fun_invSwap) F(md1_invSwap) F(md2_invSwap) \
\ \
/*55*/ IF_WRAP(F(funWrap) F(md1Wrap) F(md2Wrap)) /*49*/ IF_WRAP(F(funWrap) F(md1Wrap) F(md2Wrap))
enum Type { enum Type {
#define F(X) t_##X, #define F(X) t_##X,

View File

@ -39,6 +39,10 @@ void mm_forFreedHeap(V2v f) {
b1_forFreedHeap(f); b1_forFreedHeap(f);
b3_forFreedHeap(f); b3_forFreedHeap(f);
} }
void mm_dumpHeap(FILE* f) {
b1_dumpHeap(f);
b3_dumpHeap(f);
}
u64 mm_heapUsed() { u64 mm_heapUsed() {
u64 r = 0; u64 r = 0;

View File

@ -40,6 +40,7 @@ static u64 mm_size(Value* x) {
else return 1ull<<(x->mmInfo&63); else return 1ull<<(x->mmInfo&63);
} }
void mm_forHeap(V2v f); void mm_forHeap(V2v f);
void mm_dumpHeap(FILE* f);
#undef BN #undef BN
#undef BSZ #undef BSZ

View File

@ -32,6 +32,7 @@ static u64 mm_size(Value* x) {
return BSZ(x->mmInfo&63); return BSZ(x->mmInfo&63);
} }
void mm_forHeap(V2v f); void mm_forHeap(V2v f);
void mm_dumpHeap(FILE* f);
#undef LOG2 #undef LOG2
#undef BN #undef BN

View File

@ -1,3 +1,4 @@
#include "../utils/file.h"
#define AllocInfo BN(AllocInfo) #define AllocInfo BN(AllocInfo)
#define buckets BN(buckets) #define buckets BN(buckets)
#define al BN(al) #define al BN(al)
@ -97,6 +98,16 @@ void BN(forFreedHeap)(V2v f) {
} }
} }
void BN(dumpHeap)(FILE* f) {
for (u64 i = 0; i < alSize; i++) {
AllocInfo ci = al[i];
u64 addrI = (u64) ci.p;
u8 size[8]; for (i32 i = 0; i < 8; i++) size[i] = (ci.sz>>(8*i)) & 0xff; fwrite(&size, 1, 8, f);
u8 addr[8]; for (i32 i = 0; i < 8; i++) addr[i] = (addrI>>(8*i)) & 0xff; fwrite(&addr, 1, 8, f);
fwrite(ci.p, 1, ci.sz, f);
}
}
#undef MMAP #undef MMAP
#undef AllocInfo #undef AllocInfo
#undef buckets #undef buckets

View File

@ -9,3 +9,4 @@ void gc_forceGC() { }
void gc_visitRoots() { } void gc_visitRoots() { }
void mm_forHeap(V2v f) { } void mm_forHeap(V2v f) { }
u64 mm_heapUsed() { return 123; } // idk u64 mm_heapUsed() { return 123; } // idk
void mm_dumpHeap(FILE* f) { }

View File

@ -30,6 +30,7 @@ void gc_maybeGC(void);
void gc_forceGC(void); void gc_forceGC(void);
void gc_visitRoots(void); void gc_visitRoots(void);
void mm_forHeap(V2v f); void mm_forHeap(V2v f);
void mm_dumpHeap(FILE* f);
static u64 mm_round(usz x) { return x; } static u64 mm_round(usz x) { return x; }
static u64 mm_size(Value* x) { return malloc_usable_size(x); } static u64 mm_size(Value* x) { return malloc_usable_size(x); }

View File

@ -236,3 +236,37 @@ char path_type(B path) {
if (S_ISCHR (mode)) return 'c'; if (S_ISCHR (mode)) return 'c';
thrM("Unexpected file type"); thrM("Unexpected file type");
} }
void cbqn_heapDump() {
char* name = "CBQNHeapDump";
FILE* f = fopen(name, "w");
if (f==NULL) {
fprintf(stderr, "Failed to dump heap - could not open file for writing\n");
return;
}
// fwrite(&size, 8, 1, f);
u8 t8 = 0;
fwrite(&t8, 1, 1, f); // version
// sizeof(ur), sizeof(usz)
u8 urW[4]; for (i32 i = 0; i < 4; i++) urW[i] = (sizeof(ur )>>(8*i)) & 0xff; fwrite( &urW, 1, 4, f);
u8 uszW[4]; for (i32 i = 0; i < 4; i++) uszW[i] = (sizeof(usz)>>(8*i)) & 0xff; fwrite(&uszW, 1, 4, f);
// t_names
#define F(X) { t8=t_##X; fwrite(&t8, 1, 1, f); char* s = #X; fwrite(s, 1, strlen(s)+1, f); }
FOR_TYPE(F)
#undef F
t8 = 255; fwrite(&t8, 1, 1, f); // end of t_names
t8 = 12; fwrite(&t8, 1, 1, f); // number of tag names
u8 t16a[2];
#define F(X) { \
t16a[0] = X##_TAG&0xff; t16a[1] = (X##_TAG>>8)&0xff; fwrite(&t16a, 1, 2, f); \
char* s = #X"_TAG"; fwrite(s, 1, strlen(s)+1, f); \
}
F(C32)F(TAG)F(VAR)F(EXT)F(RAW)F(MD1)F(MD2)F(FUN)F(NSP)F(OBJ)F(ARR)F(VAL)
#undef F
mm_dumpHeap(f);
fprintf(stderr, "Heap dumped to \"%s\"\n", name);
fclose(f);
}

View File

@ -19,3 +19,4 @@ void file_wBytes(FILE* file, B name, B x); // consumes x
B path_list(B path); // consumes B path_list(B path); // consumes
char path_type(B path); // consumes char path_type(B path); // consumes
void cbqn_heapDump();