MemoryStatsWidget.cpp 5.8 KB

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