summaryrefslogtreecommitdiff
path: root/Userland/Applications/SystemMonitor/ProcessMemoryMapWidget.cpp
blob: f25c17cf98acd4ef465beb7e5a4691a2f9d25610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ProcessMemoryMapWidget.h"
#include <LibCore/Timer.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/Painter.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/TableView.h>
#include <LibGfx/Palette.h>

REGISTER_WIDGET(SystemMonitor, ProcessMemoryMapWidget)

namespace SystemMonitor {

class PagemapPaintingDelegate final : public GUI::TableCellPaintingDelegate {
public:
    virtual ~PagemapPaintingDelegate() override = default;

    virtual void paint(GUI::Painter& painter, Gfx::IntRect const& a_rect, Gfx::Palette const&, const GUI::ModelIndex& index) override
    {
        auto rect = a_rect.shrunken(2, 2);
        auto pagemap = index.data(GUI::ModelRole::Custom).to_string();

        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
                VERIFY_NOT_REACHED();

            painter.draw_line({ x, rect.top() }, { x, rect.bottom() }, color);
        }

        painter.draw_rect(rect, Color::Black);
    }
};

ProcessMemoryMapWidget::ProcessMemoryMapWidget()
{
    set_layout<GUI::VerticalBoxLayout>();
    layout()->set_margins(4);
    m_table_view = add<GUI::TableView>();
    Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields;
    pid_vm_fields.empend(
        "Address", Gfx::TextAlignment::CenterLeft,
        [](auto& object) { return String::formatted("{:p}", object.get("address").to_u64()); },
        [](auto& object) { return object.get("address").to_u64(); });
    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) {
        StringBuilder builder;
        if (object.get("readable").to_bool())
            builder.append('R');
        if (object.get("writable").to_bool())
            builder.append('W');
        if (object.get("executable").to_bool())
            builder.append('X');
        if (object.get("shared").to_bool())
            builder.append('S');
        if (object.get("syscall").to_bool())
            builder.append('C');
        if (object.get("stack").to_bool())
            builder.append('T');
        return builder.to_string();
    });
    pid_vm_fields.empend("VMObject type", Gfx::TextAlignment::CenterLeft, [](auto& object) {
        auto type = object.get("vmobject").to_string();
        if (type.ends_with("VMObject"))
            type = type.substring(0, type.length() - 8);
        return type;
    });
    pid_vm_fields.empend("Purgeable", Gfx::TextAlignment::CenterLeft, [](auto& object) {
        if (object.get("volatile").to_bool())
            return "Volatile";
        return "Non-volatile";
    });
    pid_vm_fields.empend(
        "Page map", Gfx::TextAlignment::CenterLeft,
        [](auto&) {
            return GUI::Variant();
        },
        [](auto&) {
            return GUI::Variant(0);
        },
        [](JsonObject const& object) {
            auto pagemap = object.get("pagemap").as_string_or({});
            return pagemap;
        });
    pid_vm_fields.empend("cow_pages", "# CoW", Gfx::TextAlignment::CenterRight);
    pid_vm_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
    m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
    m_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_json_model)));

    m_table_view->set_column_painting_delegate(7, make<PagemapPaintingDelegate>());

    m_table_view->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
    m_timer = add<Core::Timer>(1000, [this] { refresh(); });
}

void ProcessMemoryMapWidget::set_pid(pid_t pid)
{
    if (m_pid == pid)
        return;
    m_pid = pid;
    m_json_model->set_json_path(String::formatted("/proc/{}/vm", pid));
}

void ProcessMemoryMapWidget::refresh()
{
    if (m_pid != -1)
        m_json_model->invalidate();
}

}