Skip to content
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

Make std::thread::available_concurrency support process-limited number of CPUs #89310

Merged
merged 2 commits into from
Nov 7, 2021

Conversation

joshtriplett
Copy link
Member

@joshtriplett joshtriplett commented Sep 27, 2021

Use libc::sched_getaffinity and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via taskset or similar.

This also covers cgroup cpusets.

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 27, 2021
@joshtriplett joshtriplett changed the title Make std::thread::available_concurrency support process-limited number of CPUs Make std::thread::available_concurrency support process-limited number of CPUs Sep 27, 2021
@rust-log-analyzer

This comment has been minimized.

@joshtriplett joshtriplett added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 28, 2021
Copy link
Member

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

Compared this against num_cpus/linux.rs, and this looks good!

@m-ou-se m-ou-se added the O-linux Operating system: Linux label Oct 3, 2021
@yoshuawuyts
Copy link
Member

I believe this PR should be ready to be merged?

@m-ou-se
Copy link
Member

m-ou-se commented Oct 15, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Oct 15, 2021

📌 Commit 433439e has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 15, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2021
…affinity, r=m-ou-se

Make `std::thread::available_concurrency` support process-limited number of CPUs

Use `libc::sched_getaffinity` and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via `taskset` or similar.

This also covers cgroup cpusets.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2021
…affinity, r=m-ou-se

Make `std::thread::available_concurrency` support process-limited number of CPUs

Use `libc::sched_getaffinity` and count the number of CPUs in the returned mask. This handles cases where the process doesn't have access to all CPUs, such as when limited via `taskset` or similar.

This also covers cgroup cpusets.
@jackh726
Copy link
Member

Failed in #89928

@bors r- rollup=iffy

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 16, 2021
@joshtriplett
Copy link
Member Author

Fixed the missing macros on Android in rust-lang/libc#2470 . Once that goes in, I'll do a libc release and update.

@joshtriplett
Copy link
Member Author

@bors r=m-ou-se

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 24, 2021
@rust-log-analyzer

This comment has been minimized.

…r of CPUs

Use libc::sched_getaffinity and count the number of CPUs in the returned
mask. This handles cases where the process doesn't have access to all
CPUs, such as when limited via taskset or similar.
@joshtriplett
Copy link
Member Author

Updated to libc 0.2.106 which includes the fix for the issue that broke CI on macOS.

@bors r=m-ou-se

@bors
Copy link
Contributor

bors commented Oct 30, 2021

📌 Commit 7c9611d has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 30, 2021
@bors
Copy link
Contributor

bors commented Oct 31, 2021

⌛ Testing commit 7c9611d with merge bd73825db1627e06ead2c3e17513f3aaa9b2c46d...

@rust-log-analyzer
Copy link
Collaborator

The job dist-x86_64-freebsd failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
[RUSTC-TIMING] core test:false 23.404
[RUSTC-TIMING] addr2line test:false 0.502
[RUSTC-TIMING] gimli test:false 5.216
[RUSTC-TIMING] object test:false 5.303
error: linking with `x86_64-unknown-freebsd11-clang` failed: exit status: 1
  |
  = note: "x86_64-unknown-freebsd11-clang" "-Wl,--version-script=/tmp/rustcC2gN1Z/list" "-m64" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/std-1ae97444a67f7a6a.std.b4776720-cgu.0.rcgu.o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/std-1ae97444a67f7a6a.5atogxoi6xx0faz7.rcgu.o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/std-1ae97444a67f7a6a.124kwnzhr2vpbg6o.rcgu.o" "-Wl,--as-needed" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/release/deps" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/build/compiler_builtins-4112ecccb4c4b963/out" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/build/profiler_builtins-d3e3ed361039c169/out" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-freebsd/lib" "-lexecinfo" "-lpthread" "-Wl,-Bstatic" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libpanic_unwind-10f6ade17d2c8cd7.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libminiz_oxide-a23b55dac10cfe0c.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libadler-6c2bc1eea1d9ab98.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libobject-8c3214801652da69.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libmemchr-769d6ab78fa1e74f.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libaddr2line-4cf62080a5eb7c5c.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libgimli-d00b1d976307cbc3.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libstd_detect-acefed2b1ae207e4.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/librustc_demangle-51697ed9df91b6f7.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libhashbrown-35dd819092c68ace.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/librustc_std_workspace_alloc-3b3d40e2b0883322.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libunwind-75c498b6d20774ec.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libcfg_if-ce2863d1cef84933.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/liblibc-74dd87227f02f43f.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/liballoc-bbf2299c4789bf29.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/librustc_std_workspace_core-174ebf435975ffef.rlib" "-Wl,--no-whole-archive" "-Wl,--whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libcore-ce9110d7ae9a5977.rlib" "-Wl,--no-whole-archive" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libcompiler_builtins-69eab8a62240c916.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lrt" "-lutil" "-lexecinfo" "-lkvm" "-lutil" "-lprocstat" "-lrt" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-freebsd/lib" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage1-std/x86_64-unknown-freebsd/release/deps/libstd-1ae97444a67f7a6a.so" "-shared" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-Wl,-rpath,$ORIGIN/../lib"
  = note: /usr/local/x86_64-unknown-freebsd11/bin/ld: cannot find -lkvm
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

[RUSTC-TIMING] std test:false 25.538
error: could not compile `std` due to previous error
Build completed unsuccessfully in 0:07:51

@bors
Copy link
Contributor

bors commented Oct 31, 2021

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 31, 2021
@joshtriplett
Copy link
Member Author

Let's try again now that #90495 is fixed:

@bors r=m-ou-se

@bors
Copy link
Contributor

bors commented Nov 7, 2021

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Nov 7, 2021

📌 Commit 7c9611d has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2021
@joshtriplett
Copy link
Member Author

@bors retry

@bors
Copy link
Contributor

bors commented Nov 7, 2021

⌛ Testing commit 7c9611d with merge fecfc0e...

@bors
Copy link
Contributor

bors commented Nov 7, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing fecfc0e to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 7, 2021
@bors bors merged commit fecfc0e into rust-lang:master Nov 7, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 7, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fecfc0e): comparison url.

Summary: This benchmark run did not return any relevant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

@joshtriplett joshtriplett deleted the available-concurrency-affinity branch November 8, 2021 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-linux Operating system: Linux S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants