allocate swap size for containers based on system ratio

This commit is contained in:
Girish Ramakrishnan
2021-01-20 11:45:04 -08:00
parent 7b24239d38
commit 0d535d2d5c
3 changed files with 21 additions and 8 deletions

View File

@@ -3,7 +3,8 @@
exports = module.exports = {
getDisks,
checkDiskSpace,
getMemory
getMemory,
getMemoryAllocation
};
const apps = require('./apps.js'),
@@ -148,14 +149,24 @@ function checkDiskSpace(callback) {
});
}
function getMemory(callback) {
assert.strictEqual(typeof callback, 'function');
function getSwapSize() {
const stdout = safe.child_process.execSync('swapon --noheadings --raw --bytes --show=SIZE', { encoding: 'utf8' });
const swap = !stdout ? 0 : stdout.trim().split('\n').map(x => parseInt(x, 10) || 0).reduce((acc, cur) => acc + cur);
return swap;
}
function getMemory(callback) {
assert.strictEqual(typeof callback, 'function');
callback(null, {
memory: os.totalmem(),
swap: swap
swap: getSwapSize()
});
}
function getMemoryAllocation(limit) {
const pc = os.totalmem() / (os.totalmem() + getSwapSize());
const ratio = Math.round(pc * 10) / 10; // a simple ratio
return Math.round(Math.round(limit * ratio) / 1048576) * 1048576; // nearest MB
}