|
93 | 93 | end
|
94 | 94 | # Note also that libuv does not support affinity in macOS and it is known to
|
95 | 95 | # hang in FreeBSD. So, it's tested only in Linux and Windows:
|
96 |
| -if Sys.islinux() || Sys.iswindows() |
97 |
| - if Sys.CPU_THREADS > 1 && !running_under_rr() |
| 96 | +const AFFINITY_SUPPORTED = (Sys.islinux() || Sys.iswindows()) && !running_under_rr() |
| 97 | + |
| 98 | +if AFFINITY_SUPPORTED |
| 99 | + if Sys.CPU_THREADS > 1 |
98 | 100 | @test run_with_affinity([2]) == "2"
|
99 | 101 | @test run_with_affinity([1, 2]) == "1,2"
|
100 | 102 | end
|
101 | 103 | end
|
102 | 104 |
|
| 105 | +function get_nthreads(options = ``; cpus = nothing) |
| 106 | + cmd = `$(Base.julia_cmd()) --startup-file=no $(options)` |
| 107 | + cmd = `$cmd -e "print(Threads.nthreads())"` |
| 108 | + cmd = addenv(cmd, "JULIA_EXCLUSIVE" => "0", "JULIA_NUM_THREADS" => "auto") |
| 109 | + if cpus !== nothing |
| 110 | + cmd = setcpuaffinity(cmd, cpus) |
| 111 | + end |
| 112 | + return parse(Int, read(cmd, String)) |
| 113 | +end |
| 114 | + |
| 115 | +@testset "nthreads determined based on CPU affinity" begin |
| 116 | + if AFFINITY_SUPPORTED && Sys.CPU_THREADS ≥ 2 |
| 117 | + @test get_nthreads() ≥ 2 |
| 118 | + @test get_nthreads(cpus = [1]) == 1 |
| 119 | + @test get_nthreads(cpus = [2]) == 1 |
| 120 | + @test get_nthreads(cpus = [1, 2]) == 2 |
| 121 | + @test get_nthreads(`-t1`, cpus = [1]) == 1 |
| 122 | + @test get_nthreads(`-t1`, cpus = [2]) == 1 |
| 123 | + @test get_nthreads(`-t1`, cpus = [1, 2]) == 1 |
| 124 | + |
| 125 | + if Sys.CPU_THREADS ≥ 3 |
| 126 | + @test get_nthreads(cpus = [1, 3]) == 2 |
| 127 | + @test get_nthreads(cpus = [2, 3]) == 2 |
| 128 | + end |
| 129 | + end |
| 130 | +end |
| 131 | + |
103 | 132 | # issue #34769
|
104 | 133 | function idle_callback(handle)
|
105 | 134 | idle = @Base.handle_as handle UvTestIdle
|
|
0 commit comments