From a7d9d416d565921104b79ca09ae8744ce30a1fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 23 Mar 2022 01:28:28 +0100 Subject: [PATCH] SystemMonitor: Register a graph_widget pseudo-property in MemoryStatsW The property graph_widget on MemoryStatsWidget is a-pseudo property that specifies the name of the graph widget which should be attached to the MemoryStatsWidget. When the property is set, the widget looks up the graph with that name in its parent, therefore automatically linking to the correct widget given that it's a sibling or descendant of a sibling. --- .../SystemMonitor/MemoryStatsWidget.cpp | 31 +++++++++++++++++++ .../SystemMonitor/MemoryStatsWidget.h | 5 +++ 2 files changed, 36 insertions(+) diff --git a/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp b/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp index 22678d23a4..c33bf14816 100644 --- a/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp +++ b/Userland/Applications/SystemMonitor/MemoryStatsWidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,8 @@ MemoryStatsWidget::MemoryStatsWidget(GraphWidget* graph) VERIFY(!s_the); s_the = this; + REGISTER_STRING_PROPERTY("memory_graph", graph_widget_name, set_graph_widget_via_name); + set_fixed_height(110); set_layout(); @@ -73,6 +76,29 @@ void MemoryStatsWidget::set_graph_widget(GraphWidget& graph) m_graph = &graph; } +void MemoryStatsWidget::set_graph_widget_via_name(String name) +{ + m_graph_widget_name = move(name); + if (!m_graph_widget_name.is_null()) { + // FIXME: We assume here that the graph widget is a sibling or descendant of a sibling. This prevents more complex hierarchies. + auto* maybe_graph = parent_widget()->find_descendant_of_type_named(m_graph_widget_name); + if (maybe_graph) { + m_graph = maybe_graph; + // Delete the stored graph name to signal that we found the widget + m_graph_widget_name = {}; + } else { + dbgln("MemoryStatsWidget: Couldn't find graph of name '{}', retrying later.", m_graph_widget_name); + } + } +} + +String MemoryStatsWidget::graph_widget_name() +{ + if (m_graph) + return m_graph->name(); + return m_graph_widget_name; +} + static inline u64 page_count_to_bytes(size_t count) { return count * 4096; @@ -115,6 +141,11 @@ 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)); + // Because the initialization order of us and the graph is unknown, we might get a couple of updates where the graph widget lookup fails. + // Therefore, we can retry indefinitely. (Should not be too much of a performance hit, as we don't update that often.) + if (!m_graph) + set_graph_widget_via_name(move(m_graph_widget_name)); + 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 f0c005a8e1..c384a5c74f 100644 --- a/Userland/Applications/SystemMonitor/MemoryStatsWidget.h +++ b/Userland/Applications/SystemMonitor/MemoryStatsWidget.h @@ -22,6 +22,9 @@ public: void set_graph_widget(GraphWidget& graph); + void set_graph_widget_via_name(String name); + String graph_widget_name(); + void refresh(); private: @@ -29,6 +32,8 @@ private: MemoryStatsWidget(); GraphWidget* m_graph; + // Is null if we have a valid graph + String m_graph_widget_name {}; RefPtr m_user_physical_pages_label; RefPtr m_user_physical_pages_committed_label; RefPtr m_supervisor_physical_pages_label;