MemoryStatsWidget.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGfx/FontDatabase.h>
  16. #include <LibGfx/StylePainter.h>
  17. #include <stdlib.h>
  18. static MemoryStatsWidget* s_the;
  19. MemoryStatsWidget* MemoryStatsWidget::the()
  20. {
  21. return s_the;
  22. }
  23. MemoryStatsWidget::MemoryStatsWidget(SystemMonitor::GraphWidget& graph)
  24. : m_graph(graph)
  25. {
  26. VERIFY(!s_the);
  27. s_the = this;
  28. set_fixed_height(110);
  29. set_layout<GUI::VerticalBoxLayout>();
  30. layout()->set_margins({ 8, 0, 0 });
  31. layout()->set_spacing(3);
  32. auto build_widgets_for_label = [this](String const& description) -> RefPtr<GUI::Label> {
  33. auto& container = add<GUI::Widget>();
  34. container.set_layout<GUI::HorizontalBoxLayout>();
  35. container.set_fixed_size(275, 12);
  36. auto& description_label = container.add<GUI::Label>(description);
  37. description_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
  38. description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  39. auto& label = container.add<GUI::Label>();
  40. label.set_text_alignment(Gfx::TextAlignment::CenterRight);
  41. return label;
  42. };
  43. m_user_physical_pages_label = build_widgets_for_label("Physical memory:");
  44. m_user_physical_pages_committed_label = build_widgets_for_label("Committed memory:");
  45. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  46. m_kmalloc_space_label = build_widgets_for_label("Kernel heap:");
  47. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:");
  48. m_kfree_count_label = build_widgets_for_label("Calls kfree:");
  49. m_kmalloc_difference_label = build_widgets_for_label("Difference:");
  50. refresh();
  51. }
  52. static inline u64 page_count_to_bytes(size_t count)
  53. {
  54. return count * 4096;
  55. }
  56. void MemoryStatsWidget::refresh()
  57. {
  58. auto proc_memstat = Core::File::construct("/proc/memstat");
  59. if (!proc_memstat->open(Core::OpenMode::ReadOnly))
  60. VERIFY_NOT_REACHED();
  61. auto file_contents = proc_memstat->read_all();
  62. auto json_result = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
  63. auto const& json = json_result.as_object();
  64. u32 kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  65. u32 kmalloc_available = json.get("kmalloc_available").to_u32();
  66. u64 user_physical_allocated = json.get("user_physical_allocated").to_u64();
  67. u64 user_physical_available = json.get("user_physical_available").to_u64();
  68. u64 user_physical_committed = json.get("user_physical_committed").to_u64();
  69. u64 user_physical_uncommitted = json.get("user_physical_uncommitted").to_u64();
  70. u64 super_physical_alloc = json.get("super_physical_allocated").to_u64();
  71. u64 super_physical_free = json.get("super_physical_available").to_u64();
  72. u32 kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  73. u32 kfree_call_count = json.get("kfree_call_count").to_u32();
  74. u64 kmalloc_bytes_total = kmalloc_allocated + kmalloc_available;
  75. u64 user_physical_pages_total = user_physical_allocated + user_physical_available;
  76. u64 supervisor_pages_total = super_physical_alloc + super_physical_free;
  77. u64 physical_pages_total = user_physical_pages_total + supervisor_pages_total;
  78. u64 physical_pages_in_use = user_physical_allocated + super_physical_alloc;
  79. u64 total_userphysical_and_swappable_pages = user_physical_allocated + user_physical_committed + user_physical_uncommitted;
  80. m_kmalloc_space_label->set_text(String::formatted("{}/{}", human_readable_size(kmalloc_allocated), human_readable_size(kmalloc_bytes_total)));
  81. 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))));
  82. m_user_physical_pages_committed_label->set_text(String::formatted("{}", human_readable_size(page_count_to_bytes(user_physical_committed))));
  83. 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))));
  84. m_kmalloc_count_label->set_text(String::formatted("{}", kmalloc_call_count));
  85. m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count));
  86. m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count));
  87. m_graph.set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total);
  88. m_graph.add_value({ page_count_to_bytes(user_physical_committed), page_count_to_bytes(user_physical_allocated), kmalloc_bytes_total });
  89. }