add garbage collector and memory allocator info. use '-d' to activate this function.

This commit is contained in:
ValKmjolnir 2022-02-08 23:40:52 +08:00
parent a09c6ae2f9
commit 24a1e39ad3
4 changed files with 45 additions and 13 deletions

View File

@ -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"

View File

@ -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

View File

@ -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<nasal_val*> memory; // gc memory
std::queue<nasal_val*> free_list[vm_type_size]; // gc free list
std::vector<nasal_ref> local;
size_t size[vm_type_size];
uint64_t count[vm_type_size];
void mark();
void sweep();
void init(const std::vector<std::string>&);
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<std::string>& s)
{
for(uint8_t i=0;i<vm_type_size;++i)
{
size[i]=increment[i];
count[i]=0;
}
for(uint8_t i=vm_str;i<vm_type_size;++i)
for(uint32_t j=0;j<increment[i];++j)
{
@ -416,20 +424,38 @@ void nasal_gc::clear()
delete i.value.gcobj;
strs.clear();
}
void nasal_gc::info()
{
const char* name[]={
"null","cnt ","ret ",
"nil ","num ","str ",
"func","vec ","hash","obj "
};
std::cout<<"\ngarbage collector info:\n";
for(uint8_t i=vm_str;i<vm_type_size;++i)
std::cout<<name[i]<<" | "<<count[i]<<"\n";
std::cout<<"\nmemory allocator info(max size):\n";
for(uint8_t i=vm_str;i<vm_type_size;++i)
std::cout<<name[i]<<" | "<<size[i]<<"\n";
}
nasal_ref nasal_gc::alloc(uint8_t type)
{
if(free_list[type].empty())
{
++count[type];
mark();
sweep();
}
if(free_list[type].empty())
{
size[type]+=increment[type];
for(uint32_t i=0;i<increment[type];++i)
{
nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp);
free_list[type].push(tmp);
}
}
nasal_ref ret={type,free_list[type].front()};
ret.value.gcobj->mark=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;i<increment[type];++i)
{
nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp);
free_list[type].push(tmp);
}
}
nasal_ref ret={type,free_list[type].front()};
ret.value.gcobj->mark=GC_UNCOLLECTED;
free_list[type].pop();

View File

@ -886,6 +886,8 @@ vmexit:
die("stack overflow");
if(opcnt)
opcallsort(count);
if(detail_info)
gc.info();
gc.clear();
imm.clear();
return;