Allow customizing the scaling threshold with an environment variable

This commit is contained in:
Henry Mercer 2023-09-05 13:14:47 +02:00
parent 466ed42568
commit 574dbbc517
9 changed files with 82 additions and 12 deletions

5
lib/environment.js generated
View file

@ -31,6 +31,11 @@ var EnvVar;
/** UUID representing the current job run. */
EnvVar["JOB_RUN_UUID"] = "JOB_RUN_UUID";
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.
*/
EnvVar["SCALING_RESERVED_RAM_PERCENTAGE"] = "CODEQL_ACTION_SCALING_RESERVED_RAM_PERCENTAGE";
/** Whether to suppress the warning if the current CLI will soon be unsupported. */
EnvVar["SUPPRESS_DEPRECATED_SOON_WARNING"] = "CODEQL_ACTION_SUPPRESS_DEPRECATED_SOON_WARNING";
/** Whether to disable uploading SARIF results or status reports to the GitHub API */

View file

@ -1 +1 @@
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":";;;AAAA,IAAY,MA0DX;AA1DD,WAAY,MAAM;IAChB,2DAA2D;IAC3D,+FAAqF,CAAA;IAErF,gEAAgE;IAChE,qEAA2D,CAAA;IAE3D;;;OAGG;IACH,yFAA+E,CAAA;IAE/E;;;OAGG;IACH,yEAA+D,CAAA;IAE/D,gFAAgF;IAChF,6DAAmD,CAAA;IAEnD;;;OAGG;IACH,uEAA6D,CAAA;IAE7D,gEAAgE;IAChE,mEAAyD,CAAA;IAEzD,kFAAkF;IAClF,mFAAyE,CAAA;IAEzE,6CAA6C;IAC7C,uCAA6B,CAAA;IAE7B,mEAAyD,CAAA;IAEzD,mFAAmF;IACnF,6FAAmF,CAAA;IAEnF,qFAAqF;IACrF,+CAAqC,CAAA;IAErC,mEAAyD,CAAA;IAEzD,kEAAkE;IAClE,2CAAiC,CAAA;IAEjC;;;;;;OAMG;IACH,4DAAkD,CAAA;AACpD,CAAC,EA1DW,MAAM,sBAAN,MAAM,QA0DjB"}
{"version":3,"file":"environment.js","sourceRoot":"","sources":["../src/environment.ts"],"names":[],"mappings":";;;AAAA,IAAY,MAgEX;AAhED,WAAY,MAAM;IAChB,2DAA2D;IAC3D,+FAAqF,CAAA;IAErF,gEAAgE;IAChE,qEAA2D,CAAA;IAE3D;;;OAGG;IACH,yFAA+E,CAAA;IAE/E;;;OAGG;IACH,yEAA+D,CAAA;IAE/D,gFAAgF;IAChF,6DAAmD,CAAA;IAEnD;;;OAGG;IACH,uEAA6D,CAAA;IAE7D,gEAAgE;IAChE,mEAAyD,CAAA;IAEzD,kFAAkF;IAClF,mFAAyE,CAAA;IAEzE,6CAA6C;IAC7C,uCAA6B,CAAA;IAE7B,mEAAyD,CAAA;IAEzD;;;OAGG;IACH,2FAAiF,CAAA;IAEjF,mFAAmF;IACnF,6FAAmF,CAAA;IAEnF,qFAAqF;IACrF,+CAAqC,CAAA;IAErC,mEAAyD,CAAA;IAEzD,kEAAkE;IAClE,2CAAiC,CAAA;IAEjC;;;;;;OAMG;IACH,4DAAkD,CAAA;AACpD,CAAC,EAhEW,MAAM,sBAAN,MAAM,QAgEjB"}

18
lib/util.js generated
View file

@ -55,6 +55,10 @@ exports.DEFAULT_DEBUG_ARTIFACT_NAME = "debug-artifacts";
* Default name of the database in the debugging artifact.
*/
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;
/**
* Get the extra options for the codeql commands.
*/
@ -110,15 +114,23 @@ function getSystemReservedMemoryMegaBytes(totalMemoryMegaBytes, platform, isScal
// Windows needs more memory for OS processes.
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() {
const envVar = Number.parseInt(process.env[environment_1.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

File diff suppressed because one or more lines are too long

17
lib/util.test.js generated
View file

@ -30,6 +30,7 @@ const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path_1 = __importDefault(require("path"));
const ava_1 = __importDefault(require("ava"));
const environment_1 = require("./environment");
const logging_1 = require("./logging");
const testing_utils_1 = require("./testing-utils");
const util = __importStar(require("./util"));
@ -82,10 +83,22 @@ 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,
reservedPercentageValue: "10",
},
];
for (const { input, totalMemoryMb, platform, expectedMemoryValue, expectedMemoryValueWithScaling, } of GET_MEMORY_FLAG_TESTS) {
for (const { input, totalMemoryMb, platform, expectedMemoryValue, expectedMemoryValueWithScaling, reservedPercentageValue, } of GET_MEMORY_FLAG_TESTS) {
(0, ava_1.default)(`Memory flag value is ${expectedMemoryValue} without scaling and ${expectedMemoryValueWithScaling} with scaling ` +
`for ${input ?? "no user input"} on ${platform} with ${totalMemoryMb} MB total system RAM`, async (t) => {
`for ${input ?? "no user input"} on ${platform} with ${totalMemoryMb} MB total system RAM${reservedPercentageValue
? ` and reserved percentage env var set to ${reservedPercentageValue}`
: ""}`, async (t) => {
process.env[environment_1.EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] =
reservedPercentageValue || undefined;
for (const withScaling of [true, false]) {
const flag = util.getMemoryFlagValueForPlatform(input, totalMemoryMb * 1024 * 1024, platform, withScaling);
t.deepEqual(flag, withScaling ? expectedMemoryValueWithScaling : expectedMemoryValue);

File diff suppressed because one or more lines are too long

View file

@ -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",

View file

@ -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,

View file

@ -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