-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmonitor.js
More file actions
435 lines (385 loc) · 14.2 KB
/
Copy pathmonitor.js
File metadata and controls
435 lines (385 loc) · 14.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
$(document).ready(function () {
"use strict";
// Number formatters
function commaify(n)
{
var nStr = n.toString();
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function formatSuffix(val, opt_prec) {
if (val === null) {
return "N/A";
}
var prec = opt_prec || 1;
if (val >= 1000000000) {
return (val / 1000000000).toFixed(prec) + " GB";
} else if (val >= 1000000) {
return (val / 1000000).toFixed(prec) + " MB";
} else if (val >= 1000) {
return (val / 1000).toFixed(prec) + " kB";
} else {
return val.toFixed(prec) + " B";
}
}
function formatRate(val, prec) {
if (val === null) {
return "N/A";
}
return formatSuffix(val, prec) + "/s";
}
function formatPercent(val, opt_prec) {
if (val === null) {
return "N/A";
}
var prec = opt_prec || 1;
return val.toFixed(prec) + " %";
}
// Set up polling interval control
var updateInterval = 1000; // ms
$("#updateInterval").val(updateInterval).change(function () {
updateInterval = $(this).val();
});
// Allow the UI to be paused
var paused = false;
$('#pause-ui').click(function () {
if (paused) {
$(this).text("Pause UI");
paused = false;
} else {
$(this).text("Unpause UI");
paused = true;
}
});
// Plot formatters
function suffixFormatter(val, axis) {
return formatSuffix(val, axis.tickDecimals);
}
function suffixFormatterGeneric(val, axis) {
if (val >= 1000000000) {
return (val / 1000000000).toFixed(axis.tickDecimals) + " G";
} else if (val >= 1000000) {
return (val / 1000000).toFixed(axis.tickDecimals) + " M";
} else if (val >= 1000) {
return (val / 1000).toFixed(axis.tickDecimals) + " k";
} else {
return val.toFixed(axis.tickDecimals);
}
}
function rateFormatter(val, axis) {
return formatRate(val, axis.tickDecimals);
}
function percentFormatter(val, axis) {
return formatPercent(val, axis.tickDecimals);
}
// Fetch data periodically and notify interested parties.
var listeners = [];
function subscribe(fn) {
listeners.push(fn);
}
function unsubscribe(fn) {
listeners = listeners.filter(function (el) {
if (el !== fn) {
return el;
}
});
}
var alertVisible = false;
function fetchData() {
function onDataReceived(stats) {
if (alertVisible) {
$(".alert-message").hide();
}
alertVisible = false;
for (var i = 0; i < listeners.length; i++) {
listeners[i](stats, stats.ekg.server_timestamp_ms[0].val);
}
}
function onError() {
$(".alert-message").show();
alertVisible = true;
}
$.ajax({
dataType: 'json',
success: onDataReceived,
error: onError,
cache: false
});
setTimeout(fetchData, updateInterval);
}
fetchData();
function addPlot(elem, series, opts) {
var defaultOptions = {
series: { shadowSize: 0 }, // drawing is faster without shadows
xaxis: { mode: "time", tickSize: [10, "second"] }
};
var options = $.extend(true, {}, defaultOptions, opts);
var data = new Array(series.length);
var maxPoints = 60;
for(var i = 0; i < series.length; i++) {
data[i] = [];
}
var plot = $.plot(elem, [], options);
var prev_stats, prev_time;
function onDataReceived(stats, time) {
for(var i = 0; i < series.length; i++) {
if (data[i].length >= maxPoints) {
data[i] = data[i].slice(1);
}
data[i].push([time, series[i].fn(stats, time,
prev_stats, prev_time)]);
// the data may arrive out-of-order, so sort by time stamp first
data[i].sort(function (a, b) { return a[0] - b[0]; });
}
// zip legends with data
var res = [];
for(var i = 0; i < series.length; i++)
res.push({ label: series[i].label, data: data[i] });
if (!paused) {
plot.setData(res);
plot.setupGrid();
plot.draw();
}
prev_stats = stats;
prev_time = time;
}
subscribe(onDataReceived);
return onDataReceived;
}
function addCounter(elem, fn, formatter) {
var prev_stats, prev_time;
function onDataReceived(stats, time) {
if (!paused)
elem.text(formatter(fn(stats, time, prev_stats, prev_time)));
prev_stats = stats;
prev_time = time;
}
subscribe(onDataReceived);
}
function addDynamicPlot(key, button, graph_fn, label_fn) {
function getStats(stats, time, prev_stats, prev_time) {
return graph_fn(stats, time, prev_stats, prev_time);
}
// jQuery has problem with IDs containing dots.
var plotId = key.replace(/\./g, "-")
.replace(/ /g,"__")
.replace(/:/g,"_") + "-plot";
$("#plots:last").append(
'<div id="' + plotId + '" class="plot-container">' +
'<img src="cross.png" class="close-button"><h3>' + key +
'</h3><div class="plot"></div></div>');
var plot = $("#plots > .plot-container:last > div");
var observer = addPlot(plot,
[{ label: label_fn(key), fn: getStats }],
{ yaxis: { tickFormatter: suffixFormatterGeneric } });
var plotContainer = $("#" + plotId);
var closeButton = plotContainer.find("img");
closeButton.hide();
closeButton.click(function () {
plotContainer.remove();
button.show();
unsubscribe(observer);
});
plotContainer.hover(
function () {
closeButton.show();
},
function () {
closeButton.hide();
}
);
}
function addMetrics(table) {
var COUNTER = "c";
var GAUGE = "g";
var DISTRIBUTION = "d";
var metrics = {};
// Utility function to test for arrays of strings equality.
var sameDimensions = function(xs, ys) {
if (xs.length != ys.length) {
return false;
}
var ret = true;
$.each(xs, function(xy_index, x) {
var y = ys[xy_index];
if (x != y) {
ret = false;
return;
}
})
return ret;
}
var lookupStat = function(name, dims, stats) {
var pieces = name.split(".");
// find the nested object
var arrayValues = stats;
$.each(pieces, function(unused_index, piece) {
arrayValues = arrayValues[piece];
});
// find the correct dimensional break-down
var value = undefined;
$.each(arrayValues, function(unused_index, obj) {
if (sameDimensions(obj.dims, dims)) {
value = obj;
}
})
return value;
}
function makeDataGetter(name, dims) {
function get(stats, time, prev_stats, prev_time) {
// find the nested object
var value = lookupStat(name, dims, stats);
// do something here
if (value.type === COUNTER) {
if (prev_stats == undefined) {
return null;
}
var prev_value = lookupStat(name, dims, prev_stats);
return 1000 * (value.val - prev_value.val) /
(time - prev_time);
} else if (value.type === DISTRIBUTION) {
return value.mean;
} else { // value.type === GAUGE || value.type === LABEL
return value.val;
}
}
return get;
}
function counterLabel(label) {
return label + "/s";
}
function gaugeLabel(label) {
return label;
}
/** Adds the table row. */
function addElem(name, value) {
var elem;
var key = name + " " + value.dims.join(" ");
if (key in metrics) {
elem = metrics[key];
} else {
// Add UI element
table.find("tbody:last").append(
'<tr><td>' + key +
' <img src="chart_line_add.png" class="graph-button"' +
' width="16" height="16"' +
' alt="Add graph" title="Add graph"></td>' +
'<td class="value">N/A</td></tr>');
elem = table.find("tbody > tr > td:last");
metrics[key] = elem;
var button = table.find("tbody > tr:last > td:first > img");
var graph_fn = makeDataGetter(name, value.dims);
var label_fn = gaugeLabel;
if (value.type === COUNTER) {
label_fn = counterLabel;
}
button.click(function () {
addDynamicPlot(key, button, graph_fn, label_fn);
$(this).hide();
});
}
if (!paused) {
if (value.type === DISTRIBUTION) {
if (value.mean !== null) {
var val = value.mean.toPrecision(8) + '\n+/-' +
Math.sqrt(value.variance).toPrecision(8) + ' sd';
}
else {
var val = "N/A";
}
} else { // COUNTER, GAUGE, LABEL
var val = value.val;
}
if ($.inArray(value.type, [COUNTER, GAUGE]) !== -1) {
val = commaify(val);
}
elem.text(val);
}
}
/** Updates UI for all metrics. */
function onDataReceived(stats, time) {
function build(prefix, obj) {
$.each(obj, function (suffix, values) {
var name = prefix + suffix;
// leaves are arrays of dimensional break-down
if (Array.isArray(values)) {
$.each(values, function (index, value) {
addElem(name, value);
});
} else {
build(name + '.', values);
}
});
}
build('', stats);
}
subscribe(onDataReceived);
}
function initAll() {
// Metrics
var current_bytes_used = function (stats) {
return stats.rts.gc.current_bytes_used[0].val;
};
var max_bytes_used = function (stats) {
return stats.rts.gc.max_bytes_used[0].val;
};
var max_bytes_slop = function (stats) {
return stats.rts.gc.max_bytes_slop[0].val;
};
var current_bytes_slop = function (stats) {
return stats.rts.gc.current_bytes_slop[0].val;
};
var productivity_wall_percent = function (stats, time, prev_stats, prev_time) {
if (prev_stats == undefined)
return null;
var mutator_ms = stats.rts.gc.mutator_wall_ms[0].val -
prev_stats.rts.gc.mutator_wall_ms[0].val;
var gc_ms = stats.rts.gc.gc_wall_ms[0].val -
prev_stats.rts.gc.gc_wall_ms[0].val;
return 100 * mutator_ms / (mutator_ms + gc_ms);
};
var productivity_cpu_percent = function (stats, time, prev_stats, prev_time) {
if (prev_stats == undefined)
return null;
var mutator_ms = stats.rts.gc.mutator_cpu_ms[0].val -
prev_stats.rts.gc.mutator_cpu_ms[0].val;
var gc_ms = stats.rts.gc.gc_cpu_ms[0].val -
prev_stats.rts.gc.gc_cpu_ms[0].val;
return 100 * mutator_ms / (mutator_ms + gc_ms);
};
var allocation_rate = function (stats, time, prev_stats, prev_time) {
if (prev_stats == undefined)
return null;
return 1000 * (stats.rts.gc.bytes_allocated[0].val -
prev_stats.rts.gc.bytes_allocated[0].val) /
(time - prev_time);
};
addMetrics($("#metric-table"));
// Plots
addPlot($("#current-bytes-used-plot > div"),
[{ label: "residency", fn: current_bytes_used }],
{ yaxis: { tickFormatter: suffixFormatter } });
addPlot($("#allocation-rate-plot > div"),
[{ label: "rate", fn: allocation_rate }],
{ yaxis: { tickFormatter: rateFormatter } });
addPlot($("#productivity-plot > div"),
[{ label: "wall clock time", fn: productivity_wall_percent },
{ label: "cpu time", fn: productivity_cpu_percent }],
{ yaxis: { tickDecimals: 1, tickFormatter: percentFormatter } });
// GC and memory statistics
addCounter($("#max-bytes-used"), max_bytes_used, formatSuffix);
addCounter($("#current-bytes-used"), current_bytes_used, formatSuffix);
addCounter($("#max-bytes-slop"), max_bytes_slop, formatSuffix);
addCounter($("#current-bytes-slop"), current_bytes_slop, formatSuffix);
addCounter($("#productivity-wall"), productivity_wall_percent, formatPercent);
addCounter($("#productivity-cpu"), productivity_cpu_percent, formatPercent);
addCounter($("#allocation-rate"), allocation_rate, formatRate);
}
initAll();
});