2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-10 16:28:48 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
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>
|
2022-03-22 21:28:28 -03:00
|
|
|
#include <LibCore/Object.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
|
#include <LibGUI/Painter.h>
|
2022-04-09 04:28:38 -03:00
|
|
|
#include <LibGfx/Font/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>
|
|
|
|
|
|
2022-03-18 20:19:56 -03:00
|
|
|
REGISTER_WIDGET(SystemMonitor, MemoryStatsWidget)
|
|
|
|
|
|
|
|
|
|
namespace SystemMonitor {
|
|
|
|
|
|
2019-10-02 15:26:19 -03:00
|
|
|
static MemoryStatsWidget* s_the;
|
|
|
|
|
|
|
|
|
|
MemoryStatsWidget* MemoryStatsWidget::the()
|
|
|
|
|
{
|
|
|
|
|
return s_the;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 20:19:56 -03:00
|
|
|
MemoryStatsWidget::MemoryStatsWidget()
|
|
|
|
|
: MemoryStatsWidget(nullptr)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MemoryStatsWidget::MemoryStatsWidget(GraphWidget* graph)
|
2020-02-23 06:57:42 -03:00
|
|
|
: 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;
|
|
|
|
|
|
2022-03-22 21:28:28 -03:00
|
|
|
REGISTER_STRING_PROPERTY("memory_graph", graph_widget_name, set_graph_widget_via_name);
|
|
|
|
|
|
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);
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
auto build_widgets_for_label = [this](String const& 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;
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-14 09:27:22 -03:00
|
|
|
m_physical_pages_label = build_widgets_for_label("Physical memory:");
|
|
|
|
|
m_physical_pages_committed_label = build_widgets_for_label("Committed memory:");
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 20:19:56 -03:00
|
|
|
void MemoryStatsWidget::set_graph_widget(GraphWidget& graph)
|
|
|
|
|
{
|
|
|
|
|
m_graph = &graph;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 21:28:28 -03:00
|
|
|
void MemoryStatsWidget::set_graph_widget_via_name(String name)
|
|
|
|
|
{
|
|
|
|
|
m_graph_widget_name = move(name);
|
|
|
|
|
if (!m_graph_widget_name.is_null()) {
|
|
|
|
|
// FIXME: We assume here that the graph widget is a sibling or descendant of a sibling. This prevents more complex hierarchies.
|
|
|
|
|
auto* maybe_graph = parent_widget()->find_descendant_of_type_named<GraphWidget>(m_graph_widget_name);
|
|
|
|
|
if (maybe_graph) {
|
|
|
|
|
m_graph = maybe_graph;
|
|
|
|
|
// Delete the stored graph name to signal that we found the widget
|
|
|
|
|
m_graph_widget_name = {};
|
|
|
|
|
} else {
|
|
|
|
|
dbgln("MemoryStatsWidget: Couldn't find graph of name '{}', retrying later.", m_graph_widget_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String MemoryStatsWidget::graph_widget_name()
|
|
|
|
|
{
|
|
|
|
|
if (m_graph)
|
|
|
|
|
return m_graph->name();
|
|
|
|
|
return m_graph_widget_name;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 19:32:56 -03:00
|
|
|
static inline u64 page_count_to_bytes(size_t count)
|
2019-03-10 08:13:22 -03:00
|
|
|
{
|
2021-09-03 13:26:58 -03:00
|
|
|
return count * 4096;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-10 08:13:22 -03:00
|
|
|
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();
|
2021-11-14 21:46:51 -03:00
|
|
|
auto json_result = JsonValue::from_string(file_contents).release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
auto const& json = json_result.as_object();
|
2019-03-10 08:13:22 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
u32 kmalloc_allocated = json.get("kmalloc_allocated"sv).to_u32();
|
|
|
|
|
u32 kmalloc_available = json.get("kmalloc_available"sv).to_u32();
|
2022-07-14 09:27:22 -03:00
|
|
|
u64 physical_allocated = json.get("physical_allocated"sv).to_u64();
|
|
|
|
|
u64 physical_available = json.get("physical_available"sv).to_u64();
|
|
|
|
|
u64 physical_committed = json.get("physical_committed"sv).to_u64();
|
|
|
|
|
u64 physical_uncommitted = json.get("physical_uncommitted"sv).to_u64();
|
2022-07-11 14:32:29 -03:00
|
|
|
u32 kmalloc_call_count = json.get("kmalloc_call_count"sv).to_u32();
|
|
|
|
|
u32 kfree_call_count = json.get("kfree_call_count"sv).to_u32();
|
2021-10-06 19:32:56 -03:00
|
|
|
|
|
|
|
|
u64 kmalloc_bytes_total = kmalloc_allocated + kmalloc_available;
|
2022-07-14 09:27:22 -03:00
|
|
|
u64 physical_pages_total = physical_allocated + physical_available;
|
2021-10-06 19:32:56 -03:00
|
|
|
|
2022-07-14 09:27:22 -03:00
|
|
|
u64 physical_pages_in_use = physical_allocated;
|
|
|
|
|
u64 total_userphysical_and_swappable_pages = physical_allocated + physical_committed + physical_uncommitted;
|
2021-01-05 00:14:52 -03:00
|
|
|
|
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)));
|
2022-07-14 09:27:22 -03:00
|
|
|
m_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_physical_pages_committed_label->set_text(String::formatted("{}", human_readable_size(page_count_to_bytes(physical_committed))));
|
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
|
|
|
|
2022-03-22 21:28:28 -03:00
|
|
|
// Because the initialization order of us and the graph is unknown, we might get a couple of updates where the graph widget lookup fails.
|
|
|
|
|
// Therefore, we can retry indefinitely. (Should not be too much of a performance hit, as we don't update that often.)
|
|
|
|
|
if (!m_graph)
|
|
|
|
|
set_graph_widget_via_name(move(m_graph_widget_name));
|
|
|
|
|
|
2022-03-18 20:19:56 -03:00
|
|
|
if (m_graph) {
|
|
|
|
|
m_graph->set_max(page_count_to_bytes(total_userphysical_and_swappable_pages) + kmalloc_bytes_total);
|
2022-07-14 09:27:22 -03:00
|
|
|
m_graph->add_value({ page_count_to_bytes(physical_committed), page_count_to_bytes(physical_allocated), kmalloc_bytes_total });
|
2022-03-18 20:19:56 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-10 08:13:22 -03:00
|
|
|
}
|