Skip to content

Commit

Permalink
fix: improve the calculation of available memory in the docker
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 committed Jan 21, 2021
1 parent 4796468 commit cb78982
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/orders/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,28 @@ var getLoadAvg = function (callback) {

var dockerFreeMemory = function (callback) {
const mem_used_path = path.join(cgroupBaseDir, '/memory/memory.usage_in_bytes');
fs.readFile(mem_used_path, 'utf8', function (err, data) {
const mem_stat_path = path.join(cgroupBaseDir, '/memory/memory.stat');

fs.readFile(mem_used_path, 'utf8', function (err, mem_used) {
if (err) {
return callback(err);
}
var used = Number(data.trim());
return callback(null, totalMemory - used);
});

const used_size = Number(mem_used.trim());
fs.readFile(mem_stat_path, 'utf8', function (err, mem_stat) {
if (err) {
return callback(err);
}

const mem_stat_obj = mem_stat.trim().split('\n').map(v => v.split(' '))
.reduce((r, v) => { r[v[0]] = v[1]; return r; }, {});
const total_active_file_size = Number(mem_stat_obj.total_active_file.trim());
const total_inactive_file_size = Number(mem_stat_obj.total_inactive_file.trim());

const freeMemory = totalMemory - used_size + (total_active_file_size + total_inactive_file_size);
return callback(null, freeMemory);
});
})
};

/*
Expand All @@ -406,7 +421,7 @@ var linuxFreeMemroy = function (callback) {

if (isMemAvailable) {
if (pair[0] === 'MemAvailable') {
free = parseInt(pair[1], 10) * 1024;
free = parseInt(pair[1], 10) * 1024;
}
} else {
if (['MemFree', 'Buffers', 'Cached'].indexOf(pair[0]) >= 0) {
Expand Down Expand Up @@ -448,7 +463,7 @@ var getFreeMemory = function (callback) {
}
};

var initTotalMemory = function() {
var initTotalMemory = function () {
totalMemory = os.totalmem();
if (!isDocker) {
// keep os.totalmem()
Expand Down

0 comments on commit cb78982

Please sign in to comment.