diff --git a/external/xen/src/hypercall/mod.rs b/external/xen/src/hypercall/mod.rs index ded5bdd..2add984 100644 --- a/external/xen/src/hypercall/mod.rs +++ b/external/xen/src/hypercall/mod.rs @@ -49,26 +49,18 @@ pub trait XenHypercall: Sized { self.hypercall1(cmd, 0) } - fn make_const_object( - &self, - buffer: &T, - ) -> Result, Self::Error>; + fn make_const_object(&self, buffer: &T) + -> Result, Self::Error>; - fn make_mut_buffer( - &self, - buffer: &mut T, - ) -> Result, Self::Error>; + fn make_mut_buffer(&self, buffer: &mut T) + -> Result, Self::Error>; // Slices needs some special handling as they are not Copy themselves // and a pointer to a slice doesn't point to its first element. - fn make_const_slice( - &self, - slice: &[T], - ) -> Result, Self::Error>; + fn make_const_slice(&self, slice: &[T]) + -> Result, Self::Error>; - fn make_mut_slice( - &self, - slice: &mut [T], - ) -> Result, Self::Error>; + fn make_mut_slice(&self, slice: &mut [T]) + -> Result, Self::Error>; } diff --git a/external/xen/src/hypercall/unix/buffer.rs b/external/xen/src/hypercall/unix/buffer.rs index 1def587..3aa0e68 100644 --- a/external/xen/src/hypercall/unix/buffer.rs +++ b/external/xen/src/hypercall/unix/buffer.rs @@ -51,7 +51,7 @@ impl UnixXenHypercall { }) } else { // Get the number of page to hold the object layout. - let page_count = (size + (PAGE_SIZE - 1)) / PAGE_SIZE; + let page_count = size.div_ceil(PAGE_SIZE); let length = NonZeroUsize::new(page_count * PAGE_SIZE) .expect("Invalid size to page count convertion"); @@ -159,7 +159,7 @@ impl Drop for XenCallBuffer<'_, T> { } } -pub struct UnixConstXenBuffer<'a, 'hyp, T: Copy + ?Sized> { +pub struct UnixConstXenBuffer<'a, 'hyp, T: Copy> { // As const objects are actually being copied they actually don't // need to hold a reference to their original counterpart. // Use a PhantomData to make the borrow checker happy. @@ -167,18 +167,18 @@ pub struct UnixConstXenBuffer<'a, 'hyp, T: Copy + ?Sized> { pub(super) buffer: XenCallBuffer<'hyp, T>, } -pub struct UnixMutXenBuffer<'a, 'hyp, T: Copy + ?Sized> { +pub struct UnixMutXenBuffer<'a, 'hyp, T: Copy> { pub(super) original: &'a mut T, pub(super) buffer: XenCallBuffer<'hyp, T>, } -impl XenConstBuffer for UnixConstXenBuffer<'_, '_, T> { +impl XenConstBuffer for UnixConstXenBuffer<'_, '_, T> { fn as_hypercall_ptr(&self) -> *const T { self.buffer.ptr.as_ptr() } } -impl XenMutBuffer for UnixMutXenBuffer<'_, '_, T> { +impl XenMutBuffer for UnixMutXenBuffer<'_, '_, T> { fn as_hypercall_ptr(&mut self) -> *mut T { self.buffer.ptr.as_ptr() } @@ -189,7 +189,7 @@ impl XenMutBuffer for UnixMutXenBuffer<'_, '_, T> { } } -pub struct UnixConstXenSlice<'a, 'hyp, T: Copy + ?Sized> { +pub struct UnixConstXenSlice<'a, 'hyp, T: Copy> { // As const objects are actually being copied they actually don't // need to hold a reference to their original counterpart. // Use a PhantomData to make the compiler happy. @@ -197,18 +197,18 @@ pub struct UnixConstXenSlice<'a, 'hyp, T: Copy + ?Sized> { pub buffer: XenCallBuffer<'hyp, T>, } -pub struct UnixMutXenSlice<'a, 'b, T: Copy + ?Sized> { +pub struct UnixMutXenSlice<'a, 'b, T: Copy> { pub original: &'a mut [T], pub buffer: XenCallBuffer<'b, T>, } -impl XenConstBuffer for UnixConstXenSlice<'_, '_, T> { +impl XenConstBuffer for UnixConstXenSlice<'_, '_, T> { fn as_hypercall_ptr(&self) -> *const T { self.buffer.ptr.as_ptr() } } -impl XenMutBuffer for UnixMutXenSlice<'_, '_, T> { +impl XenMutBuffer for UnixMutXenSlice<'_, '_, T> { fn as_hypercall_ptr(&mut self) -> *mut T { self.buffer.ptr.as_ptr() } diff --git a/external/xen/src/hypercall/unix/mod.rs b/external/xen/src/hypercall/unix/mod.rs index 3e81595..2467398 100644 --- a/external/xen/src/hypercall/unix/mod.rs +++ b/external/xen/src/hypercall/unix/mod.rs @@ -56,10 +56,7 @@ const PATH_HYPERCALL: &str = "/dev/xen/hypercall"; impl UnixXenHypercall { pub fn new() -> Result { Ok(Self { - privcmd_device: File::options() - .read(true) - .write(true) - .open(PATH_PRIVCMD)?, + privcmd_device: File::options().read(true).write(true).open(PATH_PRIVCMD)?, hypercall_device: File::options() .read(true) .write(true) @@ -91,7 +88,7 @@ impl XenHypercall for UnixXenHypercall { } } - fn make_const_object<'a, T: Copy + ?Sized>( + fn make_const_object<'a, T: Copy>( &self, buffer: &'a T, ) -> Result, Self::Error> { @@ -104,7 +101,7 @@ impl XenHypercall for UnixXenHypercall { }) } - fn make_mut_buffer( + fn make_mut_buffer( &'_ self, buffer: &mut T, ) -> Result, Self::Error> { @@ -117,7 +114,7 @@ impl XenHypercall for UnixXenHypercall { }) } - fn make_const_slice<'a, T: Copy + Sized>( + fn make_const_slice<'a, T: Copy>( &self, slice: &'a [T], ) -> Result, Self::Error> { @@ -130,7 +127,7 @@ impl XenHypercall for UnixXenHypercall { }) } - fn make_mut_slice( + fn make_mut_slice( &self, slice: &mut [T], ) -> Result, Self::Error> { diff --git a/plugins/xcp-metrics-plugin-xen/src/plugin.rs b/plugins/xcp-metrics-plugin-xen/src/plugin.rs index 0403610..26286a6 100644 --- a/plugins/xcp-metrics-plugin-xen/src/plugin.rs +++ b/plugins/xcp-metrics-plugin-xen/src/plugin.rs @@ -77,14 +77,11 @@ impl PluginState { stream: &mut UnixStream, (kind, mut metric): (PluginMetricKind, Metric), ) -> anyhow::Result<()> { - let domain_metrics = self - .domid_metrics - .entry(domid.0) - .or_insert_with(|| HashMap::new()); + let domain_metrics = self.domid_metrics.entry(domid.0).or_default(); let family_name = kind.family_name.to_compact_string(); - let &mut uuid = domain_metrics.entry(kind).or_insert_with(|| Uuid::new_v4()); + let &mut uuid = domain_metrics.entry(kind).or_insert_with(Uuid::new_v4); // Inject domain UUID label into metric. let mut labels = metric.labels.into_vec(); @@ -109,10 +106,7 @@ impl PluginState { (kind, metric): (PluginMetricKind, Metric), ) -> anyhow::Result<()> { let family_name = kind.family_name.to_compact_string(); - let &mut uuid = self - .host_metrics - .entry(kind) - .or_insert_with(|| Uuid::new_v4()); + let &mut uuid = self.host_metrics.entry(kind).or_insert_with(Uuid::new_v4); stream.send_message(ProtocolMessage::UpdateMetric(UpdateMetric { family_name, @@ -133,7 +127,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re PCpuFreq.into(), ]; - for xen_metric in metrics.as_ref() { + for xen_metric in metrics.iter() { xen_metric.make_families(stream)?; } @@ -148,8 +142,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re // Get host metrics for metric in metrics .iter_mut() - .map(|xen_metric| xen_metric.read_host_metrics(physinfo, hyp)) - .flatten() + .flat_map(|xen_metric| xen_metric.read_host_metrics(physinfo, hyp)) { tracing::debug!("Pushing {metric:?}"); state.push_host_metric(stream, metric)?; @@ -161,8 +154,7 @@ pub fn run_plugin(stream: &mut UnixStream, hyp: &UnixXenHypercall) -> anyhow::Re for metric in metrics .iter_mut() - .map(|xen_metric| xen_metric.read_domain_metrics(domain, hyp)) - .flatten() + .flat_map(|xen_metric| xen_metric.read_domain_metrics(domain, hyp)) { tracing::debug!("Pushing {metric:?}"); state.push_domain_metric((domid, dom_uuid), stream, metric)?; diff --git a/plugins/xcp-metrics-plugin-xenstore/src/plugin.rs b/plugins/xcp-metrics-plugin-xenstore/src/plugin.rs index 7649377..fc8d552 100644 --- a/plugins/xcp-metrics-plugin-xenstore/src/plugin.rs +++ b/plugins/xcp-metrics-plugin-xenstore/src/plugin.rs @@ -170,9 +170,9 @@ pub async fn run_plugin( let &mut uuid = state .metrics_map .entry(domid) - .or_insert_with(|| HashMap::new()) + .or_insert_with(HashMap::new) .entry(handler.family_name().into()) - .or_insert_with(|| Uuid::new_v4()); + .or_insert_with(Uuid::new_v4); stream .send_message_async(ProtocolMessage::UpdateMetric(UpdateMetric { diff --git a/plugins/xcp-metrics-plugin-xenstore/src/plugin/metrics.rs b/plugins/xcp-metrics-plugin-xenstore/src/plugin/metrics.rs index c53c0db..c795c69 100644 --- a/plugins/xcp-metrics-plugin-xenstore/src/plugin/metrics.rs +++ b/plugins/xcp-metrics-plugin-xenstore/src/plugin/metrics.rs @@ -27,7 +27,7 @@ impl MetricHandler for MemInfoTotal { return None; } - let mut mem_total = xs.read(&path).await.ok()?.parse().ok()?; + let mut mem_total = xs.read(path).await.ok()?.parse().ok()?; mem_total *= 1024; // KiB to bytes Some(Metric { @@ -54,7 +54,7 @@ impl MetricHandler for MemInfoFree { return None; } - let mut mem_total = xs.read(&path).await.ok()?.parse().ok()?; + let mut mem_total = xs.read(path).await.ok()?.parse().ok()?; mem_total *= 1024; // KiB to bytes Some(Metric { diff --git a/xcp-metrics-common/src/protocol.rs b/xcp-metrics-common/src/protocol.rs index 7f5ef94..5816b96 100644 --- a/xcp-metrics-common/src/protocol.rs +++ b/xcp-metrics-common/src/protocol.rs @@ -6,8 +6,8 @@ //! TODO: Protocol negociation use std::io::{self, Read, Write}; -use serde::{Deserialize, Serialize}; use compact_str::CompactString; +use serde::{Deserialize, Serialize}; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use crate::metrics::{Metric, MetricType}; @@ -80,8 +80,8 @@ pub trait XcpMetricsStream { fn recv_message(&mut self) -> io::Result { let buffer = self.recv_message_raw()?; - Ok(ciborium::from_reader(buffer.as_ref()) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?) + ciborium::from_reader(buffer.as_ref()) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)) } } @@ -91,7 +91,7 @@ where { fn send_message_raw(&mut self, message: &[u8]) -> io::Result<()> { self.write_all(&(message.len() as u32).to_be_bytes())?; - self.write_all(&message)?; + self.write_all(message)?; Ok(()) } @@ -133,8 +133,8 @@ pub trait XcpMetricsAsyncStream { async fn recv_message_async(&mut self) -> io::Result { let buffer = self.recv_message_raw_async().await?; - Ok(ciborium::from_reader(buffer.as_ref()) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?) + ciborium::from_reader(buffer.as_ref()) + .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)) } } @@ -144,7 +144,7 @@ where { async fn send_message_raw_async(&mut self, message: &[u8]) -> io::Result<()> { self.write_u32(message.len() as u32).await?; - self.write_all(&message).await?; + self.write_all(message).await?; Ok(()) } diff --git a/xcp-metrics-common/src/utils/delta.rs b/xcp-metrics-common/src/utils/delta.rs index 1f6b485..3929700 100644 --- a/xcp-metrics-common/src/utils/delta.rs +++ b/xcp-metrics-common/src/utils/delta.rs @@ -111,9 +111,8 @@ pub struct MetricSetDelta<'a> { /// New added families (name only) pub added_families: Vec<(&'a str, &'a MetricFamily)>, - /// Changed family metadata + // /// Changed family metadata // changed_families: Vec<&'a str>, - /// Metrics that no longer contain a family. /// In case they reappears, they will need to be registered again. pub orphaned_families: Vec, @@ -179,14 +178,14 @@ impl MetricSetModel { // Combine family name with each family metric. .flat_map(|(name, family)| iter::zip(iter::repeat(name), family.metrics.iter())) // Only consider metrics we don't have, and generate a new proper UUID. - .filter_map(|(name, (&uuid, metric))| { + .filter(|&(name, (_, metric))| { // Due to contains_key expecting a tuple, we need to provide it a proper tuple (by cloning). // TODO: Find a better solution than cloning. - (!self + !self .metrics_map - .contains_key(&(name.clone(), metric.labels.clone()))) - .then(|| (name.as_ref(), metric, uuid)) + .contains_key(&(name.clone(), metric.labels.clone())) }) + .map(|(name, (&uuid, metric))| (name.as_ref(), metric, uuid)) .collect(); // Check for families that doesn't exist anymore.