Skip to content

Commit 9147d8f

Browse files
committed
cpu affinity
1 parent c83480a commit 9147d8f

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ tokio = ["ntex/tokio"]
3737
compio = ["ntex/compio", ]
3838

3939
[dependencies]
40-
ntex = "2.4"
41-
ntex-compio = "0.1.2"
40+
ntex = "2.8"
41+
ntex-compio = "0.2"
4242
ntex-bytes = { version = "0.1.21", features=["simd"] }
4343
mimalloc = { version = "0.1.25", default-features = false }
4444
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
@@ -47,16 +47,19 @@ buf-min = { version = "0.7", features = ["ntex-bytes"] }
4747
env_logger = "0.11"
4848
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
4949
atoi = "2.0"
50-
num_cpus = "1.16"
50+
core_affinity = "0.8"
5151
smallvec = "1.13"
5252
futures = "0.3"
53+
sonic-rs = "0.3.16"
5354
serde = { version = "1.0", features = ["derive"] }
5455
serde_json = "1.0"
5556
log = { version = "0.4", features = ["release_max_level_off"] }
56-
compio-driver = { version = "0.4", features = ["io-uring", "io-uring-socket"]}
5757
tok_io = {version = "1", package = "tokio" }
5858
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-2" }
5959

60+
[target.'cfg(target_os = "linux")'.dependencies]
61+
compio-driver = { version = "*", features = ["io-uring"]}
62+
6063
[profile.release]
6164
opt-level = 3
6265
codegen-units = 1

frameworks/Rust/ntex/src/main.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use ntex::http::header::{CONTENT_TYPE, SERVER};
5-
use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};
5+
use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, util::Ready, web};
66
use yarte::Serialize;
77

88
mod utils;
@@ -45,6 +45,10 @@ async fn plaintext() -> web::HttpResponse {
4545
async fn main() -> std::io::Result<()> {
4646
println!("Started http server: 127.0.0.1:8080");
4747

48+
let cores = core_affinity::get_core_ids().unwrap();
49+
let total_cores = cores.len();
50+
let cores = std::sync::Arc::new(std::sync::Mutex::new(cores));
51+
4852
// start http server
4953
ntex::server::build()
5054
.backlog(1024)
@@ -60,7 +64,17 @@ async fn main() -> std::io::Result<()> {
6064
.payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
6165
.h1(web::App::new().service(json).service(plaintext).finish())
6266
})?
63-
.workers(num_cpus::get())
67+
.configure(move |cfg| {
68+
let cores = cores.clone();
69+
cfg.on_worker_start(move |_| {
70+
if let Some(core) = cores.lock().unwrap().pop() {
71+
core_affinity::set_for_current(core);
72+
}
73+
Ready::<_, &str>::Ok(())
74+
});
75+
Ok(())
76+
})?
77+
.workers(total_cores)
6478
.run()
6579
.await
6680
}

frameworks/Rust/ntex/src/main_db.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#[cfg(not(target_os = "macos"))]
22
#[global_allocator]
33
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
4-
// static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
54

65
use ntex::http::header::{CONTENT_TYPE, SERVER};
76
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
87
use ntex::service::{Service, ServiceCtx, ServiceFactory};
98
use ntex::web::{Error, HttpResponse};
10-
use ntex::{time::Seconds, util::PoolId};
9+
use ntex::{time::Seconds, util::PoolId, util::Ready};
1110

1211
mod db;
1312
mod utils;
@@ -83,6 +82,10 @@ impl ServiceFactory<Request> for AppFactory {
8382
async fn main() -> std::io::Result<()> {
8483
println!("Starting http server: 127.0.0.1:8080");
8584

85+
let cores = core_affinity::get_core_ids().unwrap();
86+
let total_cores = cores.len();
87+
let cores = std::sync::Arc::new(std::sync::Mutex::new(cores));
88+
8689
ntex::server::build()
8790
.backlog(1024)
8891
.bind("techempower", "0.0.0.0:8080", |cfg| {
@@ -97,7 +100,17 @@ async fn main() -> std::io::Result<()> {
97100
.payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
98101
.h1(AppFactory)
99102
})?
100-
.workers(num_cpus::get())
103+
.configure(move |cfg| {
104+
let cores = cores.clone();
105+
cfg.on_worker_start(move |_| {
106+
if let Some(core) = cores.lock().unwrap().pop() {
107+
core_affinity::set_for_current(core);
108+
}
109+
Ready::<_, &str>::Ok(())
110+
});
111+
Ok(())
112+
})?
113+
.workers(total_cores)
101114
.run()
102115
.await
103116
}

frameworks/Rust/ntex/src/main_plt.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use std::{future::Future, io, pin::Pin, task::Context, task::Poll};
55

6-
use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::ready, util::PoolId};
6+
use ntex::util::{ready, PoolId, Ready};
7+
use ntex::{fn_service, http::h1, io::Io, io::RecvError};
78
use yarte::Serialize;
89

910
mod utils;
@@ -75,6 +76,10 @@ impl Future for App {
7576
async fn main() -> io::Result<()> {
7677
println!("Started http server: 127.0.0.1:8080");
7778

79+
let cores = core_affinity::get_core_ids().unwrap();
80+
let total_cores = cores.len();
81+
let cores = std::sync::Arc::new(std::sync::Mutex::new(cores));
82+
7883
// start http server
7984
ntex::server::build()
8085
.backlog(1024)
@@ -88,7 +93,17 @@ async fn main() -> io::Result<()> {
8893
codec: h1::Codec::default(),
8994
})
9095
})?
91-
.workers(num_cpus::get())
96+
.configure(move |cfg| {
97+
let cores = cores.clone();
98+
cfg.on_worker_start(move |_| {
99+
if let Some(core) = cores.lock().unwrap().pop() {
100+
core_affinity::set_for_current(core);
101+
}
102+
Ready::<_, &str>::Ok(())
103+
});
104+
Ok(())
105+
})?
106+
.workers(total_cores)
92107
.run()
93108
.await
94109
}

0 commit comments

Comments
 (0)