Address review comments
This commit is contained in:
parent
0e74cd1660
commit
6846be007d
3 changed files with 107 additions and 64 deletions
68
lib/util.js
generated
68
lib/util.js
generated
|
|
@ -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.
|
* 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;
|
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.
|
* 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
|
* Get the total amount of memory available to the Action, taking into account constraints imposed
|
||||||
* by cgroups on Linux.
|
* by cgroups on Linux.
|
||||||
*/
|
*/
|
||||||
function getTotalMemoryAvailable(logger) {
|
function getTotalMemoryBytes(logger) {
|
||||||
const osTotalMemory = os.totalmem();
|
const limits = [os.totalmem()];
|
||||||
if (os.platform() === "linux") {
|
if (os.platform() === "linux") {
|
||||||
// Respect constraints imposed by Linux cgroups v1 and v2
|
limits.push(...[
|
||||||
for (const limitFile of [
|
|
||||||
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
||||||
"/sys/fs/cgroup/memory.max",
|
"/sys/fs/cgroup/memory.max",
|
||||||
]) {
|
]
|
||||||
if (fs.existsSync(limitFile)) {
|
.map((file) => getCgroupMemoryLimitBytes(file, logger))
|
||||||
const limit = Number(fs.readFileSync(limitFile, "utf8"));
|
.filter((limit) => limit !== undefined)
|
||||||
if (Number.isInteger(limit)) {
|
.map((limit) => 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}.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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.
|
* 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
|
* @returns {number} the amount of RAM to use, in megabytes
|
||||||
*/
|
*/
|
||||||
function getMemoryFlagValue(userInput, logger) {
|
function getMemoryFlagValue(userInput, logger) {
|
||||||
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryAvailable(logger), process.platform);
|
return getMemoryFlagValueForPlatform(userInput, getTotalMemoryBytes(logger), process.platform);
|
||||||
}
|
}
|
||||||
exports.getMemoryFlagValue = getMemoryFlagValue;
|
exports.getMemoryFlagValue = getMemoryFlagValue;
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
101
src/util.ts
101
src/util.ts
|
|
@ -42,6 +42,12 @@ export const DEFAULT_DEBUG_DATABASE_NAME = "db";
|
||||||
*/
|
*/
|
||||||
const DEFAULT_RESERVED_RAM_SCALING_FACTOR = 0.05;
|
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;
|
||||||
|
|
||||||
export interface SarifFile {
|
export interface SarifFile {
|
||||||
version?: string | null;
|
version?: string | null;
|
||||||
runs: SarifRun[];
|
runs: SarifRun[];
|
||||||
|
|
@ -215,42 +221,67 @@ export function getMemoryFlagValueForPlatform(
|
||||||
* Get the total amount of memory available to the Action, taking into account constraints imposed
|
* Get the total amount of memory available to the Action, taking into account constraints imposed
|
||||||
* by cgroups on Linux.
|
* by cgroups on Linux.
|
||||||
*/
|
*/
|
||||||
function getTotalMemoryAvailable(logger: Logger): number {
|
function getTotalMemoryBytes(logger: Logger): number {
|
||||||
const osTotalMemory = os.totalmem();
|
const limits = [os.totalmem()];
|
||||||
if (os.platform() === "linux") {
|
if (os.platform() === "linux") {
|
||||||
// Respect constraints imposed by Linux cgroups v1 and v2
|
limits.push(
|
||||||
for (const limitFile of [
|
...[
|
||||||
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
|
||||||
"/sys/fs/cgroup/memory.max",
|
"/sys/fs/cgroup/memory.max",
|
||||||
]) {
|
]
|
||||||
if (fs.existsSync(limitFile)) {
|
.map((file) => getCgroupMemoryLimitBytes(file, logger))
|
||||||
const limit = Number(fs.readFileSync(limitFile, "utf8"));
|
.filter((limit) => limit !== undefined)
|
||||||
if (Number.isInteger(limit)) {
|
.map((limit) => limit as number),
|
||||||
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}.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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: string,
|
||||||
|
logger: Logger,
|
||||||
|
): number | undefined {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -266,7 +297,7 @@ export function getMemoryFlagValue(
|
||||||
): number {
|
): number {
|
||||||
return getMemoryFlagValueForPlatform(
|
return getMemoryFlagValueForPlatform(
|
||||||
userInput,
|
userInput,
|
||||||
getTotalMemoryAvailable(logger),
|
getTotalMemoryBytes(logger),
|
||||||
process.platform,
|
process.platform,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue