MemoryStatsWidget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, 60 });
  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({ 250, 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. start_timer(1000);
  32. refresh();
  33. }
  34. MemoryStatsWidget::~MemoryStatsWidget()
  35. {
  36. }
  37. static inline size_t page_count_to_kb(size_t kb)
  38. {
  39. return (kb * 4096) / 1024;
  40. }
  41. static inline size_t bytes_to_kb(size_t bytes)
  42. {
  43. return bytes / 1024;
  44. }
  45. void MemoryStatsWidget::refresh()
  46. {
  47. FILE* fp = fopen("/proc/memstat", "r");
  48. if (!fp) {
  49. perror("failed to open /proc/memstat");
  50. exit(1);
  51. }
  52. for (;;) {
  53. char buf[BUFSIZ];
  54. char* ptr = fgets(buf, sizeof(buf), fp);
  55. if (!ptr)
  56. break;
  57. auto parts = String(buf, Chomp).split(',');
  58. if (parts.size() < 7)
  59. break;
  60. bool ok;
  61. unsigned kmalloc_sum_eternal = parts[0].to_uint(ok);
  62. ASSERT(ok);
  63. (void)kmalloc_sum_eternal;
  64. unsigned kmalloc_sum_alloc = parts[1].to_uint(ok);
  65. ASSERT(ok);
  66. unsigned kmalloc_sum_free = parts[2].to_uint(ok);
  67. ASSERT(ok);
  68. unsigned user_pages_alloc = parts[3].to_uint(ok);
  69. ASSERT(ok);
  70. unsigned user_pages_free = parts[4].to_uint(ok);
  71. ASSERT(ok);
  72. unsigned supervisor_pages_alloc = parts[5].to_uint(ok);
  73. ASSERT(ok);
  74. unsigned supervisor_pages_free = parts[6].to_uint(ok);
  75. ASSERT(ok);
  76. size_t kmalloc_sum_available = kmalloc_sum_alloc + kmalloc_sum_free;
  77. size_t user_pages_available = user_pages_alloc + user_pages_free;
  78. size_t supervisor_pages_available = supervisor_pages_alloc + supervisor_pages_free;
  79. m_kmalloc_label->set_text(String::format("%uK/%uK\n", bytes_to_kb(kmalloc_sum_alloc), bytes_to_kb(kmalloc_sum_available)));
  80. m_user_physical_pages_label->set_text(String::format("%uK/%uK\n", page_count_to_kb(user_pages_alloc), page_count_to_kb(user_pages_available)));
  81. m_supervisor_physical_pages_label->set_text(String::format("%uK/%uK\n", page_count_to_kb(supervisor_pages_alloc), page_count_to_kb(supervisor_pages_available)));
  82. break;
  83. }
  84. fclose(fp);
  85. }
  86. void MemoryStatsWidget::timer_event(GTimerEvent&)
  87. {
  88. refresh();
  89. }
  90. void MemoryStatsWidget::paint_event(GPaintEvent& event)
  91. {
  92. GPainter painter(*this);
  93. painter.set_clip_rect(event.rect());
  94. StylePainter::the().paint_surface(painter, rect());
  95. }