节点集群 worker 内存使用情况
Node cluster workers memoryUsage
有谁知道是否有独立于平台的方法来获取 worker 的内存使用情况?
我希望它会像这样工作:
console.log('App process memoryUsage: ',process.memoryUsage());
cluster.on('online',function(worker){ // doesn't work!
console.log('Workers memory usage: ',worker.process.memoryUsage());
});
但是worker进程没有方法memoryUsage()
。
是否有未实施的正当理由?
实现这一点的唯一想法是在 linux 上使用 unix top -pid 1234
(macosx) 或 top -p 1234
。并按 process.plattform
.
切换
是的,您确实无法从工作进程 属性 中获取 memoryUsage
。我不确定为什么它没有实现,但你可以用这种方法实现同样的效果:
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
cluster.on('online', function(worker, code, signal) {
worker.send('memoryUsage');
worker.on('message', function(dd) {
if (dd.event == 'memoryUsage') {
console.log("Worker with ID: %d consumes %imb of memory", worker.id, dd.data.heapTotal / 1024 / 1024);
}
});
});
console.log("Master consumes %imb of memory", process.memoryUsage().heapTotal / 1024 / 1024);
} else {
if (cluster.worker.id == 1) {
a = [];
for (var i = 0; i < 1000000; i++) {
//just to see the difference in memory usage.
a.push(Number.MAX_SAFE_INTEGER);
};
}
process.on('message', function(msg) {
if (msg == 'memoryUsage') {
process.send({
event: msg,
data: process.memoryUsage()
});
}
});
}
8核系统输出:
user$ node ClusterTest.js
Master consumes 6mb of memory
Worker with ID: 2 consumes 5mb of memory
Worker with ID: 6 consumes 5mb of memory
Worker with ID: 3 consumes 5mb of memory
Worker with ID: 4 consumes 5mb of memory
Worker with ID: 7 consumes 5mb of memory
Worker with ID: 5 consumes 5mb of memory
Worker with ID: 8 consumes 5mb of memory
Worker with ID: 1 consumes 39mb of memory
^C
user$
有谁知道是否有独立于平台的方法来获取 worker 的内存使用情况? 我希望它会像这样工作:
console.log('App process memoryUsage: ',process.memoryUsage());
cluster.on('online',function(worker){ // doesn't work!
console.log('Workers memory usage: ',worker.process.memoryUsage());
});
但是worker进程没有方法memoryUsage()
。
是否有未实施的正当理由?
实现这一点的唯一想法是在 linux 上使用 unix top -pid 1234
(macosx) 或 top -p 1234
。并按 process.plattform
.
是的,您确实无法从工作进程 属性 中获取 memoryUsage
。我不确定为什么它没有实现,但你可以用这种方法实现同样的效果:
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
cluster.on('online', function(worker, code, signal) {
worker.send('memoryUsage');
worker.on('message', function(dd) {
if (dd.event == 'memoryUsage') {
console.log("Worker with ID: %d consumes %imb of memory", worker.id, dd.data.heapTotal / 1024 / 1024);
}
});
});
console.log("Master consumes %imb of memory", process.memoryUsage().heapTotal / 1024 / 1024);
} else {
if (cluster.worker.id == 1) {
a = [];
for (var i = 0; i < 1000000; i++) {
//just to see the difference in memory usage.
a.push(Number.MAX_SAFE_INTEGER);
};
}
process.on('message', function(msg) {
if (msg == 'memoryUsage') {
process.send({
event: msg,
data: process.memoryUsage()
});
}
});
}
8核系统输出:
user$ node ClusterTest.js
Master consumes 6mb of memory
Worker with ID: 2 consumes 5mb of memory
Worker with ID: 6 consumes 5mb of memory
Worker with ID: 3 consumes 5mb of memory
Worker with ID: 4 consumes 5mb of memory
Worker with ID: 7 consumes 5mb of memory
Worker with ID: 5 consumes 5mb of memory
Worker with ID: 8 consumes 5mb of memory
Worker with ID: 1 consumes 39mb of memory
^C
user$