main.cpp 15 KB

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