Comment by herobird

Comment by herobird 8 days ago

29 replies

> Every iteration of the loop polls the network and input drivers, draws the desktop interface, runs one step of each active WASM application, and flushes the GPU framebuffer.

This is really interesting and I was wondering how you implemented that using Wasmi. Seems like the code for that is here:

https://github.com/Askannz/munal-os/blob/2d3d361f67888cb2fe8...

It might interest you that newer versions of Wasmi (v0.45+) extended the resumable function call feature to make it possible to yield upon running out of fuel: https://docs.rs/wasmi/latest/wasmi/struct.TypedFunc.html#met...

Seeing that you are already using Wasmi's fuel metering this might be a more efficient or failure proof approach to execute Wasm apps in steps.

An example for how to do this can be found in Wasmi's own Wast runner: https://github.com/wasmi-labs/wasmi/blob/019806547aae542d148...

Gazoche 8 days ago

Thanks again for making Wasmi :)

> It might interest you that newer versions of Wasmi (v0.45+) extended the resumable function call feature to make it possible to yield upon running out of fuel:

That is really interesting! I remember looking for something like that in the Wasmi docs at some point but it must have been before that feature was implemented. I would probably have chosen a different design for the WASM apps if I had it.

  • herobird 7 days ago

    I am really sorry I have waited so long to extend Wasmi's resumable calls with this very useful feature. :S Feel free to message me if you ever plan to adjust your design to make use of it.

    • Gazoche 7 days ago

      Please don't take it as a reproach! Not your fault at all, and at least it forced me into creative problem-solving ;)

9d 8 days ago

Not OP, but I'm confused how this would be helpful. You're saying for example, he can use this function to create a coroutine out of a function, begin it, and if the function fails by e.g. running out of memory, you can give the module more memory and then resume the coroutine? If so, how is that different than what naturally happens? Does wasm not have try/catch? Also, wouldn't the module then need to back up manually and retry the malloc after it failed? I'm so lost.

  • herobird 8 days ago

    Great question!

    Wasmi's fuel metering can be thought of as is there was an adjustable counter and for each instruction that Wasmi executes this counter is decreased by some amount. If it reached 0 the resumable call will yield back to the host (in this case the OS) where it can be decided how to, or if, the call shall be resumed.

    For efficiency reasons fuel metering in Wasmi is not implemented as described above but I wanted to provide a simple description.

    With this, one is no longer reliant on clocks or on other measures to provide each call its own time frame by providing an amount of fuel for each Wasm app that can be renewed (or not) when it runs out of fuel. So this is useful for building a Wasm scheduler.

    • 112233 7 days ago

      Is it deterministic? I.e. would running the same function "time out" at the exactly same state, if run in different environments?

    • pimeys 7 days ago

      We used fuel metering with wasmtime, but that made everything quite slow, certain things veeery slow.

      How is the performance when using fuel with wasmi?

      We are considering to use epoch counter, but for now we just turned fuel off.

      • phickey 7 days ago

        Wasmtime's epoch system was designed specifically to have a much, much lower performance impact than fuel metering, at the cost of being nondeterministic. Since different embeddings have different needs there, wasmtime provides both mechanisms. Turning epochs on should be trivial if your system provides any sort of concurrency: https://github.com/bytecodealliance/wasmtime/blob/main/examp...

      • herobird 7 days ago

        I don't know how fuel metering in Wasmtime works and what its overhead is but keep in mind that Wasmi is an interpreter based Wasm runtime whereas Wasmtime generates machine code (JIT).

        In past experiments I remember that fuel metering adds roughly 5-10% overhead to Wasmi executions. The trick is to not bump or decrease a counter for every single executed instruction but instead to group instructions together in so-called basic blocks and bump a counter for the whole group of instructions.

        This is also the approach that is implemented by certain Wasm tools to add fuel metering to an existing Wasm binary.

        • huem0n 6 days ago

          This is really cool stuff. I've always wanted fuel-based work with a high level programming languages. Having a language compile to wasm with wasmi now seems like a nice way to achieve that.

      • 9d 7 days ago

        I had no idea what fuel is until this discussion.

        What's the rationale? Just preventing infinite loops from hanging the host?

        If the inefficiency is the counter, what if you just calculated an instruction offset - start < threshold every once in a while?

        This probably makes no sense, ignore it, I'm way in over my head.

        [1] https://github.com/bytecodealliance/wasmtime/issues/4109

        [2] https://github.com/bytecodealliance/wasmtime/blob/main/examp...

    • 9d 8 days ago

      > Great question!

      Thanks! I have lots more too. Are there directions in space? What kind of matter is fire made of? If you shine a laser into a box with one-way mirrors on the inside, will it reflect forever? Do ants feel like they're going in regular motion and we're just going in slow motion? Why do people mainly marry and make friends with people who look extraordinarily similar to themselves? How do futures work in Rust? Why is the C standard still behind a paywall? Let me know if you need any more great questions.

      • coolcoder613 7 days ago

        Flame is what you see when gases burn in the air. As the material burns, it breaks down and releases flammable gases, which burn too, giving the effect of flame. If you have ever tried burning fine-grade steel wool, you will have seen that it burns without any flame because the iron burns directly without making gases first.

      • lukan 7 days ago

        "If you shine a laser into a box with one-way mirrors on the inside, will it reflect forever?"

        No, because each reflection comes at a cost (some light transformed to heat)

        "Why do people mainly marry and make friends with people who look extraordinarily similar to themselves?"

        To not get so much surprises and have a more stable life. (I didn't choose that path.)

        (But I feel it would be too much OT answering the other questions and don't want to distract from this great submission or the interesting Wasmi concept)

  • huem0n 6 days ago

    > Does wasm not have try/catch

    Not currently. There's an accepted proposal, but its in progress.

apitman 7 days ago

It's awesome that Wasmi is fast enough to run GUI apps. I'm working on an app runtime for making highly portable GUI apps. I'm targeting wasm because it seems to strike a good balance between performance and implementation simplicity. Ideally it would be possible to run apps on a runtime hacked together by a small team or even a single person. The fact that an interpreted (if highly optimized) wasm runtime like Wasmi is clearly capable of running GUI apps is exciting.