Skip to content

Commit 70b730e

Browse files
committedOct 28, 2021
Add RAM and threads options to init action
1 parent 2905689 commit 70b730e

25 files changed

+631
-58
lines changed
 

‎.github/workflows/__extractor-ram-threads.yml

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

‎.github/workflows/pr-checks.yml

+39
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,42 @@ jobs:
393393
# Deliberately don't use TEST_MODE here. This is specifically testing
394394
# the compatibility with the API.
395395
runner/dist/codeql-runner-linux upload --sarif-file src/testdata/empty-sarif.sarif --repository $GITHUB_REPOSITORY --commit $GITHUB_SHA --ref $GITHUB_REF --github-url $GITHUB_SERVER_URL --github-auth ${{ github.token }}
396+
397+
runner-extractor-ram-threads-options:
398+
name: Runner ubuntu extractor RAM and threads options
399+
needs: [check-js, check-node-modules]
400+
runs-on: ubuntu-latest
401+
402+
steps:
403+
- uses: actions/checkout@v2
404+
405+
- name: Build runner
406+
run: |
407+
cd runner
408+
npm install
409+
npm run build-runner
410+
411+
- name: Run init
412+
run: |
413+
runner/dist/codeql-runner-linux init --ram=230 --threads=1 --repository $GITHUB_REPOSITORY --languages java --github-url $GITHUB_SERVER_URL --github-auth ${{ github.token }}
414+
415+
- name: Assert Results
416+
shell: bash
417+
run: |
418+
. ./codeql-runner/codeql-env.sh
419+
if [ "${CODEQL_RAM}" != "230" ]; then
420+
echo "CODEQL_RAM is '${CODEQL_RAM}' instead of 230"
421+
exit 1
422+
fi
423+
if [ "${CODEQL_EXTRACTOR_JAVA_RAM}" != "230" ]; then
424+
echo "CODEQL_EXTRACTOR_JAVA_RAM is '${CODEQL_EXTRACTOR_JAVA_RAM}' instead of 230"
425+
exit 1
426+
fi
427+
if [ "${CODEQL_THREADS}" != "1" ]; then
428+
echo "CODEQL_THREADS is '${CODEQL_THREADS}' instead of 1"
429+
exit 1
430+
fi
431+
if [ "${CODEQL_EXTRACTOR_JAVA_THREADS}" != "1" ]; then
432+
echo "CODEQL_EXTRACTOR_JAVA_THREADS is '${CODEQL_EXTRACTOR_JAVA_THREADS}' instead of 1"
433+
exit 1
434+
fi

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [UNRELEASED]
44

