MemoryStatsWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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)
  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, 110);
  49. set_layout<GUI::VerticalBoxLayout>();
  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 = add<GUI::Widget>();
  54. container.set_layout<GUI::HorizontalBoxLayout>();
  55. container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  56. container.set_preferred_size(275, 12);
  57. auto& description_label = container.add<GUI::Label>(description);
  58. description_label.set_font(Gfx::Font::default_bold_font());
  59. description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  60. auto& label = container.add<GUI::Label>();
  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_space_label = build_widgets_for_label("Kernel heap:");
  67. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:");
  68. m_kfree_count_label = build_widgets_for_label("Calls kfree:");
  69. m_kmalloc_difference_label = build_widgets_for_label("Difference:");
  70. refresh();
  71. }
  72. MemoryStatsWidget::~MemoryStatsWidget()
  73. {
  74. }
  75. static inline size_t page_count_to_kb(size_t kb)
  76. {
  77. return (kb * 4096) / 1024;
  78. }
  79. static inline size_t bytes_to_kb(size_t bytes)
  80. {
  81. return bytes / 1024;
  82. }
  83. void MemoryStatsWidget::refresh()
  84. {
  85. auto proc_memstat = Core::File::construct("/proc/memstat");
  86. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  87. ASSERT_NOT_REACHED();
  88. auto file_contents = proc_memstat->read_all();
  89. auto json_result = JsonValue::from_string(file_contents);
  90. ASSERT(json_result.has_value());
  91. auto json = json_result.value().as_object();
  92. unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  93. (void)kmalloc_eternal_allocated;
  94. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  95. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  96. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u32();
  97. unsigned user_physical_available = json.get("user_physical_available").to_u32();
  98. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u32();
  99. unsigned super_physical_free = json.get("super_physical_available").to_u32();
  100. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  101. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  102. size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
  103. size_t user_pages_available = user_physical_allocated + user_physical_available;
  104. size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
  105. m_kmalloc_space_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
  106. 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)));
  107. 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)));
  108. m_kmalloc_count_label->set_text(String::format("%u", kmalloc_call_count));
  109. m_kfree_count_label->set_text(String::format("%u", kfree_call_count));
  110. m_kmalloc_difference_label->set_text(String::format("+%u", kmalloc_call_count - kfree_call_count));
  111. m_graph.set_max(page_count_to_kb(user_pages_available));
  112. m_graph.add_value(page_count_to_kb(user_physical_allocated));
  113. }