66extern const char wasmModuleBuffer [];
77extern int wasmModuleBuffer_length ;
88
9+ static inline unsigned long rdinstret (void ) {
10+ unsigned long v ;
11+ asm volatile ("csrr %0, instret" : "=r" (v ));
12+ return v ;
13+ }
14+
915int main (void ) {
1016 int argc = 0 ;
1117 char * argv [0 ];
@@ -17,6 +23,10 @@ int main(void) {
1723 wasm_function_inst_t func ;
1824 wasm_exec_env_t exec_env ;
1925 uint32 size , stack_size = 16 * 1024 * 1024 ;
26+ unsigned long instrs_start , instrs_init , instrs_load , instrs_inst ,
27+ instrs_exec , instrs_unload , instrs_end ;
28+
29+ instrs_start = rdinstret ();
2030
2131 wasm_runtime_set_log_level (WASM_LOG_LEVEL_VERBOSE );
2232
@@ -25,28 +35,40 @@ int main(void) {
2535 printf ("runtime init failed\n" );
2636 exit (1 );
2737 }
38+ instrs_init = rdinstret ();
2839
2940 /* parse the WASM file from buffer and create a WASM module */
3041 module = wasm_runtime_load (wasmModuleBuffer , wasmModuleBuffer_length , error_buf , sizeof (error_buf ));
3142 if (module == 0 ) {
3243 printf ("runtime load module failed: %s\n" , error_buf );
3344 exit (1 );
3445 }
46+ instrs_load = rdinstret ();
3547
3648 /* create an instance of the WASM module (WASM linear memory is ready) */
3749 module_inst = wasm_runtime_instantiate (module , stack_size , 0 , error_buf , sizeof (error_buf ));
3850 if (module_inst == 0 ) {
3951 printf ("wasm_runtime_instantiate failed as module_inst=%p: %s\n" , module_inst , error_buf );
4052 exit (1 );
4153 }
54+ instrs_inst = rdinstret ();
4255
4356 if (!wasm_application_execute_main (module_inst , argc , argv )) {
4457 printf ("error executing main\n" );
4558 printf ("exception: %s\n" , wasm_runtime_get_exception (module_inst ));
4659 }
60+ instrs_exec = rdinstret ();
4761
4862 printf ("wasm_runtime_unload...\n" );
4963 wasm_runtime_unload (module );
64+ instrs_end = instrs_unload = rdinstret ();
65+
66+ printf ("instructions runtime init: %lu\n" , instrs_init - instrs_start );
67+ printf ("instructions runtime load: %lu\n" , instrs_load - instrs_init );
68+ printf ("instructions runtime instantiate: %lu\n" , instrs_inst - instrs_load );
69+ printf ("instructions runtime exec: %lu\n" , instrs_exec - instrs_inst );
70+ printf ("instructions runtime unload: %lu\n" , instrs_unload - instrs_exec );
71+ printf ("instructions in total: %lu\n" , instrs_end - instrs_start );
5072
5173 return 0 ;
5274}
0 commit comments