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

Metering middleware. #1839

Merged
merged 15 commits into from
Dec 1, 2020
Merged

Metering middleware. #1839

merged 15 commits into from
Dec 1, 2020

Conversation

nlewycky
Copy link
Contributor

Based on https://github.com/wasmerio/wasmer-reborn/pull/129 . Original description:

  • Necessary APIs for modifying module info.
  • Global transformation.
  • Per-function transformation.
  • Testing and examples.

@webmaster128
Copy link
Contributor

Thanks for bringing this over. Happy to test this work in an external project. Just ping me when you think this makes sense.

Copy link
Contributor

@MarkMcCaskey MarkMcCaskey left a comment

Choose a reason for hiding this comment

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

Looks pretty reasonable to me!

I'd like to see an example using metering too as the place where we explain how to use it, but that can be done separately after this merges

Cargo.toml Outdated
@@ -23,6 +23,7 @@ wasmer-wasi = { version = "1.0.0-alpha5", path = "lib/wasi", optional = true }
wasmer-wast = { version = "1.0.0-alpha5", path = "tests/lib/wast", optional = true }
wasmer-cache = { version = "1.0.0-alpha5", path = "lib/cache", optional = true }
wasmer-types = { version = "1.0.0-alpha5", path = "lib/wasmer-types" }
wasmer-middlewares = { version = "1.0.0-alpha.5", path = "lib/middlewares", optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

Slight typo in the version, should drop the .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks.

Comment on lines +59 to +61
let mut module = (*compile_info.module).clone();
self.config.middlewares.apply_on_module_info(&mut module);
compile_info.module = Arc::new(module);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will leave all the old Arc<Module>s unaffected, this should be looked at closely for bugs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed that was a source of the bug fixed in 363a28c of this PR. I'm trusting that the tests will catch any real problems here. (Also, there are no old Arc's? At least not in this function?)

Copy link
Contributor

Choose a reason for hiding this comment

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

compile_info.module before it gets overwritten is pointing to the old Arc, anything that it's shared with won't get the update

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I'm not sure about its correctness in context, but it's possible that this could cause problems later

Comment on lines 136 to 147
state.push_operator(Operator::GlobalGet { global_index: self.remaining_points_index.as_u32() });
state.push_operator(Operator::I64Const { value: self.accumulated_cost as i64 });
state.push_operator(Operator::I64LtU);
state.push_operator(Operator::If { ty: WpTypeOrFuncType::Type(WpType::EmptyBlockType) });
state.push_operator(Operator::Unreachable); // FIXME: Signal the error properly.
state.push_operator(Operator::End);

// globals[remaining_points_index] -= self.accumulated_cost;
state.push_operator(Operator::GlobalGet { global_index: self.remaining_points_index.as_u32() });
state.push_operator(Operator::I64Const { value: self.accumulated_cost as i64 });
state.push_operator(Operator::I64Sub);
state.push_operator(Operator::GlobalSet { global_index: self.remaining_points_index.as_u32() });
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be ergonomic to implement https://doc.rust-lang.org/std/iter/trait.Extend.html for this, so you can put a bunch of operators in a collection like &[] and push them all in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Two new Extends.

@jubianchi jubianchi added 1.0 Wasmer at 1.0 ⏱ metering 🎉 enhancement New feature! labels Nov 25, 2020
Comment on lines +20 to +22
/// An instance of `Metering` should not be shared among different modules, since it tracks
/// module-specific information like the global index to store metering state. Attempts to use
/// a `Metering` instance from multiple modules will result in a panic.
Copy link
Contributor

Choose a reason for hiding this comment

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

If an instance of Metering can be used for one module only, and it is installed via CompilerConfig::push_middleware, does this mean you need a new compiler (and engine and store) for each metered module?

Copy link
Member

Choose a reason for hiding this comment

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

That's right. The current structure makes easier reusing the modules in headless engines.

@nlewycky nlewycky marked this pull request as ready for review November 30, 2020 23:12
@nlewycky
Copy link
Contributor Author

bors r+

bors bot added a commit that referenced this pull request Nov 30, 2020
1839: Metering middleware. r=nlewycky a=nlewycky

Based on wasmerio/wasmer-reborn#129 . Original description:

- [x] Necessary APIs for modifying module info.
- [x] Global transformation.
- [x] Per-function transformation.
- [x] Testing and examples.


Co-authored-by: losfair <[email protected]>
Co-authored-by: Syrus <[email protected]>
Co-authored-by: Nick Lewycky <[email protected]>
Co-authored-by: nlewycky <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 1, 2020

Build failed:

@syrusakbary
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Dec 1, 2020
1839: Metering middleware. r=syrusakbary a=nlewycky

Based on wasmerio/wasmer-reborn#129 . Original description:

- [x] Necessary APIs for modifying module info.
- [x] Global transformation.
- [x] Per-function transformation.
- [x] Testing and examples.


Co-authored-by: losfair <[email protected]>
Co-authored-by: Syrus <[email protected]>
Co-authored-by: Nick Lewycky <[email protected]>
Co-authored-by: nlewycky <[email protected]>
Co-authored-by: Syrus Akbary <[email protected]>
@syrusakbary
Copy link
Member

bors r-

@bors
Copy link
Contributor

bors bot commented Dec 1, 2020

Canceled.

@syrusakbary
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented Dec 1, 2020

@bors bors bot merged commit 5371543 into master Dec 1, 2020
@bors bors bot deleted the feature/middleware branch December 1, 2020 21:55
@syrusakbary syrusakbary mentioned this pull request Dec 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Wasmer at 1.0 🎉 enhancement New feature! ⏱ metering
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants