From 6199608aeb79cc7291a10bfdfaeba2380f98459f Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Fri, 31 Mar 2023 08:33:02 -0400 Subject: [PATCH] reduce tfclient CPU usage by 99.96%! --- Cargo.lock | 2 +- tfclient/Cargo.toml | 6 +++++- tfclient/src/apiworker.rs | 13 ++++--------- tfclient/src/nebulaworker.rs | 17 ++++++++--------- tfclient/src/socketworker.rs | 17 ++++++++--------- tfclient/src/timerworker.rs | 19 +++++++++++++++++++ 6 files changed, 45 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a072344..a4227c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2480,7 +2480,7 @@ dependencies = [ [[package]] name = "tfclient" -version = "0.1.4" +version = "0.1.6" dependencies = [ "base64 0.21.0", "base64-serde", diff --git a/tfclient/Cargo.toml b/tfclient/Cargo.toml index e105dc5..1166c9b 100644 --- a/tfclient/Cargo.toml +++ b/tfclient/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tfclient" -version = "0.1.5" +version = "0.1.6" edition = "2021" description = "An open-source reimplementation of a Defined Networking-compatible client" license = "GPL-3.0-or-later" @@ -40,3 +40,7 @@ tar = "0.4.38" hex = "0.4.3" tempfile = "3.4.0" 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"] \ No newline at end of file diff --git a/tfclient/src/apiworker.rs b/tfclient/src/apiworker.rs index 3b2a1de..4102499 100644 --- a/tfclient/src/apiworker.rs +++ b/tfclient/src/apiworker.rs @@ -1,5 +1,5 @@ use std::fs; -use std::sync::mpsc::{Receiver, TryRecvError}; +use std::sync::mpsc::{Receiver, RecvError, TryRecvError}; 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(); loop { - match rx.try_recv() { + match rx.recv() { Ok(msg) => { match msg { APIWorkerMessage::Shutdown => { @@ -206,13 +206,8 @@ pub fn apiworker_main(_config: TFClientConfig, instance: String, url: String, tx } }, Err(e) => { - match e { - TryRecvError::Empty => {} - TryRecvError::Disconnected => { - error!("apiworker command socket disconnected, shutting down to prevent orphaning"); - break; - } - } + error!("error on command socket: {}", e); + return; } } } diff --git a/tfclient/src/nebulaworker.rs b/tfclient/src/nebulaworker.rs index 6678670..f9dcc7f 100644 --- a/tfclient/src/nebulaworker.rs +++ b/tfclient/src/nebulaworker.rs @@ -12,7 +12,8 @@ use crate::embedded_nebula::run_embedded_nebula; pub enum NebulaWorkerMessage { Shutdown, - ConfigUpdated + ConfigUpdated, + WakeUp } fn insert_private_key(instance: &str) -> Result<(), Box> { @@ -86,9 +87,12 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter last_restart_time = SystemTime::now(); } } - match rx.try_recv() { + match rx.recv() { Ok(msg) => { match msg { + NebulaWorkerMessage::WakeUp => { + continue; + }, NebulaWorkerMessage::Shutdown => { info!("recv on command socket: shutdown, stopping"); info!("shutting down nebula binary"); @@ -140,13 +144,8 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter } }, Err(e) => { - match e { - TryRecvError::Empty => {} - TryRecvError::Disconnected => { - error!("nebulaworker command socket disconnected, shutting down to prevent orphaning"); - break; - } - } + error!("nebulaworker command socket errored: {}", e); + return; } } } diff --git a/tfclient/src/socketworker.rs b/tfclient/src/socketworker.rs index 498e53c..63d3633 100644 --- a/tfclient/src/socketworker.rs +++ b/tfclient/src/socketworker.rs @@ -15,7 +15,8 @@ use crate::nebulaworker::NebulaWorkerMessage; use crate::timerworker::TimerWorkerMessage; pub enum SocketWorkerMessage { - Shutdown + Shutdown, + WakeUp } pub fn socketworker_main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSender, rx: Receiver) { @@ -51,23 +52,21 @@ fn _main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSen Err(e) => { Err(e)?; } } - match rx.try_recv() { + match rx.recv() { Ok(msg) => { match msg { SocketWorkerMessage::Shutdown => { info!("recv on command socket: shutdown, stopping"); break; + }, + SocketWorkerMessage::WakeUp => { + continue; } } }, Err(e) => { - match e { - TryRecvError::Empty => {} - TryRecvError::Disconnected => { - error!("socketworker command socket disconnected, shutting down to prevent orphaning"); - break; - } - } + error!("socketworker command socket errored: {}", e); + break; } } } diff --git a/tfclient/src/timerworker.rs b/tfclient/src/timerworker.rs index 7fabba1..f1d5386 100644 --- a/tfclient/src/timerworker.rs +++ b/tfclient/src/timerworker.rs @@ -1,9 +1,12 @@ use std::ops::Add; use std::sync::mpsc::{Receiver, TryRecvError}; +use std::thread; use std::time::{Duration, SystemTime}; use log::{error, info}; use crate::apiworker::APIWorkerMessage; use crate::daemon::ThreadMessageSender; +use crate::nebulaworker::NebulaWorkerMessage; +use crate::socketworker::SocketWorkerMessage; pub enum TimerWorkerMessage { Shutdown @@ -13,6 +16,8 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver) { let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60)); loop { + thread::sleep(Duration::from_secs(10)); + match rx.try_recv() { Ok(msg) => { match msg { @@ -43,5 +48,19 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver) { } } } + + 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); + } + } } } \ No newline at end of file