forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-top.js
145 lines (131 loc) · 4.62 KB
/
process-top.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict'
/**
* This file is extracted from the 'process-top' project copyright by
* Mathias Buus. It has been modified slightly to be used in the current
* context and where possible changes have been contributed back to the
* original project.
*
* https://github.com/mafintosh/process-top
*
* Original file:
*
* https://github.com/mafintosh/process-top/blob/master/index.js
*
* License:
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Mathias Buus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
// const os = require('os')
// const p = require('prettier-bytes')
// const eld = require('event-loop-delay')
module.exports = top
// function top (opts) {
function top () { // NOTE: Replacing line above
// if (!opts) opts = {}
// const tick = opts.interval || 1000
// const started = Date.now()
// const interval = setInterval(perSecond, tick)
// const win = [{ time: process.hrtime(), cpu: process.cpuUsage(), delay: 0 }, null, null, null]
const win = [{ time: process.hrtime(), cpu: process.cpuUsage() }, null, null, null] // NOTE: Replacing line above
// const loopSampler = eld()
let sec = 1
// interval.unref()
return {
// pid: process.pid,
// command: process.argv.join(' '),
// started,
// time () {
// return Date.now() - started
// },
// delay () {
// const btm = oldest()
// const timeDelta = process.hrtime(win[btm].time)
// const ms = timeDelta[0] * 1e3 + Math.floor(timeDelta[1] / 1e6)
// return Math.floor((loopSampler.delay - win[btm].delay) / (ms / tick))
// },
cpu () {
const btm = oldest()
const cpuDelta = process.cpuUsage(win[btm].cpu)
const timeDelta = process.hrtime(win[btm].time)
const us = timeDelta[0] * 1e6 + timeDelta[1] / 1e3
perSecond() // NOTE: Added to skip timer and update at each check
return {
time: us,
percent: (cpuDelta.system + cpuDelta.user) / us,
system: cpuDelta.system,
user: cpuDelta.user
}
}
// memory () {
// const mem = process.memoryUsage()
// const total = os.totalmem()
// return {
// percent: mem.rss / total,
// rss: mem.rss,
// total,
// heapPercent: mem.heapUsed / mem.heapTotal,
// heapUsed: mem.heapUsed,
// heapTotal: mem.heapTotal,
// external: mem.external
// }
// },
// loadavg () {
// return os.loadavg()
// },
// destroy () {
// clearInterval(interval)
// },
// toString () {
// const mem = this.memory()
// return `cpu: ${pct(this.cpu().percent)} | rss: ${p(mem.rss)} (${pct(mem.percent)}) | heap: ${p(mem.heapUsed)} / ${p(mem.heapTotal)} (${pct(mem.heapPercent)}) | ext: ${p(mem.external)} | delay: ${this.delay()} ms | ${time(this.time())} | loadavg: ${os.loadavg().map(fixed2).join(', ')}`
// }
}
function oldest () {
let btm = (sec - 4) & 3
while (!win[btm]) btm = (btm + 1) & 3
return btm
}
function perSecond () {
const ptr = sec++ & 3
// win[ptr] = { time: process.hrtime(), cpu: process.cpuUsage(), delay: loopSampler.delay }
win[ptr] = { time: process.hrtime(), cpu: process.cpuUsage() } // NOTE: Replacing line above
}
}
// function pct (n) {
// return (100 * n).toFixed(1) + '%'
// }
// function fixed2 (n) {
// return n.toFixed(2)
// }
// function time (n) {
// let secs = Math.floor(n / 1000)
// let hours = Math.floor(secs / 3600)
// secs -= hours * 3600
// let mins = Math.floor(secs / 60)
// secs -= mins * 60
// return pad(hours) + ':' + pad(mins) + ':' + pad(secs)
// }
// function pad (n) {
// return n < 10 ? '0' + n : '' + n
// }