Skip to content

Commit 7b6664f

Browse files
authored
Merge pull request #1796 from github/henrymercer/scaling-ram-larger-runners-only
Scale the amount of reserved RAM on large runners only
2 parents ce84bed + fda93d8 commit 7b6664f

File tree

6 files changed

+172
-54
lines changed

6 files changed

+172
-54
lines changed

lib/util.js

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js

Lines changed: 53 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util.test.ts

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,79 @@ test("getToolNames", (t) => {
1919
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
2020
});
2121

22-
test("getMemoryFlag() should return the correct --ram flag", async (t) => {
23-
const totalMem = os.totalmem() / (1024 * 1024);
24-
const fixedAmount = process.platform === "win32" ? 1536 : 1024;
25-
const scaledAmount = 0.02 * totalMem;
26-
const expectedMemoryValue = Math.floor(totalMem - fixedAmount);
27-
const expectedMemoryValueWithScaling = Math.floor(
28-
totalMem - fixedAmount - scaledAmount
22+
const GET_MEMORY_FLAG_TESTS = [
23+
{
24+
input: undefined,
25+
totalMemoryMb: 8 * 1024,
26+
platform: "linux",
27+
expectedMemoryValue: 7 * 1024,
28+
expectedMemoryValueWithScaling: 7 * 1024,
29+
},
30+
{
31+
input: undefined,
32+
totalMemoryMb: 8 * 1024,
33+
platform: "win32",
34+
expectedMemoryValue: 6.5 * 1024,
35+
expectedMemoryValueWithScaling: 6.5 * 1024,
36+
},
37+
{
38+
input: "",
39+
totalMemoryMb: 8 * 1024,
40+
platform: "linux",
41+
expectedMemoryValue: 7 * 1024,
42+
expectedMemoryValueWithScaling: 7 * 1024,
43+
},
44+
{
45+
input: "512",
46+
totalMemoryMb: 8 * 1024,
47+
platform: "linux",
48+
expectedMemoryValue: 512,
49+
expectedMemoryValueWithScaling: 512,
50+
},
51+
{
52+
input: undefined,
53+
totalMemoryMb: 64 * 1024,
54+
platform: "linux",
55+
expectedMemoryValue: 63 * 1024,
56+
expectedMemoryValueWithScaling: 63078, // Math.floor(1024 * (64 - 1 - 0.025 * (64 - 8)))
57+
},
58+
{
59+
input: undefined,
60+
totalMemoryMb: 64 * 1024,
61+
platform: "win32",
62+
expectedMemoryValue: 62.5 * 1024,
63+
expectedMemoryValueWithScaling: 62566, // Math.floor(1024 * (64 - 1.5 - 0.025 * (64 - 8)))
64+
},
65+
];
66+
67+
for (const {
68+
input,
69+
totalMemoryMb,
70+
platform,
71+
expectedMemoryValue,
72+
expectedMemoryValueWithScaling,
73+
} of GET_MEMORY_FLAG_TESTS) {
74+
test(
75+
`Memory flag value is ${expectedMemoryValue} without scaling and ${expectedMemoryValueWithScaling} with scaling ` +
76+
`for ${
77+
input ?? "no user input"
78+
} on ${platform} with ${totalMemoryMb} MB total system RAM`,
79+
async (t) => {
80+
for (const withScaling of [true, false]) {
81+
const flag = util.getMemoryFlagValueForPlatform(
82+
input,
83+
totalMemoryMb * 1024 * 1024,
84+
platform,
85+
withScaling
86+
);
87+
t.deepEqual(
88+
flag,
89+
withScaling ? expectedMemoryValueWithScaling : expectedMemoryValue
90+
);
91+
}
92+
}
2993
);
30-
31-
const tests: Array<[string | undefined, boolean, string]> = [
32-
[undefined, false, `--ram=${expectedMemoryValue}`],
33-
["", false, `--ram=${expectedMemoryValue}`],
34-
["512", false, "--ram=512"],
35-
[undefined, true, `--ram=${expectedMemoryValueWithScaling}`],
36-
["", true, `--ram=${expectedMemoryValueWithScaling}`],
37-
];
38-
39-
for (const [input, withScaling, expectedFlag] of tests) {
40-
const flag = util.getMemoryFlag(input, withScaling);
41-
t.deepEqual(flag, expectedFlag);
42-
}
43-
});
94+
}
4495

4596
test("getMemoryFlag() throws if the ram input is < 0 or NaN", async (t) => {
4697
for (const input of ["-1", "hello!"]) {

src/util.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ export async function withTmpDir<T>(
153153
*/
154154
function getSystemReservedMemoryMegaBytes(
155155
totalMemoryMegaBytes: number,
156+
platform: string,
156157
isScalingReservedRamEnabled: boolean
157158
): number {
158159
// Windows needs more memory for OS processes.
159-
const fixedAmount = 1024 * (process.platform === "win32" ? 1.5 : 1);
160+
const fixedAmount = 1024 * (platform === "win32" ? 1.5 : 1);
160161

161162
if (isScalingReservedRamEnabled) {
162-
// Reserve an additional 2% of the total memory, since the amount used by
163+
// Reserve an additional 2.5% of the amount of memory above 8 GB, since the amount used by
163164
// the kernel for page tables scales with the size of physical memory.
164-
const scaledAmount = 0.02 * totalMemoryMegaBytes;
165+
const scaledAmount = 0.025 * Math.max(totalMemoryMegaBytes - 8 * 1024, 0);
165166
return fixedAmount + scaledAmount;
166167
} else {
167168
return fixedAmount;
@@ -175,8 +176,10 @@ function getSystemReservedMemoryMegaBytes(
175176
*
176177
* @returns {number} the amount of RAM to use, in megabytes
177178
*/
178-
export function getMemoryFlagValue(
179+
export function getMemoryFlagValueForPlatform(
179180
userInput: string | undefined,
181+
totalMemoryBytes: number,
182+
platform: string,
180183
isScalingReservedRamEnabled: boolean
181184
): number {
182185
let memoryToUseMegaBytes: number;
@@ -186,17 +189,36 @@ export function getMemoryFlagValue(
186189
throw new Error(`Invalid RAM setting "${userInput}", specified.`);
187190
}
188191
} else {
189-
const totalMemoryBytes = os.totalmem();
190192
const totalMemoryMegaBytes = totalMemoryBytes / (1024 * 1024);
191193
const reservedMemoryMegaBytes = getSystemReservedMemoryMegaBytes(
192194
totalMemoryMegaBytes,
195+
platform,
193196
isScalingReservedRamEnabled
194197
);
195198
memoryToUseMegaBytes = totalMemoryMegaBytes - reservedMemoryMegaBytes;
196199
}
197200
return Math.floor(memoryToUseMegaBytes);
198201
}
199202

203+
/**
204+
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
205+
* If no value was specified, the total available memory will be used minus a
206+
* threshold reserved for the OS.
207+
*
208+
* @returns {number} the amount of RAM to use, in megabytes
209+
*/
210+
export function getMemoryFlagValue(
211+
userInput: string | undefined,
212+
isScalingReservedRamEnabled: boolean
213+
): number {
214+
return getMemoryFlagValueForPlatform(
215+
userInput,
216+
os.totalmem(),
217+
process.platform,
218+
isScalingReservedRamEnabled
219+
);
220+
}
221+
200222
/**
201223
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
202224
* specified, the total available memory will be used minus a threshold

0 commit comments

Comments
 (0)