mirror of
https://github.com/github/codeql-action.git
synced 2025-12-31 11:40:24 +08:00
Allow customizing the scaling threshold with an environment variable
This commit is contained in:
@@ -37,6 +37,12 @@ export enum EnvVar {
|
||||
|
||||
ODASA_TRACER_CONFIGURATION = "ODASA_TRACER_CONFIGURATION",
|
||||
|
||||
/**
|
||||
* What percentage of the total amount of RAM over 8 GB that the Action should reserve for the
|
||||
* system.
|
||||
*/
|
||||
SCALING_RESERVED_RAM_PERCENTAGE = "CODEQL_ACTION_SCALING_RESERVED_RAM_PERCENTAGE",
|
||||
|
||||
/** Whether to suppress the warning if the current CLI will soon be unsupported. */
|
||||
SUPPRESS_DEPRECATED_SOON_WARNING = "CODEQL_ACTION_SUPPRESS_DEPRECATED_SOON_WARNING",
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import path from "path";
|
||||
|
||||
import test from "ava";
|
||||
|
||||
import { EnvVar } from "./environment";
|
||||
import { getRunnerLogger } from "./logging";
|
||||
import { getRecordingLogger, LoggedMessage, setupTests } from "./testing-utils";
|
||||
import * as util from "./util";
|
||||
@@ -62,6 +63,14 @@ const GET_MEMORY_FLAG_TESTS = [
|
||||
expectedMemoryValue: 62.5 * 1024,
|
||||
expectedMemoryValueWithScaling: 61132, // Math.floor(1024 * (64 - 1.5 - 0.05 * (64 - 8)))
|
||||
},
|
||||
{
|
||||
input: undefined,
|
||||
totalMemoryMb: 64 * 1024,
|
||||
platform: "linux",
|
||||
expectedMemoryValue: 63 * 1024,
|
||||
expectedMemoryValueWithScaling: 58777, // Math.floor(1024 * (64 - 1 - 0.1 * (64 - 8)))
|
||||
reservedPercentageValue: "10",
|
||||
},
|
||||
];
|
||||
|
||||
for (const {
|
||||
@@ -70,13 +79,20 @@ for (const {
|
||||
platform,
|
||||
expectedMemoryValue,
|
||||
expectedMemoryValueWithScaling,
|
||||
reservedPercentageValue,
|
||||
} of GET_MEMORY_FLAG_TESTS) {
|
||||
test(
|
||||
`Memory flag value is ${expectedMemoryValue} without scaling and ${expectedMemoryValueWithScaling} with scaling ` +
|
||||
`for ${
|
||||
input ?? "no user input"
|
||||
} on ${platform} with ${totalMemoryMb} MB total system RAM`,
|
||||
} on ${platform} with ${totalMemoryMb} MB total system RAM${
|
||||
reservedPercentageValue
|
||||
? ` and reserved percentage env var set to ${reservedPercentageValue}`
|
||||
: ""
|
||||
}`,
|
||||
async (t) => {
|
||||
process.env[EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] =
|
||||
reservedPercentageValue || undefined;
|
||||
for (const withScaling of [true, false]) {
|
||||
const flag = util.getMemoryFlagValueForPlatform(
|
||||
input,
|
||||
|
||||
24
src/util.ts
24
src/util.ts
@@ -37,6 +37,11 @@ export const DEFAULT_DEBUG_ARTIFACT_NAME = "debug-artifacts";
|
||||
*/
|
||||
export const 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;
|
||||
|
||||
export interface SarifFile {
|
||||
version?: string | null;
|
||||
runs: SarifRun[];
|
||||
@@ -161,15 +166,28 @@ function getSystemReservedMemoryMegaBytes(
|
||||
const fixedAmount = 1024 * (platform === "win32" ? 1.5 : 1);
|
||||
|
||||
if (isScalingReservedRamEnabled) {
|
||||
// Reserve an additional 5% of the amount of memory above 8 GB, since the amount used by the
|
||||
// kernel for page tables scales with the size of physical memory.
|
||||
const scaledAmount = 0.05 * Math.max(totalMemoryMegaBytes - 8 * 1024, 0);
|
||||
// Reserve an additional percentage of the amount of memory above 8 GB, since the amount used by
|
||||
// the kernel for page tables scales with the size of physical memory.
|
||||
const scaledAmount =
|
||||
getReservedRamScaleFactor() *
|
||||
Math.max(totalMemoryMegaBytes - 8 * 1024, 0);
|
||||
return fixedAmount + scaledAmount;
|
||||
} else {
|
||||
return fixedAmount;
|
||||
}
|
||||
}
|
||||
|
||||
function getReservedRamScaleFactor(): number {
|
||||
const envVar = Number.parseInt(
|
||||
process.env[EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] || "",
|
||||
10,
|
||||
);
|
||||
if (envVar < 0 || envVar > 100 || Number.isNaN(envVar)) {
|
||||
return DEFAULT_RESERVED_RAM_SCALING_FACTOR;
|
||||
}
|
||||
return envVar / 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
Reference in New Issue
Block a user