added some code for gc profiling

This commit is contained in:
hyunghwan.chung 2020-11-29 16:12:28 +00:00
parent 7d7bedc2bb
commit 26631d231c
6 changed files with 116 additions and 25 deletions

View File

@ -6355,11 +6355,19 @@ int moo_execute (moo_t* moo)
moo->proc_switched = 0; moo->proc_switched = 0;
moo->abort_req = 0; moo->abort_req = 0;
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
moo->gci.lazy_sweep = 1; /* TODO: make it configurable?? */ moo->gci.lazy_sweep = 1; /* TODO: make it configurable?? */
MOO_INIT_NTIME (&moo->gci.stat.alloc, 0, 0);
MOO_INIT_NTIME (&moo->gci.stat.mark, 0, 0);
MOO_INIT_NTIME (&moo->gci.stat.sweep, 0, 0);
#endif
n = __execute (moo); n = __execute (moo);
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
moo->gci.lazy_sweep = 0; moo->gci.lazy_sweep = 0;
#endif
vm_cleanup (moo); vm_cleanup (moo);
@ -6446,6 +6454,9 @@ int moo_invoke (moo_t* moo, const moo_oocs_t* objname, const moo_oocs_t* mthname
MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "GC - gci.bsz: %zu, gci.stack.max: %zu\n", moo->gci.bsz, moo->gci.stack.max); MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "GC - gci.bsz: %zu, gci.stack.max: %zu\n", moo->gci.bsz, moo->gci.stack.max);
if (moo->heap->xma) moo_xma_dump (moo->heap->xma, xma_dumper, moo); if (moo->heap->xma) moo_xma_dump (moo->heap->xma, xma_dumper, moo);
} }
MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "GC - gci.stat.alloc: %ld.%09u\n", (unsigned long int)moo->gci.stat.alloc.sec, (unsigned int)moo->gci.stat.alloc.nsec);
MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "GC - gci.stat.mark: %ld.%09u\n", (unsigned long int)moo->gci.stat.mark.sec, (unsigned int)moo->gci.stat.mark.nsec);
MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "GC - gci.stat.sweep: %ld.%09u\n", (unsigned long int)moo->gci.stat.sweep.sec, (unsigned int)moo->gci.stat.sweep.nsec);
#endif #endif
#endif #endif

View File

