MemoryStatsWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "MemoryStatsWidget.h"
  2. #include <LibGUI/GPainter.h>
  3. #include <LibGUI/GBoxLayout.h>
  4. #include <LibGUI/GLabel.h>
  5. #include <SharedGraphics/StylePainter.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. MemoryStatsWidget::MemoryStatsWidget(GWidget* parent)
  9. : GWidget(parent)
  10. , m_proc_memstat("/proc/memstat")
  11. {
  12. if (!m_proc_memstat.open(CIODevice::OpenMode::ReadOnly))
  13. ASSERT_NOT_REACHED();
  14. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  15. set_preferred_size({ 0, 72 });
  16. set_layout(make<GBoxLayout>(Orientation::Vertical));
  17. layout()->set_margins({ 0, 8, 0, 0 });
  18. layout()->set_spacing(3);
  19. auto build_widgets_for_label = [this] (const String& description) -> GLabel* {
  20. auto* container = new GWidget(this);
  21. container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  22. container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  23. container->set_preferred_size({ 255, 12 });
  24. auto* description_label = new GLabel(description, container);
  25. description_label->set_font(Font::default_bold_font());
  26. description_label->set_text_alignment(TextAlignment::CenterLeft);
  27. auto* label = new GLabel(container);
  28. label->set_text_alignment(TextAlignment::CenterRight);
  29. return label;
  30. };
  31. m_user_physical_pages_label = build_widgets_for_label("Userspace physical:");
  32. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  33. m_kmalloc_label = build_widgets_for_label("Kernel heap:");
  34. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc/kfree:");
  35. refresh();
  36. }
  37. MemoryStatsWidget::~MemoryStatsWidget()
  38. {
  39. }
  40. static inline size_t page_count_to_kb(size_t kb)
  41. {
  42. return (kb * 4096) / 1024;
  43. }
  44. static inline size_t bytes_to_kb(size_t bytes)
  45. {
  46. return bytes / 1024;
  47. }
  48. void MemoryStatsWidget::refresh()
  49. {
  50. m_proc_memstat.seek(0);
  51. for (;;) {
  52. auto line = m_proc_memstat.read_line(BUFSIZ);
  53. if (line.is_null())
  54. break;
  55. auto parts = String::from_byte_buffer(line, Chomp).split(',');
  56. if (parts.size() < 9)
  57. break;
  58. bool ok;
  59. unsigned kmalloc_sum_eternal = parts[0].to_uint(ok);
  60. ASSERT(ok);
  61. (void)kmalloc_sum_eternal;
  62. unsigned kmalloc_sum_alloc = parts[1].to_uint(ok);
  63. ASSERT(ok);
  64. unsigned kmalloc_sum_free = parts[2].to_uint(ok);
  65. ASSERT(ok);
  66. unsigned user_pages_alloc = parts[3].to_uint(ok);
  67. ASSERT(ok);
  68. unsigned user_pages_free = parts[4].to_uint(ok);
  69. ASSERT(ok);
  70. unsigned supervisor_pages_alloc = parts[5].to_uint(ok);
  71. ASSERT(ok);
  72. unsigned supervisor_pages_free = parts[6].to_uint(ok);
  73. ASSERT(ok);
  74. unsigned kmalloc_call_count = parts[7].to_uint(ok);
  75. ASSERT(ok);
  76. unsigned kfree_call_count = parts[8].to_uint(ok);
  77. ASSERT(ok);
  78. size_t kmalloc_sum_available = kmalloc_sum_alloc + kmalloc_sum_free;
  79. size_t user_pages_available = user_pages_alloc + user_pages_free;
  80. size_t supervisor_pages_available = supervisor_pages_alloc + supervisor_pages_free;
  81. m_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_sum_alloc), bytes_to_kb(kmalloc_sum_available)));
  82. m_user_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(user_pages_alloc), page_count_to_kb(user_pages_available)));
  83. m_supervisor_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(supervisor_pages_alloc), page_count_to_kb(supervisor_pages_available)));
  84. m_kmalloc_count_label->set_text(String::format("%u/%u (+%u)", kmalloc_call_count, kfree_call_count, kmalloc_call_count - kfree_call_count));
  85. break;
  86. }
  87. }
  88. void MemoryStatsWidget::timer_event(CTimerEvent&)
  89. {
  90. refresh();
  91. }
  92. void MemoryStatsWidget::paint_event(GPaintEvent& event)
  93. {
  94. GPainter painter(*this);
  95. painter.add_clip_rect(event.rect());
  96. StylePainter::paint_surface(painter, rect());
  97. }