3.0.2 • Published 1 year ago

pidusage v3.0.2

Weekly downloads
1,002,638
License
MIT
Repository
github
Last release
1 year ago

pidusage

Lint MacOS Ubuntu Windows Alpine Code coverage npm version license

Cross-platform process cpu % and memory usage of a PID.

Synopsis

Ideas from https://github.com/arunoda/node-usage but with no C-bindings.

Please note that if you need to check a Node.JS script process cpu and memory usage, you can use process.cpuUsage and process.memoryUsage since node v6.1.0. This script remain useful when you have no control over the remote script, or if the process is not a Node.JS process.

Usage

var pidusage = require('pidusage')

pidusage(process.pid, function (err, stats) {
  console.log(stats)
  // => {
  //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //   memory: 357306368,    // bytes
  //   ppid: 312,            // PPID
  //   pid: 727,             // PID
  //   ctime: 867000,        // ms user + system time
  //   elapsed: 6650000,     // ms since the start of the process
  //   timestamp: 864000000  // ms since epoch
  // }
  cb()
})

// It supports also multiple pids
pidusage([727, 1234], function (err, stats) {
  console.log(stats)
  // => {
  //   727: {
  //     cpu: 10.0,            // percentage (from 0 to 100*vcore)
  //     memory: 357306368,    // bytes
  //     ppid: 312,            // PPID
  //     pid: 727,             // PID
  //     ctime: 867000,        // ms user + system time
  //     elapsed: 6650000,     // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   },
  //   1234: {
  //     cpu: 0.1,             // percentage (from 0 to 100*vcore)
  //     memory: 3846144,      // bytes
  //     ppid: 727,            // PPID
  //     pid: 1234,            // PID
  //     ctime: 0,             // ms user + system time
  //     elapsed: 20000,       // ms since the start of the process
  //     timestamp: 864000000  // ms since epoch
  //   }
  // }
})

// If no callback is given it returns a promise instead
const stats = await pidusage(process.pid)
console.log(stats)
// => {
//   cpu: 10.0,            // percentage (from 0 to 100*vcore)
//   memory: 357306368,    // bytes
//   ppid: 312,            // PPID
//   pid: 727,             // PID
//   ctime: 867000,        // ms user + system time
//   elapsed: 6650000,     // ms since the start of the process
//   timestamp: 864000000  // ms since epoch
// }

// Avoid using setInterval as they could overlap with asynchronous processing
function compute(cb) {
  pidusage(process.pid, function (err, stats) {
    console.log(stats)
    // => {
    //   cpu: 10.0,            // percentage (from 0 to 100*vcore)
    //   memory: 357306368,    // bytes
    //   ppid: 312,            // PPID
    //   pid: 727,             // PID
    //   ctime: 867000,        // ms user + system time
    //   elapsed: 6650000,     // ms since the start of the process
    //   timestamp: 864000000  // ms since epoch
    // }
    cb()
  })
}

function interval(time) {
  setTimeout(function() {
    compute(function() {
      interval(time)
    })
  }, time)
}

// Compute statistics every second:
interval(1000)

// Above example using async/await
const compute = async () => {
  const stats = await pidusage(process.pid)
  // do something
}

// Compute statistics every second:
const interval = async (time) => {
  setTimeout(async () => {
    await compute()
    interval(time)
  }, time)
}

interval(1000)

Compatibility

PropertyLinuxFreeBSDNetBSDSunOSmacOSWinAIXAlpine
cpuℹ️
memory
pid
ctime
elapsed
timestamp

✅ = Working ℹ️ = Not Accurate ❓ = Should Work ❌ = Not Working

Please if your platform is not supported or if you have reported wrong readings file an issue.

By default, pidusage will use procfile parsing on most unix systems. If you want to use ps instead use the usePs option:

pidusage(pid, {usePs: true})

API

pidusage(pids, options = {}, callback) ⇒ Promise.<Object>

Get pid informations.

Kind: global function Returns: Promise.<Object> - Only when the callback is not provided. Access: public

ParamTypeDescription
pidsNumber | Array.<Number> | String | Array.<String>A pid or a list of pids.
optionsobjectOptions object. See the table below.
callbackfunctionCalled when the statistics are ready. If not provided a promise is returned instead.

options

Setting the options programatically will override environment variables

ParamTypeEnvironment variableDefaultDescription
usePsbooleanPIDUSAGE_USE_PSfalseWhen true uses ps instead of proc files to fetch process information
maxagenumberPIDUSAGE_MAXAGE60000Max age of a process on history.

PIDUSAGE_SILENT=1 can be used to remove every console message triggered by pidusage.

pidusage.clear()

If needed this function can be used to delete all in-memory metrics and clear the event loop. This is not necessary before exiting as the interval we're registring does not hold up the event loop.

Related

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

@cotep/role-and-taskhttpin-serviceryoma_rss@aburkov/exologdna-workerstritanbot-express-monitorelectron-process-monitormenlolab-runnergrapherjsblitz-utils@gelatonetwork/js-resolver-sdk@infinitebrahmanuniverse/nolb-pidelectron-ptrsshub@everything-registry/sub-chunk-2435forgeapikoishi-plugin-pokemon-for-qqbot@mai1211/koishi-plugin-pokemon@aburkov/graveldeseruntliberoserver-status-monitorsimple-apm-agentservice_logshady-backgroundshady-workersimpmonitsobixegg-plugin-monitorsocket-process-monitorsocialstack-huddle-serverstats-influxdbssrerdimensions-aidimensions-ai-temp@discolytics/corersshub-serverlessrtview-nodemonrtp-monitoring-metricsrunsa-pm2role-and-taskreflect-nexusremacrenpmrestifyrestify-legacyrestify-new-nodejs-compatibleeasyportspark-node-metricselectron-process-reporterelectron-recross-toolssmc-projecteasy-node-clustercaliper-core@bishopcais/cog-cli@capaj/artilleryclustycluster-adapter-pm2cluster-remote-controlcode-olymp-testercodecheck@clusterio/slave@clusterio/hostconsole-agentcophacopromatic-koa-monitor@cocalc/project@conga/framework-dashboard@conga/framework-profilerps-tree-usageprotractor-memory@crdtech/express-node-metrics@cozy-workflows/cli@cromwell/server@crosswing/cli@bunchtogether/ffmpeg-process-manager@bibigpt/rsshubcdktf-cliclean-scripts@cutii/pm2cpu-memory-monitor@cryptonteam/pm2-zabbix@brandon.cs/bnjsblinksocks-guibittorrent-relaybitclock-agent15v-pm2@arcblock/forge-clibtc2-rpc-explorerbtc-rpc-explorerbtc-dashboard@comet/dev-process-manager@davidkel/caliper-coreqless-js@dottoxd/statusmonitor-processdeclarative-crawler@667/restifybhs-pm2@airiot/pm2@9cats/rsshub
3.0.2

1 year ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.18

4 years ago

2.0.17

6 years ago

2.0.16

6 years ago

2.0.15

6 years ago

2.0.14

6 years ago

2.0.12

6 years ago

2.0.13

6 years ago

2.0.11

6 years ago

2.0.10

6 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.0

6 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.1

9 years ago

0.1.0

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago