os.cpus()方法是os模塊的內置應用程序編程接口,用於獲取有關計算機的每個邏輯CPU內核的信息。
用法:
os.cpus()
參數:此方法不接受任何參數。
返回:此方法返回一個對象,其中包含有關每個邏輯CPU內核的信息。每個返回的對象將包含以下屬性:
- model:一個字符串,指定CPU內核的型號。
- speed:一個數字,指定CPU內核的速度(以MHz為單位)。
- times:包含以下屬性的對象:
- user:一個數字指定CPU在用戶模式下花費的時間(以毫秒為單位)。
- nice:數字以毫秒為單位指定CPU在正常模式下花費的時間。
- sys:一個數字指定CPU在sys模式下花費的時間(以毫秒為單位)。
- idle:一個數字指定CPU在空閑模式下花費的時間(以毫秒為單位)。
- irq:一個數字指定CPU在irq模式下花費的時間(以毫秒為單位)。
注意:的nice
值僅用於POSIX。在Windows操作係統上,nice
所有處理器的值始終為0。
以下示例說明了Node.js中os.cpus()方法的使用:
範例1:
// Node.js program to demonstrate the
// os.cpus() method
// Allocating os module
const os = require('os');
// Printing os.cpus() values
console.log(os.cpus());
輸出:
[ { 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 } } ]
範例2:
// Node.js program to demonstrate the
// os.cpus() method
// Allocating os module
const os = require('os');
// Printing os.cpus()
var cpu_s=os.cpus();
var 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);
輸出:
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
注意:上麵的程序將通過使用node index.js
命令。
參考: https://nodejs.org/api/os.html#os_os_cpus
相關用法
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 Node.js | os.cpus() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。