mirror of
https://github.com/github/codeql-action.git
synced 2026-01-07 07:00:28 +08:00
Respect scaling_reserved_ram feature flag
The amount of RAM given to the CodeQL evaluator is the machine's total memory size, minus a reserved amount. Currently, the reserved amount is fixed at 1 GB (or 1.5 GB on Windows). When the scaling_reserved_ram feature flag is enabled, we also add 2% of the total memory size to the reserved amount. This allows for the fact that the kernel will consume more RAM (e.g. for page tables) on machines with more physical RAM.
This commit is contained in:
34
src/util.ts
34
src/util.ts
@@ -157,9 +157,21 @@ export async function withTmpDir<T>(
|
||||
* from committing too much of the available memory to CodeQL.
|
||||
* @returns number
|
||||
*/
|
||||
function getSystemReservedMemoryMegaBytes(): number {
|
||||
async function getSystemReservedMemoryMegaBytes(
|
||||
totalMemoryMegaBytes: number,
|
||||
features: FeatureEnablement
|
||||
): Promise<number> {
|
||||
// Windows needs more memory for OS processes.
|
||||
return 1024 * (process.platform === "win32" ? 1.5 : 1);
|
||||
const fixedAmount = 1024 * (process.platform === "win32" ? 1.5 : 1);
|
||||
|
||||
if (await features.getValue(Feature.ScalingReservedRam)) {
|
||||
// Reserve an additional 2% of the total memory, since the amount used by
|
||||
// the kernel for page tables scales with the size of physical memory.
|
||||
const scaledAmount = 0.02 * totalMemoryMegaBytes;
|
||||
return fixedAmount + scaledAmount;
|
||||
} else {
|
||||
return fixedAmount;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +181,10 @@ function getSystemReservedMemoryMegaBytes(): number {
|
||||
*
|
||||
* @returns {number} the amount of RAM to use, in megabytes
|
||||
*/
|
||||
export function getMemoryFlagValue(userInput: string | undefined): number {
|
||||
export async function getMemoryFlagValue(
|
||||
userInput: string | undefined,
|
||||
features: FeatureEnablement
|
||||
): Promise<number> {
|
||||
let memoryToUseMegaBytes: number;
|
||||
if (userInput) {
|
||||
memoryToUseMegaBytes = Number(userInput);
|
||||
@@ -179,7 +194,10 @@ export function getMemoryFlagValue(userInput: string | undefined): number {
|
||||
} else {
|
||||
const totalMemoryBytes = os.totalmem();
|
||||
const totalMemoryMegaBytes = totalMemoryBytes / (1024 * 1024);
|
||||
const reservedMemoryMegaBytes = getSystemReservedMemoryMegaBytes();
|
||||
const reservedMemoryMegaBytes = await getSystemReservedMemoryMegaBytes(
|
||||
totalMemoryMegaBytes,
|
||||
features
|
||||
);
|
||||
memoryToUseMegaBytes = totalMemoryMegaBytes - reservedMemoryMegaBytes;
|
||||
}
|
||||
return Math.floor(memoryToUseMegaBytes);
|
||||
@@ -192,8 +210,12 @@ export function getMemoryFlagValue(userInput: string | undefined): number {
|
||||
*
|
||||
* @returns string
|
||||
*/
|
||||
export function getMemoryFlag(userInput: string | undefined): string {
|
||||
return `--ram=${getMemoryFlagValue(userInput)}`;
|
||||
export async function getMemoryFlag(
|
||||
userInput: string | undefined,
|
||||
features: FeatureEnablement
|
||||
): Promise<string> {
|
||||
const megabytes = await getMemoryFlagValue(userInput, features);
|
||||
return `--ram=${megabytes}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user