main.cpp 19 KB

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