cubecl_runtime/memory_management/
base.rs1use alloc::string::{String, ToString};
2
3#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct MemoryUsage {
8 pub number_allocs: u64,
13 pub bytes_in_use: u64,
19 pub bytes_padding: u64,
21 pub bytes_reserved: u64,
27}
28
29impl MemoryUsage {
30 pub fn combine(&self, other: MemoryUsage) -> MemoryUsage {
32 MemoryUsage {
33 number_allocs: self.number_allocs + other.number_allocs,
34 bytes_in_use: self.bytes_in_use + other.bytes_in_use,
35 bytes_padding: self.bytes_padding + other.bytes_padding,
36 bytes_reserved: self.bytes_reserved + other.bytes_reserved,
37 }
38 }
39}
40
41#[derive(new)]
42pub(crate) struct BytesFormat {
43 bytes: u64,
44}
45
46impl core::fmt::Display for BytesFormat {
47 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48 let unit = 1000;
49
50 if self.bytes < unit {
51 f.write_fmt(format_args!("{} B", self.bytes))
52 } else {
53 let size = self.bytes as f64;
54 let exp = match size.log(1000.0).floor() as usize {
55 0 => 1,
56 e => e,
57 };
58 let unit_prefix = "KMGTPEZY".as_bytes();
59 f.write_fmt(format_args!(
60 "{:.2} {}B",
61 (size / unit.pow(exp as u32) as f64),
62 unit_prefix[exp - 1] as char,
63 ))
64 }
65 }
66}
67
68fn bytes_format(bytes: u64) -> String {
69 BytesFormat::new(bytes).to_string()
70}
71
72impl core::fmt::Display for MemoryUsage {
73 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74 let usage_percentage = (self.bytes_in_use as f32 / self.bytes_reserved as f32) * 100.0;
77 let padding_percentage = (self.bytes_padding as f32 / self.bytes_in_use as f32) * 100.0;
78 writeln!(f, "Memory Usage Report:")?;
79 writeln!(f, " Number of allocations: {}", self.number_allocs)?;
80 writeln!(f, " Bytes in use: {}", bytes_format(self.bytes_in_use))?;
81 writeln!(
82 f,
83 " Bytes used for padding: {}",
84 bytes_format(self.bytes_padding)
85 )?;
86 writeln!(
87 f,
88 " Total bytes reserved: {}",
89 bytes_format(self.bytes_reserved)
90 )?;
91 writeln!(f, " Usage efficiency: {usage_percentage:.2}%")?;
92 writeln!(f, " Padding overhead: {padding_percentage:.2}%")
93 }
94}
95
96pub trait MemoryHandle<Binding>: Clone + Send + Sync + core::fmt::Debug {
99 fn can_mut(&self) -> bool;
101 fn binding(self) -> Binding;
103}