MemoryStatsWidget.cpp 5.1 KB

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