add garbage collector and memory allocator info. use '-d' to activate this function.
This commit is contained in:
parent
a09c6ae2f9
commit
24a1e39ad3
1
main.cpp
1
main.cpp
|
@ -32,6 +32,7 @@ void help()
|
||||||
<<" -t, --time | execute and get the running time.\n"
|
<<" -t, --time | execute and get the running time.\n"
|
||||||
<<" -o, --opcnt | execute and count used operands.\n"
|
<<" -o, --opcnt | execute and count used operands.\n"
|
||||||
<<" -d, --detail | execute and get detail crash info.\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"
|
<<" -op, --optimize| use optimizer(beta).\n"
|
||||||
<<" | if want to use -op and run, please use -op -e/-t/-o/-d.\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"
|
<<" -dbg, --debug | debug mode (this will ignore -t -o -d).\n"
|
||||||
|
|
24
makefile
24
makefile
|
@ -5,30 +5,30 @@ test:nasal
|
||||||
./nasal -op -e test/ascii-art.nas
|
./nasal -op -e test/ascii-art.nas
|
||||||
./nasal -op -c test/bf.nas
|
./nasal -op -c test/bf.nas
|
||||||
./nasal -op -c test/bfconvertor.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 -t test/bigloop.nas
|
||||||
./nasal -op -e test/bp.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/choice.nas
|
||||||
./nasal -op -e test/class.nas
|
./nasal -op -e test/class.nas
|
||||||
./nasal -op -c test/exception.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/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/json.nas
|
||||||
./nasal -op -e test/leetcode1319.nas
|
./nasal -op -e test/leetcode1319.nas
|
||||||
./nasal -op -e test/lexer.nas
|
./nasal -op -e -d test/lexer.nas
|
||||||
./nasal -op -e test/life.nas
|
./nasal -op -e -d test/life.nas
|
||||||
./nasal -op -t test/loop.nas
|
./nasal -op -t test/loop.nas
|
||||||
./nasal -op -c test/mandel.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 -c test/module_test.nas
|
||||||
./nasal -op -e test/nasal_test.nas
|
./nasal -op -e test/nasal_test.nas
|
||||||
./nasal -op -t test/pi.nas
|
./nasal -op -t -d test/pi.nas
|
||||||
./nasal -op -t test/prime.nas
|
./nasal -op -t -d test/prime.nas
|
||||||
./nasal -op -t test/quick_sort.nas
|
./nasal -op -t -d test/quick_sort.nas
|
||||||
./nasal -op -e test/scalar.nas
|
./nasal -op -e test/scalar.nas
|
||||||
./nasal -op -e test/trait.nas
|
./nasal -op -e test/trait.nas
|
||||||
./nasal -op -t test/turingmachine.nas
|
./nasal -op -t -d test/turingmachine.nas
|
||||||
./nasal -op -t test/ycombinator.nas
|
./nasal -op -t -d test/ycombinator.nas
|
||||||
|
|
31
nasal_gc.h
31
nasal_gc.h
|
@ -31,7 +31,7 @@ const uint32_t increment[vm_type_size]=
|
||||||
/* gc object */
|
/* gc object */
|
||||||
256, // vm_str
|
256, // vm_str
|
||||||
512, // vm_func
|
512, // vm_func
|
||||||
1024,// vm_vec
|
512, // vm_vec
|
||||||
128, // vm_hash
|
128, // vm_hash
|
||||||
16 // vm_obj
|
16 // vm_obj
|
||||||
};
|
};
|
||||||
|
@ -322,10 +322,13 @@ struct nasal_gc
|
||||||
std::vector<nasal_val*> memory; // gc memory
|
std::vector<nasal_val*> memory; // gc memory
|
||||||
std::queue<nasal_val*> free_list[vm_type_size]; // gc free list
|
std::queue<nasal_val*> free_list[vm_type_size]; // gc free list
|
||||||
std::vector<nasal_ref> local;
|
std::vector<nasal_ref> local;
|
||||||
|
size_t size[vm_type_size];
|
||||||
|
uint64_t count[vm_type_size];
|
||||||
void mark();
|
void mark();
|
||||||
void sweep();
|
void sweep();
|
||||||
void init(const std::vector<std::string>&);
|
void init(const std::vector<std::string>&);
|
||||||
void clear();
|
void clear();
|
||||||
|
void info();
|
||||||
nasal_ref alloc(const uint8_t);
|
nasal_ref alloc(const uint8_t);
|
||||||
nasal_ref builtin_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)
|
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(uint8_t i=vm_str;i<vm_type_size;++i)
|
||||||
for(uint32_t j=0;j<increment[i];++j)
|
for(uint32_t j=0;j<increment[i];++j)
|
||||||
{
|
{
|
||||||
|
@ -416,20 +424,38 @@ void nasal_gc::clear()
|
||||||
delete i.value.gcobj;
|
delete i.value.gcobj;
|
||||||
strs.clear();
|
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)
|
nasal_ref nasal_gc::alloc(uint8_t type)
|
||||||
{
|
{
|
||||||
if(free_list[type].empty())
|
if(free_list[type].empty())
|
||||||
{
|
{
|
||||||
|
++count[type];
|
||||||
mark();
|
mark();
|
||||||
sweep();
|
sweep();
|
||||||
}
|
}
|
||||||
if(free_list[type].empty())
|
if(free_list[type].empty())
|
||||||
|
{
|
||||||
|
size[type]+=increment[type];
|
||||||
for(uint32_t i=0;i<increment[type];++i)
|
for(uint32_t i=0;i<increment[type];++i)
|
||||||
{
|
{
|
||||||
nasal_val* tmp=new nasal_val(type);
|
nasal_val* tmp=new nasal_val(type);
|
||||||
memory.push_back(tmp);
|
memory.push_back(tmp);
|
||||||
free_list[type].push(tmp);
|
free_list[type].push(tmp);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
nasal_ref ret={type,free_list[type].front()};
|
nasal_ref ret={type,free_list[type].front()};
|
||||||
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
||||||
free_list[type].pop();
|
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
|
// 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
|
// so use builtin_alloc in builtin functions if this function uses alloc more then one time
|
||||||
if(free_list[type].empty())
|
if(free_list[type].empty())
|
||||||
|
{
|
||||||
|
size[type]+=increment[type];
|
||||||
for(uint32_t i=0;i<increment[type];++i)
|
for(uint32_t i=0;i<increment[type];++i)
|
||||||
{
|
{
|
||||||
nasal_val* tmp=new nasal_val(type);
|
nasal_val* tmp=new nasal_val(type);
|
||||||
memory.push_back(tmp);
|
memory.push_back(tmp);
|
||||||
free_list[type].push(tmp);
|
free_list[type].push(tmp);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
nasal_ref ret={type,free_list[type].front()};
|
nasal_ref ret={type,free_list[type].front()};
|
||||||
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
||||||
free_list[type].pop();
|
free_list[type].pop();
|
||||||
|
|
|
@ -886,6 +886,8 @@ vmexit:
|
||||||
die("stack overflow");
|
die("stack overflow");
|
||||||
if(opcnt)
|
if(opcnt)
|
||||||
opcallsort(count);
|
opcallsort(count);
|
||||||
|
if(detail_info)
|
||||||
|
gc.info();
|
||||||
gc.clear();
|
gc.clear();
|
||||||
imm.clear();
|
imm.clear();
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue