shaped up vm profiling by tracking the number of method cache hits/misses

This commit is contained in:
hyunghwan.chung 2019-09-27 03:41:03 +00:00
parent 9d74ec842f
commit c0b8048081
2 changed files with 40 additions and 17 deletions

View File

@ -1866,7 +1866,9 @@ moo_oop_method_t moo_findmethod (moo_t* moo, moo_oop_t receiver, moo_oop_char_t
if (mcitm->receiver_class == c && mcitm->selector == selector /*&& mcitm->method_type == mth_type*/) if (mcitm->receiver_class == c && mcitm->selector == selector /*&& mcitm->method_type == mth_type*/)
{ {
/* cache hit */ /* cache hit */
/* TODO: moo->method_cache_hits++; */ #if defined(MOO_PROFILE_VM)
moo->stat.method_cache_hits++;
#endif
return mcitm->method; return mcitm->method;
} }
@ -1874,7 +1876,9 @@ moo_oop_method_t moo_findmethod (moo_t* moo, moo_oop_t receiver, moo_oop_char_t
mth = find_method_in_class_chain(moo, c, mth_type, &message); mth = find_method_in_class_chain(moo, c, mth_type, &message);
if (mth) if (mth)
{ {
/* TODO: moo->method_cache_misses++; */ #if defined(MOO_PROFILE_VM)
moo->stat.method_cache_misses++;
#endif
mcitm->receiver_class = c; mcitm->receiver_class = c;
mcitm->selector = selector; mcitm->selector = selector;
/*mcitm->method_type = mth_type;*/ /*mcitm->method_type = mth_type;*/
@ -1899,14 +1903,18 @@ not_found:
if (mcitm->receiver_class == _class && mcitm->selector == selector /*&& mcitm->method_type == MOO_METHOD_INSTANCE*/) if (mcitm->receiver_class == _class && mcitm->selector == selector /*&& mcitm->method_type == MOO_METHOD_INSTANCE*/)
{ {
/* cache hit */ /* cache hit */
/* TODO: moo->method_cache_hits++; */ #if defined(MOO_PROFILE_VM)
moo->stat.method_cache_hits++;
#endif
return mcitm->method; return mcitm->method;
} }
mth = find_method_in_class(moo, _class, MOO_METHOD_INSTANCE, &message); mth = find_method_in_class(moo, _class, MOO_METHOD_INSTANCE, &message);
if (mth) if (mth)
{ {
/* TODO: moo->method_cache_misses++; */ #if defined(MOO_PROFILE_VM)
moo->stat.method_cache_misses++;
#endif
mcitm->receiver_class = c; mcitm->receiver_class = c;
mcitm->selector = selector; mcitm->selector = selector;
/*mcitm->method_type = MOO_METHOD_INSTANCE;*/ /*mcitm->method_type = MOO_METHOD_INSTANCE;*/
@ -2275,7 +2283,7 @@ static moo_pfrc_t pf_perform (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
MOO_STACK_POP (moo); MOO_STACK_POP (moo);
/* emulate message sending */ /* emulate message sending */
if (send_message (moo, (moo_oop_char_t)selector, nargs - 1, 0) <= -1) return MOO_PF_HARD_FAILURE; if (send_message(moo, (moo_oop_char_t)selector, nargs - 1, 0) <= -1) return MOO_PF_HARD_FAILURE;
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
@ -4873,6 +4881,10 @@ static int send_message (moo_t* moo, moo_oop_char_t selector, moo_ooi_t nargs, i
receiver = MOO_STACK_GET(moo, moo->sp - nargs); receiver = MOO_STACK_GET(moo, moo->sp - nargs);
#if defined(MOO_PROFILE_VM)
moo->stat.message_sends++;
#endif
method = moo_findmethod(moo, receiver, selector, to_super); method = moo_findmethod(moo, receiver, selector, to_super);
if (!method) if (!method)
{ {
@ -5515,7 +5527,7 @@ static int __execute (moo_t* moo)
bcode = FETCH_BYTE_CODE(moo); bcode = FETCH_BYTE_CODE(moo);
#if defined(MOO_PROFILE_VM) #if defined(MOO_PROFILE_VM)
moo->inst_counter++; moo->stat.inst_counter++;
#endif #endif
/* ==== DISPATCH TABLE ==== */ /* ==== DISPATCH TABLE ==== */
@ -6381,7 +6393,6 @@ static int __execute (moo_t* moo)
return 0; return 0;
} }
int moo_execute (moo_t* moo) int moo_execute (moo_t* moo)
{ {
int n; int n;
@ -6390,10 +6401,6 @@ int moo_execute (moo_t* moo)
log_default_type_mask = moo->log.default_type_mask; log_default_type_mask = moo->log.default_type_mask;
moo->log.default_type_mask |= MOO_LOG_VM; moo->log.default_type_mask |= MOO_LOG_VM;
#if defined(MOO_PROFILE_VM)
moo->inst_counter = 0;
#endif
if (vm_startup(moo) <= -1) return -1; if (vm_startup(moo) <= -1) return -1;
moo->proc_switched = 0; moo->proc_switched = 0;
@ -6403,10 +6410,6 @@ int moo_execute (moo_t* moo)
vm_cleanup (moo); vm_cleanup (moo);
#if defined(MOO_PROFILE_VM)
MOO_LOG1 (moo, MOO_LOG_IC | MOO_LOG_INFO, "TOTAL_INST_COUTNER = %zu\n", moo->inst_counter);
#endif
moo->log.default_type_mask = log_default_type_mask; moo->log.default_type_mask = log_default_type_mask;
return n; return n;
} }
@ -6424,6 +6427,13 @@ int moo_invoke (moo_t* moo, const moo_oocs_t* objname, const moo_oocs_t* mthname
MOO_ASSERT (moo, moo->active_context == MOO_NULL); MOO_ASSERT (moo, moo->active_context == MOO_NULL);
MOO_ASSERT (moo, moo->active_method == MOO_NULL); MOO_ASSERT (moo, moo->active_method == MOO_NULL);
#if defined(MOO_PROFILE_VM)
moo->stat.inst_counter = 0;
moo->stat.method_cache_hits = 0;
moo->stat.method_cache_misses = 0;
moo->stat.message_sends = 0;
#endif
moo_clearmethodcache (moo); moo_clearmethodcache (moo);
if (start_initial_process_and_context(moo, objname, mthname) <= -1) return -1; if (start_initial_process_and_context(moo, objname, mthname) <= -1) return -1;
@ -6435,6 +6445,13 @@ int moo_invoke (moo_t* moo, const moo_oocs_t* objname, const moo_oocs_t* mthname
moo->initial_context = MOO_NULL; moo->initial_context = MOO_NULL;
moo->active_context = MOO_NULL; moo->active_context = MOO_NULL;
moo->active_method = MOO_NULL; moo->active_method = MOO_NULL;
#if defined(MOO_PROFILE_VM)
MOO_LOG1 (moo, MOO_LOG_IC | MOO_LOG_INFO, "Total message sends: %zu\n", moo->stat.message_sends);
MOO_LOG2 (moo, MOO_LOG_IC | MOO_LOG_INFO, "Method cache - hits: %zu, misses: %zu\n", moo->stat.method_cache_hits, moo->stat.method_cache_misses);
MOO_LOG1 (moo, MOO_LOG_IC | MOO_LOG_INFO, "Total instructions: %zu\n", moo->stat.inst_counter);
#endif
return n; return n;
} }

View File

@ -1704,8 +1704,14 @@ struct moo_t
moo_method_cache_item_t method_cache[2][MOO_METHOD_CACHE_SIZE]; moo_method_cache_item_t method_cache[2][MOO_METHOD_CACHE_SIZE];
moo_uintmax_t inst_counter;
moo_ooi_t last_inst_pointer; moo_ooi_t last_inst_pointer;
struct
{
moo_oow_t method_cache_hits;
moo_oow_t method_cache_misses;
moo_oow_t message_sends;
moo_uintmax_t inst_counter;
} stat;
#if defined(MOO_INCLUDE_COMPILER) #if defined(MOO_INCLUDE_COMPILER)
moo_compiler_t* c; moo_compiler_t* c;