From 5697687762ac5f333538702b74c5b6813a849aff Mon Sep 17 00:00:00 2001 From: Sydhds Date: Tue, 9 Jan 2024 08:36:44 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20protocol=20handler=20retrieval=20thread?= =?UTF-8?q?=20CPU=20usage=20when=20no=20blocks=20are=20pr=E2=80=A6=20(#461?= =?UTF-8?q?7)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix protocol handler retrieval thread CPU usage when no blocks are processed * Fix thread name max length Signed-off-by: Jean-François * Shorten more thread name + static assert * Update note message * Clippy fixes --------- Signed-off-by: Jean-François Co-authored-by: sydhds Co-authored-by: Jean-François --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + massa-node/src/survey.rs | 5 ++++- massa-protocol-worker/Cargo.toml | 1 + massa-protocol-worker/src/connectivity.rs | 6 +++++- .../src/handlers/block_handler/propagation.rs | 6 +++++- .../src/handlers/block_handler/retrieval.rs | 9 ++++++++- .../src/handlers/endorsement_handler/propagation.rs | 6 +++++- .../src/handlers/endorsement_handler/retrieval.rs | 6 +++++- .../src/handlers/operation_handler/propagation.rs | 6 +++++- .../src/handlers/operation_handler/retrieval.rs | 6 +++++- massa-protocol-worker/src/handlers/peer_handler/mod.rs | 6 +++++- .../src/handlers/peer_handler/tester.rs | 6 +++++- 13 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e44df38f2c6..284e08ca8a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3278,6 +3278,7 @@ dependencies = [ "schnellru", "serde_json", "serial_test", + "static_assertions", "tempfile", "tracing", ] @@ -5162,6 +5163,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "str-buf" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index ad3b962f4f4..70f293ff428 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -178,6 +178,7 @@ serial_test = "2.0" sha2 = "=0.10" sha3 = "=0.10" socket2 = "0.5" +static_assertions = "1.1" stream_limiter = "3.2" structopt = "0.3" strum = "0.25" diff --git a/massa-node/src/survey.rs b/massa-node/src/survey.rs index 137802db292..26018f3cda8 100644 --- a/massa-node/src/survey.rs +++ b/massa-node/src/survey.rs @@ -49,13 +49,16 @@ impl MassaSurvey { if massa_metrics.is_enabled() { #[cfg(all(not(feature = "sandbox"), not(test)))] { + // massa-survey + const THREAD_NAME: &str = "massa-survey"; + let mut data_sent = 0; let mut data_received = 0; let (tx_stop, rx_stop) = MassaChannel::new("massa_survey_stop".to_string(), Some(1)); let update_tick = tick(tick_delay); match std::thread::Builder::new() - .name("massa-survey".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || loop { select! { recv(rx_stop) -> _ => { diff --git a/massa-protocol-worker/Cargo.toml b/massa-protocol-worker/Cargo.toml index 5420ce6b269..044daf9f429 100644 --- a/massa-protocol-worker/Cargo.toml +++ b/massa-protocol-worker/Cargo.toml @@ -34,6 +34,7 @@ massa_serialization = {workspace = true} massa_signature = {workspace = true} massa_time = {workspace = true} massa_versioning = {workspace = true} +static_assertions = {workspace = true} [dev-dependencies] tempfile = {workspace = true} # BOM UPGRADE Revert to "3.3" if problem diff --git a/massa-protocol-worker/src/connectivity.rs b/massa-protocol-worker/src/connectivity.rs index 80e4b6fb847..c19c128c835 100644 --- a/massa-protocol-worker/src/connectivity.rs +++ b/massa-protocol-worker/src/connectivity.rs @@ -35,6 +35,10 @@ use crate::{ wrap_network::NetworkController, }; +// protocol-connectivity +const THREAD_NAME: &str = "p-connectivity"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + #[derive(Clone)] pub enum ConnectivityCommand { Stop, @@ -82,7 +86,7 @@ pub(crate) fn start_connectivity_thread( massa_metrics: MassaMetrics, ) -> Result<(MassaSender, JoinHandle<()>), ProtocolError> { let handle = std::thread::Builder::new() - .name("protocol-connectivity".to_string()) + .name(THREAD_NAME.to_string()) .spawn({ let sender_endorsements_propagation_ext = protocol_channels.endorsement_handler_propagation.0.clone(); let sender_blocks_retrieval_ext = protocol_channels.block_handler_retrieval.0.clone(); diff --git a/massa-protocol-worker/src/handlers/block_handler/propagation.rs b/massa-protocol-worker/src/handlers/block_handler/propagation.rs index 85f66a5dfb8..56637139703 100644 --- a/massa-protocol-worker/src/handlers/block_handler/propagation.rs +++ b/massa-protocol-worker/src/handlers/block_handler/propagation.rs @@ -32,6 +32,10 @@ use std::thread::JoinHandle; use std::time::Instant; use tracing::{debug, info, warn}; +// protocol-block-handler-propagation +const THREAD_NAME: &str = "pbh-propagation"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + #[derive(Debug)] struct BlockPropagationData { /// Time when propagation was initiated @@ -222,7 +226,7 @@ pub fn start_propagation_thread( cache: SharedBlockCache, ) -> JoinHandle<()> { std::thread::Builder::new() - .name("protocol-block-handler-propagation".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let block_serializer = MessagesSerializer::new() .with_block_message_serializer(BlockMessageSerializer::new()); diff --git a/massa-protocol-worker/src/handlers/block_handler/retrieval.rs b/massa-protocol-worker/src/handlers/block_handler/retrieval.rs index 97770d11555..64f19961501 100644 --- a/massa-protocol-worker/src/handlers/block_handler/retrieval.rs +++ b/massa-protocol-worker/src/handlers/block_handler/retrieval.rs @@ -63,6 +63,10 @@ use super::{ BlockMessageSerializer, }; +// protocol-block-handler-retrieval +const THREAD_NAME: &str = "pbh-retrieval"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + /// Info about a block we've seen #[derive(Debug, Clone)] pub(crate) struct BlockInfo { @@ -933,6 +937,9 @@ impl RetrievalThread { .expect("could not compute next block retrieval timer tick"); if self.asked_blocks.is_empty() && self.block_wishlist.is_empty() { + // Note: in mainnet and before genesis, no blocks are processed but the timer needs to be updated + // or the thread will use the CPU at 100% + self.next_timer_ask_block = next_tick; return; } @@ -1275,7 +1282,7 @@ pub fn start_retrieval_thread( let block_message_serializer = MessagesSerializer::new().with_block_message_serializer(BlockMessageSerializer::new()); std::thread::Builder::new() - .name("protocol-block-handler-retrieval".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let mut retrieval_thread = RetrievalThread { active_connections, diff --git a/massa-protocol-worker/src/handlers/endorsement_handler/propagation.rs b/massa-protocol-worker/src/handlers/endorsement_handler/propagation.rs index 15eb040b719..f797ee5f6c3 100644 --- a/massa-protocol-worker/src/handlers/endorsement_handler/propagation.rs +++ b/massa-protocol-worker/src/handlers/endorsement_handler/propagation.rs @@ -9,6 +9,10 @@ use massa_storage::Storage; use std::thread::JoinHandle; use tracing::{info, log::warn}; +// protocol-endorsement-handler-propagation +const THREAD_NAME: &str = "peh-propagation"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + /// Endorsements need to propagate fast, so no buffering struct PropagationThread { receiver: MassaReceiver, @@ -142,7 +146,7 @@ pub fn start_propagation_thread( active_connections: Box, ) -> JoinHandle<()> { std::thread::Builder::new() - .name("protocol-endorsement-handler-propagation".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let endorsement_serializer = MessagesSerializer::new() .with_endorsement_message_serializer(EndorsementMessageSerializer::new()); diff --git a/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs b/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs index 8aaea5c0294..f62f981b26c 100644 --- a/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs +++ b/massa-protocol-worker/src/handlers/endorsement_handler/retrieval.rs @@ -33,6 +33,10 @@ use super::{ messages::{EndorsementMessageDeserializer, EndorsementMessageDeserializerArgs}, }; +// protocol-endorsement-handler-retrieval +const THREAD_NAME: &str = "peh-retrieval"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + pub struct RetrievalThread { receiver: MassaReceiver, receiver_ext: MassaReceiver, @@ -302,7 +306,7 @@ pub fn start_retrieval_thread( chain_id: config.chain_id, }); std::thread::Builder::new() - .name("protocol-endorsement-handler-retrieval".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let mut retrieval_thread = RetrievalThread { receiver, diff --git a/massa-protocol-worker/src/handlers/operation_handler/propagation.rs b/massa-protocol-worker/src/handlers/operation_handler/propagation.rs index b0ca650494e..c3fd9be93b6 100644 --- a/massa-protocol-worker/src/handlers/operation_handler/propagation.rs +++ b/massa-protocol-worker/src/handlers/operation_handler/propagation.rs @@ -24,6 +24,10 @@ use super::{ OperationMessageSerializer, }; +// protocol-operation-handler-propagation +const THREAD_NAME: &str = "poh-tester"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + struct PropagationThread { internal_receiver: MassaReceiver, active_connections: Box, @@ -206,7 +210,7 @@ pub fn start_propagation_thread( massa_metrics: MassaMetrics, ) -> JoinHandle<()> { std::thread::Builder::new() - .name("protocol-operation-handler-propagation".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let mut propagation_thread = PropagationThread { internal_receiver, diff --git a/massa-protocol-worker/src/handlers/operation_handler/retrieval.rs b/massa-protocol-worker/src/handlers/operation_handler/retrieval.rs index b28d225e210..fbf274b957a 100644 --- a/massa-protocol-worker/src/handlers/operation_handler/retrieval.rs +++ b/massa-protocol-worker/src/handlers/operation_handler/retrieval.rs @@ -35,6 +35,10 @@ use super::{ OperationMessageSerializer, }; +// protocol-operation-handler-retrieval +const THREAD_NAME: &str = "poh-retrieval"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + /// Structure containing a Batch of `operation_ids` we would like to ask /// to a `peer_id` now or later. Mainly used in protocol and translated into /// simple combination of a `peer_id` and `operations_prefix_ids` @@ -478,7 +482,7 @@ pub fn start_retrieval_thread( massa_metrics: MassaMetrics, ) -> JoinHandle<()> { std::thread::Builder::new() - .name("protocol-operation-handler-retrieval".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let mut retrieval_thread = RetrievalThread { receiver, diff --git a/massa-protocol-worker/src/handlers/peer_handler/mod.rs b/massa-protocol-worker/src/handlers/peer_handler/mod.rs index a8c8b9a4be6..bc531327bbb 100644 --- a/massa-protocol-worker/src/handlers/peer_handler/mod.rs +++ b/massa-protocol-worker/src/handlers/peer_handler/mod.rs @@ -63,6 +63,10 @@ pub struct PeerManagementHandler { testers: Vec, } +// protocol-peer-handler +const THREAD_NAME: &str = "pph"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + impl PeerManagementHandler { #[allow(clippy::too_many_arguments)] pub fn new( @@ -97,7 +101,7 @@ impl PeerManagementHandler { ); let thread_join = std::thread::Builder::new() - .name("protocol-peer-handler".to_string()) + .name(THREAD_NAME.to_string()) .spawn({ let peer_db = peer_db.clone(); let ticker = tick(Duration::from_secs(10)); diff --git a/massa-protocol-worker/src/handlers/peer_handler/tester.rs b/massa-protocol-worker/src/handlers/peer_handler/tester.rs index 860f8b64a91..62bd7d49fc2 100644 --- a/massa-protocol-worker/src/handlers/peer_handler/tester.rs +++ b/massa-protocol-worker/src/handlers/peer_handler/tester.rs @@ -26,6 +26,10 @@ use super::{ SharedPeerDB, }; use crate::wrap_network::ActiveConnectionsTrait; + +const THREAD_NAME: &str = "pph-tester"; +static_assertions::const_assert!(THREAD_NAME.len() < 16); + pub struct Tester { pub handler: Option>, } @@ -275,7 +279,7 @@ impl Tester { massa_metrics: MassaMetrics, ) -> Self { let handle = std::thread::Builder::new() - .name("protocol-peer-handler-tester".to_string()) + .name(THREAD_NAME.to_string()) .spawn(move || { let db = peer_db; let active_connections = active_connections.clone();