procfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const fs = require('fs')
  2. const path = require('path')
  3. const updateCpu = require('./helpers/cpu')
  4. const parallel = require('./helpers/parallel')
  5. const history = require('./history')
  6. let cpuInfo = null
  7. const Buffer = require('safe-buffer').Buffer
  8. const SIZE = 1024 // if the stat file is bigger then this I'll buy you a drink
  9. function noop () {}
  10. function open (path, history, cb) {
  11. if (history.fd) { return cb(null, history.fd) }
  12. fs.open(path, 'r', cb)
  13. }
  14. function close (history) {
  15. if (history.fd) {
  16. fs.close(history.fd, noop)
  17. }
  18. }
  19. function readUntilEnd (fd, buf, cb) {
  20. let firstRead = false
  21. if (typeof buf === 'function') {
  22. cb = buf
  23. buf = Buffer.alloc(SIZE)
  24. firstRead = true
  25. }
  26. fs.read(fd, buf, 0, SIZE, 0, function (err, bytesRead, buffer) {
  27. if (err) {
  28. cb(err)
  29. return
  30. }
  31. const data = Buffer.concat([buf, buffer], firstRead ? bytesRead : buf.length + bytesRead)
  32. if (bytesRead === SIZE) {
  33. readUntilEnd(fd, data, cb)
  34. return
  35. }
  36. cb(null, buf)
  37. })
  38. }
  39. function readProcFile (pid, options, done) {
  40. let hst = history.get(pid, options.maxage)
  41. let again = false
  42. if (hst === undefined) {
  43. again = true
  44. hst = {}
  45. }
  46. // Arguments to path.join must be strings
  47. open(path.join('/proc', '' + pid, 'stat'), hst, function (err, fd) {
  48. if (err) {
  49. if (err.code === 'ENOENT') {
  50. err.message = 'No matching pid found'
  51. }
  52. return done(err, null)
  53. }
  54. if (err) {
  55. return done(err)
  56. }
  57. readUntilEnd(fd, function (err, buffer) {
  58. if (err) {
  59. return done(err)
  60. }
  61. let infos = buffer.toString('utf8')
  62. const date = Date.now()
  63. // https://github.com/arunoda/node-usage/commit/a6ca74ecb8dd452c3c00ed2bde93294d7bb75aa8
  64. // preventing process space in name by removing values before last ) (pid (name) ...)
  65. const index = infos.lastIndexOf(')')
  66. infos = infos.substr(index + 2).split(' ')
  67. // according to http://man7.org/linux/man-pages/man5/proc.5.html (index 0 based - 2)
  68. // In kernels before Linux 2.6, start was expressed in jiffies. Since Linux 2.6, the value is expressed in clock ticks
  69. const stat = {
  70. ppid: parseInt(infos[1]),
  71. utime: parseFloat(infos[11]) * 1000 / cpuInfo.clockTick,
  72. stime: parseFloat(infos[12]) * 1000 / cpuInfo.clockTick,
  73. cutime: parseFloat(infos[13]) * 1000 / cpuInfo.clockTick,
  74. cstime: parseFloat(infos[14]) * 1000 / cpuInfo.clockTick,
  75. start: parseFloat(infos[19]) * 1000 / cpuInfo.clockTick,
  76. rss: parseFloat(infos[21]),
  77. uptime: cpuInfo.uptime * 1000,
  78. fd: fd
  79. }
  80. const memory = stat.rss * cpuInfo.pageSize
  81. // https://stackoverflow.com/a/16736599/3921589
  82. const childrens = options.childrens ? stat.cutime + stat.cstime : 0
  83. // process usage since last call in seconds
  84. const total = (stat.stime - (hst.stime || 0) + stat.utime - (hst.utime || 0) + childrens)
  85. // time elapsed between calls in seconds
  86. const seconds = Math.abs(hst.uptime !== undefined ? stat.uptime - hst.uptime : stat.start - stat.uptime)
  87. const cpu = seconds > 0 ? (total / seconds) * 100 : 0
  88. history.set(pid, stat, options.maxage, close)
  89. if (again) {
  90. return readProcFile(pid, options, done)
  91. }
  92. return done(null, {
  93. cpu: cpu,
  94. memory: memory,
  95. ctime: stat.utime + stat.stime,
  96. elapsed: stat.uptime - stat.start,
  97. timestamp: date,
  98. pid: pid,
  99. ppid: stat.ppid
  100. })
  101. })
  102. })
  103. }
  104. function procfile (pids, options, done) {
  105. updateCpu(cpuInfo, function (err, result) {
  106. if (err) return done(err)
  107. cpuInfo = result
  108. const fns = {}
  109. pids.forEach(function (pid, i) {
  110. fns[pid] = function (cb) {
  111. readProcFile(pid, options, cb)
  112. }
  113. })
  114. parallel(fns, { graceful: true }, done)
  115. })
  116. }
  117. module.exports = procfile