MemoryStatsWidget.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "MemoryStatsWidget.h"
  8. #include "GraphWidget.h"
  9. #include <AK/JsonObject.h>
  10. #include <AK/NumberFormat.h>
  11. #include <LibCore/EventReceiver.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGfx/Font/FontDatabase.h>
  16. #include <LibGfx/StylePainter.h>
  17. REGISTER_WIDGET(SystemMonitor, MemoryStatsWidget)
  18. namespace SystemMonitor {
  19. static MemoryStatsWidget* s_the;
  20. MemoryStatsWidget* MemoryStatsWidget::the()
  21. {
  22. return s_the;
  23. }
  24. MemoryStatsWidget::MemoryStatsWidget()
  25. : MemoryStatsWidget(nullptr)
  26. {
  27. }
  28. MemoryStatsWidget::MemoryStatsWidget(GraphWidget* graph)
  29. : m_graph(graph)
  30. {
  31. VERIFY(!s_the);
  32. s_the = this;
  33. REGISTER_DEPRECATED_STRING_PROPERTY("memory_graph", graph_widget_name, set_graph_widget_via_name);
  34. set_fixed_height(110);
  35. set_layout<GUI::VerticalBoxLayout>(GUI::Margins { 8, 0, 0 }, 3);
  36. auto build_widgets_for_label = [this](String const& description) -> RefPtr<GUI::Label> {
  37. auto& container = add<GUI::Widget>();
  38. container.set_layout<GUI::HorizontalBoxLayout>();
  39. container.set_fixed_size(275, 12);
  40. auto& description_label = container.add<GUI::Label>(description);
  41. description_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
  42. description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  43. auto& label = container.add<GUI::Label>();
  44. label.set_text_alignment(Gfx::TextAlignment::CenterRight);
  45. return label;
  46. };
  47. m_physical_pages_label = build_widgets_for_label("Physical memory:"_string);
  48. m_physical_pages_committed_label = build_widgets_for_label("Committed memory:"_string);
  49. m_kmalloc_space_label = build_widgets_for_label("Kernel heap:"_string);
  50. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:"_string);
  51. m_kfree_count_label = build_widgets_for_label("Calls kfree:"_string);
  52. m_kmalloc_difference_label = build_widgets_for_label("Difference:"_string);
  53. refresh();
  54. }
  55. void MemoryStatsWidget::set_graph_widget(GraphWidget& graph)
  56. {
  57. m_graph = &graph;
  58. }
  59. void MemoryStatsWidget::set_graph_widget_via_name(DeprecatedString name)
  60. {
  61. m_graph_widget_name = move(name);
  62. if (!m_graph_widget_name.is_empty()) {
  63. // FIXME: We assume here that the graph widget is a sibling or descendant of a sibling. This prevents more complex hierarchies.
  64. auto* maybe_graph = parent_widget()->find_descendant_of_type_named<GraphWidget>(m_graph_widget_name);
  65. if (maybe_graph) {
  66. m_graph = maybe_graph;
  67. // Delete the stored graph name to signal that we found the widget
  68. m_graph_widget_name = {};
  69. } else {
  70. dbgln("MemoryStatsWidget: Couldn't find graph of name '{}', retrying later.", m_graph_widget_name);
  71. }
  72. }
  73. }
  74. DeprecatedString MemoryStatsWidget::graph_widget_name()
  75. {
  76. if (m_graph)
  77. return m_graph->name();
  78. return m_graph_widget_name;
  79. }
  80. static inline u64 page_count_to_bytes(size_t count)
  81. {
  82. return count * 4096;
  83. }
  84. void MemoryStatsWidget::refresh()
  85. {
  86. auto proc_memstat = Core::File::open("/sys/kernel/memstat"sv, Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors();
  87. auto file_contents = proc_memstat->read_until_eof().release_value_but_fixme_should_propagate_errors();
  88. auto json_result = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
  89. auto const& json = json_result.as_object();
  90. u32 kmalloc_allocated = json.get_u32("kmalloc_allocated"sv).value_or(0);
  91. u32 kmalloc_available = json.get_u32("kmalloc_available"sv).value_or(0);
  92. u64 physical_allocated = json.get_u64("physical_allocated"sv).value_or(0);
  93. u64 physical_available = json.get_u64("physical_available"sv).value_or(0);
  94. u64 physical_committed = json.get_u64("physical_committed"sv).value_or(0);
  95. u64 physical_uncommitted = json.get_u64("physical_uncommitted"sv).value_or(0);
  96. u32 kmalloc_call_count = json.get_u32("kmalloc_call_count"sv).value_or(0);
  97. u32 kfree_call_count = json.get_u32("kfree_call_count"sv).value_or(0);
  98. u64 kmalloc_bytes_total = kmalloc_allocated + kmalloc_available;
  99. u64 physical_pages_total = physical_allocated + physical_available;
  100. u64 physical_pages_in_use = physical_allocated;
  101. u64 total_userphysical_and_swappable_pages = physical_allocated + physical_committed + physical_uncommitted;
  102. m_kmalloc_space_label->set_text(String::formatted("{}/{}", human_readable_size(kmalloc_allocated), human_readable_size(kmalloc_bytes_total)).release_value_but_fixme_should_propagate_errors());
  103. m_physical_pages_label->set_text(String::formatted("{}/{}", human_readable_size(page_count_to_bytes(physical_pages_in_use)), human_readable_size(page_count_to_bytes(physical_pages_total))).release_value_but_fixme_should_propagate_errors());
  104. m_physical_pages_committed_label->set_text(String::formatted("{}", human_readable_size(page_count_to_bytes(physical_committed))).release_value_but_fixme_should_propagate_errors());
  105. m_kmalloc_count_label->set_text(String::formatted("{}", kmalloc_call_count).release_value_but_fixme_should_propagate_errors());
  106. m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count).release_value_but_fixme_should_propagate_errors());
  107. m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count).release_value_but_fixme_should_propagate_errors());
  108. // Because the initialization order of us and the graph is unknown, we might get a couple of updates where the graph widget lookup fails.
  109. // Therefore, we can retry indefinitely. (Should not be too much of a performance hit, as we don't update that often.)
  110. if (!m_graph)
  111. set_graph_widget_via_name(move(m_graph_widget_name));
  112. if (m_graph) {
  113. m_graph->set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total);
  114. m_graph->add_value({ page_count_to_bytes(physical_committed), page_count_to_bytes(physical_allocated), kmalloc_bytes_total });
  115. }
  116. }
  117. }