@ -26,6 +26,10 @@
#include "moo-prv.h" #include "moo-prv.h"
#if defined(MOO_PROFILE_VM)
#include <sys/time.h>
#include <sys/resource.h> /* getrusage */
#endif
/* /*
* Apex...................... * Apex......................
@ -903,6 +907,13 @@ static MOO_INLINE void gc_ms_mark_roots (moo_t* moo)
moo_oow_t i, gcfin_count; moo_oow_t i, gcfin_count;
moo_evtcb_t* cb; moo_evtcb_t* cb;
#if defined(MOO_PROFILE_VM)
struct rusage ru;
moo_ntime_t rut;
getrusage(RUSAGE_SELF, &ru);
MOO_INIT_NTIME (&rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
#endif
if (moo->processor && moo->processor->active) if (moo->processor && moo->processor->active)
{ {
MOO_ASSERT (moo, (moo_oop_t)moo->processor != moo->_nil); MOO_ASSERT (moo, (moo_oop_t)moo->processor != moo->_nil);
@ -991,6 +1002,13 @@ static MOO_INLINE void gc_ms_mark_roots (moo_t* moo)
/* invalidate method cache. TODO: GCing entries on the method cache is also one way instead of full invalidation */ /* invalidate method cache. TODO: GCing entries on the method cache is also one way instead of full invalidation */
moo_clearmethodcache (moo); moo_clearmethodcache (moo);
#if defined(MOO_PROFILE_VM)
getrusage(RUSAGE_SELF, &ru);
MOO_SUB_NTIME_SNS (&rut, &rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
MOO_SUB_NTIME (&moo->gci.stat.mark, &moo->gci.stat.mark, &rut); /* do subtraction because rut is negative */
#endif
} }
void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize)
@ -999,7 +1017,14 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize)
moo_oop_t obj; moo_oop_t obj;
moo_oow_t freed_size; moo_oow_t freed_size;
if (!moo->gci.ls.curr) return; #if defined(MOO_PROFILE_VM)
struct rusage ru;
moo_ntime_t rut;
getrusage(RUSAGE_SELF, &ru);
MOO_INIT_NTIME (&rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
#endif
if (!moo->gci.ls.curr) goto done;
freed_size = 0; freed_size = 0;
@ -1028,11 +1053,12 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize)
moo->gci.bsz -= objsize; moo->gci.bsz -= objsize;
moo_freeheapmem (moo, moo->heap, curr); /* destroy */ moo_freeheapmem (moo, moo->heap, curr); /* destroy */
if (freed_size > allocsize) /* TODO: can it secure large enough space? */ /*if (freed_size > allocsize)*/ /* TODO: can it secure large enough space? */
if (objsize == allocsize)
{ {
moo->gci.ls.prev = prev; moo->gci.ls.prev = prev;
moo->gci.ls.curr = next; /* let the next lazy sweeping begin at this point */ moo->gci.ls.curr = next; /* let the next lazy sweeping begin at this point */
return; goto done;
} }
} }
@ -1040,6 +1066,14 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize)
} }
moo->gci.ls.curr = MOO_NULL; moo->gci.ls.curr = MOO_NULL;
done:
#if defined(MOO_PROFILE_VM)
getrusage(RUSAGE_SELF, &ru);
MOO_SUB_NTIME_SNS (&rut, &rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
MOO_SUB_NTIME (&moo->gci.stat.sweep, &moo->gci.stat.sweep, &rut); /* do subtraction because rut is negative */
#endif
return;
} }
static MOO_INLINE void gc_ms_sweep (moo_t* moo) static MOO_INLINE void gc_ms_sweep (moo_t* moo)
@ -1047,6 +1081,13 @@ static MOO_INLINE void gc_ms_sweep (moo_t* moo)
moo_gchdr_t* curr, * next, * prev; moo_gchdr_t* curr, * next, * prev;
moo_oop_t obj; moo_oop_t obj;
#if defined(MOO_PROFILE_VM)
struct rusage ru;
moo_ntime_t rut;
getrusage(RUSAGE_SELF, &ru);
MOO_INIT_NTIME (&rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
#endif
prev = MOO_NULL; prev = MOO_NULL;
curr = moo->gci.b; curr = moo->gci.b;
while (curr) while (curr)
@ -1072,6 +1113,12 @@ static MOO_INLINE void gc_ms_sweep (moo_t* moo)
} }
moo->gci.ls.curr = MOO_NULL; moo->gci.ls.curr = MOO_NULL;
#if defined(MOO_PROFILE_VM)
getrusage(RUSAGE_SELF, &ru);
MOO_SUB_NTIME_SNS (&rut, &rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
MOO_SUB_NTIME (&moo->gci.stat.sweep, &moo->gci.stat.sweep, &rut); /* do subtraction because rut is negative */
#endif
} }
#endif #endif

View File

