Skip to content

Commit

Permalink
another API fetch funciton execution time by name
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us committed Jan 11, 2024
1 parent 018658d commit 5cf3f2d
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 47 deletions.
21 changes: 21 additions & 0 deletions core/iwasm/aot/aot_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,27 @@ aot_summarize_wasm_execute_time(const AOTModuleInstance *inst)

return ret;
}

double
aot_get_wasm_func_exec_time(const AOTModuleInstance *inst,
const char *func_name)
{
AOTModule *module = (AOTModule *)inst->module;
uint32 total_func_count = module->import_func_count + module->func_count, i;

for (i = 0; i < total_func_count; i++) {
const char *name_in_wasm = get_func_name_from_index(inst, i);
if (name_in_wasm
&& strncmp(func_name, name_in_wasm, strlen(func_name)) == 0) {
AOTFuncPerfProfInfo *perf_prof =
(AOTFuncPerfProfInfo *)inst->func_perf_profilings + i;
return (perf_prof->total_exec_time - perf_prof->children_exec_time)
/ 1000.0f;
}
}

return -1.0;
}
#endif /* end of WASM_ENABLE_PERF_PROFILING */

#if WASM_ENABLE_STATIC_PGO != 0
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ aot_dump_perf_profiling(const AOTModuleInstance *module_inst);
double
aot_summarize_wasm_execute_time(const AOTModuleInstance *inst);

double
aot_get_wasm_func_exec_time(const AOTModuleInstance *inst,
const char *func_name);

const uint8 *
aot_get_custom_section(const AOTModule *module, const char *name, uint32 *len);

Expand Down
23 changes: 21 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
}

double
wasm_runtime_summarize_wasm_execute_time(WASMModuleInstanceCommon *inst)
wasm_runtime_sum_wasm_exec_time(WASMModuleInstanceCommon *inst)
{
#if WASM_ENABLE_INTERP != 0
if (inst->module_type == Wasm_Module_Bytecode)
Expand All @@ -1702,7 +1702,26 @@ wasm_runtime_summarize_wasm_execute_time(WASMModuleInstanceCommon *inst)
return aot_summarize_wasm_execute_time((AOTModuleInstance *)inst);
#endif

return 0;
return 0.0;
}

double
wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst,
const char *func_name)
{
#if WASM_ENABLE_INTERP != 0
if (inst->module_type == Wasm_Module_Bytecode)
return wasm_get_wasm_func_exec_time((WASMModuleInstance *)inst,
func_name);
#endif

#if WASM_ENABLE_AOT != 0
if (inst->module_type == Wasm_Module_AoT)
return aot_get_wasm_func_exec_time((AOTModuleInstance *)inst,
func_name);
#endif

return 0.0;
}
#endif /* WASM_ENABLE_PERF_PROFILING != 0 */

Expand Down
6 changes: 6 additions & 0 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,12 @@ void
wasm_runtime_set_linux_perf(bool flag);
#endif

#if WASM_ENABLE_PERF_PROFILING != 0
double
wasm_runtime_get_wasm_func_exec_time(WASMModuleInstanceCommon *inst,
const char *func_name);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ wasm_runtime_dump_perf_profiling(wasm_module_inst_t module_inst);
* @param module_inst the WASM module instance to profile
*/
WASM_RUNTIME_API_EXTERN double
wasm_runtime_summarize_wasm_execute_time(wasm_module_inst_t module_inst);
wasm_runtime_sum_wasm_exec_time(wasm_module_inst_t module_inst);

/* wasm thread callback function type */
typedef void *(*wasm_thread_callback_t)(wasm_exec_env_t, void *);
Expand Down
96 changes: 52 additions & 44 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2398,14 +2398,45 @@ wasm_call_function(WASMExecEnv *exec_env, WASMFunctionInstance *function,
return !wasm_copy_exception(module_inst, NULL);
}

