Limiting execution time

2,089 views
Skip to first unread message

Matt

unread,
Sep 2, 2008, 6:31:38 PM9/2/08
to v8-users
Hi,

Is there any way to limit the execution time of a script in v8 to no
more than X seconds, or no more than X javascript statements?

Thanks,
Matt

Feng Qian

unread,
Sep 2, 2008, 6:46:29 PM9/2/08
to v8-u...@googlegroups.com
No directly in V8, but V8 has stack overflow checks, which can be used to do the purpose.

Matt

unread,
Sep 2, 2008, 7:04:28 PM9/2/08
to v8-users
Hm. Would you mind giving me some pointers as to how I might do this?

Feng Qian

unread,
Sep 2, 2008, 8:00:42 PM9/2/08
to v8-u...@googlegroups.com
V8 API has a class called Locker, it has a static method called "StartPreemption", you can look at the implementation. it uses stack limit checks to do it.

Sorry I am not the expert in this area, the code should explain itself.

Jong Hian Zin

unread,
Sep 2, 2008, 8:49:49 PM9/2/08
to v8-users
Does this stack overflow checks protect against infinite loops? is
there no Rhino's observeInstructionCount equivalent in V8?

On Sep 3, 8:00 am, "Feng Qian" <feng.qian...@gmail.com> wrote:
> V8 API has a class called Locker, it has a static method called
> "StartPreemption", you can look at the implementation. it uses stack limit
> checks to do it.
>
> Sorry I am not the expert in this area, the code should explain itself.
>

Feng Qian

unread,
Sep 2, 2008, 11:01:13 PM9/2/08
to v8-u...@googlegroups.com
Stack limit check is put on the entry point of a function and the back-edge of loops, so infinite loops
will call stack limit check in each iteration.

V8 does not provide instruction counts since the source code is compiled into native code.
Unless you use CPU hardware monitors, it would be nearly impossible to count machine
instructions.

V8 does have an ARM simulator which interprets generated ARM instructions, so the simulator
is able to count instructions. And again, JS code often calls into C++ runtime code,
so the instruction count is not accurate.

i3x1...@gmail.com

unread,
Sep 3, 2008, 1:40:22 AM9/3/08
to v8-users
This is an interesting question. I could be completely wrong about
this, so please, someone correct me if I am, but it seems threading
works like this-

They expect you to have one instance of V8 per process, which means
one virtual machine ("processor") that is capable of actually running
code (probably has to do with the fact that all threads have to share
the same address space and whatnot). In order to facilitate multiple
scripts running at once, they provide a Locker class to effectively
create a "processor" scheduler, where different threads can request
exclusive access to the "processor" and then serialize their execution
state and yield to another thread when they are done. Now, with this
scheme, there is the problem that a thread can hold exclusive access
to the "processor" for a rude amount of time, and so V8 provides a way
for the implementer to tell the "processor" to force preemption after
a certain amount of time so that no one may, for example, hold
exclusive access for more than 100ms before letting someone else get a
chance to do some work.

If you're willing to jump into the V8 code a little, you could
probably count the number of times the VM forces preemption on a
thread and kill the thread if it's doing it too much.

I don't know about the whole stack limit checks thing. This is just
what I've deduced from the code, and, again, I could be completely
wrong.

On Sep 2, 11:01 pm, "Feng Qian" <feng.qian...@gmail.com> wrote:
> Stack limit check is put on the entry point of a function and the back-edge
> of loops, so infinite loopswill call stack limit check in each iteration.
>
> V8 does not provide instruction counts since the source code is compiled
> into native code.
> Unless you use CPU hardware monitors, it would be nearly impossible to count
> machine
> instructions.
>
> V8 does have an ARM simulator which interprets generated ARM instructions,
> so the simulator
> is able to count instructions. And again, JS code often calls into C++
> runtime code,
> so the instruction count is not accurate.
>
Reply all
Reply to author
Forward
0 new messages