SunilWang / node-os-utils

OS Utils - An operating system utility library.
https://www.npmjs.com/package/node-os-utils
MIT License
125 stars 19 forks source link

cpu count show wrong on MacBook pro intel chip #32

Closed Ross249 closed 1 year ago

Ross249 commented 2 years ago

my macbook pro have 8 core , but osu.cpu.count() show 16 not 8.

ulri-me commented 1 year ago

@Ross249 This may or may not be a real issue.

If your MacBook Pro uses an Intel CPU, then this CPU supports hyper-threading, a technology and trademark developed/registered by Intel.

What is this?! : An operating system is running tens to hundreds of tasks "simultaneously". In reality those tasks are handled one after another, and each task processing requires copying the task's data from RAM to the CPU's embedded cache memory, on which the CPU can perform calculations. Then the calculation results are copied back to the RAM of your computer.

The actual calculation is blazing fast while copying data from and to the RAM is quite slow in comparison. Therefore hyper-threading presents your OS two seperate CPU cores which each core being associated with a certain embedded cache section of the single physically existing CPU core. Imagine your OS has to handle three tasks. Your OS copies the data of task 1 from RAM to the embedded cache of virtual CPU core 1. As soon as this task data is copied, the single existing CPU core starts the necessary calculations. While this happens, the OS is already copying the data of task 2 to the embedded cache of virtual CPU core 2. As soon as the CPU finished calculations, it can instantly continue with the calculations for task 2. In the meanwhile the OS copies the resulted data of task 1 back to RAM and copies the data of task 3 to the embedded cache of virtual CPU core 1. Now the calculations of task 2 ended and the CPU can instantly continue with the calculations for task 3 while the OS copies back the results of the calculations of task 2 back to RAM.

In essence, with this technology your CPU can always run under full load, while the OS can perform the (in comparison) slow copying of task data from and to RAM in the background. Switching between task processing is also called context switching, is this process is massively boosted with this technology.

But due to all this, your OS show's the duplicate physical OS cores count, as it is really thinking that it can copy the data of a task to this logical CPU's cache.

That's also the difference between physical CPU cores (8 cores in your case, so 8 real calculation units), and logical CPU cores (16 in your case, so 16 CPU cache sections for task data storage).

To answer another upcoming question: No, your OS cannot detect the difference between physical and logical CPU cores, and that's good actually. The only issue is that you can't perfectly know how much performance boost this tec offers, as it only speeds up context switching instead of the actual CPU calculations.

And as your OS doesn't even know the difference, it provides this library the "wrong" logical CPU core count as well, instead of the real physical CPU core count.

ulri-me commented 1 year ago

Guess this issue can be closed :-).