@ -145,20 +145,6 @@ void* moo_allocheapspace (moo_t* moo, moo_space_t* space, moo_oow_t size)
return ptr; return ptr;
} }
void* moo_allocheapmem (moo_t* moo, moo_heap_t* heap, moo_oow_t size)
{
void* ptr;
MOO_ASSERT (moo, moo->gc_type == MOO_GC_TYPE_MARK_SWEEP);
ptr = MOO_MMGR_ALLOC(&heap->xmmgr, size);
if (MOO_UNLIKELY(!ptr))
{
MOO_DEBUG2 (moo, "Cannot allocate %zd bytes from heap - ptr %p\n", size, heap);
moo_seterrnum (moo, MOO_EOOMEM);
}
return ptr;
}
void* moo_callocheapmem (moo_t* moo, moo_heap_t* heap, moo_oow_t size) void* moo_callocheapmem (moo_t* moo, moo_heap_t* heap, moo_oow_t size)
{ {
void* ptr; void* ptr;
@ -177,6 +163,15 @@ void* moo_callocheapmem (moo_t* moo, moo_heap_t* heap, moo_oow_t size)
return ptr; return ptr;
} }
void* moo_callocheapmem_noerr (moo_t* moo, moo_heap_t* heap, moo_oow_t size)
{
void* ptr;
MOO_ASSERT (moo, moo->gc_type == MOO_GC_TYPE_MARK_SWEEP);
ptr = MOO_MMGR_ALLOC(&heap->xmmgr, size);
if (MOO_LIKELY(ptr)) MOO_MEMSET (ptr, 0, size);
return ptr;
}
void moo_freeheapmem (moo_t* moo, moo_heap_t* heap, void* ptr) void moo_freeheapmem (moo_t* moo, moo_heap_t* heap, void* ptr)
{ {
MOO_ASSERT (moo, moo->gc_type == MOO_GC_TYPE_MARK_SWEEP); MOO_ASSERT (moo, moo->gc_type == MOO_GC_TYPE_MARK_SWEEP);

View File

@ -1120,17 +1120,17 @@ void* moo_allocheapspace (
moo_oow_t size moo_oow_t size
); );
/** /**
* The moo_allocheapmem(0 function allocates \a size bytes from the given heap. * The moo_allocheapmem() function allocates \a size bytes from the given heap
* and clears it with zeros.
*/ */
void* moo_allocheapmem ( void* moo_callocheapmem (
moo_t* moo, moo_t* moo,
moo_heap_t* heap, moo_heap_t* heap,
moo_oow_t size moo_oow_t size
); );
void* moo_callocheapmem ( void* moo_callocheapmem_noerr (
moo_t* moo, moo_t* moo,
moo_heap_t* heap, moo_heap_t* heap,
moo_oow_t size moo_oow_t size
@ -1141,6 +1141,7 @@ void moo_freeheapmem (
moo_heap_t* heap, moo_heap_t* heap,
void* ptr void* ptr
); );
/* ========================================================================= */ /* ========================================================================= */
/* obj.c */ /* obj.c */
/* ========================================================================= */ /* ========================================================================= */
@ -1277,6 +1278,9 @@ void moo_deregallfinalizables (moo_t* moo);
moo_oow_t moo_getobjpayloadbytes (moo_t* moo, moo_oop_t oop); moo_oow_t moo_getobjpayloadbytes (moo_t* moo, moo_oop_t oop);
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize);
#endif
/* ========================================================================= */ /* ========================================================================= */
/* bigint.c */ /* bigint.c */
/* ========================================================================= */ /* ========================================================================= */

View File

@ -1832,6 +1832,13 @@ struct moo_t
moo_oow_t len; moo_oow_t len;
moo_oow_t max; moo_oow_t max;
} stack; } stack;
struct
{
moo_ntime_t alloc;
moo_ntime_t mark;
moo_ntime_t sweep;
} stat;
} gci; } gci;
#endif #endif

View File

