From 24a1e39ad36ae2a81dcc2c7ceb2247053f13bc58 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 8 Feb 2022 23:40:52 +0800 Subject: [PATCH] add garbage collector and memory allocator info. use '-d' to activate this function. --- main.cpp | 1 + makefile | 24 ++++++++++++------------ nasal_gc.h | 31 ++++++++++++++++++++++++++++++- nasal_vm.h | 2 ++ 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 98f99ec..b1a856a 100644 --- a/main.cpp +++ b/main.cpp @@ -32,6 +32,7 @@ void help() <<" -t, --time | execute and get the running time.\n" <<" -o, --opcnt | execute and count used operands.\n" <<" -d, --detail | execute and get detail crash info.\n" + <<" | get garbage collector info if didn't crash.\n" <<" -op, --optimize| use optimizer(beta).\n" <<" | if want to use -op and run, please use -op -e/-t/-o/-d.\n" <<" -dbg, --debug | debug mode (this will ignore -t -o -d).\n" diff --git a/makefile b/makefile index a653ac7..648db94 100644 --- a/makefile +++ b/makefile @@ -5,30 +5,30 @@ test:nasal ./nasal -op -e test/ascii-art.nas ./nasal -op -c test/bf.nas ./nasal -op -c test/bfconvertor.nas - ./nasal -op -e test/bfs.nas + ./nasal -op -e -d test/bfs.nas ./nasal -op -t test/bigloop.nas ./nasal -op -e test/bp.nas - ./nasal -op -e test/calc.nas + ./nasal -op -e -d test/calc.nas ./nasal -op -e test/choice.nas ./nasal -op -e test/class.nas ./nasal -op -c test/exception.nas - ./nasal -op -t test/fib.nas + ./nasal -op -t -d test/fib.nas ./nasal -op -e test/filesystem.nas - ./nasal -op -e test/hexdump.nas + ./nasal -op -e -d test/hexdump.nas ./nasal -op -e test/json.nas ./nasal -op -e test/leetcode1319.nas - ./nasal -op -e test/lexer.nas - ./nasal -op -e test/life.nas + ./nasal -op -e -d test/lexer.nas + ./nasal -op -e -d test/life.nas ./nasal -op -t test/loop.nas ./nasal -op -c test/mandel.nas - ./nasal -op -t test/mandelbrot.nas + ./nasal -op -t -d test/mandelbrot.nas ./nasal -op -c test/module_test.nas ./nasal -op -e test/nasal_test.nas - ./nasal -op -t test/pi.nas - ./nasal -op -t test/prime.nas - ./nasal -op -t test/quick_sort.nas + ./nasal -op -t -d test/pi.nas + ./nasal -op -t -d test/prime.nas + ./nasal -op -t -d test/quick_sort.nas ./nasal -op -e test/scalar.nas ./nasal -op -e test/trait.nas - ./nasal -op -t test/turingmachine.nas - ./nasal -op -t test/ycombinator.nas + ./nasal -op -t -d test/turingmachine.nas + ./nasal -op -t -d test/ycombinator.nas \ No newline at end of file diff --git a/nasal_gc.h b/nasal_gc.h index cfb268d..39eefb6 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -31,7 +31,7 @@ const uint32_t increment[vm_type_size]= /* gc object */ 256, // vm_str 512, // vm_func - 1024,// vm_vec + 512, // vm_vec 128, // vm_hash 16 // vm_obj }; @@ -322,10 +322,13 @@ struct nasal_gc std::vector memory; // gc memory std::queue free_list[vm_type_size]; // gc free list std::vector local; + size_t size[vm_type_size]; + uint64_t count[vm_type_size]; void mark(); void sweep(); void init(const std::vector&); void clear(); + void info(); nasal_ref alloc(const uint8_t); nasal_ref builtin_alloc(const uint8_t); }; @@ -386,6 +389,11 @@ void nasal_gc::sweep() } void nasal_gc::init(const std::vector& s) { + for(uint8_t i=0;imark=GC_UNCOLLECTED; free_list[type].pop(); @@ -442,12 +468,15 @@ nasal_ref nasal_gc::builtin_alloc(uint8_t type) // and the value got before will be collected,this is a fatal error // so use builtin_alloc in builtin functions if this function uses alloc more then one time if(free_list[type].empty()) + { + size[type]+=increment[type]; for(uint32_t i=0;imark=GC_UNCOLLECTED; free_list[type].pop(); diff --git a/nasal_vm.h b/nasal_vm.h index f166312..66bf7de 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -886,6 +886,8 @@ vmexit: die("stack overflow"); if(opcnt) opcallsort(count); + if(detail_info) + gc.info(); gc.clear(); imm.clear(); return;