Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory configuration message when running in Docker #4269

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/bridge/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { AddressInfo } from 'net';
import * as v8 from 'v8';
import * as os from 'os';
import { Worker } from 'worker_threads';
import fs from 'fs';

/**
* The maximum request body size
Expand All @@ -48,17 +49,33 @@ const MAX_REQUEST_SIZE = '50mb';
*/
const SHUTDOWN_TIMEOUT = 15_000;

const MB = 1024 * 1024;

function logMemoryConfiguration() {
const osMem = Math.floor(os.totalmem() / 1_000_000);
const heapSize = Math.floor(v8.getHeapStatistics().heap_size_limit / 1_000_000);
info(`OS memory ${osMem} MB. Node.js heap size limit: ${heapSize} MB.`);
const osMem = Math.floor(os.totalmem() / MB);
const heapSize = Math.floor(v8.getHeapStatistics().heap_size_limit / MB);
const dockerMemLimit = readDockerMemoryLimit();
const dockerMem = dockerMemLimit ? `Docker mem: ${dockerMemLimit} MB. ` : '';
info(`OS memory ${osMem} MB. ${dockerMem}Node.js heap size limit: ${heapSize} MB.`);
if (heapSize > osMem) {
warn(
`Node.js heap size limit ${heapSize} is higher than available memory ${osMem}. Check your configuration of sonar.javascript.node.maxspace`,
);
}
}

function readDockerMemoryLimit() {
try {
const mem = Number.parseInt(fs.readFileSync('/sys/fs/cgroup/memory.max', { encoding: 'utf8' }));
if (Number.isInteger(mem)) {
return mem;
}
} catch (e) {
// probably not a docker env
}
return undefined;
}

/**
* A pool of a single worker thread
*
Expand Down