admin管理员组

文章数量:1315252

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:

With NO node.js script running:

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!

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:

With NO node.js script running:

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!

Share Improve this question edited Jan 11, 2020 at 19:04 Nyxynyx asked Jan 11, 2020 at 17:16 NyxynyxNyxynyx 63.7k163 gold badges507 silver badges856 bronze badges 3
  • AFAIK at least garbage collector runs in its own thread. – szatkus Commented Jan 11, 2020 at 17:19
  • There are four instances of node are running (first picture). – Xaqron Commented Jan 11, 2020 at 19:13
  • @Xaqron I have only ran the node.js script once. Other than the garbage collector, what can the other 3 processes be for? Handling asynchronous function calls? – Nyxynyx Commented Jan 12, 2020 at 0:59
Add a ment  | 

2 Answers 2

Reset to default 7

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 municates via events.

The fact that multiple node instances are running (while you ran the script once) means this is neither an internal node mechanism nor using worker threads by script to utilize CPU.

Either the script or one of the libraries used by it, is trying to utilize CPU via the old fashion forking (child process) way which results in isolated (no shared memory) instances of node (multiple processes as in your case) which municate via IPC.

本文标签: javascriptIs This Single NodeJS App Using Multiple CoresStack Overflow