main.cpp 20 KB

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