Merge pull request #1888 from github/henrymercer/constrained-ram
Respect RAM constraints imposed by Linux cgroups
This commit is contained in:
commit
4254f3a4c1
3 changed files with 46 additions and 3 deletions
23
lib/util.js
generated
23
lib/util.js
generated
|
|
@ -148,6 +148,27 @@ function getMemoryFlagValueForPlatform(userInput, totalMemoryBytes, platform) {
|
||||||
return Math.floor(memoryToUseMegaBytes);
|
return Math.floor(memoryToUseMegaBytes);
|
||||||
}
|
}
|
||||||
exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
|
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.
|
* 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
|
* 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
|
* @returns {number} the amount of RAM to use, in megabytes
|
||||||
*/
|
*/
|
||||||
function getMemoryFlagValue(userInput) {
|
function getMemoryFlagValue(userInput) {
|
||||||
return getMemoryFlagValueForPlatform(userInput, os.totalmem(), process.platform);
|
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryAvailable(), process.platform);
|
||||||
}
|
}
|
||||||
exports.getMemoryFlagValue = getMemoryFlagValue;
|
exports.getMemoryFlagValue = getMemoryFlagValue;
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
24
src/util.ts
24
src/util.ts
|
|
@ -211,6 +211,28 @@ export function getMemoryFlagValueForPlatform(
|
||||||
return Math.floor(memoryToUseMegaBytes);
|
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.
|
* 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
|
* 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 {
|
export function getMemoryFlagValue(userInput: string | undefined): number {
|
||||||
return getMemoryFlagValueForPlatform(
|
return getMemoryFlagValueForPlatform(
|
||||||
userInput,
|
userInput,
|
||||||
os.totalmem(),
|
getTotalMemoryAvailable(),
|
||||||
process.platform,
|
process.platform,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue