From abfddd01d406df763a110a14cfd84eb069cd2125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sat, 19 Mar 2022 00:19:56 +0100 Subject: [PATCH] SystemMonitor: Register MemoryStatsWidget This also requires that the associated graph widget may be null. --- .../SystemMonitor/MemoryStatsWidget.cpp | 24 ++++++++++++++++--- .../SystemMonitor/MemoryStatsWidget.h | 11 ++++++--- Userland/Applications/SystemMonitor/main.cpp | 4 ++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp b/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp index 30d2505d87c..22678d23a4b 100644 --- a/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp +++ b/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp @@ -17,6 +17,10 @@ #include #include +REGISTER_WIDGET(SystemMonitor, MemoryStatsWidget) + +namespace SystemMonitor { + static MemoryStatsWidget* s_the; MemoryStatsWidget* MemoryStatsWidget::the() @@ -24,7 +28,12 @@ MemoryStatsWidget* MemoryStatsWidget::the() return s_the; } -MemoryStatsWidget::MemoryStatsWidget(SystemMonitor::GraphWidget& graph) +MemoryStatsWidget::MemoryStatsWidget() + : MemoryStatsWidget(nullptr) +{ +} + +MemoryStatsWidget::MemoryStatsWidget(GraphWidget* graph) : m_graph(graph) { VERIFY(!s_the); @@ -59,6 +68,11 @@ MemoryStatsWidget::MemoryStatsWidget(SystemMonitor::GraphWidget& graph) refresh(); } +void MemoryStatsWidget::set_graph_widget(GraphWidget& graph) +{ + m_graph = &graph; +} + static inline u64 page_count_to_bytes(size_t count) { return count * 4096; @@ -101,6 +115,10 @@ void MemoryStatsWidget::refresh() m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count)); m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count)); - m_graph.set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total); - m_graph.add_value({ page_count_to_bytes(user_physical_committed), page_count_to_bytes(user_physical_allocated), kmalloc_bytes_total }); + if (m_graph) { + m_graph->set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total); + m_graph->add_value({ page_count_to_bytes(user_physical_committed), page_count_to_bytes(user_physical_allocated), kmalloc_bytes_total }); + } +} + } diff --git a/Userland/Applications/SystemMonitor/MemoryStatsWidget.h b/Userland/Applications/SystemMonitor/MemoryStatsWidget.h index efe972e9a33..f0c005a8e1b 100644 --- a/Userland/Applications/SystemMonitor/MemoryStatsWidget.h +++ b/Userland/Applications/SystemMonitor/MemoryStatsWidget.h @@ -10,8 +10,8 @@ #include namespace SystemMonitor { + class GraphWidget; -} class MemoryStatsWidget final : public GUI::Widget { C_OBJECT(MemoryStatsWidget) @@ -20,12 +20,15 @@ public: virtual ~MemoryStatsWidget() override = default; + void set_graph_widget(GraphWidget& graph); + void refresh(); private: - MemoryStatsWidget(SystemMonitor::GraphWidget& graph); + MemoryStatsWidget(GraphWidget* graph); + MemoryStatsWidget(); - SystemMonitor::GraphWidget& m_graph; + GraphWidget* m_graph; RefPtr m_user_physical_pages_label; RefPtr m_user_physical_pages_committed_label; RefPtr m_supervisor_physical_pages_label; @@ -34,3 +37,5 @@ private: RefPtr m_kfree_count_label; RefPtr m_kmalloc_difference_label; }; + +} diff --git a/Userland/Applications/SystemMonitor/main.cpp b/Userland/Applications/SystemMonitor/main.cpp index 4e8976b33d7..db30e4688d5 100644 --- a/Userland/Applications/SystemMonitor/main.cpp +++ b/Userland/Applications/SystemMonitor/main.cpp @@ -212,7 +212,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto& refresh_timer = window->add( frequency * 1000, [&] { process_model->update(); - if (auto* memory_stats_widget = MemoryStatsWidget::the()) + if (auto* memory_stats_widget = SystemMonitor::MemoryStatsWidget::the()) memory_stats_widget->refresh(); }); @@ -746,6 +746,6 @@ NonnullRefPtr build_performance_tab() }, }); - graphs_container->add(memory_graph); + graphs_container->add(&memory_graph); return graphs_container; }