-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.js
202 lines (169 loc) · 6.57 KB
/
bus.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
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
'use strict';
const path = require('path');
const xBus = require('.');
const cmds = {};
let appId = '$';
let tribe = '';
try {
const xHost = require('xcraft-core-host');
appId = xHost.appId;
tribe = xHost.appArgs().tribe ? `-${xHost.appArgs().tribe}` : '';
} catch (ex) {
if (ex.code !== 'MODULE_NOT_FOUND') {
throw ex;
}
}
const cmdNamespace = `${appId}${tribe}`;
function getModuleFiles(file) {
return file ? [file] : xBus.runningModuleLocations(true);
}
function getModuleNames(name) {
return name ? [name] : xBus.runningModuleNames(true);
}
cmds['module.load'] = function* (msg, resp) {
const xFs = require('xcraft-core-fs');
const {moduleName} = msg.data;
const moduleInfo = xBus.getModuleInfo(moduleName);
const filenames = xFs.ls(moduleInfo.path, moduleInfo.pattern);
//READ DEF: FIND HOT
try {
yield xBus.loadModule(resp, filenames, moduleInfo.path, {});
resp.log.info(`module(s) ${filenames.join(', ')} successfully loaded`);
} catch (ex) {
resp.log.warn(ex.stack);
}
resp.events.send(`bus.module.load.${msg.id}.finished`);
};
cmds.xcraftMetrics = function (msg, resp) {
const v8 = require('v8');
const process = require('process');
const os = require('os');
const metrics = {};
try {
let stats;
const ns = `${os.hostname()}.${cmdNamespace}`;
/************************************************************************/
stats = v8.getHeapStatistics();
metrics[`${ns}.v8.heap.total`] = stats.total_heap_size;
metrics[`${ns}.v8.heap.executable.total`] =
stats.total_heap_size_executable;
metrics[`${ns}.v8.heap.physical.total`] = stats.total_physical_size;
metrics[`${ns}.v8.heap.available.total`] = stats.total_available_size;
metrics[`${ns}.v8.heap.used.total`] = stats.used_heap_size;
metrics[`${ns}.v8.heap.limit.total`] = stats.heap_size_limit;
metrics[`${ns}.v8.heap.malloced.total`] = stats.malloced_memory;
metrics[`${ns}.v8.heap.malloced.peak.total`] = stats.peak_malloced_memory;
metrics[`${ns}.v8.heap.nativeContexts.total`] =
stats.number_of_native_contexts; /* If it increases over time, it's a memory leak */
metrics[`${ns}.v8.heap.detachedContexts.total`] =
stats.number_of_detached_contexts; /* Potential memory leak */
/************************************************************************/
stats = v8.getHeapCodeStatistics();
metrics[`${ns}.v8.heap.codeMetadata.total`] = stats.code_and_metadata_size;
metrics[`${ns}.v8.heap.bytecodeMetadata.total`] =
stats.bytecode_and_metadata_size;
metrics[`${ns}.v8.heap.scriptSource.total`] =
stats.external_script_source_size;
/************************************************************************/
stats = process.cpuUsage();
metrics[`${ns}.process.cpuUsage.user.total`] = stats.user; /* us */
metrics[`${ns}.process.cpuUsage.system.total`] = stats.system; /* us */
/************************************************************************/
stats = process.memoryUsage();
metrics[`${ns}.process.memory.rss.total`] = stats.rss; /* c++ + js */
metrics[`${ns}.process.memory.heap.total`] =
stats.heapTotal; /* v8, see above */
metrics[`${ns}.process.memory.heapUsed.total`] =
stats.heapUsed; /* v8, see above */
metrics[`${ns}.process.memory.external.total`] =
stats.external; /* c++ objects managed by v8 */
metrics[`${ns}.process.memory.arrayBuffers.total`] =
stats.arrayBuffers; /* node js Buffers */
/************************************************************************/
stats = process.resourceUsage();
metrics[`${ns}.process.maxrss.total`] = stats.maxRSS; /* KB */
metrics[`${ns}.process.ixrss.total`] = stats.sharedMemorySize; /* KB */
metrics[`${ns}.process.idrss.total`] = stats.unsharedDataSize; /* KB */
metrics[`${ns}.process.isrss.total`] = stats.unsharedStackSize; /* KB */
metrics[`${ns}.process.pageFault.minor.total`] = stats.minorPageFault;
metrics[`${ns}.process.pageFault.major.total`] = stats.majorPageFault;
metrics[`${ns}.process.swappedOut.total`] = stats.swappedOut;
metrics[`${ns}.process.fs.read.total`] = stats.fsRead;
metrics[`${ns}.process.fs.write.total`] = stats.fsWrite;
metrics[`${ns}.process.ipc.sent.total`] = stats.ipcSent;
metrics[`${ns}.process.ipc.received.total`] = stats.ipcReceived;
metrics[`${ns}.process.signalsCount.total`] = stats.signalsCount;
metrics[`${ns}.process.contextSwitches.voluntary.total`] =
stats.voluntaryContextSwitches;
metrics[`${ns}.process.contextSwitches.involuntary.total`] =
stats.involuntaryContextSwitches;
/************************************************************************/
metrics[`${ns}.process.uptime.total`] = process.uptime(); /* sec */
/************************************************************************/
metrics[`${ns}.os.process.priority.total`] = os.getPriority();
metrics[`${ns}.os.memory.total`] = os.totalmem();
metrics[`${ns}.os.uptime.total`] = os.uptime();
/************************************************************************/
} finally {
resp.events.send(`bus.xcraftMetrics.${msg.id}.finished`, metrics);
}
};
const xcraftMetrics = `${cmdNamespace}.xcraftMetrics`;
cmds[xcraftMetrics] = function* (msg, resp, next) {
if (msg.data.from === `bus-${cmdNamespace}`) {
resp.events.send(`bus.${xcraftMetrics}.${msg.id}.finished`);
return;
}
let full = false;
if (msg.data.from === 'garona') {
full = true;
}
const metrics = {};
try {
const registry = full
? xBus.getCommander().getFullRegistry()
: xBus.getRegistry();
const metricsCommands = Object.keys(registry).filter((cmd) =>
cmd.endsWith('.xcraftMetrics')
);
for (const cmd of metricsCommands) {
const _metrics = yield resp.command.send(
cmd,
{from: `bus-${cmdNamespace}`},
next
);
Object.assign(metrics, _metrics.data);
}
} finally {
resp.events.send(`bus.${xcraftMetrics}.${msg.id}.finished`, metrics);
}
};
/**
* Retrieve the list of available commands.
*
* @returns {Object} The list and definitions of commands.
*/
exports.xcraftCommands = function () {
return {
handlers: cmds,
rc: {
'module.load': {
parallel: false,
desc: 'load a module',
options: {
params: {
required: 'moduleName',
},
},
},
'xcraftMetrics': {
parallel: true,
desc: 'extract server Xcraft metrics',
},
[xcraftMetrics]: {
parallel: true,
desc: 'extract all Xcraft metrics',
},
},
};
};