Debian Codegraph @

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

raw
1 use std::backtrace::BacktraceStatus;
2
3 use egui::{Label, Response, Sense, Ui, WidgetText};
4 use egui_extras::{Column, TableBuilder};
5
6 use crate::allocator::{fmt_bytes, AllocationReport};
7
8 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9 pub(crate) enum AllocationReportVisualizeSorting {
10 #[default]
11 None,
12 Idx,
13 Name,
14 Size,
15 }
16
17 #[derive(Debug, Default)]
18 pub(crate) struct AllocationReportVisualizeSettings {
19 pub filter: String,
20 pub sorting: AllocationReportVisualizeSorting,
21 pub ascending: bool,
22 }
23
24 pub(crate) fn render_allocation_reports_ui(
25 ui: &mut Ui,
26 settings: &mut AllocationReportVisualizeSettings,
27 allocations: impl IntoIterator<Item = AllocationReport>,
28 ) {
29 ui.horizontal(|ui| {
30 ui.label("Filter");
31 ui.text_edit_singleline(&mut settings.filter);
32 });
33 let breakdown_filter = settings.filter.to_lowercase();
34
35 let mut allocations = allocations
36 .into_iter()
37 .enumerate()
38 .filter(|(_, report)| report.name.to_lowercase().contains(&breakdown_filter))
39 .collect::<Vec<_>>();
40 let total_size_under_filter: u64 = allocations.iter().map(|a| a.1.size).sum();
41
42 ui.label(format!("Total: {}", fmt_bytes(total_size_under_filter)));
43
44 let row_height = ui.text_style_height(&egui::TextStyle::Body);
45 let table = TableBuilder::new(ui)
46 .striped(true)
47 .resizable(true)
48 .column(Column::exact(30.0))
49 .column(Column::initial(300.0).at_least(200.0).clip(true))
50 .column(Column::exact(70.0));
51
52 fn header_button(ui: &mut Ui, label: &str) -> Response {
53 let label = WidgetText::from(label).strong();
54 let label = Label::new(label).sense(Sense::click());
55 ui.add(label)
56 }
57
58 let table = table.header(row_height, |mut row| {
59 row.col(|ui| {
60 if header_button(ui, "Idx").clicked() {
61 if settings.sorting == AllocationReportVisualizeSorting::Idx {
62 settings.ascending = !settings.ascending;
63 } else {
64 settings.sorting = AllocationReportVisualizeSorting::Idx;
65 settings.ascending = false;
66 }
67 }
68 });
69 row.col(|ui| {
70 if header_button(ui, "Name").clicked() {
71 if settings.sorting == AllocationReportVisualizeSorting::Name {
72 settings.ascending = !settings.ascending;
73 } else {
74 settings.sorting = AllocationReportVisualizeSorting::Name;
75 settings.ascending = false;
76 }
77 }
78 });
79 row.col(|ui| {
80 if header_button(ui, "Size").clicked() {
81 if settings.sorting == AllocationReportVisualizeSorting::Size {
82 settings.ascending = !settings.ascending;
83 } else {
84 settings.sorting = AllocationReportVisualizeSorting::Size;
85 settings.ascending = false;
86 }
87 }
88 });
89 });
90
91 match (settings.sorting, settings.ascending) {
92 (AllocationReportVisualizeSorting::None, _) => {}
93 (AllocationReportVisualizeSorting::Idx, true) => allocations.sort_by_key(|(idx, _)| *idx),
94 (AllocationReportVisualizeSorting::Idx, false) => {
95 allocations.sort_by_key(|(idx, _)| std::cmp::Reverse(*idx))
96 }
97 (AllocationReportVisualizeSorting::Name, true) => {
98 allocations.sort_by(|(_, alloc1), (_, alloc2)| alloc1.name.cmp(&alloc2.name))
99 }
100 (AllocationReportVisualizeSorting::Name, false) => {
101 allocations.sort_by(|(_, alloc1), (_, alloc2)| alloc1.name.cmp(&alloc2.name).reverse())
102 }
103 (AllocationReportVisualizeSorting::Size, true) => {
104 allocations.sort_by_key(|(_, alloc)| alloc.size)
105 }
106 (AllocationReportVisualizeSorting::Size, false) => {
107 allocations.sort_by_key(|(_, alloc)| std::cmp::Reverse(alloc.size))
108 }
109 }
110
111 table.body(|mut body| {
112 for (idx, alloc) in allocations {
113 body.row(row_height, |mut row| {
114 let AllocationReport {
115 name,
116 size,
117 backtrace,
118 ..
119 } = alloc;
120
121 row.col(|ui| {
122 ui.label(idx.to_string());
123 });
124
125 let resp = row.col(|ui| {
126 ui.label(name);
127 });
128
129 if backtrace.status() == BacktraceStatus::Captured {
130 resp.1.on_hover_ui(|ui| {
131 ui.label(backtrace.to_string());
132 });
133 }
134
135 row.col(|ui| {
136 ui.label(fmt_bytes(size));
137 });
138 });
139 }
140 });
141 }