From 187d209a665e508f42ad9d2014d1b9e4c0a2a5dc Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Mon, 15 Jan 2024 18:17:04 +0100 Subject: [PATCH] Move stats_struct macro in zenoh-utils --- commons/zenoh-util/src/lib.rs | 2 + commons/zenoh-util/src/stats/mod.rs | 169 +++++++++++++++++++++++++ io/zenoh-transport/src/common/stats.rs | 157 +---------------------- 3 files changed, 172 insertions(+), 156 deletions(-) create mode 100644 commons/zenoh-util/src/stats/mod.rs diff --git a/commons/zenoh-util/src/lib.rs b/commons/zenoh-util/src/lib.rs index 7e02096ebb..f3f55feff8 100644 --- a/commons/zenoh-util/src/lib.rs +++ b/commons/zenoh-util/src/lib.rs @@ -42,3 +42,5 @@ mod std_only; #[cfg(feature = "std")] pub use std_only::*; + +mod stats; diff --git a/commons/zenoh-util/src/stats/mod.rs b/commons/zenoh-util/src/stats/mod.rs new file mode 100644 index 0000000000..1295af892e --- /dev/null +++ b/commons/zenoh-util/src/stats/mod.rs @@ -0,0 +1,169 @@ +// +// Copyright (c) 2023 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// +#[macro_export] +macro_rules! stats_struct { + (@field_type ) => {AtomicUsize}; + (@field_type $field_type:ident) => {std::sync::Arc<$field_type>}; + (@report_field_type ) => {usize}; + (@report_field_type $field_type:ident) => {paste::paste! {[<$field_type Report>]}}; + (@new($parent:expr) ) => {AtomicUsize::new(0)}; + (@new($parent:expr) $field_type:ident) => {std::sync::Arc::new($field_type::new($parent))}; + (@report_default ) => {0}; + (@report_default $field_type:ident) => {paste::paste! {[<$field_type Report>]::default()}}; + (@get $vis:vis $field_name:ident) => { + paste::paste! { + $vis fn [](&self) -> usize { + self.$field_name.load(Ordering::Relaxed) + } + } + }; + (@get $vis:vis $field_name:ident $field_type:ident) => { + paste::paste! { + $vis fn [](&self) -> [<$field_type Report>] { + self.$field_name.report() + } + } + }; + (@increment $vis:vis $field_name:ident) => { + paste::paste! { + $vis fn [](&self, nb: usize) { + self.$field_name.fetch_add(nb, Ordering::Relaxed); + if let Some(parent) = self.parent.as_ref() { + parent.[](nb); + } + } + } + }; + (@increment $vis:vis $field_name:ident $field_type:ident) => {}; + (@openmetrics($stats:expr, $string:expr) $field_name:ident) => { + $string.push_str(stringify!($field_name)); + $string.push_str(" "); + $string.push_str($stats.$field_name.to_string().as_str()); + $string.push_str("\n"); + }; + (@openmetrics($stats:expr, $string:expr) $field_name:ident $field_type:ident) => { + $string.push_str(&$stats.$field_name.sub_openmetrics_text(stringify!($field_name))); + }; + (@openmetrics_val($stats:expr) $field_name:ident) => { + $stats.$field_name.to_string().as_str() + }; + (@openmetrics_val($stats:expr) $field_name:ident $field_type:ident) => {""}; + ( + $(#[$meta:meta])* + $vis:vis struct $struct_name:ident { + + $( + $(# HELP $help:literal)? + $(# TYPE $type:literal)? + $(#[$field_meta:meta])* + $field_vis:vis $field_name:ident $($field_type:ident)?, + )* + } + ) => { + paste::paste! { + $vis struct $struct_name { + parent: Option>, + $( + $(#[$field_meta])* + $field_vis $field_name: stats_struct!(@field_type $($field_type)?), + )* + } + + $(#[$meta])* + $vis struct [<$struct_name Report>] { + $( + $(#[$field_meta])* + $field_vis $field_name: stats_struct!(@report_field_type $($field_type)?), + )* + } + + impl $struct_name { + $vis fn new(parent: Option>) -> Self { + $struct_name { + parent: parent.clone(), + $($field_name: stats_struct!(@new(parent.as_ref().map(|p|p.$field_name.clone())) $($field_type)?),)* + } + } + + $vis fn report(&self) -> [<$struct_name Report>] { + [<$struct_name Report>] { + $($field_name: self.[](),)* + } + } + + $( + stats_struct!(@get $vis $field_name $($field_type)?); + stats_struct!(@increment $vis $field_name $($field_type)?); + )* + } + + impl Default for $struct_name { + fn default() -> Self { + Self { + parent: None, + $($field_name: stats_struct!(@new(None) $($field_type)?),)* + } + } + } + + impl [<$struct_name Report>] { + #[allow(dead_code)] + fn sub_openmetrics_text(&self, prefix: &str) -> String { + let mut s = String::new(); + $( + s.push_str(prefix); + s.push_str("{space=\""); + s.push_str(stringify!($field_name)); + s.push_str("\"} "); + s.push_str( + stats_struct!(@openmetrics_val(self) $field_name $($field_type)?) + ); + s.push_str("\n"); + )* + s + } + + $vis fn openmetrics_text(&self) -> String { + let mut s = String::new(); + $( + $( + s.push_str("# HELP "); + s.push_str(stringify!($field_name)); + s.push_str(" "); + s.push_str($help); + s.push_str("\n"); + )? + $( + s.push_str("# TYPE "); + s.push_str(stringify!($field_name)); + s.push_str(" "); + s.push_str($type); + s.push_str("\n"); + )? + stats_struct!(@openmetrics(self, s) $field_name $($field_type)?); + )* + s + } + } + + impl Default for [<$struct_name Report>] { + fn default() -> Self { + Self { + $($field_name: stats_struct!(@report_default $($field_type)?),)* + } + } + } + } + } +} diff --git a/io/zenoh-transport/src/common/stats.rs b/io/zenoh-transport/src/common/stats.rs index f095a58273..10b4f0705f 100644 --- a/io/zenoh-transport/src/common/stats.rs +++ b/io/zenoh-transport/src/common/stats.rs @@ -11,164 +11,9 @@ // Contributors: // ZettaScale Zenoh Team, // -macro_rules! stats_struct { - (@field_type ) => {AtomicUsize}; - (@field_type $field_type:ident) => {std::sync::Arc<$field_type>}; - (@report_field_type ) => {usize}; - (@report_field_type $field_type:ident) => {paste::paste! {[<$field_type Report>]}}; - (@new($parent:expr) ) => {AtomicUsize::new(0)}; - (@new($parent:expr) $field_type:ident) => {std::sync::Arc::new($field_type::new($parent))}; - (@report_default ) => {0}; - (@report_default $field_type:ident) => {paste::paste! {[<$field_type Report>]::default()}}; - (@get $vis:vis $field_name:ident) => { - paste::paste! { - $vis fn [](&self) -> usize { - self.$field_name.load(Ordering::Relaxed) - } - } - }; - (@get $vis:vis $field_name:ident $field_type:ident) => { - paste::paste! { - $vis fn [](&self) -> [<$field_type Report>] { - self.$field_name.report() - } - } - }; - (@increment $vis:vis $field_name:ident) => { - paste::paste! { - $vis fn [](&self, nb: usize) { - self.$field_name.fetch_add(nb, Ordering::Relaxed); - if let Some(parent) = self.parent.as_ref() { - parent.[](nb); - } - } - } - }; - (@increment $vis:vis $field_name:ident $field_type:ident) => {}; - (@openmetrics($stats:expr, $string:expr) $field_name:ident) => { - $string.push_str(stringify!($field_name)); - $string.push_str(" "); - $string.push_str($stats.$field_name.to_string().as_str()); - $string.push_str("\n"); - }; - (@openmetrics($stats:expr, $string:expr) $field_name:ident $field_type:ident) => { - $string.push_str(&$stats.$field_name.sub_openmetrics_text(stringify!($field_name))); - }; - (@openmetrics_val($stats:expr) $field_name:ident) => { - $stats.$field_name.to_string().as_str() - }; - (@openmetrics_val($stats:expr) $field_name:ident $field_type:ident) => {""}; - ( - $(#[$meta:meta])* - $vis:vis struct $struct_name:ident { - - $( - $(# HELP $help:literal)? - $(# TYPE $type:literal)? - $(#[$field_meta:meta])* - $field_vis:vis $field_name:ident $($field_type:ident)?, - )* - } - ) => { - paste::paste! { - $vis struct $struct_name { - parent: Option>, - $( - $(#[$field_meta])* - $field_vis $field_name: stats_struct!(@field_type $($field_type)?), - )* - } - - $(#[$meta])* - $vis struct [<$struct_name Report>] { - $( - $(#[$field_meta])* - $field_vis $field_name: stats_struct!(@report_field_type $($field_type)?), - )* - } - - impl $struct_name { - $vis fn new(parent: Option>) -> Self { - $struct_name { - parent: parent.clone(), - $($field_name: stats_struct!(@new(parent.as_ref().map(|p|p.$field_name.clone())) $($field_type)?),)* - } - } - - $vis fn report(&self) -> [<$struct_name Report>] { - [<$struct_name Report>] { - $($field_name: self.[](),)* - } - } - - $( - stats_struct!(@get $vis $field_name $($field_type)?); - stats_struct!(@increment $vis $field_name $($field_type)?); - )* - } - - impl Default for $struct_name { - fn default() -> Self { - Self { - parent: None, - $($field_name: stats_struct!(@new(None) $($field_type)?),)* - } - } - } - - impl [<$struct_name Report>] { - #[allow(dead_code)] - fn sub_openmetrics_text(&self, prefix: &str) -> String { - let mut s = String::new(); - $( - s.push_str(prefix); - s.push_str("{space=\""); - s.push_str(stringify!($field_name)); - s.push_str("\"} "); - s.push_str( - stats_struct!(@openmetrics_val(self) $field_name $($field_type)?) - ); - s.push_str("\n"); - )* - s - } - - $vis fn openmetrics_text(&self) -> String { - let mut s = String::new(); - $( - $( - s.push_str("# HELP "); - s.push_str(stringify!($field_name)); - s.push_str(" "); - s.push_str($help); - s.push_str("\n"); - )? - $( - s.push_str("# TYPE "); - s.push_str(stringify!($field_name)); - s.push_str(" "); - s.push_str($type); - s.push_str("\n"); - )? - stats_struct!(@openmetrics(self, s) $field_name $($field_type)?); - )* - s - } - } - - impl Default for [<$struct_name Report>] { - fn default() -> Self { - Self { - $($field_name: stats_struct!(@report_default $($field_type)?),)* - } - } - } - } - } -} - use serde::{Deserialize, Serialize}; use std::sync::atomic::{AtomicUsize, Ordering}; +use zenoh_util::stats_struct; stats_struct! { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DiscriminatedStats {