summaryrefslogtreecommitdiff
path: root/Applications/SystemMonitor/ProcessMemoryMapWidget.cpp
blob: fe431b66b3dcfdbc2812174df0c6d3bc1455c5a1 (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ProcessMemoryMapWidget.h"
#include <LibCore/CTimer.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GJsonArrayModel.h>
#include <LibGUI/GSortingProxyModel.h>
#include <LibGUI/GTableView.h>

ProcessMemoryMapWidget::ProcessMemoryMapWidget(GWidget* parent)
    : GWidget(parent)
{
    set_layout(make<GVBoxLayout>());
    layout()->set_margins({ 4, 4, 4, 4 });
    m_table_view = GTableView::construct(this);
    m_table_view->set_size_columns_to_fit_content(true);
    Vector<GJsonArrayModel::FieldSpec> pid_vm_fields;
    pid_vm_fields.empend("Address", TextAlignment::CenterLeft, [](auto& object) {
        return String::format("%#x", object.get("address").to_u32());
    });
    pid_vm_fields.empend("size", "Size", TextAlignment::CenterRight);
    pid_vm_fields.empend("amount_resident", "Resident", TextAlignment::CenterRight);
    pid_vm_fields.empend("amount_dirty", "Dirty", TextAlignment::CenterRight);
    pid_vm_fields.empend("Access", TextAlignment::CenterLeft, [](auto& object) {
        StringBuilder builder;
        if (!object.get("user_accessible").to_bool())
            builder.append('K');
        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("stack").to_bool())
            builder.append('T');
        return builder.to_string();
    });
    pid_vm_fields.empend("Purgeable", TextAlignment::CenterLeft, [](auto& object) {
        if (!object.get("purgeable").to_bool())
            return "";
        if (object.get("volatile").to_bool())
            return "Volatile";
        return "Non-volatile";
    });
    pid_vm_fields.empend("cow_pages", "# CoW", TextAlignment::CenterRight);
    pid_vm_fields.empend("name", "Name", TextAlignment::CenterLeft);
    m_json_model = GJsonArrayModel::create({}, move(pid_vm_fields));
    m_table_view->set_model(GSortingProxyModel::create(*m_json_model));
    m_table_view->model()->set_key_column_and_sort_order(0, GSortOrder::Ascending);
    m_timer = CTimer::construct(1000, [this] { refresh(); }, this);
}

ProcessMemoryMapWidget::~ProcessMemoryMapWidget()
{
}

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

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