Skip to content

Commit 38e051a

Browse files
bors[bot]Hywan
andauthored
Merge #2201
2201: feat: Implement `MemoryUsage` for `Instance` r=Hywan a=Hywan # Description This patch implements `loupe::MemoryUsage` for `wasmer::Instance`. ~~This PR includes #2200. To review unique patches: https://github.com/Hywan/wasmer/compare/feat-memory-usage-module...feat-memory-usage-instance?expand=1~~ # Review - [x] Add a short description of the the change to the CHANGELOG.md file Co-authored-by: Ivan Enderlin <ivan@mnt.io>
2 parents 4026b3a + 05c7f62 commit 38e051a

16 files changed

+54
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [#2135](https://github.com/wasmerio/wasmer/pull/2135) [Documentation](./PACKAGING.md) for linux distribution maintainers
1919

2020
### Changed
21+
- [#2201](https://github.com/wasmerio/wasmer/pull/2201) Implement `loupe::MemoryUsage` for `wasmer::Instance`.
2122
- [#2200](https://github.com/wasmerio/wasmer/pull/2200) Implement `loupe::MemoryUsage` for `wasmer::Module`.
2223
- [#2199](https://github.com/wasmerio/wasmer/pull/2199) Implement `loupe::MemoryUsage` for `wasmer::Store`.
2324
- [#2140](https://github.com/wasmerio/wasmer/pull/2140) Reduce the number of dependencies in the `wasmer.dll` shared library by statically compiling CRT.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ criterion = "0.3"
6969
lazy_static = "1.4"
7070
wasmer-engine-dummy = { path = "tests/lib/engine-dummy" }
7171
tempfile = "3.1"
72-
loupe = "0.1"
7372

7473
[features]
7574
# Don't add the compiler features in default, please add them on the Makefile

examples/hello_world.rs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn main() -> anyhow::Result<()> {
4545
// (`Cranelift`) and pass it to an engine (`JIT`). We then pass the engine to
4646
// the store and are now ready to compile and run WebAssembly!
4747
let store = Store::new(&JIT::new(Cranelift::default()).engine());
48+
4849
// We then use our store and Wasm bytes to compile a `Module`.
4950
// A `Module` is a compiled WebAssembly module that isn't ready to execute yet.
5051
let module = Module::new(&store, wasm_bytes)?;
@@ -71,6 +72,7 @@ fn main() -> anyhow::Result<()> {
7172
// An `Instance` is a compiled WebAssembly module that has been set up
7273
// and is ready to execute.
7374
let instance = Instance::new(&module, &import_object)?;
75+
7476
// We get the `NativeFunc` with no parameters and no results from the instance.
7577
//
7678
// Recall that the Wasm module exported a function named "run", this is getting

lib/api/src/exports.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::import_object::LikeNamespace;
33
use crate::native::NativeFunc;
44
use crate::WasmTypeList;
55
use indexmap::IndexMap;
6+
use loupe::MemoryUsage;
67
use std::fmt;
78
use std::iter::{ExactSizeIterator, FromIterator};
89
use std::sync::Arc;
@@ -61,7 +62,7 @@ pub enum ExportError {
6162
/// the types of instances.
6263
///
6364
/// TODO: add examples of using exports
64-
#[derive(Clone, Default)]
65+
#[derive(Clone, Default, MemoryUsage)]
6566
pub struct Exports {
6667
map: Arc<IndexMap<String, Extern>>,
6768
}

lib/api/src/externals/function.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use inner::{FromToNativeWasmType, HostFunction, WasmTypeList, WithEnv, Witho
1010
#[cfg(feature = "deprecated")]
1111
pub use inner::{UnsafeMutableEnv, WithUnsafeMutableEnv};
1212

13+
use loupe::MemoryUsage;
1314
use std::cmp::max;
1415
use std::ffi::c_void;
1516
use std::fmt;
@@ -22,21 +23,22 @@ use wasmer_vm::{
2223
};
2324

2425
/// A function defined in the Wasm module
25-
#[derive(Clone, PartialEq)]
26+
#[derive(Clone, PartialEq, MemoryUsage)]
2627
pub struct WasmFunctionDefinition {
2728
// Address of the trampoline to do the call.
29+
#[loupe(skip)]
2830
pub(crate) trampoline: VMTrampoline,
2931
}
3032

3133
/// A function defined in the Host
32-
#[derive(Clone, PartialEq)]
34+
#[derive(Clone, PartialEq, MemoryUsage)]
3335
pub struct HostFunctionDefinition {
3436
/// If the host function has a custom environment attached
3537
pub(crate) has_env: bool,
3638
}
3739

3840
/// The inner helper
39-
#[derive(Clone, PartialEq)]
41+
#[derive(Clone, PartialEq, MemoryUsage)]
4042
pub enum FunctionDefinition {
4143
/// A function defined in the Wasm side
4244
Wasm(WasmFunctionDefinition),
@@ -61,7 +63,7 @@ pub enum FunctionDefinition {
6163
/// with native functions. Attempting to create a native `Function` with one will
6264
/// result in a panic.
6365
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840)
64-
#[derive(Clone, PartialEq)]
66+
#[derive(Clone, PartialEq, MemoryUsage)]
6567
pub struct Function {
6668
pub(crate) store: Store,
6769
pub(crate) definition: FunctionDefinition,

lib/api/src/externals/global.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::types::Val;
55
use crate::GlobalType;
66
use crate::Mutability;
77
use crate::RuntimeError;
8+
use loupe::MemoryUsage;
89
use std::fmt;
910
use std::sync::Arc;
1011
use wasmer_engine::{Export, ExportGlobal};
@@ -16,7 +17,7 @@ use wasmer_vm::{Global as RuntimeGlobal, VMExportGlobal};
1617
/// It consists of an individual value and a flag indicating whether it is mutable.
1718
///
1819
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
19-
#[derive(Clone)]
20+
#[derive(Clone, MemoryUsage)]
2021
pub struct Global {
2122
store: Store,
2223
global: Arc<RuntimeGlobal>,

lib/api/src/externals/memory.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::exports::{ExportError, Exportable};
22
use crate::externals::Extern;
33
use crate::store::Store;
44
use crate::{MemoryType, MemoryView};
5+
use loupe::MemoryUsage;
56
use std::convert::TryInto;
67
use std::slice;
78
use std::sync::Arc;
@@ -23,7 +24,7 @@ use wasmer_vm::{Memory as RuntimeMemory, MemoryError, VMExportMemory};
2324
/// mutable from both host and WebAssembly.
2425
///
2526
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances>
26-
#[derive(Debug, Clone)]
27+
#[derive(Debug, Clone, MemoryUsage)]
2728
pub struct Memory {
2829
store: Store,
2930
memory: Arc<dyn RuntimeMemory>,

lib/api/src/externals/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ pub use self::table::Table;
1616
use crate::exports::{ExportError, Exportable};
1717
use crate::store::{Store, StoreObject};
1818
use crate::ExternType;
19+
use loupe::MemoryUsage;
1920
use std::fmt;
2021
use wasmer_engine::Export;
2122

2223
/// An `Extern` is the runtime representation of an entity that
2324
/// can be imported or exported.
2425
///
2526
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#external-values>
26-
#[derive(Clone)]
27+
#[derive(Clone, MemoryUsage)]
2728
pub enum Extern {
2829
/// A external [`Function`].
2930
Function(Function),

lib/api/src/externals/table.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::store::Store;
44
use crate::types::{Val, ValFuncRef};
55
use crate::RuntimeError;
66
use crate::TableType;
7+
use loupe::MemoryUsage;
78
use std::sync::Arc;
89
use wasmer_engine::{Export, ExportTable};
910
use wasmer_vm::{Table as RuntimeTable, VMCallerCheckedAnyfunc, VMExportTable};
@@ -17,7 +18,7 @@ use wasmer_vm::{Table as RuntimeTable, VMCallerCheckedAnyfunc, VMExportTable};
1718
/// mutable from both host and WebAssembly.
1819
///
1920
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#table-instances>
20-
#[derive(Clone)]
21+
#[derive(Clone, MemoryUsage)]
2122
pub struct Table {
2223
store: Store,
2324
table: Arc<dyn RuntimeTable>,

lib/api/src/instance.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::externals::Extern;
33
use crate::module::Module;
44
use crate::store::Store;
55
use crate::{HostEnvInitError, LinkError, RuntimeError};
6+
use loupe::MemoryUsage;
67
use std::fmt;
78
use std::sync::{Arc, Mutex};
89
use thiserror::Error;
@@ -17,7 +18,7 @@ use wasmer_vm::{InstanceHandle, VMContext};
1718
/// interacting with WebAssembly.
1819
///
1920
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#module-instances>
20-
#[derive(Clone)]
21+
#[derive(Clone, MemoryUsage)]
2122
pub struct Instance {
2223
handle: Arc<Mutex<InstanceHandle>>,
2324
module: Module,

lib/engine/src/export.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use loupe::MemoryUsage;
2+
use std::sync::Arc;
13
use wasmer_vm::{
24
ImportInitializerFuncPtr, VMExport, VMExportFunction, VMExportGlobal, VMExportMemory,
35
VMExportTable,
46
};
57

6-
use std::sync::Arc;
7-
88
/// The value of an export passed from one instance to another.
99
#[derive(Debug, Clone)]
1010
pub enum Export {
@@ -54,7 +54,7 @@ impl From<VMExport> for Export {
5454
///
5555
/// This struct owns the original `host_env`, thus when it gets dropped
5656
/// it calls the `drop` function on it.
57-
#[derive(Debug, PartialEq)]
57+
#[derive(Debug, PartialEq, MemoryUsage)]
5858
pub struct ExportFunctionMetadata {
5959
/// This field is stored here to be accessible by `Drop`.
6060
///
@@ -69,20 +69,26 @@ pub struct ExportFunctionMetadata {
6969
/// See `wasmer_vm::export::VMExportFunction::vmctx` for the version of
7070
/// this pointer that is used by the VM when creating an `Instance`.
7171
pub(crate) host_env: *mut std::ffi::c_void,
72+
7273
/// Function pointer to `WasmerEnv::init_with_instance(&mut self, instance: &Instance)`.
7374
///
7475
/// This function is called to finish setting up the environment after
7576
/// we create the `api::Instance`.
7677
// This one is optional for now because dynamic host envs need the rest
7778
// of this without the init fn
79+
#[loupe(skip)]
7880
pub(crate) import_init_function_ptr: Option<ImportInitializerFuncPtr>,
81+
7982
/// A function analogous to `Clone::clone` that returns a leaked `Box`.
83+
#[loupe(skip)]
8084
pub(crate) host_env_clone_fn: fn(*mut std::ffi::c_void) -> *mut std::ffi::c_void,
85+
8186
/// The destructor to free the host environment.
8287
///
8388
/// # Safety
8489
/// - This function should only be called in when properly synchronized.
8590
/// For example, in the `Drop` implementation of this type.
91+
#[loupe(skip)]
8692
pub(crate) host_env_drop_fn: unsafe fn(*mut std::ffi::c_void),
8793
}
8894

@@ -132,7 +138,7 @@ impl Drop for ExportFunctionMetadata {
132138

133139
/// A function export value with an extra function pointer to initialize
134140
/// host environments.
135-
#[derive(Debug, Clone, PartialEq)]
141+
#[derive(Debug, Clone, PartialEq, MemoryUsage)]
136142
pub struct ExportFunction {
137143
/// The VM function, containing most of the data.
138144
pub vm_function: VMExportFunction,

lib/vm/src/export.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::instance::InstanceRef;
66
use crate::memory::{Memory, MemoryStyle};
77
use crate::table::{Table, TableStyle};
88
use crate::vmcontext::{VMFunctionBody, VMFunctionEnvironment, VMFunctionKind, VMTrampoline};
9+
use loupe::MemoryUsage;
910
use std::sync::Arc;
1011
use wasmer_types::{FunctionType, MemoryType, TableType};
1112

@@ -26,7 +27,7 @@ pub enum VMExport {
2627
}
2728

2829
/// A function export value.
29-
#[derive(Debug, Clone, PartialEq)]
30+
#[derive(Debug, Clone, PartialEq, MemoryUsage)]
3031
pub struct VMExportFunction {
3132
/// The address of the native-code function.
3233
pub address: *const VMFunctionBody,
@@ -46,6 +47,7 @@ pub struct VMExportFunction {
4647
///
4748
/// May be `None` when the function is a host function (`FunctionType`
4849
/// == `Dynamic` or `vmctx` == `nullptr`).
50+
#[loupe(skip)]
4951
pub call_trampoline: Option<VMTrampoline>,
5052

5153
/// A “reference” to the instance through the

lib/vm/src/instance/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub type ImportInitializerFuncPtr<ResultErr = *mut ffi::c_void> =
5757
/// contain various data. That's why the type has a C representation
5858
/// to ensure that the `vmctx` field is last. See the documentation of
5959
/// the `vmctx` field to learn more.
60+
#[derive(MemoryUsage)]
6061
#[repr(C)]
6162
pub(crate) struct Instance {
6263
/// The `ModuleInfo` this `Instance` was instantiated from.
@@ -78,6 +79,7 @@ pub(crate) struct Instance {
7879
functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
7980

8081
/// Pointers to function call trampolines in executable memory.
82+
#[loupe(skip)]
8183
function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
8284

8385
/// Passive elements in this instantiation. As `elem.drop`s happen, these
@@ -92,6 +94,7 @@ pub(crate) struct Instance {
9294
host_state: Box<dyn Any>,
9395

9496
/// Handler run when `SIGBUS`, `SIGFPE`, `SIGILL`, or `SIGSEGV` are caught by the instance thread.
97+
#[loupe(skip)]
9598
pub(crate) signal_handler: Cell<Option<Box<SignalHandler>>>,
9699

97100
/// Functions to operate on host environments in the imports
@@ -105,6 +108,7 @@ pub(crate) struct Instance {
105108
/// field is last, and represents a dynamically-sized array that
106109
/// extends beyond the nominal end of the struct (similar to a
107110
/// flexible array member).
111+
#[loupe(skip)]
108112
vmctx: VMContext,
109113
}
110114

@@ -785,7 +789,7 @@ impl Instance {
785789
///
786790
/// This is more or less a public facade of the private `Instance`,
787791
/// providing useful higher-level API.
788-
#[derive(Debug, PartialEq)]
792+
#[derive(Debug, PartialEq, MemoryUsage)]
789793
pub struct InstanceHandle {
790794
/// The [`InstanceRef`]. See its documentation to learn more.
791795
instance: InstanceRef,

lib/vm/src/instance/ref.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::Instance;
2+
use loupe::{MemoryUsage, MemoryUsageTracker};
23
use std::alloc::Layout;
4+
use std::mem;
35
use std::ptr::{self, NonNull};
46
use std::sync::{atomic, Arc};
57

@@ -208,3 +210,13 @@ impl Drop for InstanceRef {
208210
unsafe { Self::deallocate_instance(self) };
209211
}
210212
}
213+
214+
impl MemoryUsage for InstanceRef {
215+
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
216+
mem::size_of_val(self) + self.strong.size_of_val(tracker) - mem::size_of_val(&self.strong)
217+
+ self.instance_layout.size_of_val(tracker)
218+
- mem::size_of_val(&self.instance_layout)
219+
+ self.as_ref().size_of_val(tracker)
220+
- mem::size_of_val(&self.instance)
221+
}
222+
}

lib/vm/src/vmcontext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ mod test_vmfunction_body {
175175
}
176176

177177
/// A function kind is a calling convention into and out of wasm code.
178-
#[derive(Debug, Copy, Clone, PartialEq)]
178+
#[derive(Debug, Copy, Clone, PartialEq, MemoryUsage)]
179179
#[repr(C)]
180180
pub enum VMFunctionKind {
181181
/// A static function has the native signature:

lib/vm/src/vmoffsets.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use crate::module::ModuleInfo;
1010
use crate::VMBuiltinFunctionIndex;
11+
use loupe::MemoryUsage;
1112
use more_asserts::assert_lt;
1213
use std::convert::TryFrom;
1314
use wasmer_types::{
@@ -33,7 +34,7 @@ const fn align(offset: u32, width: u32) -> u32 {
3334
/// related structs that JIT code accesses directly.
3435
///
3536
/// [`VMContext`]: crate::vmcontext::VMContext
36-
#[derive(Clone, Debug)]
37+
#[derive(Clone, Debug, MemoryUsage)]
3738
pub struct VMOffsets {
3839
/// The size in bytes of a pointer on the target.
3940
pub pointer_size: u8,

0 commit comments

Comments
 (0)