From 26631d231c08bafe3b708ca3495d443cda945b7d Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 29 Nov 2020 16:12:28 +0000 Subject: [PATCH] added some code for gc profiling --- moo/lib/exec.c | 11 ++++++++++ moo/lib/gc.c | 53 ++++++++++++++++++++++++++++++++++++++++++++--- moo/lib/heap.c | 23 ++++++++------------ moo/lib/moo-prv.h | 12 +++++++---- moo/lib/moo.h | 7 +++++++ moo/lib/obj.c | 35 +++++++++++++++++++++++++++---- 6 files changed, 116 insertions(+), 25 deletions(-) diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 3c8bf60..5050c8c 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -6355,11 +6355,19 @@ int moo_execute (moo_t* moo) moo->proc_switched = 0; moo->abort_req = 0; + +#if defined(MOO_ENABLE_GC_MARK_SWEEP) 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); +#if defined(MOO_ENABLE_GC_MARK_SWEEP) moo->gci.lazy_sweep = 0; +#endif 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); 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 diff --git a/moo/lib/gc.c b/moo/lib/gc.c index c0eace3..54f3c6c 100644 --- a/moo/lib/gc.c +++ b/moo/lib/gc.c @@ -26,6 +26,10 @@ #include "moo-prv.h" +#if defined(MOO_PROFILE_VM) +#include +#include /* getrusage */ +#endif /* * Apex...................... @@ -903,6 +907,13 @@ static MOO_INLINE void gc_ms_mark_roots (moo_t* moo) moo_oow_t i, gcfin_count; 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) { 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 */ 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) @@ -999,7 +1017,14 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) moo_oop_t obj; 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; @@ -1028,11 +1053,12 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) moo->gci.bsz -= objsize; 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.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; + +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) @@ -1047,6 +1081,13 @@ static MOO_INLINE void gc_ms_sweep (moo_t* moo) moo_gchdr_t* curr, * next, * prev; 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; curr = moo->gci.b; while (curr) @@ -1072,6 +1113,12 @@ static MOO_INLINE void gc_ms_sweep (moo_t* moo) } 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 diff --git a/moo/lib/heap.c b/moo/lib/heap.c index f5552d0..7e1689e 100644 --- a/moo/lib/heap.c +++ b/moo/lib/heap.c @@ -145,20 +145,6 @@ void* moo_allocheapspace (moo_t* moo, moo_space_t* space, moo_oow_t size) 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* ptr; @@ -177,6 +163,15 @@ void* moo_callocheapmem (moo_t* moo, moo_heap_t* heap, moo_oow_t size) 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) { MOO_ASSERT (moo, moo->gc_type == MOO_GC_TYPE_MARK_SWEEP); diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index c46aae6..b6b2f23 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1120,17 +1120,17 @@ void* moo_allocheapspace ( 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_heap_t* heap, moo_oow_t size ); -void* moo_callocheapmem ( +void* moo_callocheapmem_noerr ( moo_t* moo, moo_heap_t* heap, moo_oow_t size @@ -1141,6 +1141,7 @@ void moo_freeheapmem ( moo_heap_t* heap, void* ptr ); + /* ========================================================================= */ /* obj.c */ /* ========================================================================= */ @@ -1277,6 +1278,9 @@ void moo_deregallfinalizables (moo_t* moo); 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 */ /* ========================================================================= */ diff --git a/moo/lib/moo.h b/moo/lib/moo.h index dfe6643..2f7f99f 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1832,6 +1832,13 @@ struct moo_t moo_oow_t len; moo_oow_t max; } stack; + + struct + { + moo_ntime_t alloc; + moo_ntime_t mark; + moo_ntime_t sweep; + } stat; } gci; #endif diff --git a/moo/lib/obj.c b/moo/lib/obj.c index b2ccf3c..8d42f55 100644 --- a/moo/lib/obj.c +++ b/moo/lib/obj.c @@ -26,6 +26,11 @@ #include "moo-prv.h" +#if defined(MOO_PROFILE_VM) +#include +#include /* getrusage */ +#endif + void* moo_allocbytes (moo_t* moo, moo_oow_t size) { #if defined(MOO_BUILD_DEBUG) @@ -38,6 +43,13 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size) { 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)) { 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 { moo_oow_t allocsize; + int gc_called = 0; + + allocsize = MOO_SIZEOF(*gch) + size; if (moo->gci.bsz >= moo->gci.threshold) { moo_gc (moo, 0); 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); - gch = (moo_gchdr_t*)moo_callocheapmem(moo, moo->heap, allocsize); - if (!gch && moo->errnum == MOO_EOOMEM && !(moo->option.trait & MOO_TRAIT_NOGC)) + gch = (moo_gchdr_t*)moo_callocheapmem_noerr(moo, moo->heap, allocsize); + 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); 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)) { + sweep_the_rest: if (moo->gci.lazy_sweep) { 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 { + calloc_heapmem_fail: + moo_seterrnum (moo, MOO_EOOMEM); return MOO_NULL; } } @@ -91,6 +112,12 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size) moo->gci.b = gch; 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); } else