main.cpp 19 KB

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