Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

License

NotificationsYou must be signed in to change notification settings

soyuka/pidusage

Repository files navigation

LintMacOSUbuntuWindowsAlpineCode coveragenpm versionlicense

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

Synopsis

Ideas fromhttps://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 useprocess.cpuUsage andprocess.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

varpidusage=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 pidspidusage([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 insteadconststats=awaitpidusage(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 processingfunctioncompute(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()})}functioninterval(time){setTimeout(function(){compute(function(){interval(time)})},time)}// Compute statistics every second:interval(1000)// Above example using async/awaitconstcompute=async()=>{conststats=awaitpidusage(process.pid)// do something}// Compute statistics every second:constinterval=async(time)=>{setTimeout(async()=>{awaitcompute()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 readingsfile an issue.

By default, pidusage will useprocfile parsing on most unix systems. If you want to useps instead use theusePs option:

pidusage(pid, {usePs: true})

API

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

Get pid informations.

Kind: global functionReturns: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.
[options]objectOptions object. See the table below.
[callback]functionCalled when the statistics are ready. If not provided a promise is returned instead.

options

Setting the options programatically will override environment variables

ParamTypeEnvironment variableDefaultDescription
[usePs]booleanPIDUSAGE_USE_PSfalseWhen true usesps instead of proc files to fetch process information
[maxage]numberPIDUSAGE_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 ofcontributors who participated in this project.

License

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

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp