Merge pull request #1888 from github/henrymercer/constrained-ram

Respect RAM constraints imposed by Linux cgroups
This commit is contained in:
Henry Mercer 2023-09-18 10:40:41 +01:00 committed by GitHub
commit 4254f3a4c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 3 deletions

23
lib/util.js generated
View file

@ -148,6 +148,27 @@ function getMemoryFlagValueForPlatform(userInput, totalMemoryBytes, platform) {
return Math.floor(memoryToUseMegaBytes);
}
exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
/**
* Get the total amount of memory available to the Action, taking into account constraints imposed
* by cgroups on Linux.
*/
function getTotalMemoryAvailable() {
if (os.platform() === "linux") {
// Respect constraints imposed by Linux cgroups v1 and v2
for (const limitFile of [
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
"/sys/fs/cgroup/memory.max",
]) {
if (fs.existsSync(limitFile)) {
const limit = Number(fs.readFileSync(limitFile, "utf8"));
if (Number.isInteger(limit)) {
return limit;
}
}
}
}
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 +177,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,28 @@ export function getMemoryFlagValueForPlatform(
return Math.floor(memoryToUseMegaBytes);
}
/**
* Get the total amount of memory available to the Action, taking into account constraints imposed
* by cgroups on Linux.
*/
function getTotalMemoryAvailable(): number {
if (os.platform() === "linux") {
// Respect constraints imposed by Linux cgroups v1 and v2
for (const limitFile of [
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
"/sys/fs/cgroup/memory.max",
]) {
if (fs.existsSync(limitFile)) {
const limit = Number(fs.readFileSync(limitFile, "utf8"));
if (Number.isInteger(limit)) {
return limit;
}
}
}
}
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 +243,7 @@ export function getMemoryFlagValueForPlatform(
export function getMemoryFlagValue(userInput: string | undefined): number {
return getMemoryFlagValueForPlatform(
userInput,
os.totalmem(),
getTotalMemoryAvailable(),
process.platform,
);
}