os
Node.js提供了一些基本的底层操作系统的模块OS.
api
var os = require('os');
console.log('[arch] 操作系统CPU架构'+os.arch());
console.log('[cpus] 每个CPU/内核的信息:'+JSON.stringify(os.cpus()));
console.log('[endianness] CPU 的字节序:'+os.endianness());
console.log('[freemem] 操作系统空闲内存量:'+os.freemem());
console.log('[homedir] 当前用户使用的文件夹:'+os.homedir());
console.log('[hostname] 操作系统的主机名:'+os.hostname());
console.log('[loadavg] 平均负载:'+os.loadavg());
console.log('[networkInterfaces] 网络接口:'+JSON.stringify(os.networkInterfaces()));
console.log('[platform] 操作系统名:'+os.platform());
console.log('[release] 操作系统的发行版本:'+os.release());
console.log('[tmpdir] 操作系统的默认临时文件夹:'+os.tmpdir());
console.log('[totalmem] 系统内存总量:'+os.totalmem());
console.log('[type] 操作系统名:'+os.type());
console.log('[uptime] 操作系统运行的时间:'+os.uptime());
[arch] 操作系统CPU架构x64
[cpus] 每个CPU/内核的信息:[{"model":"Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz","speed":2794,"times":{"user":12349500,"nice":0,"sys":13773515,"idle":76721437,"irq":1744218}},{"model":"Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz","speed":2794,"times":{"user":12447781,"nice":0,"sys":11801625,"idle":78594843,"irq":320156}},{"model":"Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz","speed":2794,"times":{"user":13889359,"nice":0,"sys":12343843,"idle":76611046,"irq":141578}},{"model":"Intel(R) Core(TM) i5-4200H CPU @ 2.80GHz","speed":2794,"times":{"user":12556328,"nice":0,"sys":11338265,"idle":78949640,"irq":128375}}]
[endianness] CPU 的字节序:LE
[freemem] 操作系统空闲内存量:2904788992
[homedir] 当前用户使用的文件夹:C:\Users\Administrator
[hostname] 操作系统的主机名:SKYUSER-9PEIINO
[loadavg] 平均负载:0,0,0
[networkInterfaces] 网络接口:{"VMware Network Adapter VMnet1":[{"address":"xxxxxxxxxxx","netmask":"ffff:ffff:ffff:ffff::","family":"IPv6","mac":"00:50:56:c0:00:01","scopeid":21,"internal":false},{"address":"192.168.80.1","netmask":"255.255.255.0","family":"IPv4","mac":"00:50:56:c0:00:01","internal":false}],"VMware Network Adapter VMnet8":
.......,"scopeid":0,"internal":false},{"address":"fe80::8cd:96a1:2214:bcfb","netmask":"ffff:ffff:ffff:ffff::","family":"IPv6","mac":"00:00:00:00:00:00","scopeid":8,"internal":false}]}
[platform] 操作系统名:win32
[release] 操作系统的发行版本:10.0.10240
[tmpdir] 操作系统的默认临时文件夹:C:\Users\ADMINI~1\AppData\Local\Temp
[totalmem] 系统内存总量:8473526272
[type] 操作系统名:Windows_NT
[uptime] 操作系统运行的时间:501123.2912315
与操作系统
读了 os 模块的文档,研究了几个有意思的问题:
- 识别操作系统平台
- 理解和计算“平均负载”
- 理解和计算“cpu 使用率”
- 理解和计算“内存使用率”
- 查看运行时间
识别操作系统平台
nodejs 提供了os.platform()和os.type(),可以用来识别操作系统平台。推荐使用: os.platform()
理解和计算“平均负载”
平均负载是指:单位时间内,系统处于可运行状态和不可中断状态的平均进程数。它和 cpu 使用率没有直接关系。
其中,这里的可运行状态指的是:正在使用 cpu 或正在等待 cpu 的进程。不可中断状态指的是:内核态关键流程中的进程。
在 nodejs 中,直接调用os.loadavg()可以获得 1、5 和 15 分钟的平均负载,它和 unix 命令uptime返回值一样。
为什么需要关心平均负载这个问题呢?因为进程分为 2 种,第一种就是“CPU 密集型”,它的 cpu 使用率和平均负载都是高的;第二种是“IO 密集型”,它的 cpu 使用率不一定高,但是等待 IO 会造成平均负载高。所以,cpu 使用率和平均负载共同反应系统性能。
平均活跃进程数最理想的状态是 cpu 数量=平均负载,如果 cpu 数量 < 平均负载,那么平均负载过高。
// 判断是否平均负载过高
function isHighLoad() {
const cpuNum = os.cpus().length;
return os.loadavg().map(item => item > cpuNum);
}
理解和计算“cpu 使用率”
很多监控软件都提供针对 cpu 使用率的“实时”监控,当然这个实时不是真的实时,有个时间差。这个功能,nodejs 如何实现呢?
第一步:封装getCPUInfo(),计算获取 cpu 花费的总时间与空闲模式花费的时间。
/**
* 获取cpu花费的总时间与空闲模式的时间
*/
function getCPUInfo() {
const cpus = os.cpus();
let user = 0,
nice = 0,
sys = 0,
idle = 0,
irq = 0,
total = 0;
cpus.forEach(<span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">cpu</span> =></span> {
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> { times } = cpu;
user += times.user;
nice += times.nice;
sys += times.sys;
idle += times.idle;
irq += times.irq;
});
total = user + nice + sys + idle + irq;
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> {
total,
idle
};
第二步:当前时间点 t1,选定一个时间差 intervel,计算 t1 和 t1 + interval 这两个时间点的 cpu 时间差与空闲模式时间差,返回 1 - 空闲时间差 / cpu时间差。返回的结果就是时间差 intervel 内的平均 cpu 使用率。
function getCPUUsage(interval = 1000) {
const startInfo = getCPUInfo();
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">return</span> <span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">new</span> <span class="hljs-built_in" style="color: #0086b3; line-height: 26px;">Promise</span>(<span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">resolve</span> =></span> {
setTimeout(<span class="hljs-function" style="line-height: 26px;"><span class="hljs-params" style="line-height: 26px;">()</span> =></span> {
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> endInfo = getCPUInfo();
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> idleDiff = startInfo.idle - endInfo.idle;
<span class="hljs-keyword" style="color: #333; font-weight: bold; line-height: 26px;">const</span> totalDiff = startInfo.total - endInfo.total;
resolve(<span class="hljs-number" style="color: #008080; line-height: 26px;">1</span> - <span class="hljs-built_in" style="color: #0086b3; line-height: 26px;">Math</span>.abs(idleDiff / totalDiff));
}, interval);
});
使用方式如下:
getCPUUsage().then(usage => console.log("cpu使用率:", usage));
理解和计算“内存使用率”
cpu 的指标有平均负载、cpu 使用率,内存的指标有内存使用率。
借助 nodejs 接口,实现非常简单:
function getMemUsage() {
return 1 - os.freemem() / os.totalmem();
}
查看运行时间
- nodejs 运行时间:process.uptime()
- 系统运行时间:os.uptime()