Skip to content

Commit 574dbbc

Browse files
committed
Allow customizing the scaling threshold with an environment variable
1 parent 466ed42 commit 574dbbc

9 files changed

+82
-12
lines changed

lib/environment.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/environment.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

+15-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/environment.ts

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export enum EnvVar {
3737

3838
ODASA_TRACER_CONFIGURATION = "ODASA_TRACER_CONFIGURATION",
3939

40+
/**
41+
* What percentage of the total amount of RAM over 8 GB that the Action should reserve for the
42+
* system.
43+
*/
44+
SCALING_RESERVED_RAM_PERCENTAGE = "CODEQL_ACTION_SCALING_RESERVED_RAM_PERCENTAGE",
45+
4046
/** Whether to suppress the warning if the current CLI will soon be unsupported. */
4147
SUPPRESS_DEPRECATED_SOON_WARNING = "CODEQL_ACTION_SUPPRESS_DEPRECATED_SOON_WARNING",
4248

src/util.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path";
44

55
import test from "ava";
66

7+
import { EnvVar } from "./environment";
78
import { getRunnerLogger } from "./logging";
89
import { getRecordingLogger, LoggedMessage, setupTests } from "./testing-utils";
910
import * as util from "./util";
@@ -62,6 +63,14 @@ const GET_MEMORY_FLAG_TESTS = [
6263
expectedMemoryValue: 62.5 * 1024,
6364
expectedMemoryValueWithScaling: 61132, // Math.floor(1024 * (64 - 1.5 - 0.05 * (64 - 8)))
6465
},
66+
{
67+
input: undefined,
68+
totalMemoryMb: 64 * 1024,
69+
platform: "linux",
70+
expectedMemoryValue: 63 * 1024,
71+
expectedMemoryValueWithScaling: 58777, // Math.floor(1024 * (64 - 1 - 0.1 * (64 - 8)))
72+
reservedPercentageValue: "10",
73+
},
6574
];
6675

6776
for (const {
@@ -70,13 +79,20 @@ for (const {
7079
platform,
7180
expectedMemoryValue,
7281
expectedMemoryValueWithScaling,
82+
reservedPercentageValue,
7383
} of GET_MEMORY_FLAG_TESTS) {
7484
test(
7585
`Memory flag value is ${expectedMemoryValue} without scaling and ${expectedMemoryValueWithScaling} with scaling ` +
7686
`for ${
7787
input ?? "no user input"
78-
} on ${platform} with ${totalMemoryMb} MB total system RAM`,
88+
} on ${platform} with ${totalMemoryMb} MB total system RAM${
89+
reservedPercentageValue
90+
? ` and reserved percentage env var set to ${reservedPercentageValue}`
91+
: ""
92+
}`,
7993
async (t) => {
94+
process.env[EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] =
95+
reservedPercentageValue || undefined;
8096
for (const withScaling of [true, false]) {
8197
const flag = util.getMemoryFlagValueForPlatform(
8298
input,

src/util.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export const DEFAULT_DEBUG_ARTIFACT_NAME = "debug-artifacts";
3737
*/
3838
export const DEFAULT_DEBUG_DATABASE_NAME = "db";
3939

40+
/**
41+
* The default fraction of the total RAM above 8 GB that should be reserved for the system.
42+
*/
43+
const DEFAULT_RESERVED_RAM_SCALING_FACTOR = 0.05;
44+
4045
export interface SarifFile {
4146
version?: string | null;
4247
runs: SarifRun[];
@@ -161,15 +166,28 @@ function getSystemReservedMemoryMegaBytes(
161166
const fixedAmount = 1024 * (platform === "win32" ? 1.5 : 1);
162167

163168
if (isScalingReservedRamEnabled) {
164-
// Reserve an additional 5% of the amount of memory above 8 GB, since the amount used by the
165-
// kernel for page tables scales with the size of physical memory.
166-
const scaledAmount = 0.05 * Math.max(totalMemoryMegaBytes - 8 * 1024, 0);
169+
// Reserve an additional percentage of the amount of memory above 8 GB, since the amount used by
170+
// the kernel for page tables scales with the size of physical memory.
171+
const scaledAmount =
172+
getReservedRamScaleFactor() *
173+
Math.max(totalMemoryMegaBytes - 8 * 1024, 0);
167174
return fixedAmount + scaledAmount;
168175
} else {
169176
return fixedAmount;
170177
}
171178
}
172179

180+
function getReservedRamScaleFactor(): number {
181+
const envVar = Number.parseInt(
182+
process.env[EnvVar.SCALING_RESERVED_RAM_PERCENTAGE] || "",
183+
10,
184+
);
185+
if (envVar < 0 || envVar > 100 || Number.isNaN(envVar)) {
186+
return DEFAULT_RESERVED_RAM_SCALING_FACTOR;
187+
}
188+
return envVar / 100;
189+
}
190+
173191
/**
174192
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
175193
* If no value was specified, the total available memory will be used minus a

0 commit comments

Comments
 (0)