Serenity Operating System
at hosted 497 lines 20 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 "DevicesModel.h" 28#include "GraphWidget.h" 29#include "MemoryStatsWidget.h" 30#include "NetworkStatisticsWidget.h" 31#include "ProcessFileDescriptorMapWidget.h" 32#include "ProcessMemoryMapWidget.h" 33#include "ProcessModel.h" 34#include "ProcessStacksWidget.h" 35#include "ProcessTableView.h" 36#include "ProcessUnveiledPathsWidget.h" 37#include <LibCore/Timer.h> 38#include <LibGUI/AboutDialog.h> 39#include <LibGUI/Action.h> 40#include <LibGUI/ActionGroup.h> 41#include <LibGUI/Application.h> 42#include <LibGUI/BoxLayout.h> 43#include <LibGUI/GroupBox.h> 44#include <LibGUI/JsonArrayModel.h> 45#include <LibGUI/Label.h> 46#include <LibGUI/LazyWidget.h> 47#include <LibGUI/Menu.h> 48#include <LibGUI/MenuBar.h> 49#include <LibGUI/Painter.h> 50#include <LibGUI/SortingProxyModel.h> 51#include <LibGUI/Splitter.h> 52#include <LibGUI/TabWidget.h> 53#include <LibGUI/ToolBar.h> 54#include <LibGUI/Widget.h> 55#include <LibGUI/Window.h> 56#include <LibGfx/Palette.h> 57#include <LibPCIDB/Database.h> 58#include <signal.h> 59#include <stdio.h> 60#include <unistd.h> 61 62#ifdef __OpenBSD__ 63#include <sys/mount.h> 64#endif 65 66static String human_readable_size(u32 size) 67{ 68 if (size < (64 * KB)) 69 return String::format("%u", size); 70 if (size < MB) 71 return String::format("%u KB", size / KB); 72 if (size < GB) 73 return String::format("%u MB", size / MB); 74 return String::format("%u GB", size / GB); 75} 76 77static NonnullRefPtr<GUI::Widget> build_file_systems_tab(); 78static NonnullRefPtr<GUI::Widget> build_pci_devices_tab(); 79static NonnullRefPtr<GUI::Widget> build_devices_tab(); 80static NonnullRefPtr<GUI::Widget> build_graphs_tab(); 81 82int main(int argc, char** argv) 83{ 84#ifdef __serenity__ 85 if (pledge("stdio proc shared_buffer accept rpath unix cpath fattr", nullptr) < 0) { 86 perror("pledge"); 87 return 1; 88 } 89#endif 90 91 GUI::Application app(argc, argv); 92 93#ifdef __serenity__ 94 if (pledge("stdio proc shared_buffer accept rpath", nullptr) < 0) { 95 perror("pledge"); 96 return 1; 97 } 98#endif 99 100 if (unveil("/etc/passwd", "r") < 0) { 101 perror("unveil"); 102 return 1; 103 } 104 105#ifdef __OpenBSD__ 106 if (unveil("/etc/pwd.db", "r") < 0) { 107 perror("unveil"); 108 return 1; 109 } 110#endif 111 112 if (unveil("/res", "r") < 0) { 113 perror("unveil"); 114 return 1; 115 } 116 117 if (unveil("/proc", "r") < 0) { 118 perror("unveil"); 119 return 1; 120 } 121 122 if (unveil("/dev", "r") < 0) { 123 perror("unveil"); 124 return 1; 125 } 126 127 unveil(nullptr, nullptr); 128 129 auto window = GUI::Window::construct(); 130 window->set_title("System Monitor"); 131 window->set_rect(20, 200, 680, 400); 132 133 auto& keeper = window->set_main_widget<GUI::Widget>(); 134 keeper.set_layout<GUI::VerticalBoxLayout>(); 135 keeper.set_fill_with_background_color(true); 136 keeper.layout()->set_margins({ 4, 4, 4, 4 }); 137 138 auto& tabwidget = keeper.add<GUI::TabWidget>(); 139 140 auto& process_container_splitter = tabwidget.add_tab<GUI::VerticalSplitter>("Processes"); 141 142 auto& process_table_container = process_container_splitter.add<GUI::Widget>(); 143 144 tabwidget.add_widget("Graphs", build_graphs_tab()); 145 146 tabwidget.add_widget("File systems", build_file_systems_tab()); 147 148 tabwidget.add_widget("PCI devices", build_pci_devices_tab()); 149 150 tabwidget.add_widget("Devices", build_devices_tab()); 151 152 auto network_stats_widget = NetworkStatisticsWidget::construct(); 153 tabwidget.add_widget("Network", network_stats_widget); 154 155 process_table_container.set_layout<GUI::VerticalBoxLayout>(); 156 process_table_container.layout()->set_margins({ 4, 0, 4, 0 }); 157 process_table_container.layout()->set_spacing(0); 158 159 auto& toolbar = process_table_container.add<GUI::ToolBar>(); 160 toolbar.set_has_frame(false); 161 auto& process_table_view = process_table_container.add<ProcessTableView>(); 162 163 auto& refresh_timer = window->add<Core::Timer>( 164 1000, [&] { 165 process_table_view.refresh(); 166 if (auto* memory_stats_widget = MemoryStatsWidget::the()) 167 memory_stats_widget->refresh(); 168 }); 169 170 auto kill_action = GUI::Action::create("Kill process", { Mod_Ctrl, Key_K }, Gfx::Bitmap::load_from_file("/res/icons/kill16.png"), [&process_table_view](const GUI::Action&) { 171 pid_t pid = process_table_view.selected_pid(); 172 if (pid != -1) 173 kill(pid, SIGKILL); 174 }); 175 176 auto stop_action = GUI::Action::create("Stop process", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/stop16.png"), [&process_table_view](const GUI::Action&) { 177 pid_t pid = process_table_view.selected_pid(); 178 if (pid != -1) 179 kill(pid, SIGSTOP); 180 }); 181 182 auto continue_action = GUI::Action::create("Continue process", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/continue16.png"), [&process_table_view](const GUI::Action&) { 183 pid_t pid = process_table_view.selected_pid(); 184 if (pid != -1) 185 kill(pid, SIGCONT); 186 }); 187 188 toolbar.add_action(kill_action); 189 toolbar.add_action(stop_action); 190 toolbar.add_action(continue_action); 191 192 auto menubar = make<GUI::MenuBar>(); 193 auto& app_menu = menubar->add_menu("System Monitor"); 194 app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { 195 GUI::Application::the().quit(0); 196 return; 197 })); 198 199 auto& process_menu = menubar->add_menu("Process"); 200 process_menu.add_action(kill_action); 201 process_menu.add_action(stop_action); 202 process_menu.add_action(continue_action); 203 204 auto process_context_menu = GUI::Menu::construct(); 205 process_context_menu->add_action(kill_action); 206 process_context_menu->add_action(stop_action); 207 process_context_menu->add_action(continue_action); 208 process_table_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) { 209 (void)index; 210 process_context_menu->popup(event.screen_position()); 211 }; 212 213 auto& frequency_menu = menubar->add_menu("Frequency"); 214 GUI::ActionGroup frequency_action_group; 215 frequency_action_group.set_exclusive(true); 216 217 auto make_frequency_action = [&](auto& title, int interval, bool checked = false) { 218 auto action = GUI::Action::create(title, [&refresh_timer, interval](auto& action) { 219 refresh_timer.restart(interval); 220 action.set_checked(true); 221 }); 222 action->set_checkable(true); 223 action->set_checked(checked); 224 frequency_action_group.add_action(*action); 225 frequency_menu.add_action(*action); 226 }; 227 228 make_frequency_action("0.25 sec", 250); 229 make_frequency_action("0.5 sec", 500); 230 make_frequency_action("1 sec", 1000, true); 231 make_frequency_action("3 sec", 3000); 232 make_frequency_action("5 sec", 5000); 233 234 auto& help_menu = menubar->add_menu("Help"); 235 help_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) { 236 GUI::AboutDialog::show("System Monitor", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-system-monitor.png"), window); 237 })); 238 239 app.set_menubar(move(menubar)); 240 241 auto& process_tab_widget = process_container_splitter.add<GUI::TabWidget>(); 242 243 auto& memory_map_widget = process_tab_widget.add_tab<ProcessMemoryMapWidget>("Memory map"); 244 auto& open_files_widget = process_tab_widget.add_tab<ProcessFileDescriptorMapWidget>("Open files"); 245 auto& unveiled_paths_widget = process_tab_widget.add_tab<ProcessUnveiledPathsWidget>("Unveiled paths"); 246 auto& stacks_widget = process_tab_widget.add_tab<ProcessStacksWidget>("Stacks"); 247 248 process_table_view.on_process_selected = [&](pid_t pid) { 249 open_files_widget.set_pid(pid); 250 stacks_widget.set_pid(pid); 251 memory_map_widget.set_pid(pid); 252 unveiled_paths_widget.set_pid(pid); 253 }; 254 255 window->show(); 256 257 window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-system-monitor.png")); 258 259 return app.exec(); 260} 261 262class ProgressBarPaintingDelegate final : public GUI::TableCellPaintingDelegate { 263public: 264 virtual ~ProgressBarPaintingDelegate() override {} 265 266 virtual void paint(GUI::Painter& painter, const Gfx::Rect& a_rect, const Palette& palette, const GUI::Model& model, const GUI::ModelIndex& index) override 267 { 268 auto rect = a_rect.shrunken(2, 2); 269 auto percentage = model.data(index, GUI::Model::Role::Custom).to_i32(); 270 271 auto data = model.data(index, GUI::Model::Role::Display); 272 String text; 273 if (data.is_string()) 274 text = data.as_string(); 275 Gfx::StylePainter::paint_progress_bar(painter, rect, palette, 0, 100, percentage, text); 276 painter.draw_rect(rect, Color::Black); 277 } 278}; 279 280NonnullRefPtr<GUI::Widget> build_file_systems_tab() 281{ 282 auto fs_widget = GUI::LazyWidget::construct(); 283 284 fs_widget->on_first_show = [](GUI::LazyWidget& self) { 285 self.set_layout<GUI::VerticalBoxLayout>(); 286 self.layout()->set_margins({ 4, 4, 4, 4 }); 287 auto& fs_table_view = self.add<GUI::TableView>(); 288 fs_table_view.set_size_columns_to_fit_content(true); 289 290 Vector<GUI::JsonArrayModel::FieldSpec> df_fields; 291 df_fields.empend("mount_point", "Mount point", Gfx::TextAlignment::CenterLeft); 292 df_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft); 293 df_fields.empend("source", "Source", Gfx::TextAlignment::CenterLeft); 294 df_fields.empend( 295 "Size", Gfx::TextAlignment::CenterRight, 296 [](const JsonObject& object) { 297 StringBuilder size_builder; 298 size_builder.append(" "); 299 size_builder.append(human_readable_size(object.get("total_block_count").to_u32() * object.get("block_size").to_u32())); 300 size_builder.append(" "); 301 return size_builder.to_string(); 302 }, 303 [](const JsonObject& object) { 304 return object.get("total_block_count").to_u32() * object.get("block_size").to_u32(); 305 }, 306 [](const JsonObject& object) { 307 auto total_blocks = object.get("total_block_count").to_u32(); 308 if (total_blocks == 0) 309 return 0; 310 auto free_blocks = object.get("free_block_count").to_u32(); 311 auto used_blocks = total_blocks - free_blocks; 312 int percentage = (int)((float)used_blocks / (float)total_blocks * 100.0f); 313 return percentage; 314 }); 315 df_fields.empend( 316 "Used", Gfx::TextAlignment::CenterRight, 317 [](const JsonObject& object) { 318 auto total_blocks = object.get("total_block_count").to_u32(); 319 auto free_blocks = object.get("free_block_count").to_u32(); 320 auto used_blocks = total_blocks - free_blocks; 321 return human_readable_size(used_blocks * object.get("block_size").to_u32()); }, 322 [](const JsonObject& object) { 323 auto total_blocks = object.get("total_block_count").to_u32(); 324 auto free_blocks = object.get("free_block_count").to_u32(); 325 auto used_blocks = total_blocks - free_blocks; 326 return used_blocks * object.get("block_size").to_u32(); 327 }); 328 df_fields.empend( 329 "Available", Gfx::TextAlignment::CenterRight, 330 [](const JsonObject& object) { 331 return human_readable_size(object.get("free_block_count").to_u32() * object.get("block_size").to_u32()); 332 }, 333 [](const JsonObject& object) { 334 return object.get("free_block_count").to_u32() * object.get("block_size").to_u32(); 335 }); 336 df_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](const JsonObject& object) { 337 return object.get("readonly").to_bool() ? "Read-only" : "Read/Write"; 338 }); 339 df_fields.empend("Mount flags", Gfx::TextAlignment::CenterLeft, [](const JsonObject& object) { 340 int mount_flags = object.get("mount_flags").to_int(); 341 StringBuilder builder; 342 bool first = true; 343 auto check = [&](int flag, const char* name) { 344 if (!(mount_flags & flag)) 345 return; 346 if (!first) 347 builder.append(','); 348 builder.append(name); 349 first = false; 350 }; 351 352#ifdef __OpenBSD__ 353 check(MNT_NODEV, "nodev"); 354 check(MNT_NOEXEC, "noexec"); 355 check(MNT_NOSUID, "nosuid"); 356#else 357 check(MS_NODEV, "nodev"); 358 check(MS_NOEXEC, "noexec"); 359 check(MS_NOSUID, "nosuid"); 360 check(MS_BIND, "bind"); 361#endif 362 if (builder.string_view().is_empty()) 363 return String("defaults"); 364 return builder.to_string(); 365 }); 366 df_fields.empend("free_block_count", "Free blocks", Gfx::TextAlignment::CenterRight); 367 df_fields.empend("total_block_count", "Total blocks", Gfx::TextAlignment::CenterRight); 368 df_fields.empend("free_inode_count", "Free inodes", Gfx::TextAlignment::CenterRight); 369 df_fields.empend("total_inode_count", "Total inodes", Gfx::TextAlignment::CenterRight); 370 df_fields.empend("block_size", "Block size", Gfx::TextAlignment::CenterRight); 371 fs_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/df", move(df_fields)))); 372 373 fs_table_view.set_cell_painting_delegate(3, make<ProgressBarPaintingDelegate>()); 374 375 fs_table_view.model()->update(); 376 }; 377 return fs_widget; 378} 379 380NonnullRefPtr<GUI::Widget> build_pci_devices_tab() 381{ 382 auto pci_widget = GUI::LazyWidget::construct(); 383 384 pci_widget->on_first_show = [](GUI::LazyWidget& self) { 385 self.set_layout<GUI::VerticalBoxLayout>(); 386 self.layout()->set_margins({ 4, 4, 4, 4 }); 387 auto& pci_table_view = self.add<GUI::TableView>(); 388 pci_table_view.set_size_columns_to_fit_content(true); 389 390 auto db = PCIDB::Database::open(); 391 392 Vector<GUI::JsonArrayModel::FieldSpec> pci_fields; 393 pci_fields.empend( 394 "Address", Gfx::TextAlignment::CenterLeft, 395 [](const JsonObject& object) { 396 auto seg = object.get("seg").to_u32(); 397 auto bus = object.get("bus").to_u32(); 398 auto slot = object.get("slot").to_u32(); 399 auto function = object.get("function").to_u32(); 400 return String::format("%04x:%02x:%02x.%d", seg, bus, slot, function); 401 }); 402 pci_fields.empend( 403 "Class", Gfx::TextAlignment::CenterLeft, 404 [db](const JsonObject& object) { 405 auto class_id = object.get("class").to_u32(); 406 String class_name = db->get_class(class_id); 407 return class_name == "" ? String::format("%04x", class_id) : class_name; 408 }); 409 pci_fields.empend( 410 "Vendor", Gfx::TextAlignment::CenterLeft, 411 [db](const JsonObject& object) { 412 auto vendor_id = object.get("vendor_id").to_u32(); 413 String vendor_name = db->get_vendor(vendor_id); 414 return vendor_name == "" ? String::format("%02x", vendor_id) : vendor_name; 415 }); 416 pci_fields.empend( 417 "Device", Gfx::TextAlignment::CenterLeft, 418 [db](const JsonObject& object) { 419 auto vendor_id = object.get("vendor_id").to_u32(); 420 auto device_id = object.get("device_id").to_u32(); 421 String device_name = db->get_device(vendor_id, device_id); 422 return device_name == "" ? String::format("%02x", device_id) : device_name; 423 }); 424 pci_fields.empend( 425 "Revision", Gfx::TextAlignment::CenterRight, 426 [](const JsonObject& object) { 427 auto revision_id = object.get("revision_id").to_u32(); 428 return String::format("%02x", revision_id); 429 }); 430 431 pci_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/pci", move(pci_fields)))); 432 pci_table_view.model()->update(); 433 }; 434 435 return pci_widget; 436} 437 438NonnullRefPtr<GUI::Widget> build_devices_tab() 439{ 440 auto devices_widget = GUI::LazyWidget::construct(); 441 442 devices_widget->on_first_show = [](GUI::LazyWidget& self) { 443 self.set_layout<GUI::VerticalBoxLayout>(); 444 self.layout()->set_margins({ 4, 4, 4, 4 }); 445 446 auto& devices_table_view = self.add<GUI::TableView>(); 447 devices_table_view.set_size_columns_to_fit_content(true); 448 devices_table_view.set_model(GUI::SortingProxyModel::create(DevicesModel::create())); 449 devices_table_view.model()->update(); 450 }; 451 452 return devices_widget; 453} 454 455NonnullRefPtr<GUI::Widget> build_graphs_tab() 456{ 457 auto graphs_container = GUI::LazyWidget::construct(); 458 459 graphs_container->on_first_show = [](GUI::LazyWidget& self) { 460 self.set_fill_with_background_color(true); 461 self.set_background_role(ColorRole::Button); 462 self.set_layout<GUI::VerticalBoxLayout>(); 463 self.layout()->set_margins({ 4, 4, 4, 4 }); 464 465 auto& cpu_graph_group_box = self.add<GUI::GroupBox>("CPU usage"); 466 cpu_graph_group_box.set_layout<GUI::VerticalBoxLayout>(); 467 cpu_graph_group_box.layout()->set_margins({ 6, 16, 6, 6 }); 468 cpu_graph_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); 469 cpu_graph_group_box.set_preferred_size(0, 120); 470 auto& cpu_graph = cpu_graph_group_box.add<GraphWidget>(); 471 cpu_graph.set_max(100); 472 cpu_graph.set_text_color(Color::Green); 473 cpu_graph.set_graph_color(Color::from_rgb(0x00bb00)); 474 cpu_graph.text_formatter = [](int value, int) { 475 return String::format("%d%%", value); 476 }; 477 478 ProcessModel::the().on_new_cpu_data_point = [graph = &cpu_graph](float cpu_percent) { 479 graph->add_value(cpu_percent); 480 }; 481 482 auto& memory_graph_group_box = self.add<GUI::GroupBox>("Memory usage"); 483 memory_graph_group_box.set_layout<GUI::VerticalBoxLayout>(); 484 memory_graph_group_box.layout()->set_margins({ 6, 16, 6, 6 }); 485 memory_graph_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed); 486 memory_graph_group_box.set_preferred_size(0, 120); 487 auto& memory_graph = memory_graph_group_box.add<GraphWidget>(); 488 memory_graph.set_text_color(Color::Cyan); 489 memory_graph.set_graph_color(Color::from_rgb(0x00bbbb)); 490 memory_graph.text_formatter = [](int value, int max) { 491 return String::format("%d / %d KB", value, max); 492 }; 493 494 self.add<MemoryStatsWidget>(memory_graph); 495 }; 496 return graphs_container; 497}