Address review comments

This commit is contained in:
Henry Mercer 2023-09-18 16:00:59 +01:00
parent 0e74cd1660
commit 6846be007d
3 changed files with 107 additions and 64 deletions

68
lib/util.js generated
View file

@ -59,6 +59,11 @@ exports.DEFAULT_DEBUG_DATABASE_NAME = "db";
* The default fraction of the total RAM above 8 GB that should be reserved for the system.
*/
const DEFAULT_RESERVED_RAM_SCALING_FACTOR = 0.05;
/**
* The minimum amount of memory imposed by a cgroup limit that we will consider. Memory limits below
* this amount are ignored.
*/
const MINIMUM_CGROUP_MEMORY_LIMIT_BYTES = 1024 * 1024;
/**
* Get the extra options for the codeql commands.
*/
@ -152,37 +157,44 @@ exports.getMemoryFlagValueForPlatform = getMemoryFlagValueForPlatform;
* Get the total amount of memory available to the Action, taking into account constraints imposed
* by cgroups on Linux.
*/
function getTotalMemoryAvailable(logger) {
const osTotalMemory = os.totalmem();
function getTotalMemoryBytes(logger) {
const limits = [os.totalmem()];
if (os.platform() === "linux") {
// Respect constraints imposed by Linux cgroups v1 and v2
for (const limitFile of [
limits.push(...[
"/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)) {
const displayLimit = `${Math.floor(limit / (1024 * 1024))} MiB`;
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}.`);
}
}
]
.map((file) => getCgroupMemoryLimitBytes(file, logger))
.filter((limit) => limit !== undefined)
.map((limit) => limit));
}
return osTotalMemory;
const limit = Math.min(...limits);
logger.debug(`While resolving RAM, determined that the total memory available to the Action is ${limit / (1024 * 1024)} MiB.`);
return limit;
}
/**
* Gets the number of bytes of available memory specified by the cgroup limit file at the given path.
*
* May be greater than the total memory reported by the operating system if there is no cgroup limit.
*/
function getCgroupMemoryLimitBytes(limitFile, logger) {
if (!fs.existsSync(limitFile)) {
logger.debug(`While resolving RAM, did not find a cgroup memory limit at ${limitFile}.`);
return undefined;
}
const limit = Number(fs.readFileSync(limitFile, "utf8"));
if (!Number.isInteger(limit)) {
logger.debug(`While resolving RAM, ignored the file ${limitFile} that may contain a cgroup memory limit ` +
"as this file did not contain an integer.");
return undefined;
}
const displayLimit = `${Math.floor(limit / (1024 * 1024))} MiB`;
if (limit < MINIMUM_CGROUP_MEMORY_LIMIT_BYTES) {
logger.info(`While resolving RAM, ignored a cgroup limit of ${displayLimit} in ${limitFile} as it was below ${MINIMUM_CGROUP_MEMORY_LIMIT_BYTES / (1024 * 1024)} MiB.`);
return undefined;
}
logger.info(`While resolving RAM, found a cgroup limit of ${displayLimit} in ${limitFile}.`);
return limit;
}
/**
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
@ -192,7 +204,7 @@ function getTotalMemoryAvailable(logger) {
* @returns {number} the amount of RAM to use, in megabytes
*/
function getMemoryFlagValue(userInput, logger) {
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryAvailable(logger), process.platform);
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryBytes(logger), process.platform);
}
exports.getMemoryFlagValue = getMemoryFlagValue;
/**