Skip to content

Commit 592977e

Browse files
authored
Merge pull request #2151 from angelapwen/fix-cpu-group-bug
Account for existing but empty `cpus` file
2 parents 1737b12 + 8cb81db commit 592977e

File tree

7 files changed

+68
-7
lines changed

7 files changed

+68
-7
lines changed

Diff for: CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Note that the only difference between `v2` and `v3` of the CodeQL Action is the
66

77
## [UNRELEASED]
88

9-
No user facing changes.
9+
- Fix an issue where an existing, but empty, `/sys/fs/cgroup/cpuset.cpus` file always resulted in a single-threaded run. [#2151](https://github.com/github/codeql-action/pull/2151)
1010

1111
## 3.24.3 - 15 Feb 2024
1212

Diff for: lib/util.js

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

Diff for: lib/util.js.map

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

Diff for: lib/util.test.js

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

Diff for: 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.

Diff for: src/util.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,36 @@ for (const [
449449
versionStub.restore();
450450
});
451451
}
452+
453+
test("getCgroupCpuCountFromCpus calculates the number of CPUs correctly", async (t) => {
454+
await util.withTmpDir(async (tmpDir: string) => {
455+
const testCpuFile = `${tmpDir}/cpus-file`;
456+
fs.writeFileSync(testCpuFile, "1, 9-10\n", "utf-8");
457+
t.deepEqual(
458+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
459+
3,
460+
);
461+
});
462+
});
463+
464+
test("getCgroupCpuCountFromCpus returns undefined if the CPU file doesn't exist", async (t) => {
465+
await util.withTmpDir(async (tmpDir: string) => {
466+
const testCpuFile = `${tmpDir}/cpus-file`;
467+
t.false(fs.existsSync(testCpuFile));
468+
t.deepEqual(
469+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
470+
undefined,
471+
);
472+
});
473+
});
474+
475+
test("getCgroupCpuCountFromCpus returns undefined if the CPU file exists but is empty", async (t) => {
476+
await util.withTmpDir(async (tmpDir: string) => {
477+
const testCpuFile = `${tmpDir}/cpus-file`;
478+
fs.writeFileSync(testCpuFile, "\n", "utf-8");
479+
t.deepEqual(
480+
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
481+
undefined,
482+
);
483+
});
484+
});

Diff for: src/util.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function getCgroupCpuCountFromCpuMax(
440440
/**
441441
* Gets the number of available cores listed in the cgroup cpuset.cpus file at the given path.
442442
*/
443-
function getCgroupCpuCountFromCpus(
443+
export function getCgroupCpuCountFromCpus(
444444
cpusFile: string,
445445
logger: Logger,
446446
): number | undefined {
@@ -453,7 +453,10 @@ function getCgroupCpuCountFromCpus(
453453

454454
let cpuCount = 0;
455455
// Comma-separated numbers and ranges, for eg. 0-1,3
456-
const cpusString = fs.readFileSync(cpusFile, "utf-8");
456+
const cpusString = fs.readFileSync(cpusFile, "utf-8").trim();
457+
if (cpusString.length === 0) {
458+
return undefined;
459+
}
457460
for (const token of cpusString.split(",")) {
458461
if (!token.includes("-")) {
459462
// Not a range

0 commit comments

Comments
 (0)