Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for objects' sizes estimation #5079

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 27 additions & 16 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ tracing = { version = "0.1" }
wat = { version = "=1.0.71", optional = true }
rustc-demangle = "0.1"
shared-buffer = { workspace = true }
loupe = { version = "0.1.3", optional = true, features = [
"indexmap",
"enable-indexmap",
] }

# Dependencies and Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down Expand Up @@ -67,7 +71,9 @@ macro-wasmer-universal-test = { version = "4.3.7", path = "./macro-wasmer-univer
# Dependencies and Develoment Dependencies for `js`.
[target.'cfg(target_arch = "wasm32")'.dependencies]
# - Mandatory dependencies for `js`.
wasmer-types = { path = "../types", version = "=4.3.7", default-features = false, features = ["std"] }
wasmer-types = { path = "../types", version = "=4.3.7", default-features = false, features = [
"std",
] }
wasm-bindgen = "0.2.74"
js-sys = "0.3.51"
wasmer-derive = { path = "../derive", version = "=4.3.7" }
Expand Down Expand Up @@ -99,6 +105,11 @@ default = ["sys-default"]
# default = ["js-default"]
std = []
core = ["hashbrown"]
artifact-size = [
"dep:loupe",
"wasmer-vm/artifact-size",
"wasmer-compiler/artifact-size",
]

# Features for `sys`.
sys = ["wasmer-compiler/translator", "wasmer-compiler/compiler", "std"]
Expand All @@ -125,9 +136,9 @@ js-serializable-module = []

# Optional
enable-serde = [
"wasmer-vm/enable-serde",
"wasmer-compiler/enable-serde",
"wasmer-types/enable-serde",
"wasmer-vm/enable-serde",
"wasmer-compiler/enable-serde",
"wasmer-types/enable-serde",
]

wasmer-artifact-load = ["wasmer-compiler/wasmer-artifact-load"]
Expand All @@ -137,17 +148,17 @@ static-artifact-create = ["wasmer-compiler/static-artifact-create"]

[package.metadata.docs.rs]
features = [
"compiler",
"core",
"cranelift",
"engine",
"jit",
"singlepass",
"static-artifact-create",
"static-artifact-load",
"sys",
"sys-default",
"wasmer-artifact-create",
"wasmer-artifact-load",
"compiler",
"core",
"cranelift",
"engine",
"jit",
"singlepass",
"static-artifact-create",
"static-artifact-load",
"sys",
"sys-default",
"wasmer-artifact-create",
"wasmer-artifact-load",
]
rustc-args = ["--cfg", "docsrs"]
1 change: 1 addition & 0 deletions lib/api/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub enum ExportError {
///
/// TODO: add examples of using exports
#[derive(Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Exports {
map: IndexMap<String, Extern>,
}
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ mod private {
/// result in a panic.
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840)
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Function(pub(crate) function_impl::Function);

