Comment by 9d

Comment by 9d 8 days ago

24 replies

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...

      • herobird 7 days ago

        Yes, rational is to provide a pragmatic and efficient solution to infinite loops.

        There is a variety of ways to implement fuel metering with varying trade-offs, e.g. performance, determinism and precision.

        In this comment I roughly described how Wasmi implements its fuel metering: https://news.ycombinator.com/item?id=44229953

        Wasmi's design focuses on performance and determinism but isn't as precise since instructions are always considered as group.

      • conradev 7 days ago

        I first encountered this with gas in the Ethereum VM. For Ethereum, they price different operations to reflect their real world cost: storing something forever on the blockchain is expensive whereas multiplying numbers is cheap

        I’m not sure what it’s used for in this context or how instructions are weighted

        • pimeys 7 days ago

          Let's consider that you create a serverless platform which runs wasm/wasi code. The code can do an infinite loop and suck resources while blocking the thread that runs the code in the host. Now, with a fuel mechanism the code yields after a certain amount of instructions, giving the control back to the host. The host can then do things such as stop the guest from running, or store the amount of fuel to some database, bill the user and continue execution.

  • 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.

      • 9d 7 days ago

        I was told it was plasma. Who is wrong, them or you? Either way, I can't trust one of you...

        • coolcoder613 7 days ago

          Perhaps you should look it up yourself? Plasma is not found in flame, but it is in lightning.

    • 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)

      • 9d 7 days ago

        No, I do not accept this. There must be a way. What if the mirror box has a high enough heat? Would it work then? The box could be made of a heat resistant material, like fiberglass.

huem0n 6 days ago

> Does wasm not have try/catch

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