Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how libuv threads in nodejs utilize multi core cpu

I am not able to find out whether libuv in node.js uses multi core cpus or it runs all of its threads in on single core only using time slicing? As node.js is single threaded but libuv has its own thread pool so does it use all the cores of multi core cpu?

like image 442
Praveen Pandey Avatar asked Sep 20 '15 12:09

Praveen Pandey


People also ask

How does libuv work in node JS?

libuv: libuv is a C library originally written for Node. js to abstract non-blocking I/O operations. Event-driven asynchronous I/O model is integrated. It allows the CPU and other resources to be used simultaneously while still performing I/O operations, thereby resulting in efficient use of resources and network.

Is libuv multithreaded?

Yes, libuv has a thread pool.

Does NodeJS support multiple cores?

Node. js absolutely does scale on multi-core machines. Yes, Node. js is one-thread-per-process.

How node js is multi-threaded?

js's built-in asynchronous I/O operations are more efficient than workers can be. Having said that, each thread will use the same Node. js architecture which is single-threaded based. You can achieve multithreading by generating multiple nodes or Node.

How to run a NodeJS application on multiple CPU cores?

Luckily, Node.js has a core module named Cluster that helps us run a Node.js application using all the CPU cores on a machine. In this article, we'll create a basic Node.js application and run an instance of it on each CPU core the host machine has.

Why do we need multithreading in Node JS?

essentially validating our concept. But there is also a corner of the entire node ecosystem that uses multithreading by moving out of the event loop environment. This happens because a big part of the entire node ecosystem is coded using c++ code and a special javascript bridge is used by nodeJs to execute those codes.

What is libuv in Node JS?

To prevent this, libuv is used in Node.js which facilitates a non-blocking I/O. It also has mechanisms to handle services like File System, DNS, network, child processes, pipes, signal handling, polling, and streaming.

How to increase Node JS performance?

How To Increase Node JS Performance The recommendation is to set the UV_THREADPOOL_SIZE to the number of logical cores your machine is running. In my case I will set the thread pool size to 12. It makes no sense setting the size to anything more than the logical cores your hardware is running and could actually result in poorer performance.


Video Answer


1 Answers

It does leverage multicores via the threadpool. e.g., on Linux, the underlying pthreads will uses multiple cores for multiple threads.

If you run the following code, you will notice 4 (default threadpool size) cores will peg at 100% as file system IO are running with the threadpool.

var util = require('util');
var fs = require('fs');

for (var i = 0; i < 300000; i++) {
    (function(id) {
        fs.readdir('.', function() {
           console.log(util.format('readdir %d finished.', id));
       });
    })(i);
}
like image 106
Xin Liu Avatar answered Sep 21 '22 11:09

Xin Liu