MemoryStatsWidget.cpp 4.0 KB

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