|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +// C-style type names follow, opt out of warnings for using names from headers. |
| 6 | +#![allow(non_camel_case_types)] |
| 7 | + |
| 8 | +//! Utility functions for binding LWPs to specific CPUs. |
| 9 | +//! |
| 10 | +//! This is generally a very light wrapper for illumos' `sysconf(3c)` and |
| 11 | +//! `processor_bind(2)`, plus a few constants out of related headers. |
| 12 | +
|
| 13 | +use std::io::Error; |
| 14 | + |
| 15 | +// From `<sys/types.h>` |
| 16 | +pub type id_t = i32; |
| 17 | + |
| 18 | +// From `<sys/processor.h>` |
| 19 | +pub type processorid_t = i32; |
| 20 | + |
| 21 | +// From `<sys/procset.h>` |
| 22 | +pub type idtype_t = i32; |
| 23 | + |
| 24 | +/// The enum values `idtype_t` can be. This is separate to be more explicit that |
| 25 | +/// idtype_t is the ABI type, but is `repr(i32)` to make casting to `idtype_t` |
| 26 | +/// trivial. |
| 27 | +#[allow(non_camel_case_types)] |
| 28 | +#[repr(i32)] |
| 29 | +pub enum IdType { |
| 30 | + P_PID, |
| 31 | + P_PPID, |
| 32 | + P_PGID, |
| 33 | + P_SID, |
| 34 | + P_CID, |
| 35 | + P_UID, |
| 36 | + P_GID, |
| 37 | + P_ALL, |
| 38 | + P_LWPID, |
| 39 | + P_TASKID, |
| 40 | + P_PROJID, |
| 41 | + P_POOLID, |
| 42 | + P_ZONEID, |
| 43 | + P_CTID, |
| 44 | + P_CPUID, |
| 45 | + P_PSETID, |
| 46 | +} |
| 47 | + |
| 48 | +// Returns an `i32` to match `processorid_t`, so that `0..online_cpus()` |
| 49 | +// produces a range of processor IDs without additional translation needed. |
| 50 | +pub fn online_cpus() -> Result<i32, Error> { |
| 51 | + let res = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) }; |
| 52 | + |
| 53 | + if res == -1 { |
| 54 | + return Err(Error::last_os_error()); |
| 55 | + } |
| 56 | + |
| 57 | + res.try_into().map_err(|_| { |
| 58 | + // sysconf() reports more than 2^31 processors?! |
| 59 | + Error::other(format!("too many processors: {}", res)) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(target_os = "illumos")] |
| 64 | +/// Bind the current LWP to the specified processor. |
| 65 | +pub fn bind_lwp(bind_cpu: processorid_t) -> Result<(), Error> { |
| 66 | + extern "C" { |
| 67 | + fn processor_bind( |
| 68 | + idtype: idtype_t, |
| 69 | + id: id_t, |
| 70 | + processorid: processorid_t, |
| 71 | + obind: *mut processorid_t, |
| 72 | + ) -> i32; |
| 73 | + } |
| 74 | + |
| 75 | + // From `<sys/types.h>`. |
| 76 | + const P_MYID: id_t = -1; |
| 77 | + |
| 78 | + let res = unsafe { |
| 79 | + processor_bind( |
| 80 | + IdType::P_LWPID as i32, |
| 81 | + P_MYID, |
| 82 | + bind_cpu, |
| 83 | + std::ptr::null_mut(), |
| 84 | + ) |
| 85 | + }; |
| 86 | + |
| 87 | + if res != 0 { |
| 88 | + return Err(Error::last_os_error()); |
| 89 | + } |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(not(target_os = "illumos"))] |
| 95 | +/// On non-illumos targets, we're not actually running a VM. We do need the |
| 96 | +/// crate to compile to be nicer for blanket `cargo test` invocations on other |
| 97 | +/// platforms. So a no-op function will do. |
| 98 | +pub fn bind_lwp(_bind_cpu: processorid_t) -> Result<(), Error> { |
| 99 | + Ok(()) |
| 100 | +} |
0 commit comments