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 "ProcessMemoryMapWidget.h"
28#include <LibCore/Timer.h>
29#include <LibGUI/BoxLayout.h>
30#include <LibGUI/JsonArrayModel.h>
31#include <LibGUI/SortingProxyModel.h>
32#include <LibGUI/TableView.h>
33
34ProcessMemoryMapWidget::ProcessMemoryMapWidget()
35{
36 set_layout(make<GUI::VerticalBoxLayout>());
37 layout()->set_margins({ 4, 4, 4, 4 });
38 m_table_view = add<GUI::TableView>();
39 m_table_view->set_size_columns_to_fit_content(true);
40 Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields;
41 pid_vm_fields.empend("Address", Gfx::TextAlignment::CenterLeft, [](auto& object) {
42 return String::format("%#x", object.get("address").to_u32());
43 });
44 pid_vm_fields.empend("size", "Size", Gfx::TextAlignment::CenterRight);
45 pid_vm_fields.empend("amount_resident", "Resident", Gfx::TextAlignment::CenterRight);
46 pid_vm_fields.empend("amount_dirty", "Dirty", Gfx::TextAlignment::CenterRight);
47 pid_vm_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](auto& object) {
48 StringBuilder builder;
49 if (!object.get("user_accessible").to_bool())
50 builder.append('K');
51 if (object.get("readable").to_bool())
52 builder.append('R');
53 if (object.get("writable").to_bool())
54 builder.append('W');
55 if (object.get("executable").to_bool())
56 builder.append('X');
57 if (object.get("shared").to_bool())
58 builder.append('S');
59 if (object.get("stack").to_bool())
60 builder.append('T');
61 return builder.to_string();
62 });
63 pid_vm_fields.empend("Purgeable", Gfx::TextAlignment::CenterLeft, [](auto& object) {
64 if (!object.get("purgeable").to_bool())
65 return "";
66 if (object.get("volatile").to_bool())
67 return "Volatile";
68 return "Non-volatile";
69 });
70 pid_vm_fields.empend("cow_pages", "# CoW", Gfx::TextAlignment::CenterRight);
71 pid_vm_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
72 m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
73 m_table_view->set_model(GUI::SortingProxyModel::create(*m_json_model));
74 m_table_view->model()->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
75 m_timer = add<Core::Timer>(1000, [this] { refresh(); });
76}
77
78ProcessMemoryMapWidget::~ProcessMemoryMapWidget()
79{
80}
81
82void ProcessMemoryMapWidget::set_pid(pid_t pid)
83{
84 if (m_pid == pid)
85 return;
86 m_pid = pid;
87 m_json_model->set_json_path(String::format("/proc/%d/vm", pid));
88}
89
90void ProcessMemoryMapWidget::refresh()
91{
92 if (m_pid != -1)
93 m_json_model->update();
94}