forked from newrelic/node-native-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
219 lines (194 loc) · 6.24 KB
/
index.js
File metadata and controls
219 lines (194 loc) · 6.24 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict'
var EventEmitter = require('events').EventEmitter
var util = require('util')
var preBuild = require('./lib/pre-build')
var natives = preBuild.load('native_metrics')
var DEFAULT_TIMEOUT = 15 * 1000 // 15 seconds
var GC_TYPE_NAMES = {
'1': 'Scavenge',
'2': 'MarkSweepCompact',
'4': 'IncrementalMarking',
'8': 'ProcessWeakCallbacks',
'3': 'All', // Node v4 and earlier only have Scavenge and MarkSweepCompact.
'15': 'All'
}
/**
* Constructs a metric emitter. This constructor is for internal use only.
*
* {@link NativeMetricEmitter#bind} is called as part of construction.
*
* @constructor
* @classdesc
* Emits events for various native events or periodic sampling.
*
* @param {number} [opts.timeout]
* The number of milliseconds between samplings. Defaults to 15 seconds.
*/
function NativeMetricEmitter(opts) {
opts = opts || {timeout: DEFAULT_TIMEOUT}
EventEmitter.call(this)
this.bound = false
this._timeout = null
this._rusageMeter = new natives.RUsageMeter()
this.usageEnabled = true
this._gcBinder = new natives.GCBinder()
this.gcEnabled = true
this._loopChecker = new natives.LoopChecker()
this.loopEnabled = true
this.bind(opts.timeout)
}
util.inherits(NativeMetricEmitter, EventEmitter)
/**
* @interface RUsageStats
*
* @description
* Resource usage statistics.
*
* Properties marked (X) are unmaintained by the operating system and are
* likely to be just `0`.
*
* @property {number} ru_utime - user CPU time used in milliseconds
* @property {number} ru_stime - system CPU time used in milliseconds
* @property {number} ru_maxrss - maximum resident set size in bytes
* @property {number} ru_ixrss - integral shared memory size (X)
* @property {number} ru_idrss - integral unshared data size (X)
* @property {number} ru_isrss - integral unshared stack size (X)
* @property {number} ru_minflt - page reclaims (soft page faults) (X)
* @property {number} ru_majflt - page faults (hard page faults)
* @property {number} ru_nswap - swaps (X)
* @property {number} ru_inblock - block input operations
* @property {number} ru_oublock - block output operations
* @property {number} ru_msgsnd - IPC messages sent (X)
* @property {number} ru_msgrcv - IPC messages received (X)
* @property {number} ru_nsignals - signals received (X)
* @property {number} ru_nvcsw - voluntary context switches (X)
* @property {number} ru_nivcsw - involuntary context switches (X)
*
* @see http://docs.libuv.org/en/v1.x/misc.html#c.uv_getrusage
* @see http://docs.libuv.org/en/v1.x/misc.html#c.uv_rusage_t
*/
/**
* @interface LoopMetrics
*
* @description
* A mapping of loop concepts to metrics about them. All values are in
* microseconds.
*
* @property {Metric} usage - CPU usage per tick metrics.
*/
/**
* @interface GCMetrics
*
* @description
* Garbage collection results.
*
* @property {number} typeId - The numeric ID of the gc type.
* @property {string} type - The nice name version of the gc type.
* @property {Metric} metrics - Accumulated metric data in milliseconds.
*/
/**
* @interface Metric
*
* @description
* A bundle of values taken from some measurement.
*
* @property {number} total - The sum of all values measured.
* @property {number} min - The smallest value measured.
* @property {number} max - The largest value measured.
* @property {number} sumOfSquares - The sum of the square of each value.
* @property {number} count - The number of values measured.
*/
/**
* Binds the emitter to the internal, V8 hooks to start populating data.
*
* @fires NativeMetricEmitter#gc
* @fires NativeMetricEmitter#usage
*
* @param {number} [timeout]
* The number of milliseconds between samplings. Defaults to 15 seconds.
*/
NativeMetricEmitter.prototype.bind = function bind(timeout) {
if (this.bound) {
return
}
timeout = timeout || DEFAULT_TIMEOUT
this._gcBinder.bind()
this._loopChecker.bind()
this._timeout = setTimeout(nativeMetricTimeout.bind(this), timeout).unref()
function nativeMetricTimeout() {
if (this._rusageMeter) {
/**
* Resource usage sampling event.
*
* @event NativeMetricEmitter#usage
* @type {object}
*
* @property {RUsageStats} diff - The change in stats since last sampling.
* @property {RUsageStats} current - The current usage statistics.
*/
this.emit('usage', this._rusageMeter.read())
}
if (this.bound) {
this._timeout = setTimeout(nativeMetricTimeout.bind(this), timeout).unref()
}
}
this.bound = true
}
/**
* Removes internal hooks and stops any open sampling timers.
*/
NativeMetricEmitter.prototype.unbind = function unbind() {
if (!this.bound) {
return
}
this._gcBinder.unbind()
this._loopChecker.unbind()
clearTimeout(this._timeout)
this.bound = false
}
/**
* Retrieves the current loop metrics and resets the counters.
*
* @return {LoopMetrics} The current loop metrics.
*/
NativeMetricEmitter.prototype.getLoopMetrics = function getLoopMetrics() {
return this._loopChecker.read()
}
/**
* Retrieves the accumulated garbage collection metrics.
*
* After retrieval, the metrics are reset internally. Only GC types that have
* happened at least once since the last retrieval are returned.
*
* @return {object.<string,GCMetrics>} An object mapping GC type names to
* information on the GC events that happened.
*/
NativeMetricEmitter.prototype.getGCMetrics = function getGCMetrics() {
var gcMetrics = this._gcBinder.read()
var results = Object.create(null)
for (var typeId in gcMetrics) {
if (gcMetrics.hasOwnProperty(typeId) && gcMetrics[typeId].count > 0) {
var typeName = GC_TYPE_NAMES[String(typeId)]
results[typeName] = {
typeId: parseInt(typeId, 10),
type: typeName,
metrics: gcMetrics[typeId]
}
}
}
return results
}
var emitter = null
/**
* Retrieves the {@link NativeMetricEmitter} singleton instance.
*
* @param {object} [opts]
* Options for constructing the emitter. See {@link NativeMetricEmitter} for
* default values. Only used on the first call to construct the instance.
*/
module.exports = function getMetricEmitter(opts) {
if (!emitter) {
emitter = new NativeMetricEmitter(opts)
}
return emitter
}