impl Function {
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::sys::externals::global as global_impl;
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#global-instances>
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Global(pub(crate) global_impl::Global);

impl Global {
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use wasmer_types::{MemoryError, Pages};
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#memory-instances>
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Memory(pub(crate) memory_impl::Memory);

impl Memory {
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/externals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::store::{AsStoreMut, AsStoreRef};
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#external-values>
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub enum Extern {
/// A external [`Function`].
Function(Function),
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::Value;
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#table-instances>
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Table(pub(crate) table_impl::Table);

impl Table {
Expand Down
Empty file modified lib/api/src/lib.rs
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions lib/api/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum IoCompileError {
/// Cloning a module is cheap: it does a shallow copy of the compiled
/// contents rather than a deep copy.
#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Module(pub(crate) module_imp::Module);

impl Module {
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use wasmer_vm::{
VMFunction, VMFunctionContext, VMFunctionKind, VMTrampoline,
};

#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Function {
pub(crate) handle: StoreHandle<VMFunction>,
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Mutability;
use wasmer_vm::{StoreHandle, VMExtern, VMGlobal};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Global {
handle: StoreHandle<VMGlobal>,
}
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Memory {
pub(crate) handle: StoreHandle<VMMemory>,
}
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{vm::VMExternTable, ExternRef, Function, RuntimeError};
use wasmer_vm::{StoreHandle, TableElement, Trap, VMExtern, VMTable};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Table {
handle: StoreHandle<VMTable>,
}
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
};

#[derive(Clone, PartialEq, Eq)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Module {
// The field ordering here is actually significant because of the drop
// order: we want to drop the artifact before dropping the engine.
Expand Down
18 changes: 13 additions & 5 deletions lib/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ thiserror = "1.0"
serde_bytes = { version = "0.11", optional = true }
smallvec = "1.6"
xxhash-rust = { version = "0.8.10", features = ["xxh64"] }
loupe = { version = "0.1.3", optional = true, features = [
"indexmap",
"enable-indexmap",
] }


backtrace = "0.3"
memmap2 = "0.6"
Expand All @@ -44,7 +49,9 @@ wasmer-vm = { path = "../vm", version = "=4.3.7" }
region = { version = "3.0" }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.59", features = ["Win32_System_Diagnostics_Debug"] }
windows-sys = { version = "0.59", features = [
"Win32_System_Diagnostics_Debug",
] }

[features]
default = ["std"]
Expand All @@ -60,15 +67,16 @@ static-artifact-create = ["wasmer-object"]
std = ["wasmer-types/std"]
core = ["hashbrown", "wasmer-types/core"]
enable-serde = ["serde", "serde_bytes", "wasmer-types/enable-serde"]
artifact-size = ["dep:loupe"]

[badges]
maintenance = { status = "experimental" }

[package.metadata.docs.rs]
features = [
"static-artifact-create",
"static-artifact-load",
"wasmer-artifact-create",
"wasmer-artifact-load",
"static-artifact-create",
"static-artifact-load",
"wasmer-artifact-create",
"wasmer-artifact-load",
]
rustc-args = ["--cfg", "docsrs"]
9 changes: 9 additions & 0 deletions lib/compiler/src/artifact_builders/artifact_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use wasmer_types::{
use wasmer_types::{MetadataHeader, SerializeError};

/// A compiled wasm module, ready to be instantiated.
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct ArtifactBuild {
serializable: SerializableModule,
}
Expand Down Expand Up @@ -287,8 +288,16 @@ self_cell!(
impl {Debug}
);

#[cfg(feature = "artifact-size")]
impl loupe::MemoryUsage for ArtifactBuildFromArchiveCell {
fn size_of_val(&self, _tracker: &mut dyn loupe::MemoryUsageTracker) -> usize {
std::mem::size_of_val(self.borrow_owner()) + std::mem::size_of_val(self.borrow_dependent())
}
}

/// A compiled wasm module that was loaded from a serialized archive.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct ArtifactBuildFromArchive {
cell: Arc<ArtifactBuildFromArchiveCell>,

Expand Down
6 changes: 6 additions & 0 deletions lib/compiler/src/engine/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use wasmer_types::{SerializableModule, SerializeError};
use wasmer_vm::{FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex, VMTrampoline};
use wasmer_vm::{InstanceAllocator, StoreObjects, TrapHandlerFn, VMConfig, VMExtern, VMInstance};

#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct AllocatedArtifact {
// This shows if the frame info has been regestered already or not.
// Because the 'GlobalFrameInfoRegistration' ownership can be transfered to EngineInner
Expand All @@ -55,13 +56,16 @@ pub struct AllocatedArtifact {
// so the GloabelFrameInfo and MMap stays in sync and get dropped at the same time
frame_info_registration: Option<GlobalFrameInfoRegistration>,
finished_functions: BoxedSlice<LocalFunctionIndex, FunctionBodyPtr>,

#[cfg_attr(feature = "artifact-size", loupe(skip))]
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
finished_function_lengths: BoxedSlice<LocalFunctionIndex, usize>,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[repr(transparent)]
/// A unique identifier for an Artifact.
pub struct ArtifactId {
Expand Down Expand Up @@ -91,6 +95,7 @@ impl Default for ArtifactId {
}

/// A compiled wasm module, ready to be instantiated.
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct Artifact {
id: ArtifactId,
artifact: ArtifactBuildVariant,
Expand All @@ -102,6 +107,7 @@ pub struct Artifact {
/// Artifacts may be created as the result of the compilation of a wasm
/// module, corresponding to `ArtifactBuildVariant::Plain`, or loaded
/// from an archive, corresponding to `ArtifactBuildVariant::Archived`.
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub enum ArtifactBuildVariant {
Plain(ArtifactBuild),
Archived(ArtifactBuildFromArchive),
Expand Down
1 change: 1 addition & 0 deletions lib/compiler/src/engine/trap/frame_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct GlobalFrameInfo {

/// An RAII structure used to unregister a module's frame information when the
/// module is destroyed.
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
pub struct GlobalFrameInfoRegistration {
/// The key that will be removed from the global `ranges` map when this is
/// dropped.
Expand Down
8 changes: 7 additions & 1 deletion lib/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
serde = { version = "1.0", features = ["derive", "rc"], optional = true, default-features = false }
serde = { version = "1.0", features = [
"derive",
"rc",
], optional = true, default-features = false }
serde_bytes = { version = "0.11", optional = true }
thiserror = "1.0"
more-asserts = "0.2"
Expand All @@ -26,6 +29,8 @@ bytecheck = "0.6.8"
xxhash-rust = { version = "0.8.8", features = ["xxh64"] }
sha2 = { version = "0.10" }
hex = { version = "^0.4" }
loupe = { version = "0.1.3", optional = true }


# `rand` uses `getrandom` transitively, and to be able to
# compile the project for `js`, we need to enable this feature
Expand All @@ -41,6 +46,7 @@ default = ["std"]
std = []
core = []
enable-serde = ["serde", "serde/std", "serde_bytes", "indexmap/serde-1"]
artifact-size = ["dep:loupe"]

[package.metadata.docs.rs]
rustc-args = ["--cfg", "docsrs"]
2 changes: 2 additions & 0 deletions lib/types/src/compilation/address_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};

/// Single source location to generated address mapping.
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, Copy, PartialEq, Eq)]
#[archive_attr(derive(rkyv::CheckBytes, Debug))]
pub struct InstructionAddressMap {
Expand All @@ -23,6 +24,7 @@ pub struct InstructionAddressMap {
}

/// Function and its instructions addresses mappings.
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[derive(RkyvSerialize, RkyvDeserialize, Archive, Debug, Clone, PartialEq, Eq, Default)]
#[archive_attr(derive(rkyv::CheckBytes, Debug))]
Expand Down
Loading
Loading