Monitor : Resource usage monitoring

更新时间:
2024-05-14
下载文档

Monitor : Resource usage monitoring

This module provides multi-process resource usage monitoring functions, such as how many CPU resources and how many memory resources are occupied by processes in the system.

This module only allow Privileged Mode system services to use.

User can use the following code to import the Monitor module.

var Monitor = require('monitor');

Support

The following shows Monitor module APIs available for each permissions.

 User ModePrivilege Mode
Monitor 
monitor.cpuUsage 
monitor.memUsage 
monitor.networkUsage

Monitor Class

new Monitor()

  • Returns: {Object} A monitor object.

Create a monitor object.

Example

var monitor = new Monitor();

Monitor Object

monitor.cpuUsage([time])

  • time {Integer} Measurement time in milliseconds. default: 1000.
  • Returns: {Object} Measurement result.

Measure CPU usage and return an object, this object have to items: usage {Array} and cores {Array}. Each member of the usage array is an object, this object contains the following members:

  • pid {Integer} Process ID. Zero means the kernel.
  • usage {Number} CPU usage percentage.

Each member of the cores array is a number, which specifies the usage of each CPU cores.

Example

var res = monitor.cpuUsage();
if (res) {
  res.usage.forEach((item) => {
    console.log('pid:', item.pid, 'CPU usage:', item.usage);
  });
  for (var i = 0; i < res.cores.length; i++) {
    console.log(`Core ${i} usage:`, res.cores[i]);
  }
}

monitor.cpuUsage(time, callback)

  • time {Integer} Measurement time in milliseconds. default: 1000.
  • callback {Function} Callback function at the end of measurement.
    • usage {Array} All process CPU usage.
    • cores {Array} All CPU cores usage.

Use the asynchronous mode to measure the CPU usage. The callback parameter is an array of measurement results.

Example

monitor.cpuUsage(1000, (usage, cores) => {
  usage.forEach((item) => {
    console.log('pid:', item.pid, 'CPU usage:', item.usage);
  });
  for (var i = 0; i < cores.length; i++) {
    console.log(`Core ${i} usage:`, cores[i]);
  }
});

monitor.memUsage([pid])

  • pid {Integer} Specify process. default: -1 all process.
  • Returns: {Array} | {Integer} Memory usage by each process or specify process memory usage.

Get memory usage and return an array, each member of the array is an object, this object contains the following members:

  • pid {Integer} Process ID. Zero means the kernel.
  • usage {Integer} Memory usage. Unit is Bytes.

Example

var usage = monitor.memUsage();
if (usage) {
  usage.forEach((item) => {
    console.log('pid:', item.pid, 'MEM usage:', item.usage, 'Bytes');
  });
}

var usage = monitor.memUsage(10);
console.log('pid: 10 MEM usage:', usage, 'Bytes');

monitor.networkUsage()

  • Returns: {Object} Current process network traffic usage.

This method only gets the current process network traffic usage. The network traffic data object contains the following members:

  • send {Object} Statistics of sent data.
    • generic {Integer} Number of general data transmission bytes.
    • multimedia {Integer}* Number of bytes sent by the multimedia library, such as a webcam.
  • recv {Object} Statistics of receive data.
    • generic {Integer} Number of general data receive bytes.
    • multimedia {Integer}* Number of bytes receive by the multimedia library, such as a webcam.

Example

var usage = monitor.networkUsage();

console.log('Current App total send:', usage.send.generic + usage.send.multimedia);
文档内容是否对您有所帮助?
有帮助
没帮助