-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add setcpuaffinity(cmd, cpus)
for setting CPU affinity of subprocesses
#42469
Conversation
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
setcpus(cmd, cpus)
for setting CPU affinity of subprocessessetcpuaffinity(cmd, cpus)
for setting CPU affinity of subprocesses
julia> run(setcpuaffinity(`sh -c 'taskset -p \$\$'`, [1, 2, 5])); | ||
pid 2273's current affinity mask: 13 | ||
|
||
julia> 0b010011 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic required to go from the first command to understand the point of the second feels a bit large here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, good point. I added a bit more explanations in the latest commit.
test/show.jl
Outdated
@testset "Cmd" begin | ||
@test sprint(show, `true`) == "`true`" | ||
@test sprint(show, setenv(`true`, "A" => "B")) == """setenv(`true`,["A=B"])""" | ||
@test sprint(show, setcpuaffinity(`true`, [1, 2])) == "setcpuaffinity(`true`,[1, 2])" | ||
@test sprint(show, setenv(setcpuaffinity(`true`, [1, 2]), "A" => "B")) == | ||
"""setenv(setcpuaffinity(`true`,[1, 2]),["A=B"])""" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find where show(::IO, ::Cmd)
was tested. It looks like the original commit dee7f6c didn't add the test for show
with setenv
. So, I just threw this in for completeness.
setcpuaffinity(cmd, cpus)
for setting CPU affinity of subprocessessetcpuaffinity(cmd, cpus)
for setting CPU affinity of subprocesses
@vtjnash Can you revisit the above coment? Is it good to go? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions
base/cmd.jl
Outdated
setcpuaffinity(cmd::Cmd, ::Nothing) = Cmd(cmd; cpus = nothing) | ||
setcpuaffinity(cmd::Cmd, cpus) = Cmd(cmd; cpus = collect(Int, cpus)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setcpuaffinity(cmd::Cmd, ::Nothing) = Cmd(cmd; cpus = nothing) | |
setcpuaffinity(cmd::Cmd, cpus) = Cmd(cmd; cpus = collect(Int, cpus)) | |
setcpuaffinity(cmd::Cmd, ::Nothing) = Cmd(cmd; cpus = nothing) | |
setcpuaffinity(cmd::Cmd, cpus::AbstractVector{Bool}) = Cmd(cmd; cpus = findall(cpus)) | |
setcpuaffinity(cmd::Cmd, cpus::AbstractVector) = Cmd(cmd; cpus = convert(Vector{UInt16}, cpus)) | |
setcpuaffinity(cmd::Cmd, cpus) = setcpuaffinity(cmd, collect(cpus)::AbstractVector) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm against dispatching on Bool
element type to change the meaning (in general). It will change the behavior between cpus = Bool[true]
and cpus = Integer[true]
(or Any[true]
). I prefer to add a distinct function like setcpuaffinitymask
or maybe a Mask
type for supporting this.
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
@vtjnash Can you check my comments above? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know what happened on FreeBSD? The CI logs make it seem like it hung in the precompile tests, before getting to testing the new code.
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
Co-authored-by: Jameson Nash <vtjnash@gmail.com>
I have no idea what's happening in FreeBSD but I think the CI was older than the recent fixes for FreeBSD. I merged the master branch to see if it shows something new. |
In win64 we are getting
(which is the same as #42469 (comment) but with a better error message). I guess this requires libuv/libuv#3357 too. |
Now that #43219 pulls in the fix libuv/libuv#3357, the test works in Windows CI. @vtjnash Good to go? |
…ses (JuliaLang#42469) Support running commands with cpumask.
…ses (JuliaLang#42469) Support running commands with cpumask.
While working on implementing tests for #42340, I thought it'd make sense to add a Julia API to spawn processes with an explicit CPU affinity since libuv already can do this. It may also be useful for process-based parallelism libraries like Distributed and Dagger.
So, this patch adds a new API:
Example:
We can now remove
taskset
from the test dependency.Side notes: The behavior of
cpus = nothing
withJULIA_EXCLUSIVE=1
is currently badly defined/implemented #42338. So, we'd need to be careful when documenting its behavior.What do you think? Is it an appropriate API for Julia
Base
?