-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 1.86 KB
/
index.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
var fs = require('fs')
var mmap = require('mmap.js')
var v8Metrics = require('./v8-stats');
var metrics = require('./metrics');
var buffer
function silentDelete(path) {
try {
fs.unlinkSync(path)
} catch(e) {
if(e.code != 'ENOENT') {
throw e;
}
}
}
function start() {
let offset = 00
let scheme = ""
v8Metrics.startV8Collect();
for(var item of metrics.active_metrics) {
offset += item.getSize();
let typ = item.getType()
json = JSON.stringify(item.name)
scheme += `${typ}: ${json}\n`;
}
let basepath = process.env.CANTAL_PATH
if(!basepath) {
let runtime_dir = process.env.XDG_RUNTIME_DIR
if(runtime_dir) {
basepath = `${runtime_dir}/cantal.${process.pid}`
} else {
basepath = `/tmp/cantal.${process.getuid()}.${process.pid}`
}
console.warn("No CANTAL_PATH is set in the environment, using %s.",
basepath)
}
let tmppath = basepath + '.tmp'
let metapath = basepath + '.meta'
let path = basepath + '.values'
silentDelete(tmppath)
silentDelete(path)
silentDelete(metapath)
let fd = fs.openSync(tmppath, "w+")
// TODO(optimize) buffer allocation
let size = Math.max(offset, 4096)
let zeros = Buffer.alloc(size)
fs.writeSync(fd, zeros, 0, size)
buffer = mmap.alloc(size, mmap.PROT_READ|mmap.PROT_WRITE,
mmap.MAP_SHARED, fd, 0)
fs.closeSync(fd)
fs.renameSync(tmppath, path)
fs.writeFileSync(tmppath, scheme)
fs.renameSync(tmppath, metapath)
offset = 0
for(var metric of metrics.active_metrics) {
metric.setMemory(buffer.slice(offset, offset + metric.getSize()))
offset += metric.getSize()
}
}
module.exports = {
Integer: metrics.Integer,
Counter: metrics.Counter,
start: start,
}