-
Notifications
You must be signed in to change notification settings - Fork 29
bins: intial CPU binding support #991
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
Open
iximeow
wants to merge
3
commits into
oxidecomputer:master
Choose a base branch
from
iximeow:cpu-binding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "pbind" | ||
| version = "0.0.0" | ||
| license = "MPL-2.0" | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| libc.workspace = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| // C-style type names follow, opt out of warnings for using names from headers. | ||
| #![allow(non_camel_case_types)] | ||
|
|
||
| //! Utility functions for binding LWPs to specific CPUs. | ||
| //! | ||
| //! This is generally a very light wrapper for illumos' `sysconf(3c)` and | ||
| //! `processor_bind(2)`, plus a few constants out of related headers. | ||
|
|
||
| use std::io::Error; | ||
|
|
||
| // From `<sys/types.h>` | ||
| pub type id_t = i32; | ||
|
|
||
| // From `<sys/processor.h>` | ||
| pub type processorid_t = i32; | ||
|
|
||
| // From `<sys/procset.h>` | ||
| pub type idtype_t = i32; | ||
|
|
||
| /// The enum values `idtype_t` can be. This is separate to be more explicit that | ||
| /// idtype_t is the ABI type, but is `repr(i32)` to make casting to `idtype_t` | ||
| /// trivial. | ||
| #[allow(non_camel_case_types)] | ||
| #[repr(i32)] | ||
| pub enum IdType { | ||
| P_PID, | ||
| P_PPID, | ||
| P_PGID, | ||
| P_SID, | ||
| P_CID, | ||
| P_UID, | ||
| P_GID, | ||
| P_ALL, | ||
| P_LWPID, | ||
| P_TASKID, | ||
| P_PROJID, | ||
| P_POOLID, | ||
| P_ZONEID, | ||
| P_CTID, | ||
| P_CPUID, | ||
| P_PSETID, | ||
| } | ||
|
|
||
| /// Returns an `i32` to match `processorid_t`, so that `0..online_cpus()` | ||
| /// produces a range of processor IDs without additional translation needed. | ||
| /// | ||
| /// This is really just a wrapper for `sysconf(_SC_NPROCESSORS_ONLN)`. | ||
| pub fn online_cpus() -> Result<i32, Error> { | ||
| let res = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) }; | ||
|
|
||
| if res == -1 { | ||
| return Err(Error::last_os_error()); | ||
| } | ||
|
|
||
| res.try_into().map_err(|_| { | ||
| // sysconf() reports more than 2^31 processors?! | ||
| Error::other(format!("too many processors: {res}")) | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(target_os = "illumos")] | ||
| /// Bind the current LWP to the specified processor. | ||
| pub fn bind_lwp(bind_cpu: processorid_t) -> Result<(), Error> { | ||
| extern "C" { | ||
| fn processor_bind( | ||
| idtype: idtype_t, | ||
| id: id_t, | ||
| processorid: processorid_t, | ||
| obind: *mut processorid_t, | ||
| ) -> i32; | ||
| } | ||
|
|
||
| // From `<sys/types.h>`. | ||
| const P_MYID: id_t = -1; | ||
|
|
||
| let res = unsafe { | ||
| processor_bind( | ||
| IdType::P_LWPID as i32, | ||
| P_MYID, | ||
| bind_cpu, | ||
| std::ptr::null_mut(), | ||
| ) | ||
| }; | ||
|
|
||
| if res != 0 { | ||
| return Err(Error::last_os_error()); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "illumos"))] | ||
| /// On non-illumos targets, we're not actually running a VM. We do need the | ||
| /// crate to compile to be nicer for blanket `cargo test` invocations on other | ||
| /// platforms. So a no-op function will do. | ||
| pub fn bind_lwp(_bind_cpu: processorid_t) -> Result<(), Error> { | ||
| Ok(()) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hm, since this is a low-level binding to the C headers, should this perhaps be
pbind-sys?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 was on the fence tbh, since this includes a bit more than just definitions from the headers :( idk
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.
Yeah, iunno.