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

feat: Implement MemoryUsage for Store #2199

Merged
merged 9 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
90 changes: 56 additions & 34 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ criterion = "0.3"
lazy_static = "1.4"
wasmer-engine-dummy = { path = "tests/lib/engine-dummy" }
tempfile = "3.1"
loupe = { git = "https://github.com/wasmerio/loupe", branch = "master" }

[features]
# Don't add the compiler features in default, please add them on the Makefile
Expand Down
8 changes: 8 additions & 0 deletions examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::mem;
use std::ptr::NonNull;
use std::sync::Arc;

use loupe::{MemoryUsage, MemoryUsageTracker};
use wasmer::{
imports,
vm::{self, MemoryError, MemoryStyle, TableStyle, VMMemoryDefinition, VMTableDefinition},
Expand Down Expand Up @@ -131,6 +133,12 @@ impl<T: Tunables> Tunables for LimitingTunables<T> {
}
}

impl<T: Tunables> MemoryUsage for LimitingTunables<T> {
fn size_of_val(&self, _tracker: &mut dyn MemoryUsageTracker) -> usize {
mem::size_of_val(self)
}
}

Comment on lines +136 to +141
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit surprising to me that this isn't something the macro can generate. I'm not sure how common of a base case it is, maybe it isn't worth it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because of the generic here. I need to dig into loupe-derive to see what's going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by wasmerio/loupe#14. I'll update this file in #2200 probably.

fn main() -> Result<(), Box<dyn std::error::Error>> {
// A Wasm module with one exported memory (min: 7 pages, max: unset)
let wat = br#"(module (memory 7) (export "memory" (memory 0)))"#;
Expand Down
1 change: 1 addition & 0 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ wat = { version = "1.0", optional = true }
thiserror = "1.0"
more-asserts = "0.2"
target-lexicon = { version = "0.11", default-features = false }
loupe = { git = "https://github.com/wasmerio/loupe", branch = "master" }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = "0.3"
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::tunables::BaseTunables;
use loupe::MemoryUsage;
use std::fmt;
use std::sync::Arc;
#[cfg(all(feature = "compiler", feature = "engine"))]
Expand All @@ -15,7 +16,7 @@ use wasmer_engine::{Engine, Tunables};
/// [`Tunables`] (that are used to create the memories, tables and globals).
///
/// Spec: <https://webassembly.github.io/spec/core/exec/runtime.html#store>
#[derive(Clone)]
#[derive(Clone, MemoryUsage)]
pub struct Store {
engine: Arc<dyn Engine + Send + Sync>,
tunables: Arc<dyn Tunables + Send + Sync>,
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/tunables.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{MemoryType, Pages, TableType};
use loupe::MemoryUsage;
use std::cmp::min;
use std::ptr::NonNull;
use std::sync::Arc;
Expand All @@ -19,7 +20,7 @@ use wasmer_vm::{
/// implementation or use composition to wrap your Tunables around
/// this one. The later approach is demonstrated in the
/// tunables-limit-memory example.
#[derive(Clone)]
#[derive(Clone, MemoryUsage)]
pub struct BaseTunables {
/// For static heaps, the size in wasm pages of the heap protected by bounds checking.
pub static_memory_bound: Pages,
Expand Down
1 change: 1 addition & 0 deletions lib/compiler-cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde = { version = "1.0", features = ["derive"] }
more-asserts = "0.2"
gimli = { version = "0.23", optional = true }
smallvec = "1.6"
loupe = { git = "https://github.com/wasmerio/loupe", branch = "master" }

[dev-dependencies]
target-lexicon = { version = "0.11", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cranelift_codegen::print_errors::pretty_error;
use cranelift_codegen::{binemit, Context};
#[cfg(feature = "unwind")]
use gimli::write::{Address, EhFrame, FrameTable};
use loupe::MemoryUsage;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::CompileError;
Expand All @@ -32,6 +33,7 @@ use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};

/// A compiler that compiles a WebAssembly module with Cranelift, translating the Wasm to Cranelift IR,
/// optimizing it and then translating to assembly.
#[derive(MemoryUsage)]
pub struct CraneliftCompiler {
config: Cranelift,
}
Expand Down
Loading