MemoryStatsWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "MemoryStatsWidget.h"
  7. #include "GraphWidget.h"
  8. #include <AK/JsonObject.h>
  9. #include <LibCore/File.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGfx/FontDatabase.h>
  14. #include <LibGfx/StylePainter.h>
  15. #include <stdlib.h>
  16. static MemoryStatsWidget* s_the;
  17. MemoryStatsWidget* MemoryStatsWidget::the()
  18. {
  19. return s_the;
  20. }
  21. MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph)
  22. : m_graph(graph)
  23. {
  24. VERIFY(!s_the);
  25. s_the = this;
  26. set_fixed_height(110);
  27. set_layout<GUI::VerticalBoxLayout>();
  28. layout()->set_margins({ 8, 0, 0 });
  29. layout()->set_spacing(3);
  30. auto build_widgets_for_label = [this](const String& description) -> RefPtr<GUI::Label> {
  31. auto& container = add<GUI::Widget>();
  32. container.set_layout<GUI::HorizontalBoxLayout>();
  33. container.set_fixed_size(275, 12);
  34. auto& description_label = container.add<GUI::Label>(description);
  35. description_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
  36. description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  37. auto& label = container.add<GUI::Label>();
  38. label.set_text_alignment(Gfx::TextAlignment::CenterRight);
  39. return label;
  40. };
  41. m_user_physical_pages_label = build_widgets_for_label("Physical memory:");
  42. m_user_physical_pages_committed_label = build_widgets_for_label("Committed memory:");
  43. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  44. m_kmalloc_space_label = build_widgets_for_label("Kernel heap:");
  45. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:");
  46. m_kfree_count_label = build_widgets_for_label("Calls kfree:");
  47. m_kmalloc_difference_label = build_widgets_for_label("Difference:");
  48. refresh();
  49. }
  50. MemoryStatsWidget::~MemoryStatsWidget()
  51. {
  52. }
  53. static inline u64 page_count_to_kb(u64 kb)
  54. {
  55. return (kb * 4096) / 1024;
  56. }
  57. static inline u64 bytes_to_kb(u64 bytes)
  58. {
  59. return bytes / 1024;
  60. }
  61. void MemoryStatsWidget::refresh()
  62. {
  63. auto proc_memstat = Core::File::construct("/proc/memstat");
  64. if (!proc_memstat->open(Core::OpenMode::ReadOnly))
  65. VERIFY_NOT_REACHED();
  66. auto file_contents = proc_memstat->read_all();
  67. auto json_result = JsonValue::from_string(file_contents);
  68. VERIFY(json_result.has_value());
  69. auto json = json_result.value().as_object();
  70. [[maybe_unused]] unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  71. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  72. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  73. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u64();
  74. unsigned user_physical_available = json.get("user_physical_available").to_u64();
  75. unsigned user_physical_committed = json.get("user_physical_committed").to_u64();
  76. unsigned user_physical_uncommitted = json.get("user_physical_uncommitted").to_u64();
  77. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u64();
  78. unsigned super_physical_free = json.get("super_physical_available").to_u64();
  79. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  80. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  81. size_t kmalloc_bytes_total = kmalloc_allocated + kmalloc_available;
  82. size_t user_physical_pages_total = user_physical_allocated + user_physical_available;
  83. size_t supervisor_pages_total = super_physical_alloc + super_physical_free;
  84. size_t physical_pages_total = user_physical_pages_total + supervisor_pages_total;
  85. size_t physical_pages_in_use = user_physical_allocated + super_physical_alloc;
  86. size_t total_userphysical_and_swappable_pages = user_physical_allocated + user_physical_committed + user_physical_uncommitted;
  87. m_kmalloc_space_label->set_text(String::formatted("{}K/{}K", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_bytes_total)));
  88. m_user_physical_pages_label->set_text(String::formatted("{}K/{}K", page_count_to_kb(physical_pages_in_use), page_count_to_kb(physical_pages_total)));
  89. m_user_physical_pages_committed_label->set_text(String::formatted("{}K", page_count_to_kb(user_physical_committed)));
  90. m_supervisor_physical_pages_label->set_text(String::formatted("{}K/{}K", page_count_to_kb(super_physical_alloc), page_count_to_kb(supervisor_pages_total)));
  91. m_kmalloc_count_label->set_text(String::formatted("{}", kmalloc_call_count));
  92. m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count));
  93. m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count));
  94. m_graph.set_max(page_count_to_kb(total_userphysical_and_swappable_pages) + bytes_to_kb(kmalloc_bytes_total));
  95. m_graph.add_value({ (int)page_count_to_kb(user_physical_committed), (int)page_count_to_kb(user_physical_allocated), (int)bytes_to_kb(kmalloc_bytes_total) });
  96. }