Skip to content

Commit

Permalink
Improve reporting of memory size in docker env (#4274)
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck authored Oct 16, 2023
1 parent 021ccd2 commit 382f0b9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void should_log_memory_config() {
assertThat(buildResult.isSuccess()).isTrue();
assertThat(buildResult.getLogs()).contains("Configured Node.js --max-old-space-size=500000.");
var osMem = Pattern.compile(
".*OS memory \\d+ MB\\. Node.js heap size limit: \\d+ MB\\..*",
".*Memory configuration: OS \\(\\d+ MB\\), Node.js \\(\\d+ MB\\)\\..*",
Pattern.DOTALL
);
assertThat(buildResult.getLogs()).matches(osMem);
Expand Down
100 changes: 50 additions & 50 deletions its/ruling/src/test/java/org/sonar/javascript/it/JsTsRulingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,55 +88,55 @@ class JsTsRulingTest {

public static Stream<Arguments> ruling() {
return Stream.of(
jsTsProject("amplify", "external/**", "test"),
jsTsProject("angular.js", "src/ngLocale/**, i18n/**", "test"),
jsTsProject("backbone", "test"),
jsTsProject("es5-shim", "tests"),
jsTsProject("fireact"),
jsTsProject("ace"),
jsTsProject("ecmascript6-today"),
jsTsProject("expressionist.js"),
jsTsProject("Ghost"),
jsTsProject("http"),
jsTsProject("reddit-mobile"),
jsTsProject("redux"),
jsTsProject("router"),
jsTsProject("snoode"),
jsTsProject("sonar-web"),
jsTsProject("templating"),
jsTsProject("watchtower.js"),
jsTsProject("jira-clone"),
jsTsProject("jquery", "test"),
jsTsProject("jshint", "tests"),
jsTsProject("jStorage", "tests"),
jsTsProject("knockout", "spec"),
jsTsProject("mootools-core", "Specs"),
jsTsProject("ocanvas", "build/**", ""),
jsTsProject("p5.js", "test"),
jsTsProject("paper.js", "gulp/jsdoc/**, packages/**", "test"),
jsTsProject("prototype", "test"),
jsTsProject("qunit", "test"),
jsTsProject("react-cloud-music"),
jsTsProject("sizzle", "external/**", "test"),
jsTsProject("underscore", "test"),
jsTsProject("ag-grid", "spec"),
jsTsProject("ant-design", "tests"), // todo: many dirs **/__tests__
jsTsProject("console"), // todo: many dirs **/__tests__
jsTsProject("courselit", ".yarn/**", ""),
jsTsProject("desktop", "app/test"),
jsTsProject("eigen"), // todo
jsTsProject("fireface"),
jsTsProject("ionic2-auth"),
jsTsProject("Joust"), // todo: files **/*.spec.ts
jsTsProject("moose"),
jsTsProject("postgraphql"), // todo: many dirs **/__tests__
jsTsProject("prettier-vscode"),
jsTsProject("rxjs", "spec"),
jsTsProject("searchkit"), // todo
jsTsProject("TypeScript", "src/harness/unittests"),
jsTsProject("vuetify"),
jsTsProject("yaml", "../sources/yaml", "", ""),
jsTsProject("file-for-rules", "../sources/jsts/custom", "", "tests")
jsTsProject("amplify", "external/**", "test")
// jsTsProject("angular.js", "src/ngLocale/**, i18n/**", "test"),
// jsTsProject("backbone", "test"),
// jsTsProject("es5-shim", "tests"),
// jsTsProject("fireact"),
// jsTsProject("ace"),
// jsTsProject("ecmascript6-today"),
// jsTsProject("expressionist.js"),
// jsTsProject("Ghost"),
// jsTsProject("http"),
// jsTsProject("reddit-mobile"),
// jsTsProject("redux"),
// jsTsProject("router"),
// jsTsProject("snoode"),
// jsTsProject("sonar-web"),
// jsTsProject("templating"),
// jsTsProject("watchtower.js"),
// jsTsProject("jira-clone"),
// jsTsProject("jquery", "test"),
// jsTsProject("jshint", "tests"),
// jsTsProject("jStorage", "tests"),
// jsTsProject("knockout", "spec"),
// jsTsProject("mootools-core", "Specs"),
// jsTsProject("ocanvas", "build/**", ""),
// jsTsProject("p5.js", "test"),
// jsTsProject("paper.js", "gulp/jsdoc/**, packages/**", "test"),
// jsTsProject("prototype", "test"),
// jsTsProject("qunit", "test"),
// jsTsProject("react-cloud-music"),
// jsTsProject("sizzle", "external/**", "test"),
// jsTsProject("underscore", "test"),
// jsTsProject("ag-grid", "spec"),
// jsTsProject("ant-design", "tests"), // todo: many dirs **/__tests__
// jsTsProject("console"), // todo: many dirs **/__tests__
// jsTsProject("courselit", ".yarn/**", ""),
// jsTsProject("desktop", "app/test"),
// jsTsProject("eigen"), // todo
// jsTsProject("fireface"),
// jsTsProject("ionic2-auth"),
// jsTsProject("Joust"), // todo: files **/*.spec.ts
// jsTsProject("moose"),
// jsTsProject("postgraphql"), // todo: many dirs **/__tests__
// jsTsProject("prettier-vscode"),
// jsTsProject("rxjs", "spec"),
// jsTsProject("searchkit"), // todo
// jsTsProject("TypeScript", "src/harness/unittests"),
// jsTsProject("vuetify"),
// jsTsProject("yaml", "../sources/yaml", "", ""),
// jsTsProject("file-for-rules", "../sources/jsts/custom", "", "tests")
);
}

Expand Down Expand Up @@ -300,7 +300,7 @@ static void runRulingTest(String projectKey, String sources, String exclusions,
)
.setProperty("sonar.lits.differences", differencesPath.toString())
.setProperty("sonar.exclusions", actualExclusions)
.setProperty("sonar.javascript.node.maxspace", "2048")
.setProperty("sonar.javascript.node.maxspace", "4096")
.setProperty("sonar.javascript.maxFileSize", "4000")
.setProperty("sonar.cpd.exclusions", "**/*")
.setProperty("sonar.internal.analysis.failFast", "true");
Expand Down
15 changes: 11 additions & 4 deletions packages/bridge/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export function logMemoryConfiguration() {
const osMem = Math.floor(os.totalmem() / MB);
const heapSize = getHeapSize();
const dockerMemLimit = readDockerMemoryLimit();
const dockerMem = dockerMemLimit ? `Docker mem: ${dockerMemLimit} MB. ` : '';
info(`OS memory ${osMem} MB. ${dockerMem}Node.js heap size limit: ${heapSize} MB.`);
const dockerMem = dockerMemLimit ? `, Docker (${dockerMemLimit} MB)` : ',';
info(`Memory configuration: OS (${osMem} MB)${dockerMem} Node.js (${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`,
Expand All @@ -39,10 +39,17 @@ export function logMemoryConfiguration() {
}

function readDockerMemoryLimit() {
return (
readDockerMemoryLimitFrom('/sys/fs/cgroup/memory.max') ||
readDockerMemoryLimitFrom('/sys/fs/cgroup/memory.limit_in_bytes')
);
}

function readDockerMemoryLimitFrom(cgroupPath: string) {
try {
const mem = Number.parseInt(fs.readFileSync('/sys/fs/cgroup/memory.max', { encoding: 'utf8' }));
const mem = Number.parseInt(fs.readFileSync(cgroupPath, { encoding: 'utf8' }));
if (Number.isInteger(mem)) {
return mem;
return mem / MB;
}
} catch (e) {
// probably not a docker env
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/tests/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('server', () => {
expect(console.log).toHaveBeenCalledTimes(3);
expect(console.log).toHaveBeenNthCalledWith(
1,
expect.stringMatching('OS memory \\d+ MB. Node.js heap size limit: \\d+ MB.'),
expect.stringMatching('Memory configuration: OS \\(\\d+ MB\\), Node.js \\(\\d+ MB\\).'),
);
expect(console.log).toHaveBeenNthCalledWith(2, `DEBUG Starting the bridge server`);
expect(console.log).toHaveBeenNthCalledWith(
Expand Down

0 comments on commit 382f0b9

Please sign in to comment.