reduce tfclient CPU usage by 99.96%!

This commit is contained in:
c0repwn3r 2023-03-31 08:33:02 -04:00
parent 9a5ffcd622
commit 6199608aeb
Signed by: core
GPG Key ID: FDBF740DADDCEECF
6 changed files with 45 additions and 29 deletions

2
Cargo.lock generated
View File

@ -2480,7 +2480,7 @@ dependencies = [
[[package]] [[package]]
name = "tfclient" name = "tfclient"
version = "0.1.4" version = "0.1.6"
dependencies = [ dependencies = [
"base64 0.21.0", "base64 0.21.0",
"base64-serde", "base64-serde",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "tfclient" name = "tfclient"
version = "0.1.5" version = "0.1.6"
edition = "2021" edition = "2021"
description = "An open-source reimplementation of a Defined Networking-compatible client" description = "An open-source reimplementation of a Defined Networking-compatible client"
license = "GPL-3.0-or-later" license = "GPL-3.0-or-later"
@ -40,3 +40,7 @@ tar = "0.4.38"
hex = "0.4.3" hex = "0.4.3"
tempfile = "3.4.0" tempfile = "3.4.0"
sha2 = "0.10.6" sha2 = "0.10.6"
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]

View File

@ -1,5 +1,5 @@
use std::fs; use std::fs;
use std::sync::mpsc::{Receiver, TryRecvError}; use std::sync::mpsc::{Receiver, RecvError, TryRecvError};
use log::{error, info, warn}; use log::{error, info, warn};
@ -27,7 +27,7 @@ pub fn apiworker_main(_config: TFClientConfig, instance: String, url: String, tx
let client = Client::new(format!("tfclient/{}", env!("CARGO_PKG_VERSION")), server).unwrap(); let client = Client::new(format!("tfclient/{}", env!("CARGO_PKG_VERSION")), server).unwrap();
loop { loop {
match rx.try_recv() { match rx.recv() {
Ok(msg) => { Ok(msg) => {
match msg { match msg {
APIWorkerMessage::Shutdown => { APIWorkerMessage::Shutdown => {
@ -206,13 +206,8 @@ pub fn apiworker_main(_config: TFClientConfig, instance: String, url: String, tx
} }
}, },
Err(e) => { Err(e) => {
match e { error!("error on command socket: {}", e);
TryRecvError::Empty => {} return;
TryRecvError::Disconnected => {
error!("apiworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
} }
} }
} }

View File

@ -12,7 +12,8 @@ use crate::embedded_nebula::run_embedded_nebula;
pub enum NebulaWorkerMessage { pub enum NebulaWorkerMessage {
Shutdown, Shutdown,
ConfigUpdated ConfigUpdated,
WakeUp
} }
fn insert_private_key(instance: &str) -> Result<(), Box<dyn Error>> { fn insert_private_key(instance: &str) -> Result<(), Box<dyn Error>> {
@ -86,9 +87,12 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter
last_restart_time = SystemTime::now(); last_restart_time = SystemTime::now();
} }
} }
match rx.try_recv() { match rx.recv() {
Ok(msg) => { Ok(msg) => {
match msg { match msg {
NebulaWorkerMessage::WakeUp => {
continue;
},
NebulaWorkerMessage::Shutdown => { NebulaWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping"); info!("recv on command socket: shutdown, stopping");
info!("shutting down nebula binary"); info!("shutting down nebula binary");
@ -140,13 +144,8 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter
} }
}, },
Err(e) => { Err(e) => {
match e { error!("nebulaworker command socket errored: {}", e);
TryRecvError::Empty => {} return;
TryRecvError::Disconnected => {
error!("nebulaworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
} }
} }
} }

View File

@ -15,7 +15,8 @@ use crate::nebulaworker::NebulaWorkerMessage;
use crate::timerworker::TimerWorkerMessage; use crate::timerworker::TimerWorkerMessage;
pub enum SocketWorkerMessage { pub enum SocketWorkerMessage {
Shutdown Shutdown,
WakeUp
} }
pub fn socketworker_main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSender, rx: Receiver<SocketWorkerMessage>) { pub fn socketworker_main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSender, rx: Receiver<SocketWorkerMessage>) {
@ -51,23 +52,21 @@ fn _main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSen
Err(e) => { Err(e)?; } Err(e) => { Err(e)?; }
} }
match rx.try_recv() { match rx.recv() {
Ok(msg) => { Ok(msg) => {
match msg { match msg {
SocketWorkerMessage::Shutdown => { SocketWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping"); info!("recv on command socket: shutdown, stopping");
break; break;
},
SocketWorkerMessage::WakeUp => {
continue;
} }
} }
}, },
Err(e) => { Err(e) => {
match e { error!("socketworker command socket errored: {}", e);
TryRecvError::Empty => {} break;
TryRecvError::Disconnected => {
error!("socketworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
} }
} }
} }

View File

@ -1,9 +1,12 @@
use std::ops::Add; use std::ops::Add;
use std::sync::mpsc::{Receiver, TryRecvError}; use std::sync::mpsc::{Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use log::{error, info}; use log::{error, info};
use crate::apiworker::APIWorkerMessage; use crate::apiworker::APIWorkerMessage;
use crate::daemon::ThreadMessageSender; use crate::daemon::ThreadMessageSender;
use crate::nebulaworker::NebulaWorkerMessage;
use crate::socketworker::SocketWorkerMessage;
pub enum TimerWorkerMessage { pub enum TimerWorkerMessage {
Shutdown Shutdown
@ -13,6 +16,8 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>) {
let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60)); let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
loop { loop {
thread::sleep(Duration::from_secs(10));
match rx.try_recv() { match rx.try_recv() {
Ok(msg) => { Ok(msg) => {
match msg { match msg {
@ -43,5 +48,19 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>) {
} }
} }
} }
match tx.nebula_thread.send(NebulaWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!("Error sending wakeup message to nebula worker thread: {}", e);
}
}
match tx.socket_thread.send(SocketWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!("Error sending wakeup message to socket worker thread: {}", e);
}
}
} }
} }