MemoryStatsWidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/CFile.h>
  30. #include <LibDraw/StylePainter.h>
  31. #include <LibGUI/GBoxLayout.h>
  32. #include <LibGUI/GLabel.h>
  33. #include <LibGUI/GPainter.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. static MemoryStatsWidget* s_the;
  37. MemoryStatsWidget* MemoryStatsWidget::the()
  38. {
  39. return s_the;
  40. }
  41. MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GUI::Widget* parent)
  42. : GUI::Widget(parent)
  43. , m_graph(graph)
  44. {
  45. ASSERT(!s_the);
  46. s_the = this;
  47. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  48. set_preferred_size(0, 72);
  49. set_layout(make<GUI::VBoxLayout>());
  50. layout()->set_margins({ 0, 8, 0, 0 });
  51. layout()->set_spacing(3);
  52. auto build_widgets_for_label = [this](const String& description) -> RefPtr<GUI::Label> {
  53. auto container = GUI::Widget::construct(this);
  54. container->set_layout(make<GUI::HBoxLayout>());
  55. container->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  56. container->set_preferred_size(275, 12);
  57. auto description_label = GUI::Label::construct(description, container);
  58. description_label->set_font(Gfx::Font::default_bold_font());
  59. description_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  60. auto label = GUI::Label::construct(container);
  61. label->set_text_alignment(Gfx::TextAlignment::CenterRight);
  62. return label;
  63. };
  64. m_user_physical_pages_label = build_widgets_for_label("Userspace physical:");
  65. m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
  66. m_kmalloc_label = build_widgets_for_label("Kernel heap:");
  67. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc/kfree:");
  68. refresh();
  69. }
  70. MemoryStatsWidget::~MemoryStatsWidget()
  71. {
  72. }
  73. static inline size_t page_count_to_kb(size_t kb)
  74. {
  75. return (kb * 4096) / 1024;
  76. }
  77. static inline size_t bytes_to_kb(size_t bytes)
  78. {
  79. return bytes / 1024;
  80. }
  81. void MemoryStatsWidget::refresh()
  82. {
  83. auto proc_memstat = Core::File::construct("/proc/memstat");
  84. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  85. ASSERT_NOT_REACHED();
  86. auto file_contents = proc_memstat->read_all();
  87. auto json = JsonValue::from_string(file_contents).as_object();
  88. unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  89. (void)kmalloc_eternal_allocated;
  90. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  91. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  92. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u32();
  93. unsigned user_physical_available = json.get("user_physical_available").to_u32();
  94. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u32();
  95. unsigned super_physical_free = json.get("super_physical_available").to_u32();
  96. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  97. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  98. size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
  99. size_t user_pages_available = user_physical_allocated + user_physical_available;
  100. size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
  101. m_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
  102. 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)));
  103. 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)));
  104. m_kmalloc_count_label->set_text(String::format("%u/%u (+%u)", kmalloc_call_count, kfree_call_count, kmalloc_call_count - kfree_call_count));
  105. m_graph.set_max(page_count_to_kb(user_pages_available));
  106. m_graph.add_value(page_count_to_kb(user_physical_allocated));
  107. }