Re: luajit state CPU time limit

  • From: Александр Киранов <xhaskx@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx, mike-1411@xxxxxxxxxx
  • Date: Mon, 10 Nov 2014 09:40:30 +0300

Hello Mike,

I have a question about JIT-ed code.
Is there a way to set some killswitch boolean variable or some flag in
lua_State before making JIT-ed code to abort it execution from outside
executing script (other OS thread)?
For example, JIT-ed code can check that killswitch flag/variable every N-th
operations (N = 1e+3, 1e+6 or similar).

Somebody can write script that is falling into infinite loop and I need
ability to abort that script without aborting entire OS process/thread.

Thanks!

---
Best regards,
Alex

2014-11-05 16:05 GMT+03:00 Theo Schlossnagle <jesus@xxxxxxxxxx>:

> The solution I'd like to see is very similar to what you're implying.  I'd
> love the luaJIT vm and JITted code to check for a deadman timer ever so
> often (configurable or every 100k instructions).  Then I could set a timer
> that would set the deadman and then the lua coroutine in question would
> notice and error out unwinding everything cleanly.
>
> I've not spent enough time looking into the implementation to understand
> how hard this would be.
>
> Mike would likely have insight on how difficult this would be or if it is
> wrought with peril and is a bad approach.
>
> Best regards,
>
> Theo
>
> On Wed, Nov 5, 2014 at 1:59 PM, Александр Киранов <xhaskx@xxxxxxxxx>
> wrote:
>
>> Thanks, Theo, I realized that there is nothing to do with already running
>> JITed code.
>>
>> Other question: can I change luajit С source somewhere to force it to
>> produce JITed code with some custom function call every Nth operations? (N
>> = 1000, 1 000 000 or similar)
>> Or to check some pre-defined bool variable before each Nth operation?
>>
>>
>> ---
>> С уважением,
>> Киранов Александр
>>
>> 2014-11-05 14:53 GMT+03:00 Theo Schlossnagle <jesus@xxxxxxxxxx>:
>>
>>> The real challenge here is that if you "mess up" and get in an infinited
>>> JITted loop, there's nothing to be done about it.
>>>
>>> I think the only way to do this is by using setitimer() w/ ITIMER_PROF
>>> to interrupt the thread and deal with it within the signal handler.  The
>>> bad part about this is that I haven't figured out how to get LuaJIT to
>>> unwind and cleanup cleanly.
>>>
>>> On Wed, Nov 5, 2014 at 12:47 PM, Александр Киранов <xhaskx@xxxxxxxxx>
>>> wrote:
>>>
>>>> Thanks everybody for proposals.
>>>>
>>>> Seems I found a solution - call lua_sethook() from different thread, if
>>>> I decided to terminate script execution (measured big CPU time 
>>>> consumption).
>>>> It's working in usual lua implementation.
>>>> But it's not working in luajit - hook is not called immediately after
>>>> lua_sethook() call in other thread, only at script end.
>>>> Example: http://pastebin.com/HdnfVQKB
>>>>
>>>> Main question is: is there other way to interrupt lua_State from other
>>>> thread? Consistency of interrupted lua_State is not necessary but
>>>> preferred, I only need return from luaL_dostring() in main thread with
>>>> non-zero result.
>>>>
>>>>
>>>>
>>>>
>>>> ---
>>>> С уважением,
>>>> Киранов Александр
>>>>
>>>> 2014-11-05 14:02 GMT+03:00 Victor Bombi <sonoro@xxxxxxxxxxxxxx>:
>>>>
>>>>>  May be using luaLanes and lane priorities?
>>>>> I have not tested in linux but only on win32
>>>>>
>>>>> ----- Original Message -----
>>>>> *From:* Alexander Altshuler <altexy@xxxxxxxxx>
>>>>> *To:* luajit@xxxxxxxxxxxxx
>>>>> *Sent:* Wednesday, November 05, 2014 10:54 AM
>>>>> *Subject:* Re: luajit state CPU time limit
>>>>>
>>>>>  On 4 November 2014 22:47, Javier Guerra Giraldez <javier@xxxxxxxxxxx>
>>>>> wrote:
>>>>>
>>>>>> On Tue, Nov 4, 2014 at 2:34 PM, Александр Киранов <xhaskx@xxxxxxxxx>
>>>>>> wrote:
>>>>>> > So, when some lua_State consumed too many CPU I need to know it and
>>>>>> handle
>>>>>> > it on LUA or on C level.
>>>>>>
>>>>>>
>>>>>> better handle it at the OS level.  Linux includes many different tools
>>>>>> to limit resources.  LXC and cgroups spring to mind.  I would start
>>>>>> here: https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
>>>>>>
>>>>>>
>>>>> But you will have to decompose your application into some processes to
>>>>> get proper control granularity.
>>>>>
>>>>> Regards
>>>>> Alexander
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>>
>>> Theo Schlossnagle
>>>
>>> http://omniti.com/is/theo-schlossnagle
>>>
>>
>>
>
>
> --
>
> Theo Schlossnagle
>
> http://omniti.com/is/theo-schlossnagle
>

Other related posts: