Node.js os.cpus() Method
The os.cpus() method is an inbuilt application programming interface of the os module which is used to get information about each logical CPU core of the computer.
Syntax:
os.cpus()
Parameters: This method does not accept any parameters.
Return: This method returns an object containing information about each logical CPU core. Each of the returned objects will contain the following attributes:
- model: A string that specifies the model of the CPU core.
- speed: A number that specifies the speed of the CPU core (in MHz).
- times: An Object that contains the following properties:
- user: A number specifies the time that the CPU has spent in user mode in milliseconds.
- nice: A number specifies the time that the CPU has spent in nice mode in milliseconds.
- sys: A number specifies the time that the CPU has spent in sys mode in milliseconds.
- idle: A number specifies the time that the CPU has spent in idle mode in milliseconds.
- irq: A number specifies the time that the CPU has spent in irq mode in milliseconds.
Note: The nice values are used for POSIX Only. On the Windows operating system, the nice value of all processors is always 0.
Example 1: The below example illustrates the use of os.cpus() method in Node.js:
javascript
// Node.js program to demonstrate the // os.cpus() method // Allocating os module const os = require( 'os' ); // Printing os.cpus() values console.log(os.cpus()); |
Output:
[ { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 900000, nice: 0, sys: 940265, idle: 11928546, irq: 147046 } }, { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 860875, nice: 0, sys: 507093, idle: 12400500, irq: 27062 } }, { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 1273421, nice: 0, sys: 618765, idle: 11876281, irq: 13125 } }, { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 943921, nice: 0, sys: 460109, idle: 12364453, irq: 12437 } } ]
Example 2: The below example illustrates the use of os.cpus() method in Node.js:
javascript
// Node.js program to demonstrate the // os.cpus() method // Allocating os module const os = require( 'os' ); // Printing os.cpus() let cpu_s = os.cpus(); let no_of_logical_core = 0; cpu_s.forEach(element => { no_of_logical_core++; console.log( "Logical core " + no_of_logical_core + " :" ); console.log(element); }); console.log( "total number of logical core is " + no_of_logical_core); |
Output:
Logical core 1 : { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 856437, nice: 0, sys: 866203, idle: 11070046, irq: 133562 } } Logical core 2 : { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 805296, nice: 0, sys: 462656, idle: 11524406, irq: 23218 } } Logical core 3 : { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 1225062, nice: 0, sys: 566421, idle: 11000875, irq: 12203 } } Logical core 4 : { model: 'Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz', speed: 2712, times: { user: 900234, nice: 0, sys: 420000, idle: 11472125, irq: 11781 } } total number of logical core is 4
Note: The above program will compile and run by using the node index.js command.
Reference: https://nodejs.org/api/os.html#os_os_cpus
Please Login to comment...