How do node.js and libuv use the different threads?

1000 Views Asked by At

While doing some research on Node.js and libuv I started a simple node server with UV_THREADPOOL_SIZE=1 and looked at pstree to see how much threads it really uses.

for Node.js 0.10 pstree -p | grep node produces

node(5157)-+-{node}(5158) `-{node}(5162)

To make things a bit more complicated I also tried the same with 0.12 and iojs 3.3. The number of threads differs for every version.

Total number of threads vs thread pool size

0.10: UV_THREADPOOL_SIZE + 1

0.12: UV_THREADPOOL_SIZE + 2

3.3: UV_THREADPOOL_SIZE + 4

I also tried to set higher numbers for the thread pool size to make sure that I'm not below some minimum value.

My questions are:

  1. What is executed on the main process (5157) and what on the threads below?
  2. Can we assume that (5158) is the thread id of libuv here and that (5162) is a single "worker" thread?
  3. What happens in 0.12 and iojs 3.3? Why are there up to 4 threads more than configured?
1

There are 1 best solutions below

1
On BEST ANSWER

I can't really speak for the Node/V8 part, but in general amy libuv application will use at least 1 + UV_THREADPOOL_SIZE threads.

The first thread is the one which runs the libuv loop by calling uv_run, which is usually the main thread. libuv then has a thread pool of UV_THREADPOLL_SIZE size, where filesystem and DNS operations are run. Network i/o is performed in the loop thread by using epoll / kqueue, etc.