wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

How to limit cpu consumption of wasm instance?

Open victortanjh opened this issue 4 years ago • 5 comments

for example, my wasm bytecode compiled from AssemblyScript。 For a test, there is a while loop in the AssemblyScript code: function infiniteLoop(): Response { while(1){ smlog("This is infinite loop,getAccount finished, the value is :"+ArrayBuffer2Str(value)); } }

The whole process is blocked and can't recover.

My question is : Can we limit the cpu consumption of wasm instance ? Or, can we kill the wasm instance after a while.

Thank you!

victortanjh avatar Jul 31 '21 07:07 victortanjh

We can do that by adding flag checking in the branch for loop in the runtime, and use additional thread for setting the flag when the guard time expires, the runtime raises exception for the Wasm app when the flag is set. However it will introduce a big performance penalty.

If we only want to check for specific loops, we can use a new opcode like “guarded_branch” to replace the branch opcode for the loop as we have implemented this way in the fast interpreter. The new opcode will be checked for the flag. And we need to find a way to let developer annotate those loops for guard.

From: victortanjh @.> Sent: Saturday, July 31, 2021 3:54 PM To: bytecodealliance/wasm-micro-runtime @.> Cc: Subscribed @.***> Subject: [bytecodealliance/wasm-micro-runtime] How to limit cpu consumption of wasm instance? (#679)

for example, my wasm bytecode compiled from AssemblyScript。 For a test, there is a while loop in the AssemblyScript code: function infiniteLoop(): Response { while(1){ smlog("This is infinite loop,getAccount finished, the value is :"+ArrayBuffer2Str(value)); } }

The whole process is blocked and can't recover.

My question is : Can we limit the cpu consumption of wasm instance ? Or, can we kill the wasm instance after a while.

Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/bytecodealliance/wasm-micro-runtime/issues/679, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE3MKTTFL47H2IQX7VJQPDDT2OTX3ANCNFSM5BJYUUZA.

xwang98 avatar Jul 31 '21 08:07 xwang98

sorry, I still do not know how to do. Now, in my app, the libvmlib.a is linked, and I use wasm_runtime_call_wasm function to call the function in AssemblyScript. Can I call a API of libvmlib.a to set the timeout of one wasm function call?

victortanjh avatar Jul 31 '21 09:07 victortanjh

@victortanjh This function is not supported in WAMR yet. My last post is just one idea to implement your requirement. Let's put this requirement into the want list.

xwang98 avatar Jul 31 '21 13:07 xwang98

ok. Thank you very much.

victortanjh avatar Aug 02 '21 05:08 victortanjh

We can do that by adding flag checking in the branch for loop in the runtime, and use additional thread for setting the flag when the guard time expires, the runtime raises exception for the Wasm app when the flag is set. However it will introduce a big performance penalty.

Would it be a performance problem because of multithreading? What about adding some kind of callback which is called on regular basis? Would it be a performance problem even if the callback is empty? User can write whatever he wants in the callback: checking atomic boolean flag, or merely comparing current time against limit once per 1024 calls. That's how Lua does it (see lua_sethook and LUA_MASKCOUNT). I think there are users who absolutely need to protect against hanging/blocking, and they can accept the performance penalty. It's more the problem for those who don't need protection.

P.S. For the context, I organize an annual contest for student programmers, and the funniest thing in this contest is a massive programming game. You can see one example here. Each participant implements an AI program which controls his player, and all these programs run simultaneously in real time. Basically, we need a way to run 50-100 simple sandboxed programs and execute their "think" function 60 times per second on single core of an ordinary PC. Lua does this perfectly, except that we have to force all students to learn it. The holy grail would be to allow programs in C++, and that's why I'm here =)

stgatilov avatar Nov 13 '21 07:11 stgatilov