From 9b5c8875fe7f8ca52d03842a27fe4c462978b282 Mon Sep 17 00:00:00 2001 From: Zhang Jingqiang Date: Sat, 8 Feb 2025 22:09:36 +0800 Subject: [PATCH] drop macOS cpu tag support --- Cargo.lock | 10 ---- Cargo.toml | 1 - g3keymess/src/opts.rs | 26 ++++++----- lib/g3-compat/Cargo.toml | 4 -- lib/g3-compat/src/sched/macos.rs | 74 ------------------------------ lib/g3-compat/src/sched/mod.rs | 1 - lib/g3-runtime/src/unaided/mod.rs | 12 ----- lib/g3-runtime/src/unaided/yaml.rs | 35 ++++++++------ lib/g3-yaml/src/value/mod.rs | 22 ++++++++- lib/g3-yaml/src/value/sched.rs | 16 ------- 10 files changed, 56 insertions(+), 145 deletions(-) delete mode 100644 lib/g3-compat/src/sched/macos.rs diff --git a/Cargo.lock b/Cargo.lock index cf56c62fb..e5e0195b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -971,7 +971,6 @@ name = "g3-compat" version = "0.1.0" dependencies = [ "libc", - "mach2", "rustix", "windows-sys 0.59.0", ] @@ -2576,15 +2575,6 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "227748d55f2f0ab4735d87fd623798cb6b664512fe979705f829c9f81c934465" -[[package]] -name = "mach2" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709" -dependencies = [ - "libc", -] - [[package]] name = "memchr" version = "2.7.4" diff --git a/Cargo.toml b/Cargo.toml index 2f3e599d7..5b275a0f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -170,7 +170,6 @@ capnpc = "0.20" libc = "0.2.169" rustix = { version = "0.38", default-features = false } windows-sys = "0.59" -mach2 = "0.4" # serde = "1.0" yaml-rust = { package = "yaml-rust2", version = "0.9" } diff --git a/g3keymess/src/opts.rs b/g3keymess/src/opts.rs index 5e9252c95..2a994cad1 100644 --- a/g3keymess/src/opts.rs +++ b/g3keymess/src/opts.rs @@ -15,13 +15,25 @@ */ use std::path::PathBuf; -#[cfg(all(unix, not(target_os = "openbsd")))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", +))] use std::str::FromStr; use std::sync::OnceLock; use anyhow::{anyhow, Context}; use clap::{value_parser, Arg, ArgAction, Command, ValueHint}; -#[cfg(all(unix, not(target_os = "openbsd")))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", +))] use log::info; use g3_compat::CpuAffinity; @@ -152,16 +164,6 @@ pub fn parse_clap() -> anyhow::Result> { } } } - #[cfg(target_os = "macos")] - if let Some(s) = group_name.strip_prefix("core") { - use std::num::NonZeroI32; - - if let Ok(id) = NonZeroI32::from_str(s) { - let cpu = CpuAffinity::new(id); - info!("will try to bind to cpu core {id}"); - proc_args.core_affinity = Some(cpu); - } - } } #[cfg(feature = "openssl-async-job")] diff --git a/lib/g3-compat/Cargo.toml b/lib/g3-compat/Cargo.toml index 211a4a2df..040cc7fc2 100644 --- a/lib/g3-compat/Cargo.toml +++ b/lib/g3-compat/Cargo.toml @@ -15,7 +15,3 @@ rustix = { workspace = true, features = ["system"] } [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = ["Win32_System_SystemInformation"] } - -[target.'cfg(target_os = "macos")'.dependencies] -libc.workspace = true -mach2.workspace = true diff --git a/lib/g3-compat/src/sched/macos.rs b/lib/g3-compat/src/sched/macos.rs deleted file mode 100644 index 48bb93c2c..000000000 --- a/lib/g3-compat/src/sched/macos.rs +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2023 ByteDance and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -use std::io; -use std::num::NonZeroI32; - -use libc::thread_policy_t; -use mach2::mach_init::mach_thread_self; -use mach2::thread_policy::{ - thread_affinity_policy, thread_policy_flavor_t, thread_policy_set, THREAD_AFFINITY_POLICY, - THREAD_AFFINITY_POLICY_COUNT, THREAD_AFFINITY_TAG_NULL, -}; -use mach2::vm_types::integer_t; - -#[derive(Clone)] -pub struct CpuAffinity { - cpu_tag: integer_t, -} - -impl Default for CpuAffinity { - fn default() -> Self { - CpuAffinity { - cpu_tag: THREAD_AFFINITY_TAG_NULL, - } - } -} - -impl CpuAffinity { - pub fn new(tag: NonZeroI32) -> Self { - CpuAffinity { cpu_tag: tag.get() } - } - - pub fn cpu_id_list(&self) -> &[usize] { - &[] - } - - pub fn apply_to_local_thread(&self) -> io::Result<()> { - let mut policy_info = thread_affinity_policy { - affinity_tag: self.cpu_tag, - }; - let errno = unsafe { - thread_policy_set( - mach_thread_self(), - THREAD_AFFINITY_POLICY as thread_policy_flavor_t, - &mut policy_info as *mut thread_affinity_policy as thread_policy_t, - THREAD_AFFINITY_POLICY_COUNT, - ) - }; - match errno { - 0 => Ok(()), - 46 => Err(io::Error::new( - io::ErrorKind::Unsupported, - "thread_policy_set() is not supported", - )), - n => Err(io::Error::other(format!( - "thread_policy_set({}) returned error code {n}", - self.cpu_tag - ))), - } - } -} diff --git a/lib/g3-compat/src/sched/mod.rs b/lib/g3-compat/src/sched/mod.rs index 198cc4572..ffd7f11a0 100644 --- a/lib/g3-compat/src/sched/mod.rs +++ b/lib/g3-compat/src/sched/mod.rs @@ -20,6 +20,5 @@ path = "freebsd.rs" )] #[cfg_attr(target_os = "netbsd", path = "netbsd.rs")] -#[cfg_attr(target_os = "macos", path = "macos.rs")] mod os; pub use os::CpuAffinity; diff --git a/lib/g3-runtime/src/unaided/mod.rs b/lib/g3-runtime/src/unaided/mod.rs index 9ec6450e1..acbe9a40c 100644 --- a/lib/g3-runtime/src/unaided/mod.rs +++ b/lib/g3-runtime/src/unaided/mod.rs @@ -96,18 +96,6 @@ impl UnaidedRuntimeConfig { Ok(()) } - #[cfg(target_os = "macos")] - pub fn set_mapped_sched_affinity(&mut self) -> anyhow::Result<()> { - use std::num::NonZeroI32; - - let n = self.num_threads(); - for i in 1..=n { - let cpu = CpuAffinity::new(unsafe { NonZeroI32::new_unchecked(i as i32) }); - self.sched_affinity.insert(i, cpu); - } - Ok(()) - } - pub fn set_max_io_events_per_tick(&mut self, capacity: usize) { self.max_io_events_per_tick = Some(capacity); } diff --git a/lib/g3-runtime/src/unaided/yaml.rs b/lib/g3-runtime/src/unaided/yaml.rs index f20454b7a..a7247edc3 100644 --- a/lib/g3-runtime/src/unaided/yaml.rs +++ b/lib/g3-runtime/src/unaided/yaml.rs @@ -23,7 +23,13 @@ impl UnaidedRuntimeConfig { pub fn parse_yaml(v: &Yaml) -> anyhow::Result { if let Yaml::Hash(map) = v { let mut config = UnaidedRuntimeConfig::default(); - #[cfg(all(unix, not(target_os = "openbsd")))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + ))] let mut set_mapped_sched_affinity = false; g3_yaml::foreach_kv(map, |k, v| match g3_yaml::key::normalize(k).as_str() { @@ -38,24 +44,21 @@ impl UnaidedRuntimeConfig { config.set_thread_stack_size(value); Ok(()) } - #[cfg(all(unix, not(target_os = "openbsd")))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + ))] "sched_affinity" => { if let Yaml::Hash(map) = v { for (ik, iv) in map.iter() { let id = g3_yaml::value::as_usize(ik) .context(format!("the keys for {k} should be usize value"))?; - #[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "netbsd", - ))] + let cpu = g3_yaml::value::as_cpu_set(iv) .context(format!("invalid cpu set value for {k}/{id}"))?; - #[cfg(target_os = "macos")] - let cpu = g3_yaml::value::as_cpu_tag(iv) - .context(format!("invalid cpu tag value for {k}/{id}"))?; config.set_sched_affinity(id, cpu); } @@ -87,7 +90,13 @@ impl UnaidedRuntimeConfig { _ => Err(anyhow!("invalid key {k}")), })?; - #[cfg(all(unix, not(target_os = "openbsd")))] + #[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + ))] if set_mapped_sched_affinity { config .set_mapped_sched_affinity() diff --git a/lib/g3-yaml/src/value/mod.rs b/lib/g3-yaml/src/value/mod.rs index acdd6ce78..64403dd8e 100644 --- a/lib/g3-yaml/src/value/mod.rs +++ b/lib/g3-yaml/src/value/mod.rs @@ -83,9 +83,27 @@ mod quinn; #[cfg(feature = "quinn")] pub use quinn::as_quinn_transport_config; -#[cfg(all(unix, not(target_os = "openbsd"), feature = "sched"))] +#[cfg(all( + any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + ), + feature = "sched" +))] mod sched; -#[cfg(all(unix, not(target_os = "openbsd"), feature = "sched"))] +#[cfg(all( + any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + ), + feature = "sched" +))] pub use sched::*; #[cfg(feature = "route")] diff --git a/lib/g3-yaml/src/value/sched.rs b/lib/g3-yaml/src/value/sched.rs index 2823a644d..4e42dbaf3 100644 --- a/lib/g3-yaml/src/value/sched.rs +++ b/lib/g3-yaml/src/value/sched.rs @@ -18,13 +18,6 @@ use yaml_rust::Yaml; use g3_compat::CpuAffinity; -#[cfg(any( - target_os = "linux", - target_os = "android", - target_os = "freebsd", - target_os = "dragonfly", - target_os = "netbsd", -))] pub fn as_cpu_set(v: &Yaml) -> anyhow::Result { use anyhow::{anyhow, Context}; @@ -44,12 +37,3 @@ pub fn as_cpu_set(v: &Yaml) -> anyhow::Result { Ok(set) } - -#[cfg(target_os = "macos")] -pub fn as_cpu_tag(v: &Yaml) -> anyhow::Result { - use anyhow::Context; - - let v = - crate::value::as_nonzero_i32(v).context("cpu tag should be valid nonzero isize value")?; - Ok(CpuAffinity::new(v)) -}