项目作者: soyuka

项目描述 :
Cross-platform process cpu % and memory usage of a PID
高级语言: JavaScript
项目地址: git://github.com/soyuka/pidusage.git
创建时间: 2014-05-07T17:36:47Z
项目社区:https://github.com/soyuka/pidusage

开源协议:MIT License

下载


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

  1. var pidusage = require('pidusage')
  2. pidusage(process.pid, function (err, stats) {
  3. console.log(stats)
  4. // => {
  5. // cpu: 10.0, // percentage (from 0 to 100*vcore)
  6. // memory: 357306368, // bytes
  7. // ppid: 312, // PPID
  8. // pid: 727, // PID
  9. // ctime: 867000, // ms user + system time
  10. // elapsed: 6650000, // ms since the start of the process
  11. // timestamp: 864000000 // ms since epoch
  12. // }
  13. cb()
  14. })
  15. // It supports also multiple pids
  16. pidusage([727, 1234], function (err, stats) {
  17. console.log(stats)
  18. // => {
  19. // 727: {
  20. // cpu: 10.0, // percentage (from 0 to 100*vcore)
  21. // memory: 357306368, // bytes
  22. // ppid: 312, // PPID
  23. // pid: 727, // PID
  24. // ctime: 867000, // ms user + system time
  25. // elapsed: 6650000, // ms since the start of the process
  26. // timestamp: 864000000 // ms since epoch
  27. // },
  28. // 1234: {
  29. // cpu: 0.1, // percentage (from 0 to 100*vcore)
  30. // memory: 3846144, // bytes
  31. // ppid: 727, // PPID
  32. // pid: 1234, // PID
  33. // ctime: 0, // ms user + system time
  34. // elapsed: 20000, // ms since the start of the process
  35. // timestamp: 864000000 // ms since epoch
  36. // }
  37. // }
  38. })
  39. // If no callback is given it returns a promise instead
  40. const stats = await pidusage(process.pid)
  41. console.log(stats)
  42. // => {
  43. // cpu: 10.0, // percentage (from 0 to 100*vcore)
  44. // memory: 357306368, // bytes
  45. // ppid: 312, // PPID
  46. // pid: 727, // PID
  47. // ctime: 867000, // ms user + system time
  48. // elapsed: 6650000, // ms since the start of the process
  49. // timestamp: 864000000 // ms since epoch
  50. // }
  51. // Avoid using setInterval as they could overlap with asynchronous processing
  52. function compute(cb) {
  53. pidusage(process.pid, function (err, stats) {
  54. console.log(stats)
  55. // => {
  56. // cpu: 10.0, // percentage (from 0 to 100*vcore)
  57. // memory: 357306368, // bytes
  58. // ppid: 312, // PPID
  59. // pid: 727, // PID
  60. // ctime: 867000, // ms user + system time
  61. // elapsed: 6650000, // ms since the start of the process
  62. // timestamp: 864000000 // ms since epoch
  63. // }
  64. cb()
  65. })
  66. }
  67. function interval(time) {
  68. setTimeout(function() {
  69. compute(function() {
  70. interval(time)
  71. })
  72. }, time)
  73. }
  74. // Compute statistics every second:
  75. interval(1000)
  76. // Above example using async/await
  77. const compute = async () => {
  78. const stats = await pidusage(process.pid)
  79. // do something
  80. }
  81. // Compute statistics every second:
  82. const interval = async (time) => {
  83. setTimeout(async () => {
  84. await compute()
  85. interval(time)
  86. }, time)
  87. }
  88. interval(1000)

Compatibility

Property Linux FreeBSD NetBSD SunOS macOS Win AIX Alpine
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:

  1. pidusage(pid, {usePs: true})

API

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

Get pid informations.

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

Param Type Description
pids Number \ Array. \ String \ Array. A pid or a list of pids.
[options] object Options object. See the table below.
[callback] function Called when the statistics are ready. If not provided a promise is returned instead.

options

Setting the options programatically will override environment variables

Param Type Environment variable Default Description
[usePs] boolean PIDUSAGE_USE_PS false When true uses ps instead of proc files to fetch process information
[maxage] number PIDUSAGE_MAXAGE 60000 Max 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.

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.