Increase the default amount of RAM reserved for the OS

Mitigation for OOM errors (137/SIGKILL) seen by users when we overcommit the available memory.
For Unix, reserve 1GB.
For Windows, reserve 1.5GB, as the OS needs more memory and estimates inaccurately.
This commit is contained in:
Aditya Sharad 2021-02-16 15:04:38 -08:00
Родитель 88714e3a60
Коммит 4c94e29f1b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 66D1E528054C320C
6 изменённых файлов: 39 добавлений и 12 удалений

18
lib/util.js сгенерированный
Просмотреть файл

@ -72,9 +72,21 @@ async function withTmpDir(body) {
return result;
}
exports.withTmpDir = withTmpDir;
/**
* Gets an OS-specific amount of memory (in MB) to reserve for OS processes
* when the user doesn't explicitly specify a memory setting.
* This is a heuristic to avoid OOM errors (exit code 137 / SIGKILL)
* from committing too much of the available memory to CodeQL.
* @returns number
*/
function getSystemReservedMemoryMegaBytes() {
// Windows needs more memory for OS processes.
return 1024 * (process.platform === "win32" ? 1.5 : 1);
}
/**
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
* specified, the total available memory will be used minus 256 MB.
* specified, the total available memory will be used minus a threshold
* reserved for the OS.
*
* @returns string
*/
@ -89,8 +101,8 @@ function getMemoryFlag(userInput) {
else {
const totalMemoryBytes = os.totalmem();
const totalMemoryMegaBytes = totalMemoryBytes / (1024 * 1024);
const systemReservedMemoryMegaBytes = 256;
memoryToUseMegaBytes = totalMemoryMegaBytes - systemReservedMemoryMegaBytes;
const reservedMemoryMegaBytes = getSystemReservedMemoryMegaBytes();
memoryToUseMegaBytes = totalMemoryMegaBytes - reservedMemoryMegaBytes;
}
return `--ram=${Math.floor(memoryToUseMegaBytes)}`;
}

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

5
lib/util.test.js сгенерированный
Просмотреть файл

@ -28,9 +28,10 @@ ava_1.default("getToolNames", (t) => {
});
ava_1.default("getMemoryFlag() should return the correct --ram flag", (t) => {
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const expectedThreshold = process.platform === "win32" ? 1536 : 1024;
const tests = [
[undefined, `--ram=${totalMem - 256}`],
["", `--ram=${totalMem - 256}`],
[undefined, `--ram=${totalMem - expectedThreshold}`],
["", `--ram=${totalMem - expectedThreshold}`],
["512", "--ram=512"],
];
for (const [input, expectedFlag] of tests) {

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -24,10 +24,11 @@ test("getToolNames", (t) => {
test("getMemoryFlag() should return the correct --ram flag", (t) => {
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const expectedThreshold = process.platform === "win32" ? 1536 : 1024;
const tests = [
[undefined, `--ram=${totalMem - 256}`],
["", `--ram=${totalMem - 256}`],
[undefined, `--ram=${totalMem - expectedThreshold}`],
["", `--ram=${totalMem - expectedThreshold}`],
["512", "--ram=512"],
];

Просмотреть файл

@ -82,9 +82,22 @@ export async function withTmpDir<T>(
return result;
}
/**
* Gets an OS-specific amount of memory (in MB) to reserve for OS processes
* when the user doesn't explicitly specify a memory setting.
* This is a heuristic to avoid OOM errors (exit code 137 / SIGKILL)
* from committing too much of the available memory to CodeQL.
* @returns number
*/
function getSystemReservedMemoryMegaBytes(): number {
// Windows needs more memory for OS processes.
return 1024 * (process.platform === "win32" ? 1.5 : 1);
}
/**
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
* specified, the total available memory will be used minus 256 MB.
* specified, the total available memory will be used minus a threshold
* reserved for the OS.
*
* @returns string
*/
@ -98,8 +111,8 @@ export function getMemoryFlag(userInput: string | undefined): string {
} else {
const totalMemoryBytes = os.totalmem();
const totalMemoryMegaBytes = totalMemoryBytes / (1024 * 1024);
const systemReservedMemoryMegaBytes = 256;
memoryToUseMegaBytes = totalMemoryMegaBytes - systemReservedMemoryMegaBytes;
const reservedMemoryMegaBytes = getSystemReservedMemoryMegaBytes();
memoryToUseMegaBytes = totalMemoryMegaBytes - reservedMemoryMegaBytes;
}
return `--ram=${Math.floor(memoryToUseMegaBytes)}`;
}