feat: Start implementing loupe::MemoryUsage on wasmer::Module.

This commit is contained in:
Ivan Enderlin
2021-03-23 11:23:32 +01:00
parent af59fcbf22
commit 44dc884260
39 changed files with 197 additions and 64 deletions

1
Cargo.lock generated
View File

@@ -1162,6 +1162,7 @@ checksum = "f0700ebea6c2a63815aa6f376f1c6dac93223d7b11c4728a7f71ff951a6eca67"
dependencies = [ dependencies = [
"loupe-derive", "loupe-derive",
"rustversion", "rustversion",
"indexmap",
] ]
[[package]] [[package]]

View File

@@ -1,6 +1,7 @@
use crate::store::Store; use crate::store::Store;
use crate::types::{ExportType, ImportType}; use crate::types::{ExportType, ImportType};
use crate::InstantiationError; use crate::InstantiationError;
use loupe_derive::MemoryUsage;
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
@@ -30,7 +31,7 @@ pub enum IoCompileError {
/// ///
/// Cloning a module is cheap: it does a shallow copy of the compiled /// Cloning a module is cheap: it does a shallow copy of the compiled
/// contents rather than a deep copy. /// contents rather than a deep copy.
#[derive(Clone)] #[derive(Clone, MemoryUsage)]
pub struct Module { pub struct Module {
store: Store, store: Store,
artifact: Arc<dyn Artifact>, artifact: Arc<dyn Artifact>,

View File

@@ -5,7 +5,7 @@ use inkwell::targets::{
}; };
pub use inkwell::OptimizationLevel as LLVMOptLevel; pub use inkwell::OptimizationLevel as LLVMOptLevel;
use itertools::Itertools; use itertools::Itertools;
use loupe::MemoryUsage; use loupe_derive::MemoryUsage;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
use target_lexicon::Architecture; use target_lexicon::Architecture;

View File

@@ -3,12 +3,13 @@
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use crate::sourceloc::SourceLoc; use crate::sourceloc::SourceLoc;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// Single source location to generated address mapping. /// Single source location to generated address mapping.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub struct InstructionAddressMap { pub struct InstructionAddressMap {
/// Original source location. /// Original source location.
pub srcloc: SourceLoc, pub srcloc: SourceLoc,
@@ -22,7 +23,7 @@ pub struct InstructionAddressMap {
/// Function and its instructions addresses mappings. /// Function and its instructions addresses mappings.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default, MemoryUsage)]
pub struct FunctionAddressMap { pub struct FunctionAddressMap {
/// Instructions maps. /// Instructions maps.
/// The array is sorted by the InstructionAddressMap::code_offset field. /// The array is sorted by the InstructionAddressMap::code_offset field.

View File

@@ -12,6 +12,7 @@ use crate::lib::std::vec::Vec;
use crate::section::{CustomSection, SectionIndex}; use crate::section::{CustomSection, SectionIndex};
use crate::trap::TrapInformation; use crate::trap::TrapInformation;
use crate::{CompiledFunctionUnwindInfo, FunctionAddressMap, JumpTableOffsets, Relocation}; use crate::{CompiledFunctionUnwindInfo, FunctionAddressMap, JumpTableOffsets, Relocation};
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_types::entity::PrimaryMap; use wasmer_types::entity::PrimaryMap;
@@ -22,7 +23,7 @@ use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
/// This structure is only used for reconstructing /// This structure is only used for reconstructing
/// the frame information after a `Trap`. /// the frame information after a `Trap`.
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default, MemoryUsage)]
pub struct CompiledFunctionFrameInfo { pub struct CompiledFunctionFrameInfo {
/// The traps (in the function body). /// The traps (in the function body).
/// ///
@@ -35,7 +36,7 @@ pub struct CompiledFunctionFrameInfo {
/// The function body. /// The function body.
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub struct FunctionBody { pub struct FunctionBody {
/// The function body bytes. /// The function body bytes.
#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] #[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))]
@@ -79,7 +80,7 @@ pub type CustomSections = PrimaryMap<SectionIndex, CustomSection>;
/// In the future this structure may also hold other information useful /// In the future this structure may also hold other information useful
/// for debugging. /// for debugging.
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone, MemoryUsage)]
pub struct Dwarf { pub struct Dwarf {
/// The section index in the [`Compilation`] that corresponds to the exception frames. /// The section index in the [`Compilation`] that corresponds to the exception frames.
/// [Learn /// [Learn

View File

@@ -5,6 +5,7 @@
//! [Learn more](https://en.wikipedia.org/wiki/Branch_table). //! [Learn more](https://en.wikipedia.org/wiki/Branch_table).
use super::CodeOffset; use super::CodeOffset;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_types::entity::{entity_impl, SecondaryMap}; use wasmer_types::entity::{entity_impl, SecondaryMap};
@@ -14,7 +15,7 @@ use wasmer_types::entity::{entity_impl, SecondaryMap};
/// `JumpTable`s are used for indirect branching and are specialized for dense, /// `JumpTable`s are used for indirect branching and are specialized for dense,
/// 0-based jump offsets. /// 0-based jump offsets.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, MemoryUsage)]
pub struct JumpTable(u32); pub struct JumpTable(u32);
entity_impl!(JumpTable, "jt"); entity_impl!(JumpTable, "jt");

View File

@@ -1,4 +1,5 @@
use crate::lib::std::sync::Arc; use crate::lib::std::sync::Arc;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_types::entity::PrimaryMap; use wasmer_types::entity::PrimaryMap;
@@ -10,7 +11,7 @@ use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle};
/// This differs from [`ModuleInfo`] because it have extra info only /// This differs from [`ModuleInfo`] because it have extra info only
/// possible after translation (such as the features used for compiling, /// possible after translation (such as the features used for compiling,
/// or the `MemoryStyle` and `TableStyle`). /// or the `MemoryStyle` and `TableStyle`).
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
pub struct CompileModuleInfo { pub struct CompileModuleInfo {
/// The features used for compiling the module /// The features used for compiling the module

View File

@@ -13,6 +13,7 @@ use crate::lib::std::fmt;
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use crate::section::SectionIndex; use crate::section::SectionIndex;
use crate::{Addend, CodeOffset, JumpTable}; use crate::{Addend, CodeOffset, JumpTable};
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_types::entity::PrimaryMap; use wasmer_types::entity::PrimaryMap;
@@ -21,7 +22,7 @@ use wasmer_vm::libcalls::LibCall;
/// Relocation kinds for every ISA. /// Relocation kinds for every ISA.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq, MemoryUsage)]
pub enum RelocationKind { pub enum RelocationKind {
/// absolute 4-byte /// absolute 4-byte
Abs4, Abs4,
@@ -79,7 +80,7 @@ impl fmt::Display for RelocationKind {
/// A record of a relocation to perform. /// A record of a relocation to perform.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub struct Relocation { pub struct Relocation {
/// The relocation kind. /// The relocation kind.
pub kind: RelocationKind, pub kind: RelocationKind,
@@ -93,7 +94,7 @@ pub struct Relocation {
/// Destination function. Can be either user function or some special one, like `memory.grow`. /// Destination function. Can be either user function or some special one, like `memory.grow`.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq, MemoryUsage)]
pub enum RelocationTarget { pub enum RelocationTarget {
/// A relocation to a function defined locally in the wasm (not an imported one). /// A relocation to a function defined locally in the wasm (not an imported one).
LocalFunc(LocalFunctionIndex), LocalFunc(LocalFunctionIndex),

View File

@@ -7,13 +7,14 @@
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use crate::Relocation; use crate::Relocation;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_types::entity::entity_impl; use wasmer_types::entity::entity_impl;
/// Index type of a Section defined inside a WebAssembly `Compilation`. /// Index type of a Section defined inside a WebAssembly `Compilation`.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, MemoryUsage)]
pub struct SectionIndex(u32); pub struct SectionIndex(u32);
entity_impl!(SectionIndex); entity_impl!(SectionIndex);
@@ -22,7 +23,7 @@ entity_impl!(SectionIndex);
/// ///
/// Determines how a custom section may be used. /// Determines how a custom section may be used.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub enum CustomSectionProtection { pub enum CustomSectionProtection {
/// A custom section with read permission. /// A custom section with read permission.
Read, Read,
@@ -36,7 +37,7 @@ pub enum CustomSectionProtection {
/// This is used so compilers can store arbitrary information /// This is used so compilers can store arbitrary information
/// in the emitted module. /// in the emitted module.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub struct CustomSection { pub struct CustomSection {
/// Memory protection that applies to this section. /// Memory protection that applies to this section.
pub protection: CustomSectionProtection, pub protection: CustomSectionProtection,
@@ -55,7 +56,7 @@ pub struct CustomSection {
/// The bytes in the section. /// The bytes in the section.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default, MemoryUsage)]
pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>); pub struct SectionBody(#[cfg_attr(feature = "enable-serde", serde(with = "serde_bytes"))] Vec<u8>);
impl SectionBody { impl SectionBody {

View File

@@ -8,7 +8,7 @@
//! and tracing errors. //! and tracing errors.
use crate::lib::std::fmt; use crate::lib::std::fmt;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -22,7 +22,7 @@ use serde::{Deserialize, Serialize};
serde(transparent) serde(transparent)
)] )]
#[repr(transparent)] #[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq, MemoryUsage)]
pub struct SourceLoc(u32); pub struct SourceLoc(u32);
impl SourceLoc { impl SourceLoc {

View File

@@ -3,7 +3,7 @@ use crate::error::ParseCpuFeatureError;
use crate::lib::std::str::FromStr; use crate::lib::std::str::FromStr;
use crate::lib::std::string::{String, ToString}; use crate::lib::std::string::{String, ToString};
use enumset::{EnumSet, EnumSetType}; use enumset::{EnumSet, EnumSetType};
use loupe::MemoryUsage; use loupe_derive::MemoryUsage;
pub use target_lexicon::{ pub use target_lexicon::{
Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth, Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth,
Triple, Triple,

View File

@@ -1,11 +1,12 @@
use crate::CodeOffset; use crate::CodeOffset;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_vm::TrapCode; use wasmer_vm::TrapCode;
/// Information about trap. /// Information about trap.
#[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))] #[cfg_attr(feature = "enable-serde", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq, MemoryUsage)]
pub struct TrapInformation { pub struct TrapInformation {
/// The offset of the trapping instruction in native code. It is relative to the beginning of the function. /// The offset of the trapping instruction in native code. It is relative to the beginning of the function.
pub code_offset: CodeOffset, pub code_offset: CodeOffset,

View File

@@ -6,6 +6,7 @@
//! //!
//! [Learn more](https://en.wikipedia.org/wiki/Call_stack). //! [Learn more](https://en.wikipedia.org/wiki/Call_stack).
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -17,7 +18,7 @@ use serde::{Deserialize, Serialize};
/// ///
/// [unwind info]: https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019 /// [unwind info]: https://docs.microsoft.com/en-us/cpp/build/exception-handling-x64?view=vs-2019
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, MemoryUsage)]
pub enum CompiledFunctionUnwindInfo { pub enum CompiledFunctionUnwindInfo {
/// Windows UNWIND_INFO. /// Windows UNWIND_INFO.
WindowsX64(Vec<u8>), WindowsX64(Vec<u8>),

View File

@@ -6,6 +6,7 @@ use crate::link::link_module;
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
use crate::serialize::SerializableCompilation; use crate::serialize::SerializableCompilation;
use crate::serialize::SerializableModule; use crate::serialize::SerializableModule;
use loupe_derive::MemoryUsage;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use wasmer_compiler::{CompileError, Features, Triple}; use wasmer_compiler::{CompileError, Features, Triple};
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
@@ -26,9 +27,11 @@ use wasmer_vm::{
}; };
/// A compiled wasm module, ready to be instantiated. /// A compiled wasm module, ready to be instantiated.
#[derive(MemoryUsage)]
pub struct JITArtifact { pub struct JITArtifact {
serializable: SerializableModule, serializable: SerializableModule,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>, finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
#[memoryusage(ignore)]
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>, finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>, finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>, signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,

View File

@@ -1,3 +1,4 @@
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_compiler::{ use wasmer_compiler::{
CompileModuleInfo, CustomSection, Dwarf, FunctionBody, JumpTableOffsets, Relocation, CompileModuleInfo, CustomSection, Dwarf, FunctionBody, JumpTableOffsets, Relocation,
@@ -18,7 +19,7 @@ use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, Sign
// } // }
/// The compilation related data for a serialized modules /// The compilation related data for a serialized modules
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, MemoryUsage)]
pub struct SerializableCompilation { pub struct SerializableCompilation {
pub function_bodies: PrimaryMap<LocalFunctionIndex, FunctionBody>, pub function_bodies: PrimaryMap<LocalFunctionIndex, FunctionBody>,
pub function_relocations: PrimaryMap<LocalFunctionIndex, Vec<Relocation>>, pub function_relocations: PrimaryMap<LocalFunctionIndex, Vec<Relocation>>,
@@ -37,7 +38,7 @@ pub struct SerializableCompilation {
/// Serializable struct that is able to serialize from and to /// Serializable struct that is able to serialize from and to
/// a `JITArtifactInfo`. /// a `JITArtifactInfo`.
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, MemoryUsage)]
pub struct SerializableModule { pub struct SerializableModule {
pub compilation: SerializableCompilation, pub compilation: SerializableCompilation,
pub compile_info: CompileModuleInfo, pub compile_info: CompileModuleInfo,

View File

@@ -4,6 +4,7 @@
use crate::engine::{NativeEngine, NativeEngineInner}; use crate::engine::{NativeEngine, NativeEngineInner};
use crate::serialize::ModuleMetadata; use crate::serialize::ModuleMetadata;
use libloading::{Library, Symbol as LibrarySymbol}; use libloading::{Library, Symbol as LibrarySymbol};
use loupe_derive::MemoryUsage;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{Read, Write}; use std::io::{Read, Write};
@@ -37,10 +38,12 @@ use wasmer_vm::{
}; };
/// A compiled wasm module, ready to be instantiated. /// A compiled wasm module, ready to be instantiated.
#[derive(MemoryUsage)]
pub struct NativeArtifact { pub struct NativeArtifact {
sharedobject_path: PathBuf, sharedobject_path: PathBuf,
metadata: ModuleMetadata, metadata: ModuleMetadata,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>, finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
#[memoryusage(ignore)]
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>, finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>, finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>, signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,

View File

@@ -2,7 +2,7 @@
use crate::NativeArtifact; use crate::NativeArtifact;
use libloading::Library; use libloading::Library;
use loupe::MemoryUsage; use loupe_derive::MemoryUsage;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;

View File

@@ -1,10 +1,11 @@
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry}; use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry};
use wasmer_types::entity::{EntityRef, PrimaryMap}; use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex}; use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex};
/// Serializable struct that represents the compiled metadata. /// Serializable struct that represents the compiled metadata.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, MemoryUsage)]
pub struct ModuleMetadata { pub struct ModuleMetadata {
pub compile_info: CompileModuleInfo, pub compile_info: CompileModuleInfo,
pub prefix: String, pub prefix: String,

View File

@@ -3,6 +3,7 @@
use crate::engine::{ObjectFileEngine, ObjectFileEngineInner}; use crate::engine::{ObjectFileEngine, ObjectFileEngineInner};
use crate::serialize::{ModuleMetadata, ModuleMetadataSymbolRegistry}; use crate::serialize::{ModuleMetadata, ModuleMetadataSymbolRegistry};
use loupe_derive::MemoryUsage;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::error::Error; use std::error::Error;
use std::mem; use std::mem;
@@ -30,10 +31,12 @@ use wasmer_vm::{
}; };
/// A compiled wasm module, ready to be instantiated. /// A compiled wasm module, ready to be instantiated.
#[derive(MemoryUsage)]
pub struct ObjectFileArtifact { pub struct ObjectFileArtifact {
metadata: ModuleMetadata, metadata: ModuleMetadata,
module_bytes: Vec<u8>, module_bytes: Vec<u8>,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>, finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
#[memoryusage(ignore)]
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>, finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>, finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>, signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,

View File

@@ -1,10 +1,11 @@
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry}; use wasmer_compiler::{CompileModuleInfo, SectionIndex, Symbol, SymbolRegistry};
use wasmer_types::entity::{EntityRef, PrimaryMap}; use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex}; use wasmer_types::{FunctionIndex, LocalFunctionIndex, OwnedDataInitializer, SignatureIndex};
/// Serializable struct that represents the compiled metadata. /// Serializable struct that represents the compiled metadata.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, MemoryUsage)]
pub struct ModuleMetadata { pub struct ModuleMetadata {
pub compile_info: CompileModuleInfo, pub compile_info: CompileModuleInfo,
pub prefix: String, pub prefix: String,
@@ -13,6 +14,7 @@ pub struct ModuleMetadata {
pub function_body_lengths: PrimaryMap<LocalFunctionIndex, u64>, pub function_body_lengths: PrimaryMap<LocalFunctionIndex, u64>,
} }
#[derive(MemoryUsage)]
pub struct ModuleMetadataSymbolRegistry { pub struct ModuleMetadataSymbolRegistry {
pub prefix: String, pub prefix: String,
} }

View File

@@ -1,6 +1,7 @@
use crate::{ use crate::{
resolve_imports, InstantiationError, Resolver, RuntimeError, SerializeError, Tunables, resolve_imports, InstantiationError, Resolver, RuntimeError, SerializeError, Tunables,
}; };
use loupe::MemoryUsage;
use std::any::Any; use std::any::Any;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
@@ -22,7 +23,7 @@ use wasmer_vm::{
/// The `Artifact` contains the compiled data for a given /// The `Artifact` contains the compiled data for a given
/// module as well as extra information needed to run the /// module as well as extra information needed to run the
/// module at runtime, such as [`ModuleInfo`] and [`Features`]. /// module at runtime, such as [`ModuleInfo`] and [`Features`].
pub trait Artifact: Send + Sync + Upcastable { pub trait Artifact: Send + Sync + Upcastable + MemoryUsage {
/// Return a reference-counted pointer to the module /// Return a reference-counted pointer to the module
fn module(&self) -> Arc<ModuleInfo>; fn module(&self) -> Arc<ModuleInfo>;

View File

@@ -1,3 +1,4 @@
use loupe_derive::MemoryUsage;
use serde::de::{Deserializer, Visitor}; use serde::de::{Deserializer, Visitor};
use serde::ser::Serializer; use serde::ser::Serializer;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -5,7 +6,7 @@ use std::fmt;
use wasmer_compiler::CompiledFunctionFrameInfo; use wasmer_compiler::CompiledFunctionFrameInfo;
/// This is the unserialized verison of `CompiledFunctionFrameInfo`. /// This is the unserialized verison of `CompiledFunctionFrameInfo`.
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize, MemoryUsage)]
#[serde(transparent)] #[serde(transparent)]
#[repr(transparent)] #[repr(transparent)]
pub struct UnprocessedFunctionFrameInfo { pub struct UnprocessedFunctionFrameInfo {
@@ -44,7 +45,7 @@ impl UnprocessedFunctionFrameInfo {
/// of compiling at the same time that emiting the JIT. /// of compiling at the same time that emiting the JIT.
/// In that case, we don't need to deserialize/process anything /// In that case, we don't need to deserialize/process anything
/// as the data is already in memory. /// as the data is already in memory.
#[derive(Clone)] #[derive(Clone, MemoryUsage)]
pub enum SerializableFunctionFrameInfo { pub enum SerializableFunctionFrameInfo {
/// The unprocessed frame info (binary) /// The unprocessed frame info (binary)
Unprocessed(UnprocessedFunctionFrameInfo), Unprocessed(UnprocessedFunctionFrameInfo),

View File

@@ -11,6 +11,7 @@
//! FRAME_INFO.register(module, compiled_functions); //! FRAME_INFO.register(module, compiled_functions);
//! ``` //! ```
use crate::serialize::SerializableFunctionFrameInfo; use crate::serialize::SerializableFunctionFrameInfo;
use loupe_derive::MemoryUsage;
use std::cmp; use std::cmp;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@@ -44,6 +45,7 @@ pub struct GlobalFrameInfo {
/// An RAII structure used to unregister a module's frame information when the /// An RAII structure used to unregister a module's frame information when the
/// module is destroyed. /// module is destroyed.
#[derive(MemoryUsage)]
pub struct GlobalFrameInfoRegistration { pub struct GlobalFrameInfoRegistration {
/// The key that will be removed from the global `ranges` map when this is /// The key that will be removed from the global `ranges` map when this is
/// dropped. /// dropped.

View File

@@ -21,7 +21,7 @@ more-asserts = "0.2"
cfg-if = "0.1" cfg-if = "0.1"
backtrace = "0.3" backtrace = "0.3"
serde = { version = "1.0", features = ["derive", "rc"] } serde = { version = "1.0", features = ["derive", "rc"] }
loupe = "0.1" loupe = { version = "0.1", features = ["enable-indexmap"] }
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["winbase", "memoryapi", "errhandlingapi"] } winapi = { version = "0.3", features = ["winbase", "memoryapi", "errhandlingapi"] }

View File

@@ -1,11 +1,12 @@
use crate::vmcontext::VMGlobalDefinition; use crate::vmcontext::VMGlobalDefinition;
use loupe_derive::MemoryUsage;
use std::cell::UnsafeCell; use std::cell::UnsafeCell;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::sync::Mutex; use std::sync::Mutex;
use thiserror::Error; use thiserror::Error;
use wasmer_types::{GlobalType, Mutability, Type, Value}; use wasmer_types::{GlobalType, Mutability, Type, Value};
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
/// A Global instance /// A Global instance
pub struct Global { pub struct Global {
ty: GlobalType, ty: GlobalType,

View File

@@ -27,6 +27,7 @@ use crate::vmcontext::{
}; };
use crate::{FunctionBodyPtr, ModuleInfo, VMOffsets}; use crate::{FunctionBodyPtr, ModuleInfo, VMOffsets};
use crate::{VMExportFunction, VMExportGlobal, VMExportMemory, VMExportTable}; use crate::{VMExportFunction, VMExportGlobal, VMExportMemory, VMExportTable};
use loupe::{MemoryUsage, MemoryUsageTracker};
use memoffset::offset_of; use memoffset::offset_of;
use more_asserts::assert_lt; use more_asserts::assert_lt;
use std::any::Any; use std::any::Any;
@@ -34,9 +35,10 @@ use std::cell::{Cell, RefCell};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use std::ffi; use std::ffi;
use std::fmt; use std::fmt;
use std::ptr::NonNull; use std::mem;
use std::ptr::{self, NonNull};
use std::slice;
use std::sync::Arc; use std::sync::Arc;
use std::{mem, ptr, slice};
use wasmer_types::entity::{packed_option::ReservedValue, BoxedSlice, EntityRef, PrimaryMap}; use wasmer_types::entity::{packed_option::ReservedValue, BoxedSlice, EntityRef, PrimaryMap};
use wasmer_types::{ use wasmer_types::{
DataIndex, DataInitializer, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, GlobalInit, DataIndex, DataInitializer, ElemIndex, ExportIndex, FunctionIndex, GlobalIndex, GlobalInit,
@@ -122,6 +124,7 @@ pub enum ImportFunctionEnv {
/// The function environment. This is not always the user-supplied /// The function environment. This is not always the user-supplied
/// env. /// env.
env: *mut ffi::c_void, env: *mut ffi::c_void,
/// A clone function for duplicating the env. /// A clone function for duplicating the env.
clone: fn(*mut ffi::c_void) -> *mut ffi::c_void, clone: fn(*mut ffi::c_void) -> *mut ffi::c_void,
/// This field is not always present. When it is present, it /// This field is not always present. When it is present, it
@@ -187,6 +190,12 @@ impl Drop for ImportFunctionEnv {
} }
} }
impl MemoryUsage for ImportFunctionEnv {
fn size_of_val(&self, _: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
}
}
impl fmt::Debug for Instance { impl fmt::Debug for Instance {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.debug_struct("Instance").finish() formatter.debug_struct("Instance").finish()

View File

@@ -56,12 +56,13 @@ pub use crate::vmcontext::{
VMTableImport, VMTrampoline, VMTableImport, VMTrampoline,
}; };
pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets}; pub use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets};
use loupe_derive::MemoryUsage;
/// Version number of this crate. /// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// A safe wrapper around `VMFunctionBody`. /// A safe wrapper around `VMFunctionBody`.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, MemoryUsage)]
#[repr(transparent)] #[repr(transparent)]
pub struct FunctionBodyPtr(pub *const VMFunctionBody); pub struct FunctionBodyPtr(pub *const VMFunctionBody);

View File

@@ -38,6 +38,7 @@
use crate::probestack::PROBESTACK; use crate::probestack::PROBESTACK;
use crate::trap::{raise_lib_trap, Trap, TrapCode}; use crate::trap::{raise_lib_trap, Trap, TrapCode};
use crate::vmcontext::VMContext; use crate::vmcontext::VMContext;
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use wasmer_types::{DataIndex, ElemIndex, LocalMemoryIndex, MemoryIndex, TableIndex}; use wasmer_types::{DataIndex, ElemIndex, LocalMemoryIndex, MemoryIndex, TableIndex};
@@ -405,7 +406,7 @@ pub static wasmer_probestack: unsafe extern "C" fn() = PROBESTACK;
/// The name of a runtime library routine. /// The name of a runtime library routine.
/// ///
/// This list is likely to grow over time. /// This list is likely to grow over time.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, MemoryUsage)]
pub enum LibCall { pub enum LibCall {
/// ceil.f32 /// ceil.f32
CeilF32, CeilF32,

View File

@@ -7,6 +7,8 @@
use crate::mmap::Mmap; use crate::mmap::Mmap;
use crate::vmcontext::VMMemoryDefinition; use crate::vmcontext::VMMemoryDefinition;
use loupe::MemoryUsage;
use loupe_derive::MemoryUsage;
use more_asserts::assert_ge; use more_asserts::assert_ge;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
@@ -61,7 +63,7 @@ pub enum MemoryError {
} }
/// Implementation styles for WebAssembly linear memory. /// Implementation styles for WebAssembly linear memory.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, MemoryUsage)]
pub enum MemoryStyle { pub enum MemoryStyle {
/// The actual memory can be resized and moved. /// The actual memory can be resized and moved.
Dynamic { Dynamic {
@@ -96,7 +98,7 @@ impl MemoryStyle {
} }
/// Trait for implementing Wasm Memory used by Wasmer. /// Trait for implementing Wasm Memory used by Wasmer.
pub trait Memory: fmt::Debug + Send + Sync { pub trait Memory: fmt::Debug + Send + Sync + MemoryUsage {
/// Returns the memory type for this memory. /// Returns the memory type for this memory.
fn ty(&self) -> &MemoryType; fn ty(&self) -> &MemoryType;
@@ -116,7 +118,7 @@ pub trait Memory: fmt::Debug + Send + Sync {
} }
/// A linear memory instance. /// A linear memory instance.
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
pub struct LinearMemory { pub struct LinearMemory {
// The underlying allocation. // The underlying allocation.
mmap: Mutex<WasmMmap>, mmap: Mutex<WasmMmap>,
@@ -144,7 +146,7 @@ pub struct LinearMemory {
/// A type to help manage who is responsible for the backing memory of them /// A type to help manage who is responsible for the backing memory of them
/// `VMMemoryDefinition`. /// `VMMemoryDefinition`.
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
enum VMMemoryDefinitionOwnership { enum VMMemoryDefinitionOwnership {
/// The `VMMemoryDefinition` is owned by the `Instance` and we should use /// The `VMMemoryDefinition` is owned by the `Instance` and we should use
/// its memory. This is how a local memory that's exported should be stored. /// its memory. This is how a local memory that's exported should be stored.
@@ -167,7 +169,7 @@ unsafe impl Send for LinearMemory {}
/// This is correct because all internal mutability is protected by a mutex. /// This is correct because all internal mutability is protected by a mutex.
unsafe impl Sync for LinearMemory {} unsafe impl Sync for LinearMemory {}
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
struct WasmMmap { struct WasmMmap {
// Our OS allocation of mmap'd memory. // Our OS allocation of mmap'd memory.
alloc: Mmap, alloc: Mmap,

View File

@@ -5,6 +5,7 @@
//! `wasmer::Module`. //! `wasmer::Module`.
use indexmap::IndexMap; use indexmap::IndexMap;
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@@ -19,7 +20,7 @@ use wasmer_types::{
TableIndex, TableInitializer, TableType, TableIndex, TableInitializer, TableType,
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
pub struct ModuleId { pub struct ModuleId {
id: usize, id: usize,
} }
@@ -41,7 +42,7 @@ impl Default for ModuleId {
/// A translated WebAssembly module, excluding the function bodies and /// A translated WebAssembly module, excluding the function bodies and
/// memory initializers. /// memory initializers.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, MemoryUsage)]
pub struct ModuleInfo { pub struct ModuleInfo {
/// A unique identifier (within this process) for this module. /// A unique identifier (within this process) for this module.
/// ///

View File

@@ -7,6 +7,8 @@
use crate::trap::{Trap, TrapCode}; use crate::trap::{Trap, TrapCode};
use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition}; use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition};
use loupe::MemoryUsage;
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, BorrowMut}; use std::borrow::{Borrow, BorrowMut};
use std::cell::UnsafeCell; use std::cell::UnsafeCell;
@@ -17,14 +19,14 @@ use std::sync::Mutex;
use wasmer_types::{TableType, Type as ValType}; use wasmer_types::{TableType, Type as ValType};
/// Implementation styles for WebAssembly tables. /// Implementation styles for WebAssembly tables.
#[derive(Debug, Clone, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Hash, Serialize, Deserialize, MemoryUsage)]
pub enum TableStyle { pub enum TableStyle {
/// Signatures are stored in the table and checked in the caller. /// Signatures are stored in the table and checked in the caller.
CallerChecksSignature, CallerChecksSignature,
} }
/// Trait for implementing the interface of a Wasm table. /// Trait for implementing the interface of a Wasm table.
pub trait Table: fmt::Debug + Send + Sync { pub trait Table: fmt::Debug + Send + Sync + MemoryUsage {
/// Returns the style for this Table. /// Returns the style for this Table.
fn style(&self) -> &TableStyle; fn style(&self) -> &TableStyle;
@@ -103,7 +105,7 @@ pub trait Table: fmt::Debug + Send + Sync {
} }
/// A table instance. /// A table instance.
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
pub struct LinearTable { pub struct LinearTable {
// TODO: we can remove the mutex by using atomic swaps and preallocating the max table size // TODO: we can remove the mutex by using atomic swaps and preallocating the max table size
vec: Mutex<Vec<VMCallerCheckedAnyfunc>>, vec: Mutex<Vec<VMCallerCheckedAnyfunc>>,
@@ -117,7 +119,7 @@ pub struct LinearTable {
/// A type to help manage who is responsible for the backing table of the /// A type to help manage who is responsible for the backing table of the
/// `VMTableDefinition`. /// `VMTableDefinition`.
#[derive(Debug)] #[derive(Debug, MemoryUsage)]
enum VMTableDefinitionOwnership { enum VMTableDefinitionOwnership {
/// The `VMTableDefinition` is owned by the `Instance` and we should use /// The `VMTableDefinition` is owned by the `Instance` and we should use
/// its table. This is how a local table that's exported should be stored. /// its table. This is how a local table that's exported should be stored.

View File

@@ -5,13 +5,14 @@
use core::fmt::{self, Display, Formatter}; use core::fmt::{self, Display, Formatter};
use core::str::FromStr; use core::str::FromStr;
use loupe_derive::MemoryUsage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
/// A trap code describing the reason for a trap. /// A trap code describing the reason for a trap.
/// ///
/// All trap instructions have an explicit trap code. /// All trap instructions have an explicit trap code.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error)] #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize, Deserialize, Error, MemoryUsage)]
#[repr(u32)] #[repr(u32)]
pub enum TrapCode { pub enum TrapCode {
/// The current stack space was exhausted. /// The current stack space was exhausted.

View File

@@ -9,10 +9,11 @@ use crate::instance::Instance;
use crate::memory::Memory; use crate::memory::Memory;
use crate::table::Table; use crate::table::Table;
use crate::trap::{Trap, TrapCode}; use crate::trap::{Trap, TrapCode};
use loupe::MemoryUsage; use loupe::{MemoryUsage, MemoryUsageTracker, POINTER_BYTE_SIZE};
use std::any::Any; use std::any::Any;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt; use std::fmt;
use std::mem;
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
use std::sync::Arc; use std::sync::Arc;
use std::u32; use std::u32;
@@ -50,8 +51,14 @@ impl std::cmp::PartialEq for VMFunctionEnvironment {
} }
} }
impl MemoryUsage for VMFunctionEnvironment {
fn size_of_val(&self, _: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
}
}
/// An imported function. /// An imported function.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone, MemoryUsage)]
#[repr(C)] #[repr(C)]
pub struct VMFunctionImport { pub struct VMFunctionImport {
/// A pointer to the imported function body. /// A pointer to the imported function body.
@@ -189,7 +196,7 @@ pub enum VMFunctionKind {
/// The fields compiled code needs to access to utilize a WebAssembly table /// The fields compiled code needs to access to utilize a WebAssembly table
/// imported from another instance. /// imported from another instance.
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[repr(C)] #[repr(C)]
pub struct VMTableImport { pub struct VMTableImport {
/// A pointer to the imported table description. /// A pointer to the imported table description.
@@ -227,7 +234,7 @@ mod test_vmtable_import {
/// The fields compiled code needs to access to utilize a WebAssembly linear /// The fields compiled code needs to access to utilize a WebAssembly linear
/// memory imported from another instance. /// memory imported from another instance.
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[repr(C)] #[repr(C)]
pub struct VMMemoryImport { pub struct VMMemoryImport {
/// A pointer to the imported memory description. /// A pointer to the imported memory description.
@@ -265,7 +272,7 @@ mod test_vmmemory_import {
/// The fields compiled code needs to access to utilize a WebAssembly global /// The fields compiled code needs to access to utilize a WebAssembly global
/// variable imported from another instance. /// variable imported from another instance.
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[repr(C)] #[repr(C)]
pub struct VMGlobalImport { pub struct VMGlobalImport {
/// A pointer to the imported global variable description. /// A pointer to the imported global variable description.
@@ -337,6 +344,16 @@ unsafe impl Send for VMMemoryDefinition {}
/// correctness in a multi-threaded context is concerned. /// correctness in a multi-threaded context is concerned.
unsafe impl Sync for VMMemoryDefinition {} unsafe impl Sync for VMMemoryDefinition {}
impl MemoryUsage for VMMemoryDefinition {
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
if tracker.track(self.base as *const _ as *const ()) {
POINTER_BYTE_SIZE * (self.current_length as usize)
} else {
0
}
}
}
impl VMMemoryDefinition { impl VMMemoryDefinition {
/// Do an unsynchronized, non-atomic `memory.copy` for the memory. /// Do an unsynchronized, non-atomic `memory.copy` for the memory.
/// ///
@@ -446,6 +463,16 @@ pub struct VMTableDefinition {
pub current_elements: u32, pub current_elements: u32,
} }
impl MemoryUsage for VMTableDefinition {
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
if tracker.track(self.base as *const _ as *const ()) {
POINTER_BYTE_SIZE * (self.current_elements as usize)
} else {
0
}
}
}
#[cfg(test)] #[cfg(test)]
mod test_vmtable_definition { mod test_vmtable_definition {
use super::VMTableDefinition; use super::VMTableDefinition;
@@ -500,11 +527,17 @@ impl fmt::Debug for VMGlobalDefinitionStorage {
} }
} }
impl MemoryUsage for VMGlobalDefinitionStorage {
fn size_of_val(&self, _: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
}
}
/// The storage for a WebAssembly global defined within the instance. /// The storage for a WebAssembly global defined within the instance.
/// ///
/// TODO: Pack the globals more densely, rather than using the same size /// TODO: Pack the globals more densely, rather than using the same size
/// for every type. /// for every type.
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[repr(C, align(16))] #[repr(C, align(16))]
pub struct VMGlobalDefinition { pub struct VMGlobalDefinition {
storage: VMGlobalDefinitionStorage, storage: VMGlobalDefinitionStorage,
@@ -751,7 +784,7 @@ impl Default for VMSharedSignatureIndex {
/// The VM caller-checked "anyfunc" record, for caller-side signature checking. /// The VM caller-checked "anyfunc" record, for caller-side signature checking.
/// It consists of the actual function pointer and a signature id to be checked /// It consists of the actual function pointer and a signature id to be checked
/// by the caller. /// by the caller.
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[repr(C)] #[repr(C)]
pub struct VMCallerCheckedAnyfunc { pub struct VMCallerCheckedAnyfunc {
/// Function body. /// Function body.

View File

@@ -10,6 +10,8 @@ use crate::lib::std::boxed::Box;
use crate::lib::std::marker::PhantomData; use crate::lib::std::marker::PhantomData;
use crate::lib::std::ops::{Index, IndexMut}; use crate::lib::std::ops::{Index, IndexMut};
use crate::lib::std::slice; use crate::lib::std::slice;
use loupe::{MemoryUsage, MemoryUsageTracker};
use std::mem;
/// A slice mapping `K -> V` allocating dense entity references. /// A slice mapping `K -> V` allocating dense entity references.
/// ///
@@ -144,6 +146,21 @@ where
} }
} }
impl<K, V> MemoryUsage for BoxedSlice<K, V>
where
K: EntityRef,
V: MemoryUsage,
{
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
+ self
.elems
.iter()
.map(|value| value.size_of_val(tracker) - mem::size_of_val(value))
.sum::<usize>()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -12,8 +12,10 @@ use crate::lib::std::marker::PhantomData;
use crate::lib::std::ops::{Index, IndexMut}; use crate::lib::std::ops::{Index, IndexMut};
use crate::lib::std::slice; use crate::lib::std::slice;
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use loupe::{MemoryUsage, MemoryUsageTracker};
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::mem;
/// A primary mapping `K -> V` allocating dense entity references. /// A primary mapping `K -> V` allocating dense entity references.
/// ///
@@ -236,6 +238,21 @@ where
} }
} }
impl<K, V> MemoryUsage for PrimaryMap<K, V>
where
K: EntityRef,
V: MemoryUsage,
{
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
+ self
.elems
.iter()
.map(|value| value.size_of_val(tracker) - mem::size_of_val(value))
.sum::<usize>()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -11,12 +11,14 @@ use crate::lib::std::marker::PhantomData;
use crate::lib::std::ops::{Index, IndexMut}; use crate::lib::std::ops::{Index, IndexMut};
use crate::lib::std::slice; use crate::lib::std::slice;
use crate::lib::std::vec::Vec; use crate::lib::std::vec::Vec;
use loupe::{MemoryUsage, MemoryUsageTracker};
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{ use serde::{
de::{Deserializer, SeqAccess, Visitor}, de::{Deserializer, SeqAccess, Visitor},
ser::{SerializeSeq, Serializer}, ser::{SerializeSeq, Serializer},
Deserialize, Serialize, Deserialize, Serialize,
}; };
use std::mem;
/// A mapping `K -> V` for densely indexed entity references. /// A mapping `K -> V` for densely indexed entity references.
/// ///
@@ -279,6 +281,21 @@ where
} }
} }
impl<K, V> MemoryUsage for SecondaryMap<K, V>
where
K: EntityRef,
V: Clone + MemoryUsage,
{
fn size_of_val(&self, tracker: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
+ self
.elems
.iter()
.map(|value| value.size_of_val(tracker) - mem::size_of_val(value))
.sum::<usize>()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -1,11 +1,12 @@
use crate::indexes::{FunctionIndex, GlobalIndex, MemoryIndex, TableIndex}; use crate::indexes::{FunctionIndex, GlobalIndex, MemoryIndex, TableIndex};
use crate::lib::std::boxed::Box; use crate::lib::std::boxed::Box;
use loupe_derive::MemoryUsage;
#[cfg(feature = "enable-serde")] #[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// A WebAssembly table initializer. /// A WebAssembly table initializer.
#[derive(Clone, Debug, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Hash, Serialize, Deserialize, MemoryUsage)]
pub struct TableInitializer { pub struct TableInitializer {
/// The index of a table to initialize. /// The index of a table to initialize.
pub table_index: TableIndex, pub table_index: TableIndex,
@@ -19,7 +20,7 @@ pub struct TableInitializer {
/// A memory index and offset within that memory where a data initialization /// A memory index and offset within that memory where a data initialization
/// should be performed. /// should be performed.
#[derive(Clone, Debug)] #[derive(Clone, Debug, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct DataInitializerLocation { pub struct DataInitializerLocation {
/// The index of the memory to initialize. /// The index of the memory to initialize.
@@ -45,7 +46,7 @@ pub struct DataInitializer<'data> {
/// As `DataInitializer` but owning the data rather than /// As `DataInitializer` but owning the data rather than
/// holding a reference to it /// holding a reference to it
#[derive(Debug, Clone)] #[derive(Debug, Clone, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct OwnedDataInitializer { pub struct OwnedDataInitializer {
/// The location where the initialization is to be performed. /// The location where the initialization is to be performed.

View File

@@ -57,7 +57,7 @@ impl fmt::Display for Type {
} }
} }
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
/// The WebAssembly V128 type /// The WebAssembly V128 type
pub struct V128(pub(crate) [u8; 16]); pub struct V128(pub(crate) [u8; 16]);
@@ -311,7 +311,7 @@ impl From<&FunctionType> for FunctionType {
} }
/// Indicator of whether a global is mutable or not /// Indicator of whether a global is mutable or not
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum Mutability { pub enum Mutability {
/// The global is constant and its value does not change /// The global is constant and its value does not change
@@ -347,7 +347,7 @@ impl From<Mutability> for bool {
} }
/// WebAssembly global. /// WebAssembly global.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct GlobalType { pub struct GlobalType {
/// The type of the value stored in the global. /// The type of the value stored in the global.
@@ -390,7 +390,7 @@ impl fmt::Display for GlobalType {
} }
/// Globals are initialized via the `const` operators or by referring to another import. /// Globals are initialized via the `const` operators or by referring to another import.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub enum GlobalInit { pub enum GlobalInit {
/// An `i32.const`. /// An `i32.const`.
@@ -441,7 +441,7 @@ impl GlobalInit {
/// Tables are contiguous chunks of a specific element, typically a `funcref` or /// Tables are contiguous chunks of a specific element, typically a `funcref` or
/// an `externref`. The most common use for tables is a function table through /// an `externref`. The most common use for tables is a function table through
/// which `call_indirect` can invoke other functions. /// which `call_indirect` can invoke other functions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct TableType { pub struct TableType {
/// The type of data stored in elements of the table. /// The type of data stored in elements of the table.
@@ -480,7 +480,7 @@ impl fmt::Display for TableType {
/// ///
/// Memories are described in units of pages (64KB) and represent contiguous /// Memories are described in units of pages (64KB) and represent contiguous
/// chunks of addressable memory. /// chunks of addressable memory.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, MemoryUsage)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct MemoryType { pub struct MemoryType {
/// The minimum number of pages in the memory. /// The minimum number of pages in the memory.

View File

@@ -2,6 +2,7 @@
//! done as separate steps. //! done as separate steps.
use crate::engine::DummyEngine; use crate::engine::DummyEngine;
use loupe_derive::MemoryUsage;
#[cfg(feature = "serialize")] #[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
@@ -21,6 +22,7 @@ use wasmer_vm::{
/// Serializable struct for the artifact /// Serializable struct for the artifact
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(MemoryUsage)]
pub struct DummyArtifactMetadata { pub struct DummyArtifactMetadata {
pub module: Arc<ModuleInfo>, pub module: Arc<ModuleInfo>,
pub features: Features, pub features: Features,
@@ -34,10 +36,11 @@ pub struct DummyArtifactMetadata {
/// ///
/// This artifact will point to fake finished functions and trampolines /// This artifact will point to fake finished functions and trampolines
/// as no functions are really compiled. /// as no functions are really compiled.
#[derive(MemoryUsage)]
pub struct DummyArtifact { pub struct DummyArtifact {
metadata: DummyArtifactMetadata, metadata: DummyArtifactMetadata,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>, finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,
#[memoryusage(ignore)]
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>, finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>, finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>, signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,