MemoryStatsWidget.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. {
  11. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  12. set_preferred_size({ 0, 72 });
  13. set_layout(make<GBoxLayout>(Orientation::Vertical));
  14. layout()->set_margins({ 0, 8, 0, 0 });
  15. layout()->set_spacing(3);
  16. auto build_widgets_for_label = [this] (const String& description) -> GLabel* {
  17. auto* container = new GWidget(this);
  18. container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
  19. container->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  20. container->set_preferred_size({ 255, 12 });
  21. auto* description_label = new GLabel(description, container);
  22. description_label->set_font(Font::default_bold_font());
  23. description_label->set_text_alignment(TextAlignment::CenterLeft);
  24. auto* label = new GLabel(container);
  25. label->set_text_alignment(TextAlignment::CenterRight);
  26. return label;
  27. };
  28. m_user_physical_pages_label = build_widgets_for_label("Userspace physical:");
  29. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  30. m_kmalloc_label = build_widgets_for_label("Kernel heap:");
  31. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc/kfree:");
  32. start_timer(1000);
  33. refresh();
  34. }
  35. MemoryStatsWidget::~MemoryStatsWidget()
  36. {
  37. }
  38. static inline size_t page_count_to_kb(size_t kb)
  39. {
  40. return (kb * 4096) / 1024;
  41. }
  42. static inline size_t bytes_to_kb(size_t bytes)
  43. {
  44. return bytes / 1024;
  45. }
  46. void MemoryStatsWidget::refresh()
  47. {
  48. FILE* fp = fopen("/proc/memstat", "r");
  49. if (!fp) {
  50. perror("failed to open /proc/memstat");
  51. exit(1);
  52. }
  53. for (;;) {
  54. char buf[BUFSIZ];
  55. char* ptr = fgets(buf, sizeof(buf), fp);
  56. if (!ptr)
  57. break;
  58. auto parts = String(buf, Chomp).split(',');
  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. break;
  89. }
  90. fclose(fp);
  91. }
  92. void MemoryStatsWidget::timer_event(CTimerEvent&)
  93. {
  94. refresh();
  95. }
  96. void MemoryStatsWidget::paint_event(GPaintEvent& event)
  97. {
  98. GPainter painter(*this);
  99. painter.add_clip_rect(event.rect());
  100. StylePainter::paint_surface(painter, rect());
  101. }