Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is This Single Node.JS App Using Multiple Cores?

On an Ubuntu 18.04 system running only 1 Node.js script that ingests a data feed, htop shows that both CPU cores are being utilized.

With 1 node script running: enter image description here

With NO node.js script running: enter image description here

This Node.js script has multiple event listeners that receives data, does some data processing and sends them to a remote database server.

foo.on('msg', msg => { setImmediate(() => do_work(msg)) } );

Question: Why does it appear as if both CPU cores are being utilized rather equally by Node, despite Node.js being single threaded? CPU load splits observed are 60%/40% and 50%/50%

Is it actually utilizing both CPU cores? Or simply switching between them really quickly all the time, but only really utilizing 1 core at any one time? In other words, such a scenario will cause the system to choke when the CPU workload is above 1 core's worth but lesser than 2 core's.

Basically, I like to know whether a single-core system will suffice for this work load. Thank you!

like image 665
Nyxynyx Avatar asked Jan 11 '20 17:01

Nyxynyx


People also ask

How do you tell if a program is using multiple cores?

Right-click the taskbar and select Task Manager from the context menu. Go to the Performance tab and select CPU. Right-click on the graph in the right pane and select Change graph to>Logical processors. You will see a graph for each core and its usage.

Is NodeJS single threaded?

Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model mainly based on Javascript Event based model with Javascript callback mechanism.

Can 1 process use multiple cores?

Yes, a single process can run multiple threads on different cores. Caching is specific to the hardware. Many modern Intel processors have three layers of caching, where the last level cache is shared across cores.

What applications use multiple cores?

The following are examples of CPU-hungry applications that can take advantage of multiple cores: Photo and video editing apps— Adobe Photoshop, Adobe Premier, iMovie. 3D modeling and rendering programs — AutoCAD, Solidworks. Graphics-intensive games — Overwatch, Star Wars Battlefront.


1 Answers

Even though only a single JS function (per environment)1 will execute at any given time, on one core, that doesn't mean

  • that it always is the same core - the OS can move the thread at will (see Why does a single threaded process execute on several processors/cores?, Single-threaded application being balanced across CPU cores?, Why is a single thread spread across CPU's?, or How does a single thread run on multiple cores?)
  • that the node.js runtime itself is singlethreaded. It might do processing of asynchronous work in background threads, it might run garbage collection (see Can garbage collection happen while the main thread is busy? or Garbage Collector and concurrent marking in V8), and more.

1: you mention that you're running only a single script, which means there is only one realm of JS objects. Another possibility for a node.js app to run on multiple threads is to spawn workers, each of which has its own JS environment and communicates via events.

like image 171
Bergi Avatar answered Sep 29 '22 07:09

Bergi