Go deep into nodejs module OS – deal with operating system


ReadosThis paper studies several interesting problems in the documentation of the module

  • 🤔 identify the operating system platform
  • 🤔 understand and calculate “average load”
  • Understand and calculate “CPU utilization”
  • Understand and calculate “memory usage”
  • 🤔 view running time

Identify operating system platform

Nodejs providesos.platform()andos.type(), which can be used to identify the operating system platform. Recommended use:os.platform()

Understand and calculate “average load”

The average load refers to the average number of processes in which the system is in a running and non interruptible state in a unit time. It has no direct relationship with CPU utilization.

The runnable state here refers to the process that is using the CPU or waiting for the CPU. The non interruptible state refers to the process in the key process of kernel state.

In nodejs, call directlyos.loadavg()You can get an average load of 1, 5, and 15 minutes, which is in combination with UNIX commandsuptimeThe return value is the same.

Why should we care about the average load? Because there are two types of processes, the first is “CPU intensive”, which has high CPU utilization and average load; the second is “IO intensive”, which does not necessarily have high CPU utilization, but waiting for IO will cause high average load. Therefore, CPU utilization and average load reflect system performance together.

The most ideal state for the average number of active processes is the number of CPUs = average load. If the number of CPUs < average load, the average load is too high.

//Judge whether the average load is too high
function isHighLoad() {
    const cpuNum = os.cpus().length;
    return os.loadavg().map(item => item > cpuNum);

Understand and calculate “CPU usage”

Many monitoring software provide “real-time” monitoring for CPU utilization, of course, this real-time is not real-time, there is a time difference. How can nodejs implement this function?

First stepEncapsulationgetCPUInfo(), calculate the total time spent getting the CPU and the time spent in idle mode.

 *Get the total CPU time and idle mode time
function getCPUInfo() {
    const cpus = os.cpus();
    let user = 0,
        nice = 0,
        sys = 0,
        idle = 0,
        irq = 0,
        total = 0;

    cpus.forEach(cpu => {
        const { times } = cpu;
        user += times.user;
        nice += times.nice;
        sys += times.sys;
        idle += times.idle;
        irq += times.irq;

    total = user + nice + sys + idle + irq;

    return {

The second step: at the current time point T1, select a time difference interval, calculate the CPU time difference and idle mode time difference between T1 and T1 + interval, and return1 - idle time difference / CPU time difference。 The result is the average CPU usage within the time difference interval.

function getCPUUsage(interval = 1000) {
    const startInfo = getCPUInfo();

    return new Promise(resolve => {
        setTimeout(() => {
            const endInfo = getCPUInfo();

            const idleDiff = startInfo.idle - endInfo.idle;
            const totalDiff = startInfo.total - endInfo.total;
            resolve(1 - Math.abs(idleDiff / totalDiff));
        }, interval);

Use as follows:

Getcpuuusage(). Then (usage = > console. Log ("CPU usage:", usage));

Understand and calculate “memory usage”

The indexes of CPU include average load, CPU utilization, and memory utilization.

With the nodejs interface, the implementation is very simple:

function getMemUsage() {
    return 1 - os.freemem() / os.totalmem();

View run time

  • Nodejs run time:process.uptime()
  • System operation time:os.uptime()

Reference link

  • Node.js os doc
  • Third party extension library: OS utils
  • How to understand the average load (1)
  • Byte order (size end) understanding

At the end

  1. I think it’s good,Give me a recommendation, your support is the biggest incentive for me
  2. Welcome to my public address:Xintan blog, focus only onFront end + algorithmOriginal sharing of

Due to limited personal energy, many series and historical articles are not synchronized in real time,Please go to “front end map” & “algorithm problem solving”To ensure that you get something.