Skip to content

Commit

Permalink
Merge pull request #2151 from angelapwen/fix-cpu-group-bug
Browse files Browse the repository at this point in the history
Account for existing but empty `cpus` file
  • Loading branch information
angelapwen authored Feb 20, 2024
2 parents 1737b12 + 8cb81db commit 592977e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Note that the only difference between `v2` and `v3` of the CodeQL Action is the

## [UNRELEASED]

No user facing changes.
- 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)

## 3.24.3 - 15 Feb 2024

Expand Down
8 changes: 6 additions & 2 deletions lib/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.js.map

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions lib/util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/util.test.js.map

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,36 @@ for (const [
versionStub.restore();
});
}

test("getCgroupCpuCountFromCpus calculates the number of CPUs correctly", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
fs.writeFileSync(testCpuFile, "1, 9-10\n", "utf-8");
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
3,
);
});
});

test("getCgroupCpuCountFromCpus returns undefined if the CPU file doesn't exist", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
t.false(fs.existsSync(testCpuFile));
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
undefined,
);
});
});

test("getCgroupCpuCountFromCpus returns undefined if the CPU file exists but is empty", async (t) => {
await util.withTmpDir(async (tmpDir: string) => {
const testCpuFile = `${tmpDir}/cpus-file`;
fs.writeFileSync(testCpuFile, "\n", "utf-8");
t.deepEqual(
util.getCgroupCpuCountFromCpus(testCpuFile, getRunnerLogger(true)),
undefined,
);
});
});
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function getCgroupCpuCountFromCpuMax(
/**
* Gets the number of available cores listed in the cgroup cpuset.cpus file at the given path.
*/
function getCgroupCpuCountFromCpus(
export function getCgroupCpuCountFromCpus(
cpusFile: string,
logger: Logger,
): number | undefined {
Expand All @@ -453,7 +453,10 @@ function getCgroupCpuCountFromCpus(

let cpuCount = 0;
// Comma-separated numbers and ranges, for eg. 0-1,3
const cpusString = fs.readFileSync(cpusFile, "utf-8");
const cpusString = fs.readFileSync(cpusFile, "utf-8").trim();
if (cpusString.length === 0) {
return undefined;
}
for (const token of cpusString.split(",")) {
if (!token.includes("-")) {
// Not a range
Expand Down

0 comments on commit 592977e

Please sign in to comment.