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

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,
);
}