Respect RAM constraints imposed by Linux cgroups

This commit is contained in:
Henry Mercer 2023-09-15 18:09:37 +01:00
parent c459726691
commit d6f9faae0d
3 changed files with 30 additions and 3 deletions

14
lib/util.js generated
View file

@ -148,6 +148,18 @@ function getMemoryFlagValueForPlatform(userInput, totalMemoryBytes, platform) {
return Math.floor(memoryToUseMegaBytes);
}
exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
/**
* Get the total amount of memory available to the Action.
*/
function getTotalMemoryAvailable() {
if (fs.existsSync("/sys/fs/cgroup/memory/memory.limit_in_bytes")) {
return Number(fs.readFileSync("/sys/fs/cgroup/memory/memory.limit_in_bytes", "utf8"));
}
if (fs.existsSync("/sys/fs/cgroup/memory.max")) {
return Number(fs.readFileSync("/sys/fs/cgroup/memory.max", "utf8"));
}
return os.totalmem();
}
/**
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
* If no value was specified, the total available memory will be used minus a
@ -156,7 +168,7 @@ exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
* @returns {number} the amount of RAM to use, in megabytes
*/
function getMemoryFlagValue(userInput) {
return getMemoryFlagValueForPlatform(userInput, os.totalmem(), process.platform);
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryAvailable(), process.platform);
}
exports.getMemoryFlagValue = getMemoryFlagValue;
/**

File diff suppressed because one or more lines are too long

View file

@ -211,6 +211,21 @@ export function getMemoryFlagValueForPlatform(
return Math.floor(memoryToUseMegaBytes);
}
/**
* Get the total amount of memory available to the Action.
*/
function getTotalMemoryAvailable(): number {
if (fs.existsSync("/sys/fs/cgroup/memory/memory.limit_in_bytes")) {
return Number(
fs.readFileSync("/sys/fs/cgroup/memory/memory.limit_in_bytes", "utf8"),
);
}
if (fs.existsSync("/sys/fs/cgroup/memory.max")) {
return Number(fs.readFileSync("/sys/fs/cgroup/memory.max", "utf8"));
}
return os.totalmem();
}
/**
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
* If no value was specified, the total available memory will be used minus a
@ -221,7 +236,7 @@ export function getMemoryFlagValueForPlatform(
export function getMemoryFlagValue(userInput: string | undefined): number {
return getMemoryFlagValueForPlatform(
userInput,
os.totalmem(),
getTotalMemoryAvailable(),
process.platform,
);
}