Stream: wasmtime

Topic: Memory Used By Wasm


view this post on Zulip Cwasi4wasm (Mar 10 2023 at 16:23):

What would be the best way to get memory currently used by the wasm? Right now the only thing that comes into my mind is subtracting the last offset I get from my wasm program from the base pointer of the data

view this post on Zulip Lann Martin (Mar 10 2023 at 16:45):

From the guest or the host?

view this post on Zulip Cwasi4wasm (Mar 10 2023 at 17:21):

This would be from the host. Interested in the size of the current mem used by my wasm sandbox

view this post on Zulip Lann Martin (Mar 10 2023 at 18:51):

I have this snippet, which sums the size of linear memories owned by a wasmtime::Instance

    let memories = instance
        .exports(&mut store)
        .flat_map(|export| export.into_memory())
        .collect::<Vec<_>>();
    let memory_used = &memories
        .into_iter()
        .map(|mem| mem.size(&store))
        .sum::<u64>();

view this post on Zulip Lann Martin (Mar 10 2023 at 18:51):

I don't recall exactly why it has to be so convoluted... maybe because of how it borrows store? :thinking:

view this post on Zulip Lann Martin (Mar 10 2023 at 18:55):

Oh and that count is wasm pages, so * 64KB for bytes

view this post on Zulip Cwasi4wasm (Mar 10 2023 at 19:01):

thank you for the code! Very sorry I didn't explain it properly. Not looking for the page size but memory in use by the sandbox so I guess this would be less than or equal to the size of the sandbox

view this post on Zulip Lann Martin (Mar 10 2023 at 19:07):

There isn't one clear answer to your question. Some of the resources used by an instance will be shared across all instances or all instances of a single module.

view this post on Zulip Lann Martin (Mar 10 2023 at 19:07):

I don't think there are any global statistics for memory usage tracked by wasmtime

view this post on Zulip Cwasi4wasm (Mar 10 2023 at 19:29):

I see, I think the best I can do right now is try to get an estimate, I will do more research on this. Thank you

view this post on Zulip Alex Crichton (Mar 10 2023 at 19:49):

For tracking wasm memory used within a Store you can use the Store::limiter interface which will get notified when wasm grows memory


Last updated: Nov 22 2024 at 16:03 UTC