@ -26,6 +26,11 @@
#include "moo-prv.h" #include "moo-prv.h"
#if defined(MOO_PROFILE_VM)
#include <sys/time.h>
#include <sys/resource.h> /* getrusage */
#endif
void* moo_allocbytes (moo_t* moo, moo_oow_t size) void* moo_allocbytes (moo_t* moo, moo_oow_t size)
{ {
#if defined(MOO_BUILD_DEBUG) #if defined(MOO_BUILD_DEBUG)
@ -38,6 +43,13 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size)
{ {
moo_gchdr_t* gch; moo_gchdr_t* gch;
#if defined(MOO_PROFILE_VM)
struct rusage ru;
moo_ntime_t rut;
getrusage(RUSAGE_SELF, &ru);
MOO_INIT_NTIME (&rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
#endif
if (MOO_UNLIKELY(moo->igniting)) if (MOO_UNLIKELY(moo->igniting))
{ {
gch = (moo_gchdr_t*)moo_callocheapmem(moo, moo->heap, MOO_SIZEOF(*gch) + size); gch = (moo_gchdr_t*)moo_callocheapmem(moo, moo->heap, MOO_SIZEOF(*gch) + size);
@ -46,25 +58,32 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size)
else else
{ {
moo_oow_t allocsize; moo_oow_t allocsize;
int gc_called = 0;
allocsize = MOO_SIZEOF(*gch) + size;
if (moo->gci.bsz >= moo->gci.threshold) if (moo->gci.bsz >= moo->gci.threshold)
{ {
moo_gc (moo, 0); moo_gc (moo, 0);
moo->gci.threshold = moo->gci.bsz + 100000; /* TODO: change this fomula */ moo->gci.threshold = moo->gci.bsz + 100000; /* TODO: change this fomula */
gc_called = 1;
} }
allocsize = MOO_SIZEOF(*gch) + size;
if (moo->gci.lazy_sweep) moo_gc_ms_sweep_lazy (moo, allocsize); if (moo->gci.lazy_sweep) moo_gc_ms_sweep_lazy (moo, allocsize);
gch = (moo_gchdr_t*)moo_callocheapmem(moo, moo->heap, allocsize); gch = (moo_gchdr_t*)moo_callocheapmem_noerr(moo, moo->heap, allocsize);
if (!gch && moo->errnum == MOO_EOOMEM && !(moo->option.trait & MOO_TRAIT_NOGC)) if (!gch)
{ {
if (MOO_UNLIKELY(moo->option.trait & MOO_TRAIT_NOGC)) goto calloc_heapmem_fail;
if (gc_called) goto sweep_the_rest;
moo_gc (moo, 0); moo_gc (moo, 0);
if (moo->gci.lazy_sweep) moo_gc_ms_sweep_lazy (moo, allocsize); if (moo->gci.lazy_sweep) moo_gc_ms_sweep_lazy (moo, allocsize);
gch = (moo_gchdr_t*)moo_callocheapmem(moo, moo->heap, allocsize); gch = (moo_gchdr_t*)moo_callocheapmem_noerr(moo, moo->heap, allocsize);
if (MOO_UNLIKELY(!gch)) if (MOO_UNLIKELY(!gch))
{ {
sweep_the_rest:
if (moo->gci.lazy_sweep) if (moo->gci.lazy_sweep)
{ {
moo_gc_ms_sweep_lazy (moo, MOO_TYPE_MAX(moo_oow_t)); /* sweep the rest */ moo_gc_ms_sweep_lazy (moo, MOO_TYPE_MAX(moo_oow_t)); /* sweep the rest */
@ -73,6 +92,8 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size)
} }
else else
{ {
calloc_heapmem_fail:
moo_seterrnum (moo, MOO_EOOMEM);
return MOO_NULL; return MOO_NULL;
} }
} }
@ -91,6 +112,12 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size)
moo->gci.b = gch; moo->gci.b = gch;
moo->gci.bsz += size; moo->gci.bsz += size;
#if defined(MOO_PROFILE_VM)
getrusage(RUSAGE_SELF, &ru);
MOO_SUB_NTIME_SNS (&rut, &rut, ru.ru_utime.tv_sec, MOO_USEC_TO_NSEC(ru.ru_utime.tv_usec));
MOO_SUB_NTIME (&moo->gci.stat.alloc, &moo->gci.stat.alloc, &rut); /* do subtraction because rut is negative */
#endif
return (moo_uint8_t*)(gch + 1); return (moo_uint8_t*)(gch + 1);
} }
else else