/* look for the function name */
static char *
get_func_name_from_index(const WASMModuleInstance *inst, uint32 func_index)
{
char *func_name;
WASMFunctionInstance *func_inst = inst->e->functions + func_index;

if (func_inst->is_import_func) {
func_name = func_inst->u.func_import->field_name;
}
else {
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
func_name = func_inst->u.func->field_name;
#endif
/* if custom name section is not generated,
search symbols from export table */
if (!func_name) {
unsigned j;
for (j = 0; j < inst->export_func_count; j++) {
WASMExportFuncInstance *export_func =
inst->export_functions + j;
if (export_func->function == func_inst) {
func_name = export_func->name;
break;
}
}
}
}

return func_name;
}

#if WASM_ENABLE_PERF_PROFILING != 0
void
wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
{
WASMExportFuncInstance *export_func;
WASMFunctionInstance *func_inst;
char *func_name;
uint32 i, j;
uint32 i;

os_printf("Performance profiler data:\n");
for (i = 0; i < module_inst->e->function_count; i++) {
Expand All @@ -2414,25 +2445,7 @@ wasm_dump_perf_profiling(const WASMModuleInstance *module_inst)
if (func_inst->total_exec_cnt == 0)
continue;

if (func_inst->is_import_func) {
func_name = func_inst->u.func_import->field_name;
}
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
else if (func_inst->u.func->field_name) {
func_name = func_inst->u.func->field_name;
}
#endif
else {
func_name = NULL;
for (j = 0; j < module_inst->export_func_count; j++) {
export_func = module_inst->export_functions + j;
if (export_func->function == func_inst) {
func_name = export_func->name;
break;
}
}
}

func_name = get_func_name_from_index(module_inst, i);
if (func_name)
os_printf(
" func %s, execution time: %.3f ms, execution count: %" PRIu32
Expand Down Expand Up @@ -2463,6 +2476,23 @@ wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst)

return ret;
}

double
wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
const char *func_name)
{
unsigned i;
for (i = 0; i < inst->e->function_count; i++) {
char *name_in_wasm = get_func_name_from_index(inst, i);
if (name_in_wasm
&& strncmp(name_in_wasm, func_name, strlen(func_name)) == 0) {
WASMFunctionInstance *func = inst->e->functions + i;
return (func->total_exec_time - func->children_exec_time) / 1000.0f;
}
}

return -1.0;
}
#endif /*WASM_ENABLE_PERF_PROFILING != 0*/

uint32
Expand Down Expand Up @@ -2958,29 +2988,7 @@ wasm_interp_create_call_stack(struct WASMExecEnv *exec_env)
frame.func_offset = (uint32)(cur_frame->ip - func_code_base);
}

/* look for the function name */
if (func_inst->is_import_func) {
func_name = func_inst->u.func_import->field_name;
}
else {
#if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
func_name = func_inst->u.func->field_name;
#endif
/* if custom name section is not generated,
search symbols from export table */
if (!func_name) {
uint32 i;
for (i = 0; i < module_inst->export_func_count; i++) {
WASMExportFuncInstance *export_func =
module_inst->export_functions + i;
if (export_func->function == func_inst) {
func_name = export_func->name;
break;
}
}
}
}

func_name = get_func_name_from_index(module_inst, frame.func_index);
frame.func_name_wp = func_name;

if (!bh_vector_append(module_inst->frames, &frame)) {
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ wasm_dump_perf_profiling(const WASMModuleInstance *module_inst);
double
wasm_summarize_wasm_execute_time(const WASMModuleInstance *inst);

double
wasm_get_wasm_func_exec_time(const WASMModuleInstance *inst,
const char *func_name);

void
wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst);

Expand Down
6 changes: 6 additions & 0 deletions core/iwasm/libraries/thread-mgr/thread_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@ thread_manager_start_routine(void *arg)
since we will exit soon */
}

#if WASM_ENABLE_PERF_PROFILING != 0
os_printf("============= Spawned thread ===========\n");
wasm_runtime_dump_perf_profiling(module_inst);
os_printf("========================================\n");
#endif

/* Free aux stack space */
free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
/* Remove exec_env */
Expand Down

0 comments on commit 5cf3f2d

Please sign in to comment.