Struct wasmer_middlewares::metering::Metering
source · pub struct Metering<F: Fn(&Operator<'_>) -> u64 + Send + Sync> {
initial_limit: u64,
cost_function: Arc<F>,
global_indexes: Mutex<Option<MeteringGlobalIndexes>>,
}
Expand description
The module-level metering middleware.
§Panic
An instance of Metering
should not be shared among different
modules, since it tracks module-specific information like the
global index to store metering state. Attempts to use a Metering
instance from multiple modules will result in a panic.
§Example
use std::sync::Arc;
use wasmer::{wasmparser::Operator, sys::CompilerConfig};
use wasmer_middlewares::Metering;
fn create_metering_middleware(compiler_config: &mut dyn CompilerConfig) {
// Let's define a dummy cost function,
// which counts 1 for all operators.
let cost_function = |_operator: &Operator| -> u64 { 1 };
// Let's define the initial limit.
let initial_limit = 10;
// Let's creating the metering middleware.
let metering = Arc::new(Metering::new(
initial_limit,
cost_function
));
// Finally, let's push the middleware.
compiler_config.push_middleware(metering);
}
Fields§
§initial_limit: u64
Initial limit of points.
cost_function: Arc<F>
Function that maps each operator to a cost in “points”.
global_indexes: Mutex<Option<MeteringGlobalIndexes>>
The global indexes for metering points.
Implementations§
source§impl<F: Fn(&Operator<'_>) -> u64 + Send + Sync> Metering<F>
impl<F: Fn(&Operator<'_>) -> u64 + Send + Sync> Metering<F>
sourcepub fn new(initial_limit: u64, cost_function: F) -> Self
pub fn new(initial_limit: u64, cost_function: F) -> Self
Creates a Metering
middleware.
When providing a cost function, you should consider that branching operations do
additional work to track the metering points and probably need to have a higher cost.
To find out which operations are affected by this, you can call is_accounting
.
Trait Implementations§
source§impl<F: Fn(&Operator<'_>) -> u64 + Send + Sync + 'static> ModuleMiddleware for Metering<F>
impl<F: Fn(&Operator<'_>) -> u64 + Send + Sync + 'static> ModuleMiddleware for Metering<F>
source§fn generate_function_middleware(
&self,
_: LocalFunctionIndex,
) -> Box<dyn FunctionMiddleware>
fn generate_function_middleware( &self, _: LocalFunctionIndex, ) -> Box<dyn FunctionMiddleware>
Generates a FunctionMiddleware
for a given function.
source§fn transform_module_info(
&self,
module_info: &mut ModuleInfo,
) -> Result<(), MiddlewareError>
fn transform_module_info( &self, module_info: &mut ModuleInfo, ) -> Result<(), MiddlewareError>
Transforms a ModuleInfo
struct in-place. This is called before application on functions begins.
Auto Trait Implementations§
impl<F> !Freeze for Metering<F>
impl<F> RefUnwindSafe for Metering<F>where
F: RefUnwindSafe,
impl<F> Send for Metering<F>
impl<F> Sync for Metering<F>
impl<F> Unpin for Metering<F>
impl<F> UnwindSafe for Metering<F>where
F: RefUnwindSafe,
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more