wasmtime-go icon indicating copy to clipboard operation
wasmtime-go copied to clipboard

How to monitor memory usage

Open vlkv opened this issue 1 year ago • 4 comments

Hello!

In my project I need to monitor memory usage inside the Wasm VM's store. Is there a corresponding API to do this?

Thank you.

vlkv avatar Nov 17 '23 09:11 vlkv

In the Rust wasmtime crate the type for this is the ResourceLimiter trait. That API is not bound in the C API which this repository uses, however, so currently this can't be done through Go. If you're interested though adding bindings for that API to the C API would be much appreciated

alexcrichton avatar Nov 17 '23 16:11 alexcrichton

@alexcrichton Hello! Thank you for your answer It seems we misunderstood. I read about ResourceLimiter.

  1. It does the same as ResourceLimiter? https://github.com/bytecodealliance/wasmtime-go/pull/171/files
  2. We need not only limiting but monitoring also. We want monitor :
    • how much memory was reserved (is about it memory.DataSize()???),
    • how much memory was consumed (consumed of reserved),
    • when will the next increase of memory.DataSize occur,

gggrafff avatar Nov 20 '23 07:11 gggrafff

Yes, indeed, my question was not about how to limit the memory, but how to know at runtime how much memory is occupied, free, etc right now.

vlkv avatar Nov 20 '23 08:11 vlkv

Ah so the API implemented in #171 is a less-powerful version of the ResourceLimiter trait. Despite its name that trait in Rust enables "listening" effectively to changes in memory. For example the trait gets an accurate snapshot of all memory used within a Store and notifications whenever memory is grown. The trait can optionally disallow some growth (hence the "limiter" in the name) but it could also only be there for accounting purposes (which I think is what you want here).

In that sense the ResourceLimiter trait is the way to do this, but that's only exposed in a limited fashion in the C API right now because otherwise it'd involve callbacks and such in C.

alexcrichton avatar Nov 20 '23 15:11 alexcrichton