Skip to content

Commit

Permalink
More flexibly test affinity setting
Browse files Browse the repository at this point in the history
When running on a machine with `cpusets` applied, we are unable to
assign CPU affinity to CPUs 1 and 2; we may be locked to CPUs 9-16, for
example.  So we must inspect what our current cpumask is, and from that
select CPUs that we can safely assign affinity to in our tests.
  • Loading branch information
staticfloat authored and DilumAluthge committed Mar 19, 2022
1 parent 1b686b2 commit 7e1c253
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,21 @@ end
# hang in FreeBSD. So, it's tested only in Linux and Windows:
const AFFINITY_SUPPORTED = (Sys.islinux() || Sys.iswindows()) && !running_under_rr()

# If we're running inside of a cpuset, we need to test affinity with
# valid cpu core choices
function get_cpumask()
self = ccall(:uv_thread_self, Ptr{Cvoid}, ())
masksize = ccall(:uv_cpumask_size, Cint, ())
mask = zeros(UInt8, masksize)
ccall(:uv_thread_getaffinity, Cint, (Ptr{Ptr{Cvoid}}, Ptr{UInt8}, Cint), Ref(self), mask, masksize)
return mask
end

if AFFINITY_SUPPORTED
if Sys.CPU_THREADS > 1
@test run_with_affinity([2]) == "2"
@test run_with_affinity([1, 2]) == "1,2"
allowed_cpus = findall(get_cpumask() .!= 0)
if length(allowed_cpus) 2
@test run_with_affinity(allowed_cpus[1:1]) == "$(allowed_cpus[1])"
@test run_with_affinity(allowed_cpus[1:2]) == "$(allowed_cpus[1]),$(allowed_cpus[2])"
end
end

Expand All @@ -113,18 +124,19 @@ function get_nthreads(options = ``; cpus = nothing)
end

@testset "nthreads determined based on CPU affinity" begin
if AFFINITY_SUPPORTED && Sys.CPU_THREADS 2
allowed_cpus = findall(get_cpumask() .!= 0)
if AFFINITY_SUPPORTED && length(allowed_cpus) 2
@test get_nthreads() 2
@test get_nthreads(cpus = [1]) == 1
@test get_nthreads(cpus = [2]) == 1
@test get_nthreads(cpus = [1, 2]) == 2
@test get_nthreads(`-t1`, cpus = [1]) == 1
@test get_nthreads(`-t1`, cpus = [2]) == 1
@test get_nthreads(`-t1`, cpus = [1, 2]) == 1
@test get_nthreads(cpus = allowed_cpus[1:1]) == 1
@test get_nthreads(cpus = allowed_cpus[2:2]) == 1
@test get_nthreads(cpus = allowed_cpus[1:2]) == 2
@test get_nthreads(`-t1`, cpus = allowed_cpus[1:1]) == 1
@test get_nthreads(`-t1`, cpus = allowed_cpus[2:2]) == 1
@test get_nthreads(`-t1`, cpus = allowed_cpus[1:2]) == 1

if Sys.CPU_THREADS 3
@test get_nthreads(cpus = [1, 3]) == 2
@test get_nthreads(cpus = [2, 3]) == 2
if length(allowed_cpus) 3
@test get_nthreads(cpus = allowed_cpus[1:2:3]) == 2
@test get_nthreads(cpus = allowed_cpus[2:3]) == 2
end
end
end
Expand Down

0 comments on commit 7e1c253

Please sign in to comment.