5-
No user facing changes.
5+
- The `init` step of the Action now supports `ram` and `threads` inputs to limit resource use of CodeQL extractors. These inputs also serve as defaults to the subsequent `analyze` step, which finalizes the database and executes queries. [#738](https://github.com/github/codeql-action/pull/738)
66

77
## 1.0.21 - 28 Oct 2021
88

‎analyze/action.yml

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ inputs:
1818
required: false
1919
default: "brutal"
2020
ram:
21-
description: Override the amount of memory in MB to be used by CodeQL. By default, almost all the memory of the machine is used.
21+
description: >-
22+
The amount of memory in MB that can be used by CodeQL for database finalization and query execution.
23+
By default, this action will use the same amount of memory as previously set in the "init" action.
24+
If the "init" action also does not have an explicit "ram" input, this action will use most of the
25+
memory available in the system (which for GitHub-hosted runners is 6GB for Linux, 5.5GB for Windows,
26+
and 13GB for macOS).
2227
required: false
2328
add-snippets:
2429
description: Specify whether or not to add code snippets to the output sarif file.
@@ -29,7 +34,12 @@ inputs:
2934
required: false
3035
default: "false"
3136
threads:
32-
description: The number of threads to be used by CodeQL.
37+
description: >-
38+
The number of threads that can be used by CodeQL for database finalization and query execution.
39+
By default, this action will use the same number of threads as previously set in the "init" action.
40+
If the "init" action also does not have an explicit "threads" input, this action will use all the
41+
hardware threads available in the system (which for GitHub-hosted runners is 2 for Linux and Windows
42+
and 3 for macOS).
3343
required: false
3444
checkout_path:
3545
description: "The path at which the analyzed repository was checked out. Used to relativize any absolute paths in the uploaded SARIF file."

‎init/action.yml

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ inputs:
4141
source-root:
4242
description: Path of the root source code directory, relative to $GITHUB_WORKSPACE.
4343
required: false
44+
ram:
45+
description: >-
46+
The amount of memory in MB that can be used by CodeQL extractors.
47+
By default, CodeQL extractors will use most of the memory available in the system
48+
(which for GitHub-hosted runners is 6GB for Linux, 5.5GB for Windows, and 13GB for macOS).
49+
This input also sets the amount of memory that can later be used by the "analyze" action.
50+
required: false
51+
threads:
52+
description: >-
53+
The number of threads that can be used by CodeQL extractors.
54+
By default, CodeQL extractors will use all the hardware threads available in the system
55+
(which for GitHub-hosted runners is 2 for Linux and Windows and 3 for macOS).
56+
This input also sets the number of threads that can later be used by the "analyze" action.
57+
required: false
4458
outputs:
4559
codeql-path:
4660
description: The path of the CodeQL binary used for analysis

‎lib/analyze-action-env.test.js

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

‎lib/analyze-action-env.test.js.map

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

‎lib/analyze-action-input.test.js

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

‎lib/analyze-action-input.test.js.map

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

‎lib/analyze-action.js

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

‎lib/analyze-action.js.map

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

‎lib/init-action.js

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

‎lib/init-action.js.map

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

‎lib/runner.js

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

‎lib/runner.js.map

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

‎lib/util.js

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

‎lib/util.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: "Extractor ram and threads options test"
2+
description: "Tests passing RAM and threads limits to extractors"
3+
versions: ["latest"]
4+
os: ["ubuntu-latest"]
5+
steps:
6+
- uses: ./../action/init
7+
with:
8+
languages: java
9+
ram: 230
10+
threads: 1
11+
- name: Assert Results
12+
shell: bash
13+
run: |
14+
if [ "${CODEQL_RAM}" != "230" ]; then
15+
echo "CODEQL_RAM is '${CODEQL_RAM}' instead of 230"
16+
exit 1
17+
fi
18+
if [ "${CODEQL_EXTRACTOR_JAVA_RAM}" != "230" ]; then
19+
echo "CODEQL_EXTRACTOR_JAVA_RAM is '${CODEQL_EXTRACTOR_JAVA_RAM}' instead of 230"
20+
exit 1
21+
fi
22+
if [ "${CODEQL_THREADS}" != "1" ]; then
23+
echo "CODEQL_THREADS is '${CODEQL_THREADS}' instead of 1"
24+
exit 1
25+
fi
26+
if [ "${CODEQL_EXTRACTOR_JAVA_THREADS}" != "1" ]; then
27+
echo "CODEQL_EXTRACTOR_JAVA_THREADS is '${CODEQL_EXTRACTOR_JAVA_THREADS}' instead of 1"
28+
exit 1
29+
fi

‎queries/inconsistent-action-input.ql

+2
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ predicate areNotEquivalent(YAMLValue x, YAMLValue y) {
4747
from ActionDeclaration actionA, ActionDeclaration actionB, string inputName
4848
where actionA.getName() < actionB.getName() // prevent duplicates which are permutations of the names
4949
and areNotEquivalent(actionA.getInput(inputName), actionB.getInput(inputName))
50+
// ram and threads inputs in different actions are supposed to have different description
51+
and inputName != "ram" and inputName != "threads"
5052
select actionA, "Action $@ and action $@ both declare input $@, however their definitions are not identical. This may be confusing to users.",
5153
actionA, actionA.getName(), actionB, actionB.getName(), inputName, inputName

‎src/analyze-action-env.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import test from "ava";
2+
import * as sinon from "sinon";
3+
4+
import * as actionsUtil from "./actions-util";
5+
import * as analyze from "./analyze";
6+
import * as configUtils from "./config-utils";
7+
import { setupTests, setupActionsVars } from "./testing-utils";
8+
import * as util from "./util";
9+
10+
setupTests(test);
11+
12+
// This test needs to be in its own file so that ava would run it in its own
13+
// nodejs process. The code being tested is in analyze-action.ts, which runs
14+
// immediately on load. So the file needs to be loaded during part of the test,
15+
// and that can happen only once per nodejs process. If multiple such tests are
16+
// in the same test file, ava would run them in the same nodejs process, and all
17+
// but the first test would fail.
18+
19+
test("analyze action with RAM & threads from environment variables", async (t) => {
20+
await util.withTmpDir(async (tmpDir) => {
21+
process.env["GITHUB_SERVER_URL"] = "fake-server-url";
22+
process.env["GITHUB_REPOSITORY"] = "fake/repository";
23+
sinon
24+
.stub(actionsUtil, "createStatusReportBase")
25+
.resolves({} as actionsUtil.StatusReportBase);
26+
sinon.stub(actionsUtil, "sendStatusReport").resolves(true);
27+
sinon.stub(configUtils, "getConfig").resolves({
28+
languages: [],
29+
} as unknown as configUtils.Config);
30+
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
31+
requiredInputStub.withArgs("token").returns("fake-token");
32+
requiredInputStub.withArgs("upload-database").returns("false");
33+
const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput");
34+
optionalInputStub.withArgs("cleanup-level").returns("none");
35+
setupActionsVars(tmpDir, tmpDir);
36+
37+
// When there are no action inputs for RAM and threads, the action uses
38+
// environment variables (passed down from the init action) to set RAM and
39+
// threads usage.
40+
process.env["CODEQL_THREADS"] = "-1";
41+
process.env["CODEQL_RAM"] = "4992";
42+
43+
const runFinalizeStub = sinon.stub(analyze, "runFinalize");
44+
const runQueriesStub = sinon.stub(analyze, "runQueries");
45+
const analyzeAction = require("./analyze-action");
46+
47+
// When analyze-action.ts loads, it runs an async function from the top
48+
// level but does not wait for it to finish. To ensure that calls to
49+
// runFinalize and runQueries are correctly captured by spies, we explicitly
50+
// wait for the action promise to complete before starting verification.
51+
await analyzeAction.runPromise;
52+
53+
t.deepEqual(runFinalizeStub.firstCall.args[1], "--threads=-1");
54+
t.deepEqual(runFinalizeStub.firstCall.args[2], "--ram=4992");
55+
t.deepEqual(runQueriesStub.firstCall.args[3], "--threads=-1");
56+
t.deepEqual(runQueriesStub.firstCall.args[1], "--ram=4992");
57+
});
58+
});

‎src/analyze-action-input.test.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import test from "ava";
2+
import * as sinon from "sinon";
3+
4+
import * as actionsUtil from "./actions-util";
5+
import * as analyze from "./analyze";
6+
import * as configUtils from "./config-utils";
7+
import { setupTests, setupActionsVars } from "./testing-utils";
8+
import * as util from "./util";
9+
10+
setupTests(test);
11+
12+
// This test needs to be in its own file so that ava would run it in its own
13+
// nodejs process. The code being tested is in analyze-action.ts, which runs
14+
// immediately on load. So the file needs to be loaded during part of the test,
15+
// and that can happen only once per nodejs process. If multiple such tests are
16+
// in the same test file, ava would run them in the same nodejs process, and all
17+
// but the first test would fail.
18+
19+
test("analyze action with RAM & threads from action inputs", async (t) => {
20+
await util.withTmpDir(async (tmpDir) => {
21+
process.env["GITHUB_SERVER_URL"] = "fake-server-url";
22+
process.env["GITHUB_REPOSITORY"] = "fake/repository";
23+
sinon
24+
.stub(actionsUtil, "createStatusReportBase")
25+
.resolves({} as actionsUtil.StatusReportBase);
26+
sinon.stub(actionsUtil, "sendStatusReport").resolves(true);
27+
sinon.stub(configUtils, "getConfig").resolves({
28+
languages: [],
29+
} as unknown as configUtils.Config);
30+
const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput");
31+
requiredInputStub.withArgs("token").returns("fake-token");
32+
requiredInputStub.withArgs("upload-database").returns("false");
33+
const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput");
34+
optionalInputStub.withArgs("cleanup-level").returns("none");
35+
setupActionsVars(tmpDir, tmpDir);
36+
37+
process.env["CODEQL_THREADS"] = "1";
38+
process.env["CODEQL_RAM"] = "4992";
39+
40+
// Action inputs have precedence over environment variables.
41+
optionalInputStub.withArgs("threads").returns("-1");
42+
optionalInputStub.withArgs("ram").returns("3012");
43+
44+
const runFinalizeStub = sinon.stub(analyze, "runFinalize");
45+
const runQueriesStub = sinon.stub(analyze, "runQueries");
46+
const analyzeAction = require("./analyze-action");
47+
48+
// When analyze-action.ts loads, it runs an async function from the top
49+
// level but does not wait for it to finish. To ensure that calls to
50+
// runFinalize and runQueries are correctly captured by spies, we explicitly
51+
// wait for the action promise to complete before starting verification.
52+
await analyzeAction.runPromise;
53+
54+
t.deepEqual(runFinalizeStub.firstCall.args[1], "--threads=-1");
55+
t.deepEqual(runFinalizeStub.firstCall.args[2], "--ram=3012");
56+
t.deepEqual(runQueriesStub.firstCall.args[3], "--threads=-1");
57+
t.deepEqual(runQueriesStub.firstCall.args[1], "--ram=3012");
58+
});
59+
});

‎src/analyze-action.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface FinishStatusReport
3131
extends actionsUtil.StatusReportBase,
3232
AnalysisStatusReport {}
3333

34-
async function sendStatusReport(
34+
export async function sendStatusReport(
3535
startedAt: Date,
3636
stats: AnalysisStatusReport | undefined,
3737
error?: Error
@@ -91,10 +91,12 @@ async function run() {
9191
};
9292
const outputDir = actionsUtil.getRequiredInput("output");
9393
const threads = util.getThreadsFlag(
94-
actionsUtil.getOptionalInput("threads"),
94+
actionsUtil.getOptionalInput("threads") || process.env["CODEQL_THREADS"],
9595
logger
9696
);
97-
const memory = util.getMemoryFlag(actionsUtil.getOptionalInput("ram"));
97+
const memory = util.getMemoryFlag(
98+
actionsUtil.getOptionalInput("ram") || process.env["CODEQL_RAM"]
99+
);
98100
await runFinalize(outputDir, threads, memory, config, logger);
99101
if (actionsUtil.getRequiredInput("skip-queries") !== "true") {
100102
runStats = await runQueries(
@@ -188,9 +190,11 @@ async function run() {
188190
}
189191
}
190192

193+
export const runPromise = run();
194+
191195
async function runWrapper() {
192196
try {
193-
await run();
197+
await runPromise;
194198
} catch (error) {
195199
core.setFailed(`analyze action failed: ${error}`);
196200
console.log(error);

‎src/init-action.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import {
3232
getGitHubVersion,
3333
codeQlVersionAbove,
3434
enrichEnvironment,
35+
getMemoryFlagValue,
36+
getThreadsFlagValue,
3537
} from "./util";
3638

3739
// eslint-disable-next-line import/no-commonjs
@@ -204,9 +206,20 @@ async function run() {
204206
);
205207
}
206208

207-
// Setup CODEQL_RAM flag (todo improve this https://github.com/github/dsp-code-scanning/issues/935)
208-
const codeqlRam = process.env["CODEQL_RAM"] || "6500";
209-
core.exportVariable("CODEQL_RAM", codeqlRam);
209+
// Limit RAM and threads for extractors. When running extractors, the CodeQL CLI obeys the
210+
// CODEQL_RAM and CODEQL_THREADS environment variables to decide how much RAM and how many
211+
// threads it would ask extractors to use. See help text for the "--ram" and "--threads"
212+
// options at https://codeql.github.com/docs/codeql-cli/manual/database-trace-command/
213+
// for details.
214+
core.exportVariable(
215+
"CODEQL_RAM",
216+
process.env["CODEQL_RAM"] ||
217+
getMemoryFlagValue(getOptionalInput("ram")).toString()
218+
);
219+
core.exportVariable(
220+
"CODEQL_THREADS",
221+
getThreadsFlagValue(getOptionalInput("threads"), logger).toString()
222+
);
210223

211224
const sourceRoot = path.resolve(
212225
getRequiredEnvParam("GITHUB_WORKSPACE"),

‎src/runner.ts

+52-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
Mode,
2626
codeQlVersionAbove,
2727
enrichEnvironment,
28+
getMemoryFlagValue,
29+
getThreadsFlagValue,
2830
} from "./util";
2931

3032
// eslint-disable-next-line import/no-commonjs
@@ -53,11 +55,15 @@ function getToolsDir(userInput: string | undefined): string {
5355

5456
const codeqlEnvJsonFilename = "codeql-env.json";
5557

58+
function loadTracerEnvironment(config: Config): { [name: string]: string } {
59+
const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
60+
return JSON.parse(fs.readFileSync(jsonEnvFile).toString("utf-8"));
61+
}
62+
5663
// Imports the environment from codeqlEnvJsonFilename if not already present
5764
function importTracerEnvironment(config: Config) {
5865
if (!("ODASA_TRACER_CONFIGURATION" in process.env)) {
59-
const jsonEnvFile = path.join(config.tempDir, codeqlEnvJsonFilename);
60-
const env = JSON.parse(fs.readFileSync(jsonEnvFile).toString("utf-8"));
66+
const env = loadTracerEnvironment(config);
6167
for (const key of Object.keys(env)) {
6268
process.env[key] = env[key];
6369
}
@@ -109,6 +115,8 @@ interface InitArgs {
109115
githubAuth: string;
110116
githubAuthStdin: boolean;
111117
debug: boolean;
118+
ram: string | undefined;
119+
threads: string | undefined;
112120
}
113121

114122
program
@@ -167,6 +175,18 @@ program
167175
"--trace-process-level <number>",
168176
"(Advanced, windows-only) Inject a windows tracer of this process into a parent process <number> levels up."
169177
)
178+
.option(
179+
"--ram <number>",
180+
"The amount of memory in MB that can be used by CodeQL extractors. " +
181+
"By default, CodeQL extractors will use most of the memory available in the system. " +
182+
'This input also sets the amount of memory that can later be used by the "analyze" command.'
183+
)
184+
.option(
185+
"--threads <number>",
186+
"The number of threads that can be used by CodeQL extractors. " +
187+
"By default, CodeQL extractors will use all the hardware threads available in the system. " +
188+
'This input also sets the number of threads that can later be used by the "analyze" command.'
189+
)
170190
.action(async (cmd: InitArgs) => {
171191
const logger = getRunnerLogger(cmd.debug);
172192

@@ -195,6 +215,17 @@ program
195215
const gitHubVersion = await getGitHubVersion(apiDetails);
196216
checkGitHubVersionInRange(gitHubVersion, logger, Mode.runner);
197217

218+
// Limit RAM and threads for extractors. When running extractors, the CodeQL CLI obeys the
219+
// CODEQL_RAM and CODEQL_THREADS environment variables to decide how much RAM and how many
220+
// threads it would ask extractors to use. See help text for the "--ram" and "--threads"
221+
// options at https://codeql.github.com/docs/codeql-cli/manual/database-trace-command/
222+
// for details.
223+
process.env["CODEQL_RAM"] = getMemoryFlagValue(cmd.ram).toString();
224+
process.env["CODEQL_THREADS"] = getThreadsFlagValue(
225+
cmd.threads,
226+
logger
227+
).toString();
228+
198229
let codeql: CodeQL;
199230
if (cmd.codeqlPath !== undefined) {
200231
codeql = await getCodeQL(cmd.codeqlPath);
@@ -402,16 +433,21 @@ program
402433
)
403434
.option(
404435
"--ram <ram>",
405-
"Amount of memory to use when running queries. Default is to use all available memory."
436+
"The amount of memory in MB that can be used by CodeQL for database finalization and query execution. " +
437+
'By default, this command will use the same amount of memory as previously set in the "init" command. ' +
438+
'If the "init" command also does not have an explicit "ram" flag, this command will use most of the ' +
439+
"memory available in the system."
406440
)
407441
.option(
408442
"--no-add-snippets",
409443
"Specify whether to include code snippets in the sarif output."
410444
)
411445
.option(
412446
"--threads <threads>",
413-
"Number of threads to use when running queries. " +
414-
"Default is to use all available cores."
447+
"The number of threads that can be used by CodeQL for database finalization and query execution. " +
448+
'By default, this command will use the same number of threads as previously set in the "init" command. ' +
449+
'If the "init" command also does not have an explicit "threads" flag, this command will use all the ' +
450+
"hardware threads available in the system."
415451
)
416452
.option(
417453
"--temp-dir <dir>",
@@ -447,8 +483,17 @@ program
447483

448484
const outputDir =
449485
cmd.outputDir || path.join(config.tempDir, "codeql-sarif");
450-
const threads = getThreadsFlag(cmd.threads, logger);
451-
const memory = getMemoryFlag(cmd.ram);
486+
let initEnv: { [name: string]: string } = {};
487+
try {
488+
initEnv = loadTracerEnvironment(config);
489+
} catch (err) {
490+
// The init command did not generate a tracer environment file
491+
}
492+
const threads = getThreadsFlag(
493+
cmd.threads || initEnv["CODEQL_THREADS"],
494+
logger
495+
);
496+
const memory = getMemoryFlag(cmd.ram || initEnv["CODEQL_RAM"]);
452497
await runFinalize(outputDir, threads, memory, config, logger);
453498
await runQueries(
454499
outputDir,

‎src/util.ts

+38-12
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ function getSystemReservedMemoryMegaBytes(): number {
8585
}
8686

8787
/**
88-
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
89-
* specified, the total available memory will be used minus a threshold
90-
* reserved for the OS.
88+
* Get the value of the codeql `--ram` flag as configured by the `ram` input.
89+
* If no value was specified, the total available memory will be used minus a
90+
* threshold reserved for the OS.
9191
*
92-
* @returns string
92+
* @returns {number} the amount of RAM to use, in megabytes
9393
*/
94-
export function getMemoryFlag(userInput: string | undefined): string {
94+
export function getMemoryFlagValue(userInput: string | undefined): number {
9595
let memoryToUseMegaBytes: number;
9696
if (userInput) {
9797
memoryToUseMegaBytes = Number(userInput);
@@ -104,7 +104,18 @@ export function getMemoryFlag(userInput: string | undefined): string {
104104
const reservedMemoryMegaBytes = getSystemReservedMemoryMegaBytes();
105105
memoryToUseMegaBytes = totalMemoryMegaBytes - reservedMemoryMegaBytes;
106106
}
107-
return `--ram=${Math.floor(memoryToUseMegaBytes)}`;
107+
return Math.floor(memoryToUseMegaBytes);
108+
}
109+
110+
/**
111+
* Get the codeql `--ram` flag as configured by the `ram` input. If no value was
112+
* specified, the total available memory will be used minus a threshold
113+
* reserved for the OS.
114+
*
115+
* @returns string
116+
*/
117+
export function getMemoryFlag(userInput: string | undefined): string {
118+
return `--ram=${getMemoryFlagValue(userInput)}`;
108119
}
109120

110121
/**
@@ -123,17 +134,17 @@ export function getAddSnippetsFlag(
123134
}
124135

125136
/**
126-
* Get the codeql `--threads` value specified for the `threads` input.
127-
* If no value was specified, all available threads will be used.
137+
* Get the value of the codeql `--threads` flag specified for the `threads`
138+
* input. If no value was specified, all available threads will be used.
128139
*
129140
* The value will be capped to the number of available CPUs.
130141
*
131-
* @returns string
142+
* @returns {number}
132143
*/
133-
export function getThreadsFlag(
144+
export function getThreadsFlagValue(
134145
userInput: string | undefined,
135146
logger: Logger
136-
): string {
147+
): number {
137148
let numThreads: number;
138149
const maxThreads = os.cpus().length;
139150
if (userInput) {
@@ -158,7 +169,22 @@ export function getThreadsFlag(
158169
// Default to using all threads
159170
numThreads = maxThreads;
160171
}
161-
return `--threads=${numThreads}`;
172+
return numThreads;
173+
}
174+
175+
/**
176+
* Get the codeql `--threads` flag specified for the `threads` input.
177+
* If no value was specified, all available threads will be used.
178+
*
179+
* The value will be capped to the number of available CPUs.
180+
*
181+
* @returns string
182+
*/
183+
export function getThreadsFlag(
184+
userInput: string | undefined,
185+
logger: Logger
186+
): string {
187+
return `--threads=${getThreadsFlagValue(userInput, logger)}`;
162188
}
163189

164190
/**

0 commit comments

Comments
 (0)
Please sign in to comment.