2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-03-10 08:13:22 -03:00
|
|
|
#include "MemoryStatsWidget.h"
|
2019-05-05 22:21:23 -03:00
|
|
|
#include "GraphWidget.h"
|
2019-07-01 13:43:01 -03:00
|
|
|
#include <AK/JsonObject.h>
|
2021-09-03 13:26:58 -03:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/File.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
|
#include <LibGUI/Painter.h>
|
2020-12-29 14:25:13 -03:00
|
|
|
#include <LibGfx/FontDatabase.h>
|
2020-02-14 19:53:11 -03:00
|
|
|
#include <LibGfx/StylePainter.h>
|
2019-03-10 08:13:22 -03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2019-10-02 15:26:19 -03:00
|
|
|
static MemoryStatsWidget* s_the;
|
|
|
|
|
|
|
|
|
|
MemoryStatsWidget* MemoryStatsWidget::the()
|
|
|
|
|
{
|
|
|
|
|
return s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-23 06:57:42 -03:00
|
|
|
MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph)
|
|
|
|
|
: m_graph(graph)
|
2019-03-10 08:13:22 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!s_the);
|
2019-10-02 15:26:19 -03:00
|
|
|
s_the = this;
|
|
|
|
|
|
2020-12-29 14:57:29 -03:00
|
|
|
set_fixed_height(110);
|
2019-03-10 08:13:22 -03:00
|
|
|
|
2020-03-04 05:43:54 -03:00
|
|
|
set_layout<GUI::VerticalBoxLayout>();
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-16 21:11:38 -03:00
|
|
|
layout()->set_margins({ 8, 0, 0 });
|
2019-03-10 08:13:22 -03:00
|
|
|
layout()->set_spacing(3);
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
auto build_widgets_for_label = [this](const String& description) -> RefPtr<GUI::Label> {
|
2020-03-04 15:07:55 -03:00
|
|
|
auto& container = add<GUI::Widget>();
|
|
|
|
|
container.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-29 14:57:29 -03:00
|
|
|
container.set_fixed_size(275, 12);
|
2020-03-04 15:07:55 -03:00
|
|
|
auto& description_label = container.add<GUI::Label>(description);
|
2021-05-20 13:40:48 -03:00
|
|
|
description_label.set_font(Gfx::FontDatabase::default_font().bold_variant());
|
2020-03-04 15:07:55 -03:00
|
|
|
description_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
|
|
|
|
auto& label = container.add<GUI::Label>();
|
|
|
|
|
label.set_text_alignment(Gfx::TextAlignment::CenterRight);
|
2019-03-10 08:34:44 -03:00
|
|
|
return label;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-05 00:14:52 -03:00
|
|
|
m_user_physical_pages_label = build_widgets_for_label("Physical memory:");
|
|
|
|
|
m_user_physical_pages_committed_label = build_widgets_for_label("Committed memory:");
|
2019-03-10 08:34:44 -03:00
|
|
|
m_supervisor_physical_pages_label = build_widgets_for_label("Supervisor physical:");
|
2020-08-02 11:35:49 -03:00
|
|
|
m_kmalloc_space_label = build_widgets_for_label("Kernel heap:");
|
|
|
|
|
m_kmalloc_count_label = build_widgets_for_label("Calls kmalloc:");
|
|
|
|
|
m_kfree_count_label = build_widgets_for_label("Calls kfree:");
|
|
|
|
|
m_kmalloc_difference_label = build_widgets_for_label("Difference:");
|
2019-03-10 08:13:22 -03:00
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemoryStatsWidget::~MemoryStatsWidget()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 13:26:58 -03:00
|
|
|
static inline u64 page_count_to_bytes(u64 count)
|
2019-03-10 08:13:22 -03:00
|
|
|
{
|
2021-09-03 13:26:58 -03:00
|
|
|
return count * 4096;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline u64 page_count_to_kb(u64 count)
|
|
|
|
|
{
|
|
|
|
|
return page_count_to_bytes(count) / 1024;
|
2019-03-10 08:13:22 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-07 00:35:15 -03:00
|
|
|
static inline u64 bytes_to_kb(u64 bytes)
|
2019-03-10 08:13:22 -03:00
|
|
|
{
|
|
|
|
|
return bytes / 1024;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MemoryStatsWidget::refresh()
|
|
|
|
|
{
|
2020-02-02 08:34:39 -03:00
|
|
|
auto proc_memstat = Core::File::construct("/proc/memstat");
|
2021-05-12 06:26:43 -03:00
|
|
|
if (!proc_memstat->open(Core::OpenMode::ReadOnly))
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-03-10 08:13:22 -03:00
|
|
|
|
2020-01-15 18:45:02 -03:00
|
|
|
auto file_contents = proc_memstat->read_all();
|
2020-06-11 01:40:27 -03:00
|
|
|
auto json_result = JsonValue::from_string(file_contents);
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(json_result.has_value());
|
2020-06-11 01:40:27 -03:00
|
|
|
auto json = json_result.value().as_object();
|
2019-03-10 08:13:22 -03:00
|
|
|
|
2020-12-20 20:09:48 -03:00
|
|
|
[[maybe_unused]] unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_u32();
|
2019-07-03 16:17:35 -03:00
|
|
|
unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_u32();
|
|
|
|
|
unsigned kmalloc_available = json.get("kmalloc_available").to_u32();
|
2021-07-07 00:35:15 -03:00
|
|
|
unsigned user_physical_allocated = json.get("user_physical_allocated").to_u64();
|
|
|
|
|
unsigned user_physical_available = json.get("user_physical_available").to_u64();
|
|
|
|
|
unsigned user_physical_committed = json.get("user_physical_committed").to_u64();
|
|
|
|
|
unsigned user_physical_uncommitted = json.get("user_physical_uncommitted").to_u64();
|
|
|
|
|
unsigned super_physical_alloc = json.get("super_physical_allocated").to_u64();
|
|
|
|
|
unsigned super_physical_free = json.get("super_physical_available").to_u64();
|
2019-07-03 16:17:35 -03:00
|
|
|
unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_u32();
|
|
|
|
|
unsigned kfree_call_count = json.get("kfree_call_count").to_u32();
|
2019-03-10 08:34:44 -03:00
|
|
|
|
2021-01-05 00:14:52 -03:00
|
|
|
size_t kmalloc_bytes_total = kmalloc_allocated + kmalloc_available;
|
|
|
|
|
size_t user_physical_pages_total = user_physical_allocated + user_physical_available;
|
|
|
|
|
size_t supervisor_pages_total = super_physical_alloc + super_physical_free;
|
2019-05-05 22:21:23 -03:00
|
|
|
|
2021-01-05 00:14:52 -03:00
|
|
|
size_t physical_pages_total = user_physical_pages_total + supervisor_pages_total;
|
|
|
|
|
size_t physical_pages_in_use = user_physical_allocated + super_physical_alloc;
|
|
|
|
|
size_t total_userphysical_and_swappable_pages = user_physical_allocated + user_physical_committed + user_physical_uncommitted;
|
|
|
|
|
|
2021-09-03 13:26:58 -03:00
|
|
|
m_kmalloc_space_label->set_text(String::formatted("{}/{}", human_readable_size(kmalloc_allocated), human_readable_size(kmalloc_bytes_total)));
|
|
|
|
|
m_user_physical_pages_label->set_text(String::formatted("{}/{}", human_readable_size(page_count_to_bytes(physical_pages_in_use)), human_readable_size(page_count_to_bytes(physical_pages_total))));
|
|
|
|
|
m_user_physical_pages_committed_label->set_text(String::formatted("{}", human_readable_size(page_count_to_bytes(user_physical_committed))));
|
|
|
|
|
m_supervisor_physical_pages_label->set_text(String::formatted("{}/{}", human_readable_size(page_count_to_bytes(super_physical_alloc)), human_readable_size(page_count_to_bytes(supervisor_pages_total))));
|
2020-10-06 13:04:36 -03:00
|
|
|
m_kmalloc_count_label->set_text(String::formatted("{}", kmalloc_call_count));
|
|
|
|
|
m_kfree_count_label->set_text(String::formatted("{}", kfree_call_count));
|
|
|
|
|
m_kmalloc_difference_label->set_text(String::formatted("{:+}", kmalloc_call_count - kfree_call_count));
|
2019-07-01 13:43:01 -03:00
|
|
|
|
2021-09-03 13:26:58 -03:00
|
|
|
m_graph.set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total);
|
|
|
|
|
m_graph.add_value({ (int)page_count_to_bytes(user_physical_committed), (int)page_count_to_bytes(user_physical_allocated), (int)kmalloc_bytes_total });
|
2019-03-10 08:13:22 -03:00
|
|
|
}
|