MemoryStatsWidget.cpp 3.8 KB

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