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

add openssl async job size config option to yaml parser #495

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/g3-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ g3-types = { workspace = true, features = ["async-log"] }
g3-stdlog.workspace = true
g3-syslog = { workspace = true, features = ["yaml"] }
g3-fluentd = { workspace = true, optional = true, features = ["yaml"] }
g3-runtime.workspace = true
g3-runtime = { workspace = true, features = ["yaml"] }
g3-yaml = { workspace = true, features = ["sched"] }
g3-statsd-client = { workspace = true, features = ["yaml"] }
g3-io-ext.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion lib/g3-daemon/src/runtime/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn load(v: &Yaml) -> anyhow::Result<()> {
}

pub fn load_worker(v: &Yaml) -> anyhow::Result<()> {
let config = g3_yaml::value::as_unaided_runtime_config(v)?;
let config = UnaidedRuntimeConfig::parse_yaml(v)?;
WORKER_CONFIG.with_mut(|v| v.replace(config));
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions lib/g3-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ rust-version = "1.70.0"
anyhow.workspace = true
log.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "rt", "sync", "net"] }
yaml-rust = { workspace = true, optional = true }
g3-compat.workspace = true
g3-openssl = { workspace = true, optional = true }
g3-yaml = { workspace = true, optional = true, features = ["sched"] }

[features]
default = []
openssl-async-job = ["g3-openssl/async-job"]
yaml = ["dep:g3-yaml", "dep:yaml-rust"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 ByteDance and/or its affiliates.
* Copyright 2025 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.
Expand All @@ -24,6 +24,9 @@ use tokio::sync::{oneshot, watch};

use g3_compat::CpuAffinity;

#[cfg(feature = "yaml")]
mod yaml;

pub struct WorkersGuard {
_close_sender: watch::Sender<()>,
}
Expand Down Expand Up @@ -155,17 +158,17 @@ impl UnaidedRuntimeConfig {
#[cfg(feature = "openssl-async-job")]
if openssl_async_job_size > 0 {
builder.on_thread_start(move || {
if let Err(e) = g3_openssl::async_job::async_thread_init(
openssl_async_job_size,
openssl_async_job_size,
) {
if let Err(e) =
g3_openssl::async_job::async_thread_init(0, openssl_async_job_size)
{
warn!(
"failed to init {openssl_async_job_size} openssl async jobs: {e}"
);
}
});
builder.on_thread_stop(g3_openssl::async_job::async_thread_cleanup);
}
#[cfg(feature = "openssl-async-job")]
builder.on_thread_stop(g3_openssl::async_job::async_thread_cleanup);

match builder.build() {
Ok(rt) => {
Expand Down
98 changes: 98 additions & 0 deletions lib/g3-runtime/src/unaided/yaml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2025 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 anyhow::{anyhow, Context};
use yaml_rust::Yaml;

use super::UnaidedRuntimeConfig;

impl UnaidedRuntimeConfig {
pub fn parse_yaml(v: &Yaml) -> anyhow::Result<Self> {
if let Yaml::Hash(map) = v {
let mut config = UnaidedRuntimeConfig::default();
#[cfg(all(unix, not(target_os = "openbsd")))]
let mut set_mapped_sched_affinity = false;

g3_yaml::foreach_kv(map, |k, v| match g3_yaml::key::normalize(k).as_str() {
"thread_number" => {
let value = g3_yaml::value::as_usize(v)?;
config.set_thread_number(value);
Ok(())
}
"thread_stack_size" => {
let value = g3_yaml::humanize::as_usize(v)
.context(format!("invalid humanize usize value for key {k}"))?;
config.set_thread_stack_size(value);
Ok(())
}
#[cfg(all(unix, not(target_os = "openbsd")))]
"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);
}
Ok(())
} else if let Ok(map_all) = g3_yaml::value::as_bool(v) {
set_mapped_sched_affinity = map_all;
Ok(())
} else {
Err(anyhow!("invalid map value for key {k}"))
}
}
"max_io_events_per_tick" => {
let capacity = g3_yaml::value::as_usize(v)?;
config.set_max_io_events_per_tick(capacity);
Ok(())
}
#[cfg(feature = "openssl-async-job")]
"openssl_async_job_size" => {
let size = g3_yaml::value::as_usize(v)?;
config.set_openssl_async_job_size(size);
Ok(())
}
_ => Err(anyhow!("invalid key {k}")),
})?;

#[cfg(all(unix, not(target_os = "openbsd")))]
if set_mapped_sched_affinity {
config
.set_mapped_sched_affinity()
.context("failed to set all mapped sched affinity")?;
}

Ok(config)
} else {
Err(anyhow!(
"yaml value type for 'unaided runtime config' should be 'map'"
))
}
}
}
3 changes: 1 addition & 2 deletions lib/g3-yaml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ openssl = { workspace = true, optional = true }
http = { workspace = true, optional = true }
g3-types.workspace = true
g3-histogram = { workspace = true, optional = true }
g3-runtime = { workspace = true, optional = true }
g3-compat = { workspace = true, optional = true }
g3-dpi = { workspace = true, optional = true }
g3-geoip-types = { workspace = true, optional = true }
Expand All @@ -39,6 +38,6 @@ quinn = ["g3-types/quinn"]
http = ["g3-types/http", "dep:http"]
acl-rule = ["g3-types/acl-rule", "dep:ip_network", "dep:regex"]
route = ["g3-types/route"]
sched = ["dep:g3-runtime", "dep:g3-compat"]
sched = ["dep:g3-compat"]
dpi = ["dep:g3-dpi", "acl-rule"]
geoip = ["dep:g3-geoip-types"]
5 changes: 0 additions & 5 deletions lib/g3-yaml/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ mod sched;
#[cfg(all(unix, not(target_os = "openbsd"), feature = "sched"))]
pub use sched::*;

#[cfg(feature = "sched")]
mod runtime;
#[cfg(feature = "sched")]
pub use runtime::as_unaided_runtime_config;

#[cfg(feature = "route")]
mod route;
#[cfg(feature = "route")]
Expand Down
Loading
Loading