MemoryStatsWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "MemoryStatsWidget.h"
  2. #include "GraphWidget.h"
  3. #include <AK/JsonObject.h>
  4. #include <LibCore/CFile.h>
  5. #include <LibDraw/StylePainter.h>
  6. #include <LibGUI/GBoxLayout.h>
  7. #include <LibGUI/GLabel.h>
  8. #include <LibGUI/GPainter.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. static MemoryStatsWidget* s_the;
  12. MemoryStatsWidget* MemoryStatsWidget::the()
  13. {
  14. return s_the;
  15. }
  16. MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent)
  17. : GWidget(parent)
  18. , m_graph(graph)
  19. {
  20. ASSERT(!s_the);
  21. s_the = this;
  22. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  23. set_preferred_size(0, 72);
  24. set_layout(make<GBoxLayout>(Orientation::Vertical));
  25. layout()->set_margins({ 0, 8, 0, 0 });
  26. layout()->set_spacing(3);
  27. auto build_widgets_for_label = [this](const String& description) -> RefPtr<GLabel> {
  28. auto container = GWidget::construct(this);
  29. container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  30. container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  31. container->set_preferred_size(275, 12);
  32. auto description_label = GLabel::construct(description, container);
  33. description_label->set_font(Font::default_bold_font());
  34. description_label->set_text_alignment(TextAlignment::CenterLeft);
  35. auto label = GLabel::construct(container);
  36. label->set_text_alignment(TextAlignment::CenterRight);
  37. return label;
  38. };
  39. m_user_physical_pages_label = build_widgets_for_label("Userspace physical:");
  40. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  41. m_kmalloc_label = build_widgets_for_label("Kernel heap:");
  42. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc/kfree:");
  43. refresh();
  44. }
  45. MemoryStatsWidget::~MemoryStatsWidget()
  46. {
  47. }
  48. static inline size_t page_count_to_kb(size_t kb)
  49. {
  50. return (kb * 4096) / 1024;
  51. }
  52. static inline size_t bytes_to_kb(size_t bytes)
  53. {
  54. return bytes / 1024;
  55. }
  56. void MemoryStatsWidget::refresh()
  57. {
  58. auto proc_memstat = CFile::construct("/proc/memstat");
  59. if (!proc_memstat->open(CIODevice::OpenMode::ReadOnly))
  60. ASSERT_NOT_REACHED();
  61. auto file_contents = proc_memstat->read_all();
  62. auto json = JsonValue::from_string(file_contents).as_object();
  63. unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  64. (void)kmalloc_eternal_allocated;
  65. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  66. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  67. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u32();
  68. unsigned user_physical_available = json.get("user_physical_available").to_u32();
  69. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u32();
  70. unsigned super_physical_free = json.get("super_physical_available").to_u32();
  71. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  72. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  73. size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
  74. size_t user_pages_available = user_physical_allocated + user_physical_available;
  75. size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
  76. m_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
  77. m_user_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(user_physical_allocated), page_count_to_kb(user_pages_available)));
  78. m_supervisor_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(super_physical_alloc), page_count_to_kb(supervisor_pages_available)));
  79. m_kmalloc_count_label->set_text(String::format("%u/%u (+%u)", kmalloc_call_count, kfree_call_count, kmalloc_call_count - kfree_call_count));
  80. m_graph.set_max(page_count_to_kb(user_pages_available));
  81. m_graph.add_value(page_count_to_kb(user_physical_allocated));
  82. }