main.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "DevicesModel.h"
  2. #include "GraphWidget.h"
  3. #include "MemoryStatsWidget.h"
  4. #include "NetworkStatisticsWidget.h"
  5. #include "ProcessFileDescriptorMapWidget.h"
  6. #include "ProcessMemoryMapWidget.h"
  7. #include "ProcessModel.h"
  8. #include "ProcessStacksWidget.h"
  9. #include "ProcessTableView.h"
  10. #include <LibCore/CTimer.h>
  11. #include <LibDraw/PNGLoader.h>
  12. #include <LibGUI/GAboutDialog.h>
  13. #include <LibGUI/GAction.h>
  14. #include <LibGUI/GApplication.h>
  15. #include <LibGUI/GBoxLayout.h>
  16. #include <LibGUI/GGroupBox.h>
  17. #include <LibGUI/GJsonArrayModel.h>
  18. #include <LibGUI/GLabel.h>
  19. #include <LibGUI/GLazyWidget.h>
  20. #include <LibGUI/GMenuBar.h>
  21. #include <LibGUI/GPainter.h>
  22. #include <LibGUI/GSortingProxyModel.h>
  23. #include <LibGUI/GSplitter.h>
  24. #include <LibGUI/GTabWidget.h>
  25. #include <LibGUI/GToolBar.h>
  26. #include <LibGUI/GWidget.h>
  27. #include <LibGUI/GWindow.h>
  28. #include <LibPCIDB/Database.h>
  29. #include <signal.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. static String human_readable_size(u32 size)
  33. {
  34. if (size < (64 * KB))
  35. return String::format("%u", size);
  36. if (size < MB)
  37. return String::format("%u KB", size / KB);
  38. if (size < GB)
  39. return String::format("%u MB", size / MB);
  40. return String::format("%u GB", size / GB);
  41. }
  42. static RefPtr<GWidget> build_file_systems_tab();
  43. static RefPtr<GWidget> build_pci_devices_tab();
  44. static RefPtr<GWidget> build_devices_tab();
  45. static NonnullRefPtr<GWidget> build_graphs_tab();
  46. int main(int argc, char** argv)
  47. {
  48. GApplication app(argc, argv);
  49. auto keeper = GWidget::construct();
  50. keeper->set_layout(make<GBoxLayout>(Orientation::Vertical));
  51. keeper->set_fill_with_background_color(true);
  52. keeper->set_background_color(Color::WarmGray);
  53. keeper->layout()->set_margins({ 4, 4, 4, 4 });
  54. auto tabwidget = GTabWidget::construct(keeper);
  55. auto process_container_splitter = GSplitter::construct(Orientation::Vertical, nullptr);
  56. tabwidget->add_widget("Processes", process_container_splitter);
  57. auto process_table_container = GWidget::construct(process_container_splitter.ptr());
  58. tabwidget->add_widget("Graphs", build_graphs_tab());
  59. tabwidget->add_widget("File systems", build_file_systems_tab());
  60. tabwidget->add_widget("PCI devices", build_pci_devices_tab());
  61. tabwidget->add_widget("Devices", build_devices_tab());
  62. auto network_stats_widget = NetworkStatisticsWidget::construct(nullptr);
  63. tabwidget->add_widget("Network", network_stats_widget);
  64. process_table_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
  65. process_table_container->layout()->set_margins({ 4, 0, 4, 4 });
  66. process_table_container->layout()->set_spacing(0);
  67. auto toolbar = GToolBar::construct(process_table_container);
  68. toolbar->set_has_frame(false);
  69. auto process_table_view = ProcessTableView::construct(process_table_container);
  70. auto refresh_timer = CTimer::construct(1000, [&] {
  71. process_table_view->refresh();
  72. if (auto* memory_stats_widget = MemoryStatsWidget::the())
  73. memory_stats_widget->refresh();
  74. });
  75. auto kill_action = GAction::create("Kill process", GraphicsBitmap::load_from_file("/res/icons/kill16.png"), [process_table_view](const GAction&) {
  76. pid_t pid = process_table_view->selected_pid();
  77. if (pid != -1)
  78. kill(pid, SIGKILL);
  79. });
  80. auto stop_action = GAction::create("Stop process", GraphicsBitmap::load_from_file("/res/icons/stop16.png"), [process_table_view](const GAction&) {
  81. pid_t pid = process_table_view->selected_pid();
  82. if (pid != -1)
  83. kill(pid, SIGSTOP);
  84. });
  85. auto continue_action = GAction::create("Continue process", GraphicsBitmap::load_from_file("/res/icons/continue16.png"), [process_table_view](const GAction&) {
  86. pid_t pid = process_table_view->selected_pid();
  87. if (pid != -1)
  88. kill(pid, SIGCONT);
  89. });
  90. toolbar->add_action(kill_action);
  91. toolbar->add_action(stop_action);
  92. toolbar->add_action(continue_action);
  93. auto window = GWindow::construct();
  94. window->set_title("System Monitor");
  95. window->set_rect(20, 200, 680, 400);
  96. window->set_main_widget(keeper);
  97. auto menubar = make<GMenuBar>();
  98. auto app_menu = GMenu::construct("System Monitor");
  99. app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
  100. GApplication::the().quit(0);
  101. return;
  102. }));
  103. menubar->add_menu(move(app_menu));
  104. auto process_menu = GMenu::construct("Process");
  105. process_menu->add_action(kill_action);
  106. process_menu->add_action(stop_action);
  107. process_menu->add_action(continue_action);
  108. menubar->add_menu(move(process_menu));
  109. auto process_context_menu = GMenu::construct();
  110. process_context_menu->add_action(kill_action);
  111. process_context_menu->add_action(stop_action);
  112. process_context_menu->add_action(continue_action);
  113. process_table_view->on_context_menu_request = [&](const GModelIndex& index, const GContextMenuEvent& event) {
  114. (void)index;
  115. process_context_menu->popup(event.screen_position());
  116. };
  117. auto frequency_menu = GMenu::construct("Frequency");
  118. frequency_menu->add_action(GAction::create("0.25 sec", [&](auto&) {
  119. refresh_timer->restart(250);
  120. }));
  121. frequency_menu->add_action(GAction::create("0.5 sec", [&](auto&) {
  122. refresh_timer->restart(500);
  123. }));
  124. frequency_menu->add_action(GAction::create("1 sec", [&](auto&) {
  125. refresh_timer->restart(1000);
  126. }));
  127. frequency_menu->add_action(GAction::create("3 sec", [&](auto&) {
  128. refresh_timer->restart(3000);
  129. }));
  130. frequency_menu->add_action(GAction::create("5 sec", [&](auto&) {
  131. refresh_timer->restart(5000);
  132. }));
  133. menubar->add_menu(move(frequency_menu));
  134. auto help_menu = GMenu::construct("Help");
  135. help_menu->add_action(GAction::create("About", [&](const GAction&) {
  136. GAboutDialog::show("SystemMonitor", load_png("/res/icons/32x32/app-system-monitor.png"), window);
  137. }));
  138. menubar->add_menu(move(help_menu));
  139. app.set_menubar(move(menubar));
  140. auto process_tab_widget = GTabWidget::construct(process_container_splitter);
  141. auto open_files_widget = ProcessFileDescriptorMapWidget::construct(nullptr);
  142. process_tab_widget->add_widget("Open files", open_files_widget);
  143. auto memory_map_widget = ProcessMemoryMapWidget::construct(nullptr);
  144. process_tab_widget->add_widget("Memory map", memory_map_widget);
  145. auto stacks_widget = ProcessStacksWidget::construct(nullptr);
  146. process_tab_widget->add_widget("Stacks", stacks_widget);
  147. process_table_view->on_process_selected = [&](pid_t pid) {
  148. open_files_widget->set_pid(pid);
  149. stacks_widget->set_pid(pid);
  150. memory_map_widget->set_pid(pid);
  151. };
  152. window->show();
  153. window->set_icon(load_png("/res/icons/16x16/app-system-monitor.png"));
  154. return app.exec();
  155. }
  156. class ProgressBarPaintingDelegate final : public GTableCellPaintingDelegate {
  157. public:
  158. virtual ~ProgressBarPaintingDelegate() override {}
  159. virtual void paint(GPainter& painter, const Rect& a_rect, const GModel& model, const GModelIndex& index) override
  160. {
  161. auto rect = a_rect.shrunken(2, 2);
  162. auto percentage = model.data(index, GModel::Role::Custom).to_int();
  163. auto data = model.data(index, GModel::Role::Display);
  164. String text;
  165. if (data.is_string())
  166. text = data.as_string();
  167. StylePainter::paint_progress_bar(painter, rect, 0, 100, percentage, text);
  168. painter.draw_rect(rect, Color::Black);
  169. }
  170. };
  171. RefPtr<GWidget> build_file_systems_tab()
  172. {
  173. auto fs_widget = GLazyWidget::construct();
  174. fs_widget->on_first_show = [](auto& self) {
  175. self.set_layout(make<GBoxLayout>(Orientation::Vertical));
  176. self.layout()->set_margins({ 4, 4, 4, 4 });
  177. auto fs_table_view = GTableView::construct(&self);
  178. fs_table_view->set_size_columns_to_fit_content(true);
  179. Vector<GJsonArrayModel::FieldSpec> df_fields;
  180. df_fields.empend("mount_point", "Mount point", TextAlignment::CenterLeft);
  181. df_fields.empend("class_name", "Class", TextAlignment::CenterLeft);
  182. df_fields.empend("device", "Device", TextAlignment::CenterLeft);
  183. df_fields.empend(
  184. "Size", TextAlignment::CenterRight,
  185. [](const JsonObject& object) {
  186. return human_readable_size(object.get("total_block_count").to_u32() * object.get("block_size").to_u32());
  187. },
  188. [](const JsonObject& object) {
  189. return object.get("total_block_count").to_u32() * object.get("block_size").to_u32();
  190. });
  191. df_fields.empend(
  192. "Used", TextAlignment::CenterRight,
  193. [](const JsonObject& object) {
  194. auto total_blocks = object.get("total_block_count").to_u32();
  195. auto free_blocks = object.get("free_block_count").to_u32();
  196. auto used_blocks = total_blocks - free_blocks;
  197. return human_readable_size(used_blocks * object.get("block_size").to_u32()); },
  198. [](const JsonObject& object) {
  199. auto total_blocks = object.get("total_block_count").to_u32();
  200. auto free_blocks = object.get("free_block_count").to_u32();
  201. auto used_blocks = total_blocks - free_blocks;
  202. return used_blocks * object.get("block_size").to_u32();
  203. },
  204. [](const JsonObject& object) {
  205. auto total_blocks = object.get("total_block_count").to_u32();
  206. if (total_blocks == 0)
  207. return 0;
  208. auto free_blocks = object.get("free_block_count").to_u32();
  209. auto used_blocks = total_blocks - free_blocks;
  210. int percentage = (int)((float)used_blocks / (float)total_blocks * 100.0f);
  211. return percentage;
  212. });
  213. df_fields.empend(
  214. "Available", TextAlignment::CenterRight,
  215. [](const JsonObject& object) {
  216. return human_readable_size(object.get("free_block_count").to_u32() * object.get("block_size").to_u32());
  217. },
  218. [](const JsonObject& object) {
  219. return object.get("free_block_count").to_u32() * object.get("block_size").to_u32();
  220. });
  221. df_fields.empend("Access", TextAlignment::CenterLeft, [](const JsonObject& object) {
  222. return object.get("readonly").to_bool() ? "Read-only" : "Read/Write";
  223. });
  224. df_fields.empend("free_block_count", "Free blocks", TextAlignment::CenterRight);
  225. df_fields.empend("total_block_count", "Total blocks", TextAlignment::CenterRight);
  226. df_fields.empend("free_inode_count", "Free inodes", TextAlignment::CenterRight);
  227. df_fields.empend("total_inode_count", "Total inodes", TextAlignment::CenterRight);
  228. df_fields.empend("block_size", "Block size", TextAlignment::CenterRight);
  229. fs_table_view->set_model(GSortingProxyModel::create(GJsonArrayModel::create("/proc/df", move(df_fields))));
  230. fs_table_view->set_cell_painting_delegate(3, make<ProgressBarPaintingDelegate>());
  231. fs_table_view->model()->update();
  232. };
  233. return fs_widget;
  234. }
  235. RefPtr<GWidget> build_pci_devices_tab()
  236. {
  237. auto pci_widget = GLazyWidget::construct();
  238. pci_widget->on_first_show = [](auto& self) {
  239. self.set_layout(make<GBoxLayout>(Orientation::Vertical));
  240. self.layout()->set_margins({ 4, 4, 4, 4 });
  241. auto pci_table_view = GTableView::construct(&self);
  242. pci_table_view->set_size_columns_to_fit_content(true);
  243. auto db = PCIDB::Database::open();
  244. Vector<GJsonArrayModel::FieldSpec> pci_fields;
  245. pci_fields.empend(
  246. "Address", TextAlignment::CenterLeft,
  247. [](const JsonObject& object) {
  248. auto bus = object.get("bus").to_u32();
  249. auto slot = object.get("slot").to_u32();
  250. auto function = object.get("function").to_u32();
  251. return String::format("%02x:%02x.%d", bus, slot, function);
  252. });
  253. pci_fields.empend(
  254. "Class", TextAlignment::CenterLeft,
  255. [db](const JsonObject& object) {
  256. auto class_id = object.get("class").to_u32();
  257. String class_name = db->get_class(class_id);
  258. return class_name == "" ? String::format("%04x", class_id) : class_name;
  259. });
  260. pci_fields.empend(
  261. "Vendor", TextAlignment::CenterLeft,
  262. [db](const JsonObject& object) {
  263. auto vendor_id = object.get("vendor_id").to_u32();
  264. String vendor_name = db->get_vendor(vendor_id);
  265. return vendor_name == "" ? String::format("%02x", vendor_id) : vendor_name;
  266. });
  267. pci_fields.empend(
  268. "Device", TextAlignment::CenterLeft,
  269. [db](const JsonObject& object) {
  270. auto vendor_id = object.get("vendor_id").to_u32();
  271. auto device_id = object.get("device_id").to_u32();
  272. String device_name = db->get_device(vendor_id, device_id);
  273. return device_name == "" ? String::format("%02x", device_id) : device_name;
  274. });
  275. pci_fields.empend(
  276. "Revision", TextAlignment::CenterRight,
  277. [](const JsonObject& object) {
  278. auto revision_id = object.get("revision_id").to_u32();
  279. return String::format("%02x", revision_id);
  280. });
  281. pci_table_view->set_model(GSortingProxyModel::create(GJsonArrayModel::create("/proc/pci", move(pci_fields))));
  282. pci_table_view->model()->update();
  283. };
  284. return pci_widget;
  285. }
  286. RefPtr<GWidget> build_devices_tab()
  287. {
  288. auto devices_widget = GLazyWidget::construct();
  289. devices_widget->on_first_show = [](auto& self) {
  290. self.set_layout(make<GBoxLayout>(Orientation::Vertical));
  291. self.layout()->set_margins({ 4, 4, 4, 4 });
  292. auto devices_table_view = GTableView::construct(&self);
  293. devices_table_view->set_size_columns_to_fit_content(true);
  294. devices_table_view->set_model(GSortingProxyModel::create(DevicesModel::create()));
  295. devices_table_view->model()->update();
  296. };
  297. return devices_widget;
  298. }
  299. NonnullRefPtr<GWidget> build_graphs_tab()
  300. {
  301. auto graphs_container = GLazyWidget::construct();
  302. graphs_container->on_first_show = [](auto& self) {
  303. self.set_fill_with_background_color(true);
  304. self.set_background_color(Color::WarmGray);
  305. self.set_layout(make<GBoxLayout>(Orientation::Vertical));
  306. self.layout()->set_margins({ 4, 4, 4, 4 });
  307. auto cpu_graph_group_box = GGroupBox::construct("CPU usage", &self);
  308. cpu_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
  309. cpu_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  310. cpu_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  311. cpu_graph_group_box->set_preferred_size(0, 120);
  312. auto cpu_graph = GraphWidget::construct(cpu_graph_group_box);
  313. cpu_graph->set_max(100);
  314. cpu_graph->set_text_color(Color::Green);
  315. cpu_graph->set_graph_color(Color::from_rgb(0x00bb00));
  316. cpu_graph->text_formatter = [](int value, int) {
  317. return String::format("%d%%", value);
  318. };
  319. ProcessModel::the().on_new_cpu_data_point = [graph = cpu_graph.ptr()](float cpu_percent) {
  320. graph->add_value(cpu_percent);
  321. };
  322. auto memory_graph_group_box = GGroupBox::construct("Memory usage", &self);
  323. memory_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
  324. memory_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
  325. memory_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  326. memory_graph_group_box->set_preferred_size(0, 120);
  327. auto memory_graph = GraphWidget::construct(memory_graph_group_box);
  328. memory_graph->set_text_color(Color::Cyan);
  329. memory_graph->set_graph_color(Color::from_rgb(0x00bbbb));
  330. memory_graph->text_formatter = [](int value, int max) {
  331. return String::format("%d / %d KB", value, max);
  332. };
  333. auto memory_stats_widget = MemoryStatsWidget::construct(*memory_graph, &self);
  334. };
  335. return graphs_container;
  336. }