@@ -398,18 +398,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
398398
399399 let call_result = call_result. map ( |code| handle_error_sink_code ( code, error) ) ;
400400
401- // Compute fuel and heap usage.
402- let remaining_fuel = get_store_fuel ( store) ;
403- let remaining: FunctionBudget = remaining_fuel. into ( ) ;
404- let energy = module_host_actor:: EnergyStats { budget, remaining } ;
405- let memory_allocation = store. data ( ) . get_mem ( ) . memory . data_size ( & store) ;
406-
407401 module_host_actor:: ReducerExecuteResult {
408- stats : ExecutionStats {
409- energy,
410- timings,
411- memory_allocation,
412- } ,
402+ stats : get_execution_stats ( store, budget, timings) ,
413403 call_result,
414404 }
415405 }
@@ -432,11 +422,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
432422
433423 let Some ( call_view) = self . call_view . as_ref ( ) else {
434424 return module_host_actor:: ViewExecuteResult {
435- stats : ExecutionStats {
436- energy : module_host_actor:: EnergyStats :: ZERO ,
437- timings : module_host_actor:: ExecutionTimings :: zero ( ) ,
438- memory_allocation : get_memory_size ( store) ,
439- } ,
425+ stats : zero_execution_stats ( store) ,
440426 call_result : Err ( anyhow:: anyhow!(
441427 "Module defines view {} but does not export `{}`" ,
442428 op. name,
@@ -468,18 +454,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
468454 . and_then ( |code| handle_result_sink_code ( code, result_bytes) . map_err ( |e| anyhow:: anyhow!( e) ) )
469455 . map ( |r| r. into ( ) ) ;
470456
471- // Compute fuel and heap usage.
472- let remaining_fuel = get_store_fuel ( store) ;
473- let remaining: FunctionBudget = remaining_fuel. into ( ) ;
474- let energy = module_host_actor:: EnergyStats { budget, remaining } ;
475- let memory_allocation = store. data ( ) . get_mem ( ) . memory . data_size ( & store) ;
476-
477457 module_host_actor:: ViewExecuteResult {
478- stats : ExecutionStats {
479- energy,
480- timings,
481- memory_allocation,
482- } ,
458+ stats : get_execution_stats ( store, budget, timings) ,
483459 call_result,
484460 }
485461 }
@@ -503,11 +479,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
503479
504480 let Some ( call_view_anon) = self . call_view_anon . as_ref ( ) else {
505481 return module_host_actor:: ViewExecuteResult {
506- stats : ExecutionStats {
507- energy : module_host_actor:: EnergyStats :: ZERO ,
508- timings : module_host_actor:: ExecutionTimings :: zero ( ) ,
509- memory_allocation : get_memory_size ( store) ,
510- } ,
482+ stats : zero_execution_stats ( store) ,
511483 call_result : Err ( anyhow:: anyhow!(
512484 "Module defines anonymous view {} but does not export `{}`" ,
513485 op. name,
@@ -526,18 +498,9 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
526498 let call_result = call_result
527499 . and_then ( |code| handle_result_sink_code ( code, result_bytes) . map_err ( |e| anyhow:: anyhow!( e) ) )
528500 . map ( |r| r. into ( ) ) ;
529- // Compute fuel and heap usage.
530- let remaining_fuel = get_store_fuel ( store) ;
531- let remaining: FunctionBudget = remaining_fuel. into ( ) ;
532- let energy = module_host_actor:: EnergyStats { budget, remaining } ;
533- let memory_allocation = store. data ( ) . get_mem ( ) . memory . data_size ( & store) ;
534501
535502 module_host_actor:: ViewExecuteResult {
536- stats : ExecutionStats {
537- energy,
538- timings,
539- memory_allocation,
540- } ,
503+ stats : get_execution_stats ( store, budget, timings) ,
541504 call_result,
542505 }
543506 }
@@ -567,11 +530,7 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
567530
568531 let Some ( call_procedure) = self . call_procedure . as_ref ( ) else {
569532 return module_host_actor:: ProcedureExecuteResult {
570- stats : ExecutionStats {
571- energy : module_host_actor:: EnergyStats :: ZERO ,
572- timings : module_host_actor:: ExecutionTimings :: zero ( ) ,
573- memory_allocation : get_memory_size ( store) ,
574- } ,
533+ stats : zero_execution_stats ( store) ,
575534 call_result : Err ( anyhow:: anyhow!(
576535 "Module defines procedure {} but does not export `{}`" ,
577536 op. name,
@@ -608,18 +567,8 @@ impl module_host_actor::WasmInstance for WasmtimeInstance {
608567 } )
609568 } ) ;
610569
611- let remaining_fuel = get_store_fuel ( store) ;
612- let remaining = FunctionBudget :: from ( remaining_fuel) ;
613-
614- let energy = module_host_actor:: EnergyStats { budget, remaining } ;
615- let memory_allocation = get_memory_size ( store) ;
616-
617570 module_host_actor:: ProcedureExecuteResult {
618- stats : ExecutionStats {
619- energy,
620- timings,
621- memory_allocation,
622- } ,
571+ stats : get_execution_stats ( store, budget, timings) ,
623572 call_result,
624573 }
625574 }
@@ -671,6 +620,33 @@ fn prepare_connection_id_for_call(caller_connection_id: ConnectionId) -> [u64; 2
671620 bytemuck:: must_cast ( caller_connection_id. as_le_byte_array ( ) )
672621}
673622
623+ /// Compute fuel and heap usage for a call and construct the `ExecutionStats`.
624+ fn get_execution_stats (
625+ store : & Store < WasmInstanceEnv > ,
626+ initial_budget : FunctionBudget ,
627+ timings : module_host_actor:: ExecutionTimings ,
628+ ) -> ExecutionStats {
629+ let remaining_fuel = get_store_fuel ( store) ;
630+ let remaining: FunctionBudget = remaining_fuel. into ( ) ;
631+ let energy = module_host_actor:: EnergyStats {
632+ budget : initial_budget,
633+ remaining,
634+ } ;
635+ ExecutionStats {
636+ energy,
637+ timings,
638+ memory_allocation : get_memory_size ( store) ,
639+ }
640+ }
641+
642+ fn zero_execution_stats ( store : & Store < WasmInstanceEnv > ) -> ExecutionStats {
643+ ExecutionStats {
644+ energy : module_host_actor:: EnergyStats :: ZERO ,
645+ timings : module_host_actor:: ExecutionTimings :: zero ( ) ,
646+ memory_allocation : get_memory_size ( store) ,
647+ }
648+ }
649+
674650fn get_memory_size ( store : & Store < WasmInstanceEnv > ) -> usize {
675651 store. data ( ) . get_mem ( ) . memory . data_size ( store)
676652}
0 commit comments