MemoryStatsWidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "MemoryStatsWidget.h"
  27. #include "GraphWidget.h"
  28. #include <AK/JsonObject.h>
  29. #include <LibCore/File.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGfx/Font.h>
  34. #include <LibGfx/StylePainter.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. static MemoryStatsWidget* s_the;
  38. MemoryStatsWidget* MemoryStatsWidget::the()
  39. {
  40. return s_the;
  41. }
  42. MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GUI::Widget* parent)
  43. : GUI::Widget(parent)
  44. , m_graph(graph)
  45. {
  46. ASSERT(!s_the);
  47. s_the = this;
  48. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  49. set_preferred_size(0, 72);
  50. set_layout(make<GUI::VerticalBoxLayout>());
  51. layout()->set_margins({ 0, 8, 0, 0 });
  52. layout()->set_spacing(3);
  53. auto build_widgets_for_label = [this](const String& description) -> RefPtr<GUI::Label> {
  54. auto container = GUI::Widget::construct(this);
  55. container->set_layout(make<GUI::HorizontalBoxLayout>());
  56. container->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  57. container->set_preferred_size(275, 12);
  58. auto description_label = GUI::Label::construct(description, container);
  59. description_label->set_font(Gfx::Font::default_bold_font());
  60. description_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  61. auto label = GUI::Label::construct(container);
  62. label->set_text_alignment(Gfx::TextAlignment::CenterRight);
  63. return label;
  64. };
  65. m_user_physical_pages_label = build_widgets_for_label("Userspace physical:");
  66. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  67. m_kmalloc_label = build_widgets_for_label("Kernel heap:");
  68. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc/kfree:");
  69. refresh();
  70. }
  71. MemoryStatsWidget::~MemoryStatsWidget()
  72. {
  73. }
  74. static inline size_t page_count_to_kb(size_t kb)
  75. {
  76. return (kb * 4096) / 1024;
  77. }
  78. static inline size_t bytes_to_kb(size_t bytes)
  79. {
  80. return bytes / 1024;
  81. }
  82. void MemoryStatsWidget::refresh()
  83. {
  84. auto proc_memstat = Core::File::construct("/proc/memstat");
  85. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  86. ASSERT_NOT_REACHED();
  87. auto file_contents = proc_memstat->read_all();
  88. auto json = JsonValue::from_string(file_contents).as_object();
  89. unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  90. (void)kmalloc_eternal_allocated;
  91. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  92. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  93. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u32();
  94. unsigned user_physical_available = json.get("user_physical_available").to_u32();
  95. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u32();
  96. unsigned super_physical_free = json.get("super_physical_available").to_u32();
  97. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  98. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  99. size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
  100. size_t user_pages_available = user_physical_allocated + user_physical_available;
  101. size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
  102. m_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
  103. 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)));
  104. 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)));
  105. m_kmalloc_count_label->set_text(String::format("%u/%u (+%u)", kmalloc_call_count, kfree_call_count, kmalloc_call_count - kfree_call_count));
  106. m_graph.set_max(page_count_to_kb(user_pages_available));
  107. m_graph.add_value(page_count_to_kb(user_physical_allocated));
  108. }