Serenity Operating System
at hosted 143 lines 5.9 kB view raw
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/Painter.h> 32#include <LibGUI/SortingProxyModel.h> 33#include <LibGUI/TableView.h> 34#include <LibGfx/Palette.h> 35 36class PagemapPaintingDelegate final : public GUI::TableCellPaintingDelegate { 37public: 38 virtual ~PagemapPaintingDelegate() override {} 39 40 virtual void paint(GUI::Painter& painter, const Gfx::Rect& a_rect, const Gfx::Palette&, const GUI::Model& model, const GUI::ModelIndex& index) override 41 { 42 auto rect = a_rect.shrunken(2, 2); 43 auto pagemap = model.data(index, GUI::Model::Role::Custom).to_string(); 44 45 float scale_factor = (float)pagemap.length() / (float)rect.width(); 46 47 for (int i = 0; i < rect.width(); ++i) { 48 int x = rect.x() + i; 49 char c = pagemap[(float)i * scale_factor]; 50 Color color; 51 if (c == 'N') // Null (no page at all, typically an inode-backed page that hasn't been paged in.) 52 color = Color::White; 53 else if (c == 'Z') // Zero (globally shared zero page, typically an untouched anonymous page.) 54 color = Color::from_rgb(0xc0c0ff); 55 else if (c == 'P') // Physical (a resident page) 56 color = Color::Black; 57 else 58 ASSERT_NOT_REACHED(); 59 60 painter.draw_line({ x, rect.top() }, { x, rect.bottom() }, color); 61 } 62 63 painter.draw_rect(rect, Color::Black); 64 } 65}; 66 67ProcessMemoryMapWidget::ProcessMemoryMapWidget() 68{ 69 set_layout<GUI::VerticalBoxLayout>(); 70 layout()->set_margins({ 4, 4, 4, 4 }); 71 m_table_view = add<GUI::TableView>(); 72 m_table_view->set_size_columns_to_fit_content(true); 73 Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields; 74 pid_vm_fields.empend("Address", Gfx::TextAlignment::CenterLeft, [](auto& object) { 75 return String::format("%#x", object.get("address").to_u32()); 76 }); 77 pid_vm_fields.empend("size", "Size", Gfx::TextAlignment::CenterRight); 78 pid_vm_fields.empend("amount_resident", "Resident", Gfx::TextAlignment::CenterRight); 79 pid_vm_fields.empend("amount_dirty", "Dirty", Gfx::TextAlignment::CenterRight); 80 pid_vm_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](auto& object) { 81 StringBuilder builder; 82 if (!object.get("user_accessible").to_bool()) 83 builder.append('K'); 84 if (object.get("readable").to_bool()) 85 builder.append('R'); 86 if (object.get("writable").to_bool()) 87 builder.append('W'); 88 if (object.get("executable").to_bool()) 89 builder.append('X'); 90 if (object.get("shared").to_bool()) 91 builder.append('S'); 92 if (object.get("stack").to_bool()) 93 builder.append('T'); 94 return builder.to_string(); 95 }); 96 pid_vm_fields.empend("vmobject", "VMObject type", Gfx::TextAlignment::CenterLeft); 97 pid_vm_fields.empend("Purgeable", Gfx::TextAlignment::CenterLeft, [](auto& object) { 98 if (!object.get("purgeable").to_bool()) 99 return ""; 100 if (object.get("volatile").to_bool()) 101 return "Volatile"; 102 return "Non-volatile"; 103 }); 104 pid_vm_fields.empend( 105 "Page map", Gfx::TextAlignment::CenterLeft, 106 [](auto&) { 107 return GUI::Variant(); 108 }, 109 [](auto&) { 110 return GUI::Variant(); 111 }, 112 [](const JsonObject& object) { 113 auto pagemap = object.get("pagemap").as_string_or({}); 114 return pagemap; 115 }); 116 pid_vm_fields.empend("cow_pages", "# CoW", Gfx::TextAlignment::CenterRight); 117 pid_vm_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft); 118 m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields)); 119 m_table_view->set_model(GUI::SortingProxyModel::create(*m_json_model)); 120 121 m_table_view->set_cell_painting_delegate(7, make<PagemapPaintingDelegate>()); 122 123 m_table_view->model()->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending); 124 m_timer = add<Core::Timer>(1000, [this] { refresh(); }); 125} 126 127ProcessMemoryMapWidget::~ProcessMemoryMapWidget() 128{ 129} 130 131void ProcessMemoryMapWidget::set_pid(pid_t pid) 132{ 133 if (m_pid == pid) 134 return; 135 m_pid = pid; 136 m_json_model->set_json_path(String::format("/proc/%d/vm", pid)); 137} 138 139void ProcessMemoryMapWidget::refresh() 140{ 141 if (m_pid != -1) 142 m_json_model->update(); 143}