MemoryStatsWidget.cpp 6.4 KB

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