Handle cgroup file containing MAX_INT on ubuntu-20.04

This commit is contained in:
Henry Mercer 2023-09-18 13:41:50 +01:00
parent 379f89dc53
commit 0e74cd1660
3 changed files with 39 additions and 10 deletions

20
lib/util.js generated
View file

@ -153,6 +153,7 @@ exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
* by cgroups on Linux. * by cgroups on Linux.
*/ */
function getTotalMemoryAvailable(logger) { function getTotalMemoryAvailable(logger) {
const osTotalMemory = os.totalmem();
if (os.platform() === "linux") { if (os.platform() === "linux") {
// Respect constraints imposed by Linux cgroups v1 and v2 // Respect constraints imposed by Linux cgroups v1 and v2
for (const limitFile of [ for (const limitFile of [
@ -162,13 +163,26 @@ function getTotalMemoryAvailable(logger) {
if (fs.existsSync(limitFile)) { if (fs.existsSync(limitFile)) {
const limit = Number(fs.readFileSync(limitFile, "utf8")); const limit = Number(fs.readFileSync(limitFile, "utf8"));
if (Number.isInteger(limit)) { if (Number.isInteger(limit)) {
logger.info(`While resolving RAM, found cgroup limit of ${limit / (1024 * 1024)} MiB in ${limitFile}.`); const displayLimit = `${Math.floor(limit / (1024 * 1024))} MiB`;
return limit; if (limit < osTotalMemory) {
logger.info(`While resolving RAM, found cgroup limit of ${displayLimit} in ${limitFile}.`);
return limit;
}
else {
logger.debug(`While resolving RAM, ignoring cgroup file ${limitFile} since the limit (${displayLimit}) ` +
"is greater than the total RAM reported by the operating system.");
}
} }
else {
logger.debug(`While resolving RAM, ignoring cgroup file ${limitFile} as it does not contain an integer.`);
}
}
else {
logger.debug(`While resolving RAM, did not find cgroup constraints at ${limitFile}.`);
} }
} }
} }
return os.totalmem(); return osTotalMemory;
} }
/** /**
* 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.

File diff suppressed because one or more lines are too long

View file

@ -216,6 +216,7 @@ export function getMemoryFlagValueForPlatform(
* by cgroups on Linux. * by cgroups on Linux.
*/ */
function getTotalMemoryAvailable(logger: Logger): number { function getTotalMemoryAvailable(logger: Logger): number {
const osTotalMemory = os.totalmem();
if (os.platform() === "linux") { if (os.platform() === "linux") {
// Respect constraints imposed by Linux cgroups v1 and v2 // Respect constraints imposed by Linux cgroups v1 and v2
for (const limitFile of [ for (const limitFile of [
@ -225,17 +226,31 @@ function getTotalMemoryAvailable(logger: Logger): number {
if (fs.existsSync(limitFile)) { if (fs.existsSync(limitFile)) {
const limit = Number(fs.readFileSync(limitFile, "utf8")); const limit = Number(fs.readFileSync(limitFile, "utf8"));
if (Number.isInteger(limit)) { if (Number.isInteger(limit)) {
logger.info( const displayLimit = `${Math.floor(limit / (1024 * 1024))} MiB`;
`While resolving RAM, found cgroup limit of ${ if (limit < osTotalMemory) {
limit / (1024 * 1024) logger.info(
} MiB in ${limitFile}.`, `While resolving RAM, found cgroup limit of ${displayLimit} in ${limitFile}.`,
);
return limit;
} else {
logger.debug(
`While resolving RAM, ignoring cgroup file ${limitFile} since the limit (${displayLimit}) ` +
"is greater than the total RAM reported by the operating system.",
);
}
} else {
logger.debug(
`While resolving RAM, ignoring cgroup file ${limitFile} as it does not contain an integer.`,
); );
return limit;
} }
} else {
logger.debug(
`While resolving RAM, did not find cgroup constraints at ${limitFile}.`,
);
} }
} }
} }
return os.totalmem(); return osTotalMemory;
} }
/** /**