main.cpp 20 KB

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