Skip to content

Commit

Permalink
chore(cpu): refactor cpu notify and del heapdump
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 13, 2018
1 parent 5a33c13 commit 1e4a91e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 94 deletions.
8 changes: 0 additions & 8 deletions bin/proxy/admin.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ module.exports = {
res.end('done!\r\n');
},

'/heapdump': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'heapdump',
GET: req.GET
});
res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' });
res.end('done!\r\n');
},

'/profiler': function (req, res) {
process.emit('sendCmd2workerOnce', {
Expand Down
7 changes: 0 additions & 7 deletions bin/proxy/dump.heap.sh

This file was deleted.

139 changes: 61 additions & 78 deletions bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ methodMap.reload = function() {
process.emit('reload');
};

// heapdump
methodMap.heapdump = function(message) {
process.emit('heapdump', message.GET);
};

// profiler
methodMap.profiler = function(message) {
process.emit('profiler', message.GET);
Expand All @@ -130,24 +125,6 @@ process.on('top100', function(e) {
global.top100 = [];
});


process.on('heapdump', function(e) {
if (isWin32Like) {
return;
}

require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot', function(err, filename) {
if (err) {
logger.error(`dump heap error ${err.message}`);
return;
}
logger.info('dump written to ${filename}', {
filename: filename
});
});
});


process.on('profiler', function(data = {}) {
logger.info('profiler time: ${time}', data);
if (isWin32Like) {
Expand Down Expand Up @@ -390,61 +367,7 @@ function heartBeat() {

// 高负载告警
if (global.cpuUsed80 === 4 && !config.isTest && !isWin32Like) {
// 取进程快照
cp.exec('top -bcn1', {
env: {
COLUMNS: 200
},
encoding: 'utf8',
timeout: 5000
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
const key = `cpu80.v4:${serverInfo.intranetIp}`;
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
let str = '';

if (data) {
str = data;
str = str.replace(/</g, '&gt;');
str = str.replace(/\r\n|\r|\n/g, '<br>');

content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
}


// 获取本机信息,用来分组
require('api/cmdb').GetDeviceThisServer().done(function(data) {
data = data || {};
const business = data.business && data.business[0] || {};
let owner = '';

if (data.ownerMain) {
owner = [owner, data.ownerMain].join(';');
}

if (data.ownerBack) {
owner = [owner, data.ownerBack].join(';');
}

// 再抓一份CPU Profiler
require('util/v8-profiler.js').getProfiler({
recordTime: 5000
}, result => {
mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': owner,
'msgInfo': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
'title': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
'content': content,
'attachment': result ? {
fileType: true,
dispositionType: 'attachment',
fileName: 'cpu-profiler.cpuprofile',
content: result
} : ''
});
});
});
});
afterCpu80(cpuUsed);
}

const currMemory = process.memoryUsage();
Expand All @@ -453,3 +376,63 @@ function heartBeat() {
tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal);
tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external);
}


function afterCpu80(cpuUsed) {
// 取进程快照
cp.exec('top -bcn1', {
env: {
COLUMNS: 200
},
encoding: 'utf8',
timeout: 5000
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
const key = `cpu80.v4:${serverInfo.intranetIp}`;
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
let str = '';

if (data) {
str = data;
str = str.replace(/</g, '&gt;');
str = str.replace(/\r\n|\r|\n/g, '<br>');

content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
}


// 获取本机信息,用来分组
require('api/cmdb').GetDeviceThisServer().done(function(data) {
data = data || {};
const business = data.business && data.business[0] || {};
const profCallback = (result) => {
mail.SendMail(key, 'js', 600, {
'to': config.mailTo,
'cc': config.mailCC,
'msgInfo': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
'title': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
'content': content,
'attachment': result ? {
fileType: true,
dispositionType: 'attachment',
fileName: 'cpu-profiler.cpuprofile',
content: result
} : ''
});
};

// 再抓一份CPU Profiler
let profiler;
try {
profiler = require('util/v8-profiler.js');
} catch (err) {
logger.info(err.stack);
}

if (profiler) {
profiler.getProfiler({ recordTime: 5000 }, profCallback);
} else {
profCallback();
}
});
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"commitmsg": "commitlint -e $GIT_PARAMS",
"test": "mocha --recursive test/bin",
"coverage": "nyc --reporter=lcov --reporter=text npm test && codecov",
"optional": "npm i heapdump && npm i v8-profiler"
"optional": "npm i v8-profiler"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 1e4a91e

Please sign in to comment.