MemoryStatsWidget.cpp 5.0 KB

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