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-07-28 07:15:24 -03:00
|
|
|
#include "ProcessMemoryMapWidget.h"
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/Timer.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/JsonArrayModel.h>
|
2020-02-25 17:38:45 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/SortingProxyModel.h>
|
|
|
|
|
#include <LibGUI/TableView.h>
|
2020-02-25 17:38:45 -03:00
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
|
|
2022-03-24 09:30:52 -03:00
|
|
|
REGISTER_WIDGET(SystemMonitor, ProcessMemoryMapWidget)
|
|
|
|
|
|
|
|
|
|
namespace SystemMonitor {
|
|
|
|
|
|
2020-02-25 17:38:45 -03:00
|
|
|
class PagemapPaintingDelegate final : public GUI::TableCellPaintingDelegate {
|
|
|
|
|
public:
|
2022-02-10 16:28:48 -03:00
|
|
|
virtual ~PagemapPaintingDelegate() override = default;
|
2020-02-25 17:38:45 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
virtual void paint(GUI::Painter& painter, Gfx::IntRect const& a_rect, Gfx::Palette const&, const GUI::ModelIndex& index) override
|
2020-02-25 17:38:45 -03:00
|
|
|
{
|
|
|
|
|
auto rect = a_rect.shrunken(2, 2);
|
2022-12-05 22:12:49 -03:00
|
|
|
auto pagemap = index.data(GUI::ModelRole::Custom).to_deprecated_string();
|
2020-02-25 17:38:45 -03:00
|
|
|
|
|
|
|
|
float scale_factor = (float)pagemap.length() / (float)rect.width();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < rect.width(); ++i) {
|
|
|
|
|
int x = rect.x() + i;
|
|
|
|
|
char c = pagemap[(float)i * scale_factor];
|
|
|
|
|
Color color;
|
|
|
|
|
if (c == 'N') // Null (no page at all, typically an inode-backed page that hasn't been paged in.)
|
|
|
|
|
color = Color::White;
|
|
|
|
|
else if (c == 'Z') // Zero (globally shared zero page, typically an untouched anonymous page.)
|
|
|
|
|
color = Color::from_rgb(0xc0c0ff);
|
|
|
|
|
else if (c == 'P') // Physical (a resident page)
|
|
|
|
|
color = Color::Black;
|
|
|
|
|
else
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-25 17:38:45 -03:00
|
|
|
|
|
|
|
|
painter.draw_line({ x, rect.top() }, { x, rect.bottom() }, color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
painter.draw_rect(rect, Color::Black);
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-07-28 07:15:24 -03:00
|
|
|
|
2020-02-23 08:07:13 -03:00
|
|
|
ProcessMemoryMapWidget::ProcessMemoryMapWidget()
|
2019-07-28 07:15:24 -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(4);
|
2020-02-23 06:57:42 -03:00
|
|
|
m_table_view = add<GUI::TableView>();
|
2020-02-02 11:07:41 -03:00
|
|
|
Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields;
|
2020-07-17 22:45:02 -03:00
|
|
|
pid_vm_fields.empend(
|
|
|
|
|
"Address", Gfx::TextAlignment::CenterLeft,
|
2022-12-21 17:26:11 -03:00
|
|
|
[](auto& object) { return DeprecatedString::formatted("{:p}", object.get_u64("address"sv).value_or(0)); },
|
|
|
|
|
[](auto& object) { return object.get_u64("address"sv).value_or(0); });
|
2020-02-06 07:56:38 -03:00
|
|
|
pid_vm_fields.empend("size", "Size", Gfx::TextAlignment::CenterRight);
|
|
|
|
|
pid_vm_fields.empend("amount_resident", "Resident", Gfx::TextAlignment::CenterRight);
|
|
|
|
|
pid_vm_fields.empend("amount_dirty", "Dirty", Gfx::TextAlignment::CenterRight);
|
|
|
|
|
pid_vm_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](auto& object) {
|
2019-08-10 11:08:51 -03:00
|
|
|
StringBuilder builder;
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("readable"sv).value_or(false))
|
2019-08-10 11:08:51 -03:00
|
|
|
builder.append('R');
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("writable"sv).value_or(false))
|
2019-08-10 11:08:51 -03:00
|
|
|
builder.append('W');
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("executable"sv).value_or(false))
|
2019-12-21 12:26:42 -03:00
|
|
|
builder.append('X');
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("shared"sv).value_or(false))
|
2019-11-17 08:14:13 -03:00
|
|
|
builder.append('S');
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("syscall"sv).value_or(false))
|
2021-02-02 15:58:46 -03:00
|
|
|
builder.append('C');
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("stack"sv).value_or(false))
|
2019-11-17 08:14:13 -03:00
|
|
|
builder.append('T');
|
2022-12-05 22:12:49 -03:00
|
|
|
return builder.to_deprecated_string();
|
2019-08-10 11:08:51 -03:00
|
|
|
});
|
2021-01-29 06:51:46 -03:00
|
|
|
pid_vm_fields.empend("VMObject type", Gfx::TextAlignment::CenterLeft, [](auto& object) {
|
2022-12-21 17:26:11 -03:00
|
|
|
auto type = object.get_deprecated_string("vmobject"sv).value_or({});
|
2022-07-11 14:32:29 -03:00
|
|
|
if (type.ends_with("VMObject"sv))
|
2021-01-29 06:51:46 -03:00
|
|
|
type = type.substring(0, type.length() - 8);
|
|
|
|
|
return type;
|
|
|
|
|
});
|
2020-02-06 07:56:38 -03:00
|
|
|
pid_vm_fields.empend("Purgeable", Gfx::TextAlignment::CenterLeft, [](auto& object) {
|
2022-12-21 17:26:11 -03:00
|
|
|
if (object.get_bool("volatile"sv).value_or(false))
|
2019-12-09 15:16:58 -03:00
|
|
|
return "Volatile";
|
|
|
|
|
return "Non-volatile";
|
|
|
|
|
});
|
2020-02-25 17:38:45 -03:00
|
|
|
pid_vm_fields.empend(
|
|
|
|
|
"Page map", Gfx::TextAlignment::CenterLeft,
|
|
|
|
|
[](auto&) {
|
|
|
|
|
return GUI::Variant();
|
|
|
|
|
},
|
|
|
|
|
[](auto&) {
|
2020-09-21 19:24:27 -03:00
|
|
|
return GUI::Variant(0);
|
2020-02-25 17:38:45 -03:00
|
|
|
},
|
2022-04-01 14:58:27 -03:00
|
|
|
[](JsonObject const& object) {
|
2022-12-21 17:26:11 -03:00
|
|
|
auto pagemap = object.get_deprecated_string("pagemap"sv).value_or({});
|
2020-02-25 17:38:45 -03:00
|
|
|
return pagemap;
|
|
|
|
|
});
|
2020-02-06 07:56:38 -03:00
|
|
|
pid_vm_fields.empend("cow_pages", "# CoW", Gfx::TextAlignment::CenterRight);
|
|
|
|
|
pid_vm_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
|
2020-02-02 11:07:41 -03:00
|
|
|
m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
|
2021-12-24 09:12:04 -03:00
|
|
|
m_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_json_model)));
|
2020-02-25 17:38:45 -03:00
|
|
|
|
2020-08-25 19:15:48 -03:00
|
|
|
m_table_view->set_column_painting_delegate(7, make<PagemapPaintingDelegate>());
|
2020-02-25 17:38:45 -03:00
|
|
|
|
2020-08-16 05:44:10 -03:00
|
|
|
m_table_view->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
|
2020-02-23 06:57:42 -03:00
|
|
|
m_timer = add<Core::Timer>(1000, [this] { refresh(); });
|
2023-01-11 16:36:46 -03:00
|
|
|
m_timer->start();
|
2019-07-28 07:15:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProcessMemoryMapWidget::set_pid(pid_t pid)
|
|
|
|
|
{
|
|
|
|
|
if (m_pid == pid)
|
|
|
|
|
return;
|
|
|
|
|
m_pid = pid;
|
2022-12-04 15:02:33 -03:00
|
|
|
m_json_model->set_json_path(DeprecatedString::formatted("/proc/{}/vm", pid));
|
2019-07-28 07:15:24 -03:00
|
|
|
}
|
2019-12-02 13:36:10 -03:00
|
|
|
|
|
|
|
|
void ProcessMemoryMapWidget::refresh()
|
|
|
|
|
{
|
|
|
|
|
if (m_pid != -1)
|
2022-07-22 19:54:17 -03:00
|
|
|
m_json_model->update();
|
2019-12-02 13:36:10 -03:00
|
|
|
}
|
2022-03-24 09:30:52 -03:00
|
|
|
|
|
|
|
|
}
|