Wanting to understand V8 WorkerThread(s)

227 views
Skip to first unread message

Joel Scarfone

unread,
May 13, 2019, 2:49:14 PM5/13/19
to v8-users
Hi All! I have a very simple program as follows that just creates an isolate, then sleeps:

#include <libplatform/libplatform.h>
#include <v8-platform.h>
#include <v8.h>

#include <stdio.h>
#include <unistd.h>

using v8::Isolate;

int main() {

   std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
   v8::V8::InitializePlatform(platform.get());
   v8::V8::Initialize();

   v8::Isolate::CreateParams create_params;
   create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
   Isolate* isolate = v8::Isolate::New(create_params);


   printf("Sleeping...\n");
   usleep(1000 * 1000 * 100);
   printf("Done\n");
   return 0;
}

When i run this program, i can then check the number of threads the process has created with ps -T -p <process_id>, and i see that on my 8 core machine, v8 creates 7 extra threads all named "V8 WorkerThread", and on my 16 core machine i get 8 instances of this "V8 WorkerThread" created. 

I am looking to understand what determines the number of extra threads v8 spawns and what the purpose of these threads are. Thanks in advance!

Joel

Ben Noordhuis

unread,
May 14, 2019, 6:18:25 AM5/14/19
to v8-users
V8 spawns those threads for off-thread concurrent (re)compilation and
garbage collection.

You can pass a `thread_pool_size` argument to NewDefaultPlatform() to
configure its size (0 == default size == max(1, min(8,
number_of_cpus-1))) or plug in your Platform implementation.

You can also pass `--predictable` or `--single_threaded` to
V8::SetFlagsFromString() to disable the thread pool but you'll
probably take a performance hit when you do. Hope that helps!
Reply all
Reply to author
Forward
0 new messages