Debian Codegraph @

third_party/rust/gpu-allocator/src/visualizer/memory_chunks.rs

raw
1 use std::backtrace::BacktraceStatus;
2
3 use egui::{Color32, DragValue, Rect, ScrollArea, Sense, Ui, Vec2};
4
5 use super::ColorScheme;
6 use crate::allocator::free_list_allocator::MemoryChunk;
7
8 pub(crate) struct MemoryChunksVisualizationSettings {
9 pub width_in_bytes: u64,
10 pub show_backtraces: bool,
11 }
12
13 impl Default for MemoryChunksVisualizationSettings {
14 fn default() -> Self {
15 Self {
16 width_in_bytes: 1024,
17 show_backtraces: false,
18 }
19 }
20 }
21
22 impl MemoryChunksVisualizationSettings {
23 pub fn ui(&mut self, ui: &mut Ui, store_stack_traces: bool) {
24 if store_stack_traces {
25 ui.checkbox(&mut self.show_backtraces, "Show backtraces");
26 }
27
28 // Slider for changing the 'zoom' level of the visualizer.
29 const BYTES_PER_UNIT_MIN: i32 = 1;
30 const BYTES_PER_UNIT_MAX: i32 = 1024 * 1024;
31
32 ui.horizontal(|ui| {
33 ui.add(
34 DragValue::new(&mut self.width_in_bytes)
35 .clamp_range(BYTES_PER_UNIT_MIN..=BYTES_PER_UNIT_MAX)
36 .speed(10.0),
37 );
38 ui.label("Bytes per line");
39 });
40 }
41 }
42
43 pub(crate) fn render_memory_chunks_ui<'a>(
44 ui: &mut Ui,
45 color_scheme: &ColorScheme,
46 settings: &MemoryChunksVisualizationSettings,
47 total_size_in_bytes: u64,
48 data: impl IntoIterator<Item = &'a MemoryChunk>,
49 ) {
50 let line_height = ui.text_style_height(&egui::TextStyle::Body);
51 let number_of_rows =
52 (total_size_in_bytes as f32 / settings.width_in_bytes as f32).ceil() as usize;
53
54 ScrollArea::new([false, true]).show_rows(ui, line_height, number_of_rows, |ui, range| {
55 // Let range be in bytes
56 let start_in_bytes = range.start as u64 * settings.width_in_bytes;
57 let end_in_bytes = range.end as u64 * settings.width_in_bytes;
58
59 let mut data = data
60 .into_iter()
61 .filter(|chunk| {
62 (chunk.offset + chunk.size) > start_in_bytes && chunk.offset < end_in_bytes
63 })
64 .collect::<Vec<_>>();
65 data.sort_by_key(|chunk| chunk.offset);
66
67 let screen_width = ui.available_width();
68 let mut cursor_idx = 0;
69 let mut bytes_required = data[cursor_idx].offset + data[cursor_idx].size - start_in_bytes;
70
71 for _ in range {
72 ui.horizontal(|ui| {
73 let mut bytes_left = settings.width_in_bytes;
74 let mut cursor = ui.cursor().min;
75
76 while cursor_idx < data.len() && bytes_left > 0 {
77 // Block is depleted, so reset for more chunks
78 while bytes_required == 0 {
79 cursor_idx += 1;
80 if cursor_idx < data.len() {
81 bytes_required = data[cursor_idx].size;
82 }
83 continue;
84 }
85
86 let bytes_used = bytes_required.min(bytes_left);
87 let width_used =
88 bytes_used as f32 * screen_width / settings.width_in_bytes as f32;
89
90 // Draw the rectangle
91 let resp = ui.allocate_rect(
92 Rect::from_min_size(cursor, Vec2::new(width_used, line_height)),
93 Sense::click(),
94 );
95
96 if ui.is_rect_visible(resp.rect) {
97 ui.painter().rect(
98 resp.rect,
99 egui::Rounding::ZERO,
100 color_scheme
101 .get_allocation_type_color(data[cursor_idx].allocation_type),
102 egui::Stroke::new(1.0, Color32::BLACK),
103 );
104
105 resp.on_hover_ui_at_pointer(|ui| {
106 let chunk = &data[cursor_idx];
107 ui.label(format!("id: {}", chunk.chunk_id));
108 ui.label(format!("offset: 0x{:x}", chunk.offset));
109 ui.label(format!("size: 0x{:x}", chunk.size));
110 ui.label(format!(
111 "allocation_type: {}",
112 chunk.allocation_type.as_str()
113 ));
114 if let Some(name) = &chunk.name {
115 ui.label(format!("name: {}", name));
116 }
117 if settings.show_backtraces
118 && chunk.backtrace.status() == BacktraceStatus::Captured
119 {
120 ui.label(chunk.backtrace.to_string());
121 }
122 });
123 }
124
125 // Update our cursors
126 cursor.x += width_used;
127 bytes_left -= bytes_used;
128 bytes_required -= bytes_used;
129 }
130 });
131 }
132 });
133 }