MemoryStatsWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/ByteBuffer.h>
  29. #include <AK/JsonObject.h>
  30. #include <LibCore/File.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGfx/Font.h>
  35. #include <LibGfx/StylePainter.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. static MemoryStatsWidget* s_the;
  39. MemoryStatsWidget* MemoryStatsWidget::the()
  40. {
  41. return s_the;
  42. }
  43. MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph)
  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, 110);
  50. set_layout<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 = add<GUI::Widget>();
  55. container.set_layout<GUI::HorizontalBoxLayout>();
  56. container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  57. container.set_preferred_size(275, 12);
  58. auto& description_label = container.add<GUI::Label>(description);
  59. description_label.set_font(Gfx::Font::default_bold_font());
  60. description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  61. auto& label = container.add<GUI::Label>();
  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_space_label = build_widgets_for_label("Kernel heap:");
  68. m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:");
  69. m_kfree_count_label = build_widgets_for_label("Calls kfree:");
  70. m_kmalloc_difference_label = build_widgets_for_label("Difference:");
  71. refresh();
  72. }
  73. MemoryStatsWidget::~MemoryStatsWidget()
  74. {
  75. }
  76. static inline size_t page_count_to_kb(size_t kb)
  77. {
  78. return (kb * 4096) / 1024;
  79. }
  80. static inline size_t bytes_to_kb(size_t bytes)
  81. {
  82. return bytes / 1024;
  83. }
  84. void MemoryStatsWidget::refresh()
  85. {
  86. auto proc_memstat = Core::File::construct("/proc/memstat");
  87. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  88. ASSERT_NOT_REACHED();
  89. auto file_contents = proc_memstat->read_all();
  90. auto json_result = JsonValue::from_string(file_contents);
  91. ASSERT(json_result.has_value());
  92. auto json = json_result.value().as_object();
  93. unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
  94. (void)kmalloc_eternal_allocated;
  95. unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
  96. unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
  97. unsigned user_physical_allocated = json.get("user_physical_allocated").to_u32();
  98. unsigned user_physical_available = json.get("user_physical_available").to_u32();
  99. unsigned super_physical_alloc = json.get("super_physical_allocated").to_u32();
  100. unsigned super_physical_free = json.get("super_physical_available").to_u32();
  101. unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
  102. unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
  103. size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
  104. size_t user_pages_available = user_physical_allocated + user_physical_available;
  105. size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
  106. m_kmalloc_space_label->set_text(String::formatted("{}K/{}K", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
  107. m_user_physical_pages_label->set_text(String::formatted("{}K/{}K", page_count_to_kb(user_physical_allocated), page_count_to_kb(user_pages_available)));
  108. m_supervisor_physical_pages_label->set_text(String::formatted("{}K/{}K", page_count_to_kb(super_physical_alloc), page_count_to_kb(supervisor_pages_available)));
  109. m_kmalloc_count_label->set_text(String::formatted("{}", kmalloc_call_count));
  110. m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count));
  111. m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count));
  112. m_graph.set_max(page_count_to_kb(user_pages_available));
  113. m_graph.add_value(page_count_to_kb(user_physical_allocated));
  114. }