Skip to content

Conversation

@stemann
Copy link
Contributor

@stemann stemann commented Jan 25, 2025

Changed Pkg.test to prioritise julia_args --threads over current process threads

Ensures that tests run with threads, both when calling julia --project --threads=auto --eval 'using Pkg; Pkg.test()', and when calling julia --project --eval 'using Pkg; Pkg.test(; julia_args = ["--threads=auto"])'.

Similar to #4140

…ess threads

Ensures that tests run with threads, both when calling `julia --project --threads=auto --eval 'using Pkg; Pkg.test()'`, and when calling `julia --project --eval 'using Pkg; Pkg.test(; julia_args = ["--threads=auto"])'`.
@IanButterworth
Copy link
Member

Makes sense. Can you add a test

@stemann
Copy link
Contributor Author

stemann commented Jan 26, 2025

Not completely sure how to properly add this test, but what I am aiming for is something similar to running the following for the added TestThreads test package:

EXPECTED_NTHREADS=1 julia +nightly --project --eval 'using Pkg; Pkg.test()'

EXPECTED_NTHREADS=2 julia +nightly --project --threads=2 --eval 'using Pkg; Pkg.test()'

EXPECTED_NTHREADS=2 julia +nightly --project --eval 'using Pkg; Pkg.test(; julia_args=["--threads=2"])'

EXPECTED_NTHREADS=1 julia +nightly --project --threads=2 --eval 'using Pkg; Pkg.test(; julia_args=["--threads=1"])'

Edit: This is the behaviour on Julia 1.6, and 1.10.

@stemann
Copy link
Contributor Author

stemann commented Jan 26, 2025

I guess, one could also add:

EXPECTED_NTHREADS=2 JULIA_NUM_THREADS=2 julia +nightly --project --eval 'using Pkg; Pkg.test()'

EXPECTED_NTHREADS=2 julia +nightly --project --eval 'using Pkg; Pkg.test(; julia_args=Cmd(``; env=Dict("JULIA_NUM_THREADS" => 2)))'

EXPECTED_NTHREADS=1 JULIA_NUM_THREADS=2 julia +nightly --project --eval 'using Pkg; Pkg.test(; julia_args=Cmd(``; env=Dict("JULIA_NUM_THREADS" => 1)))'

@stemann
Copy link
Contributor Author

stemann commented Jan 30, 2025

Any suggestions for how to implement the tests?

@IanButterworth
Copy link
Member

Take a look at existing test packages and add one that can be called by Pkg.test in tests.

@IanButterworth
Copy link
Member

Just a passing comment that this should probably account for and test both threadpools

@stemann
Copy link
Contributor Author

stemann commented Feb 1, 2025

Did something change post-Julia-1.11, that remedied the issue with passing --threads=auto to the precompile process, as part of test? I.e., making the change in #4140 irrelevant for Julia 1.12+, cf. JuliaLang/julia#53572 (comment).

flush(ctx.io)
code = gen_test_code(source_path; test_args)
cmd = `$(Base.julia_cmd()) $(flags) --eval $code`
cmd = Cmd(`$(Base.julia_cmd()) $(flags) --eval $code`; env = julia_args.env)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other parts of the julia_args Cmd?

Is it safe to ignore julia_args.cpus, julia_args.dir, julia_args.flags (!= flags), and julia_args.ignorestatus?

@stemann stemann force-pushed the feature/test_julia_args_threads branch from ad66721 to 304fffd Compare February 1, 2025 19:19
@stemann
Copy link
Contributor Author

stemann commented Feb 18, 2025

Is this passable?

@stemann
Copy link
Contributor Author

stemann commented Feb 23, 2025

@IanButterworth @KristofferC Bump

Comment on lines 2322 to 2324
cmd = `$(Base.julia_cmd()) $(flags) --threads=$(get_threads_spec()) --eval $code`
julia_args_defines_threads = any(startswith("--threads"), julia_args.exec) || (!isnothing(julia_args.env) && any(startswith("JULIA_NUM_THREADS"), julia_args.env))
threads_arg = julia_args_defines_threads ? `` : `--threads=$(get_threads_spec())`
cmd = Cmd(`$(Base.julia_cmd()) $threads_arg $(flags) --eval $code`; env = julia_args.env)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a lot more change than is needed.
Don't we just need to move --threads to before $flags?

So

cmd = `$(Base.julia_cmd()) --threads=$(get_threads_spec()) $(flags)  --eval $code`

i.e.

% ./julia -t2 -t3 -E "Threads.nthreads(:default)"
3

Then the testing can be done with a normal

withenv(...) do
     Pkg.test(....)
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe I needed to also add support for passing through the environment variables to the test process in order to implement the tests...

... it could possibly be split into two PRs...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the last commit ("Added @test try catch end to count Pkg.test invocations as tests) could be reverted...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

… but yeah, the withenv approach might be sufficient…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 7aa3a80, without the modification of the environment passed to the test process via julia_args, the tests attempting to set threads via JULIA_NUM_THREADS fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cherry picked :-)

@IanButterworth IanButterworth merged commit f517489 into JuliaLang:master Feb 25, 2025
8 of 9 checks passed
stemann added a commit to stemann/Pkg.jl that referenced this pull request Feb 25, 2025
…ess threads (JuliaLang#4141)

Co-authored-by: Ian Butterworth <[email protected]>

(cherry picked from commit f517489)
KristofferC pushed a commit that referenced this pull request Mar 31, 2025
…ess threads (#4141)

Co-authored-by: Ian Butterworth <[email protected]>
(cherry picked from commit f517489)
@KristofferC KristofferC mentioned this pull request Mar 31, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants