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!
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.
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.
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.
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.
Even though only a single JS function (per environment)1 will execute at any given time, on one core, that doesn't mean
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With