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