rluvaton commented on code in PR #16926:
URL: https://github.com/apache/datafusion/pull/16926#discussion_r2233117652


##########
datafusion/execution/src/memory_pool/memory_report.rs:
##########
@@ -0,0 +1,118 @@
+use super::{human_readable_size, MemoryReservation};
+use crate::memory_pool::pool::{
+    FairSpillPool, GreedyMemoryPool, TrackConsumersPool, UnboundedMemoryPool,
+};
+use crate::memory_pool::MemoryPool;
+use datafusion_common::Result;
+use datafusion_expr::Accumulator;
+use std::any::Any;
+
+/// Helper trait to provide memory usage breakdowns for debugging.
+///
+/// Implemented for [`MemoryReservation`] and any [`Accumulator`] via a blanket
+/// implementation that relies on [`Accumulator::size`].
+///
+/// # Example
+/// ```
+/// # use std::sync::Arc;
+/// # use datafusion_execution::memory_pool::{ExplainMemory, GreedyMemoryPool, 
MemoryConsumer};
+/// let pool = Arc::new(GreedyMemoryPool::new(1024));
+/// let mut reservation = MemoryConsumer::new("example").register(&pool);
+/// reservation.try_grow(256).unwrap();
+/// println!("{}", reservation.explain_memory().unwrap());
+/// ```
+pub trait ExplainMemory {
+    /// Returns a human readable string describing memory usage.
+    fn explain_memory(&self) -> Result<String>;
+}
+
+impl ExplainMemory for MemoryReservation {
+    fn explain_memory(&self) -> Result<String> {
+        Ok(format!(
+            "{}#{} reserved {}",
+            self.consumer().name(),
+            self.consumer().id(),
+            human_readable_size(self.size())
+        ))
+    }
+}
+
+impl<T: Accumulator + ?Sized> ExplainMemory for T {

Review Comment:
   Wouldn't this will cause rust to disallow you to implement it on other trait.
   
   As I wouldn't be able to do:
   ```rust
   trait MyTrait {}
   
   impl<T: MyTrait> ExplainMemory for T {
     // ...
   }
   ```
   
   As rust will say to me that MyTrait can implement Accumulator



##########
datafusion/execution/src/memory_pool/memory_report.rs:
##########
@@ -0,0 +1,118 @@
+use super::{human_readable_size, MemoryReservation};
+use crate::memory_pool::pool::{
+    FairSpillPool, GreedyMemoryPool, TrackConsumersPool, UnboundedMemoryPool,
+};
+use crate::memory_pool::MemoryPool;
+use datafusion_common::Result;
+use datafusion_expr::Accumulator;
+use std::any::Any;
+
+/// Helper trait to provide memory usage breakdowns for debugging.
+///
+/// Implemented for [`MemoryReservation`] and any [`Accumulator`] via a blanket
+/// implementation that relies on [`Accumulator::size`].
+///
+/// # Example
+/// ```
+/// # use std::sync::Arc;
+/// # use datafusion_execution::memory_pool::{ExplainMemory, GreedyMemoryPool, 
MemoryConsumer};
+/// let pool = Arc::new(GreedyMemoryPool::new(1024));
+/// let mut reservation = MemoryConsumer::new("example").register(&pool);
+/// reservation.try_grow(256).unwrap();
+/// println!("{}", reservation.explain_memory().unwrap());
+/// ```
+pub trait ExplainMemory {

Review Comment:
   I think it should include size as well so any trait that need to report the 
size will also can report the breakdown of the size.



##########
datafusion/execution/src/memory_pool/memory_report.rs:
##########
@@ -0,0 +1,118 @@
+use super::{human_readable_size, MemoryReservation};
+use crate::memory_pool::pool::{
+    FairSpillPool, GreedyMemoryPool, TrackConsumersPool, UnboundedMemoryPool,
+};
+use crate::memory_pool::MemoryPool;
+use datafusion_common::Result;
+use datafusion_expr::Accumulator;
+use std::any::Any;
+
+/// Helper trait to provide memory usage breakdowns for debugging.
+///
+/// Implemented for [`MemoryReservation`] and any [`Accumulator`] via a blanket
+/// implementation that relies on [`Accumulator::size`].
+///
+/// # Example
+/// ```
+/// # use std::sync::Arc;
+/// # use datafusion_execution::memory_pool::{ExplainMemory, GreedyMemoryPool, 
MemoryConsumer};
+/// let pool = Arc::new(GreedyMemoryPool::new(1024));
+/// let mut reservation = MemoryConsumer::new("example").register(&pool);
+/// reservation.try_grow(256).unwrap();
+/// println!("{}", reservation.explain_memory().unwrap());
+/// ```
+pub trait ExplainMemory {
+    /// Returns a human readable string describing memory usage.
+    fn explain_memory(&self) -> Result<String>;

Review Comment:
   Do you think we should have something similar to formatter? So we will have 
easier to read memory report?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to