Skip to content

Commit

Permalink
Allow customizing the scaling threshold with an environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mercer committed Sep 5, 2023
1 parent 466ed42 commit 574dbbc
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 12 deletions.
5 changes: 5 additions & 0 deletions lib/environment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/environment.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions lib/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.js.map

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions lib/util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.test.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
18 changes: 17 additions & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
24 changes: 21 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 574dbbc

Please sign in to comment.