main.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 "ProcessUnveiledPathsWidget.h"
  34. #include "ThreadStackWidget.h"
  35. #include <AK/NumberFormat.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/TableView.h>
  53. #include <LibGUI/ToolBar.h>
  54. #include <LibGUI/Widget.h>
  55. #include <LibGUI/Window.h>
  56. #include <LibGfx/Palette.h>
  57. #include <LibPCIDB/Database.h>
  58. #include <serenity.h>
  59. #include <signal.h>
  60. #include <spawn.h>
  61. #include <stdio.h>
  62. #include <unistd.h>
  63. static NonnullRefPtr<GUI::Widget> build_file_systems_tab();
  64. static NonnullRefPtr<GUI::Widget> build_pci_devices_tab();
  65. static NonnullRefPtr<GUI::Widget> build_devices_tab();
  66. static NonnullRefPtr<GUI::Widget> build_graphs_tab();
  67. class UnavailableProcessWidget final : public GUI::Frame {
  68. C_OBJECT(UnavailableProcessWidget)
  69. public:
  70. virtual ~UnavailableProcessWidget() override { }
  71. const String& text() const { return m_text; }
  72. void set_text(String text) { m_text = move(text); }
  73. private:
  74. UnavailableProcessWidget(String text)
  75. : m_text(move(text))
  76. {
  77. }
  78. virtual void paint_event(GUI::PaintEvent& event) override
  79. {
  80. Frame::paint_event(event);
  81. if (text().is_empty())
  82. return;
  83. GUI::Painter painter(*this);
  84. painter.add_clip_rect(event.rect());
  85. painter.draw_text(frame_inner_rect(), text(), Gfx::TextAlignment::Center, palette().window_text(), Gfx::TextElision::Right);
  86. }
  87. String m_text;
  88. };
  89. static bool can_access_pid(pid_t pid)
  90. {
  91. auto path = String::format("/proc/%d", pid);
  92. return access(path.characters(), X_OK) == 0;
  93. }
  94. int main(int argc, char** argv)
  95. {
  96. if (pledge("stdio proc shared_buffer accept rpath exec unix cpath fattr", nullptr) < 0) {
  97. perror("pledge");
  98. return 1;
  99. }
  100. auto app = GUI::Application::construct(argc, argv);
  101. if (pledge("stdio proc shared_buffer accept rpath exec", nullptr) < 0) {
  102. perror("pledge");
  103. return 1;
  104. }
  105. if (unveil("/etc/passwd", "r") < 0) {
  106. perror("unveil");
  107. return 1;
  108. }
  109. if (unveil("/res", "r") < 0) {
  110. perror("unveil");
  111. return 1;
  112. }
  113. if (unveil("/proc", "r") < 0) {
  114. perror("unveil");
  115. return 1;
  116. }
  117. if (unveil("/dev", "r") < 0) {
  118. perror("unveil");
  119. return 1;
  120. }
  121. if (unveil("/bin/Profiler", "x") < 0) {
  122. perror("unveil");
  123. return 1;
  124. }
  125. if (unveil("/bin/Inspector", "x") < 0) {
  126. perror("unveil");
  127. return 1;
  128. }
  129. unveil(nullptr, nullptr);
  130. auto window = GUI::Window::construct();
  131. window->set_title("System Monitor");
  132. window->resize(680, 400);
  133. auto& keeper = window->set_main_widget<GUI::Widget>();
  134. keeper.set_layout<GUI::VerticalBoxLayout>();
  135. keeper.set_fill_with_background_color(true);
  136. keeper.layout()->set_margins({ 2, 2, 2, 2 });
  137. auto& tabwidget = keeper.add<GUI::TabWidget>();
  138. auto& process_container_splitter = tabwidget.add_tab<GUI::VerticalSplitter>("Processes");
  139. process_container_splitter.layout()->set_margins({ 4, 4, 4, 4 });
  140. auto& process_table_container = process_container_splitter.add<GUI::Widget>();
  141. tabwidget.add_widget("Graphs", build_graphs_tab());
  142. tabwidget.add_widget("File systems", build_file_systems_tab());
  143. tabwidget.add_widget("PCI devices", build_pci_devices_tab());
  144. tabwidget.add_widget("Devices", build_devices_tab());
  145. auto network_stats_widget = NetworkStatisticsWidget::construct();
  146. tabwidget.add_widget("Network", network_stats_widget);
  147. process_table_container.set_layout<GUI::VerticalBoxLayout>();
  148. process_table_container.layout()->set_spacing(0);
  149. auto& process_table_view = process_table_container.add<GUI::TableView>();
  150. process_table_view.set_headers_visible(true);
  151. process_table_view.set_model(GUI::SortingProxyModel::create(ProcessModel::create()));
  152. process_table_view.set_key_column_and_sort_order(ProcessModel::Column::CPU, GUI::SortOrder::Descending);
  153. process_table_view.model()->update();
  154. auto& refresh_timer = window->add<Core::Timer>(
  155. 3000, [&] {
  156. process_table_view.model()->update();
  157. if (auto* memory_stats_widget = MemoryStatsWidget::the())
  158. memory_stats_widget->refresh();
  159. });
  160. auto selected_id = [&](ProcessModel::Column column) -> pid_t {
  161. if (process_table_view.selection().is_empty())
  162. return -1;
  163. auto pid_index = process_table_view.model()->index(process_table_view.selection().first().row(), column);
  164. return pid_index.data().to_i32();
  165. };
  166. auto kill_action = GUI::Action::create("Kill process", { Mod_Ctrl, Key_K }, Gfx::Bitmap::load_from_file("/res/icons/kill16.png"), [&](const GUI::Action&) {
  167. pid_t pid = selected_id(ProcessModel::Column::PID);
  168. if (pid != -1)
  169. kill(pid, SIGKILL);
  170. });
  171. auto stop_action = GUI::Action::create("Stop process", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/stop16.png"), [&](const GUI::Action&) {
  172. pid_t pid = selected_id(ProcessModel::Column::PID);
  173. if (pid != -1)
  174. kill(pid, SIGSTOP);
  175. });
  176. auto continue_action = GUI::Action::create("Continue process", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/continue16.png"), [&](const GUI::Action&) {
  177. pid_t pid = selected_id(ProcessModel::Column::PID);
  178. if (pid != -1)
  179. kill(pid, SIGCONT);
  180. });
  181. auto profile_action = GUI::Action::create("Profile process", { Mod_Ctrl, Key_P },
  182. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"), [&](auto&) {
  183. pid_t pid = selected_id(ProcessModel::Column::PID);
  184. if (pid != -1) {
  185. auto pid_string = String::format("%d", pid);
  186. pid_t child;
  187. const char* argv[] = { "/bin/Profiler", "--pid", pid_string.characters(), nullptr };
  188. if ((errno = posix_spawn(&child, "/bin/Profiler", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  189. perror("posix_spawn");
  190. } else {
  191. if (disown(child) < 0)
  192. perror("disown");
  193. }
  194. }
  195. });
  196. auto inspect_action = GUI::Action::create("Inspect process", { Mod_Ctrl, Key_I },
  197. Gfx::Bitmap::load_from_file("/res/icons/16x16/app-inspector.png"), [&](auto&) {
  198. pid_t pid = selected_id(ProcessModel::Column::PID);
  199. if (pid != -1) {
  200. auto pid_string = String::format("%d", pid);
  201. pid_t child;
  202. const char* argv[] = { "/bin/Inspector", pid_string.characters(), nullptr };
  203. if ((errno = posix_spawn(&child, "/bin/Inspector", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  204. perror("posix_spawn");
  205. } else {
  206. if (disown(child) < 0)
  207. perror("disown");
  208. }
  209. }
  210. });
  211. auto menubar = GUI::MenuBar::construct();
  212. auto& app_menu = menubar->add_menu("System Monitor");
  213. app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
  214. GUI::Application::the()->quit();
  215. return;
  216. }));
  217. auto& process_menu = menubar->add_menu("Process");
  218. process_menu.add_action(kill_action);
  219. process_menu.add_action(stop_action);
  220. process_menu.add_action(continue_action);
  221. process_menu.add_separator();
  222. process_menu.add_action(profile_action);
  223. process_menu.add_action(inspect_action);
  224. auto process_context_menu = GUI::Menu::construct();
  225. process_context_menu->add_action(kill_action);
  226. process_context_menu->add_action(stop_action);
  227. process_context_menu->add_action(continue_action);
  228. process_context_menu->add_separator();
  229. process_context_menu->add_action(profile_action);
  230. process_context_menu->add_action(inspect_action);
  231. process_table_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  232. (void)index;
  233. process_context_menu->popup(event.screen_position());
  234. };
  235. auto& frequency_menu = menubar->add_menu("Frequency");
  236. GUI::ActionGroup frequency_action_group;
  237. frequency_action_group.set_exclusive(true);
  238. auto make_frequency_action = [&](auto& title, int interval, bool checked = false) {
  239. auto action = GUI::Action::create_checkable(title, [&refresh_timer, interval](auto&) {
  240. refresh_timer.restart(interval);
  241. });
  242. action->set_checked(checked);
  243. frequency_action_group.add_action(*action);
  244. frequency_menu.add_action(*action);
  245. };
  246. make_frequency_action("1 sec", 1000);
  247. make_frequency_action("3 sec", 3000, true);
  248. make_frequency_action("5 sec", 5000);
  249. auto& help_menu = menubar->add_menu("Help");
  250. help_menu.add_action(GUI::Action::create("About", [&](const GUI::Action&) {
  251. GUI::AboutDialog::show("System Monitor", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-system-monitor.png"), window);
  252. }));
  253. app->set_menubar(move(menubar));
  254. auto& process_tab_unused_widget = process_container_splitter.add<UnavailableProcessWidget>("No process selected");
  255. process_tab_unused_widget.set_visible(true);
  256. auto& process_tab_widget = process_container_splitter.add<GUI::TabWidget>();
  257. process_tab_widget.set_visible(false);
  258. auto& memory_map_widget = process_tab_widget.add_tab<ProcessMemoryMapWidget>("Memory map");
  259. auto& open_files_widget = process_tab_widget.add_tab<ProcessFileDescriptorMapWidget>("Open files");
  260. auto& unveiled_paths_widget = process_tab_widget.add_tab<ProcessUnveiledPathsWidget>("Unveiled paths");
  261. auto& stack_widget = process_tab_widget.add_tab<ThreadStackWidget>("Stack");
  262. process_table_view.on_selection = [&](auto&) {
  263. auto pid = selected_id(ProcessModel::Column::PID);
  264. auto tid = selected_id(ProcessModel::Column::TID);
  265. if (!can_access_pid(pid)) {
  266. process_tab_widget.set_visible(false);
  267. process_tab_unused_widget.set_text("Process cannot be accessed");
  268. process_tab_unused_widget.set_visible(true);
  269. return;
  270. }
  271. process_tab_widget.set_visible(true);
  272. process_tab_unused_widget.set_visible(false);
  273. open_files_widget.set_pid(pid);
  274. stack_widget.set_ids(pid, tid);
  275. memory_map_widget.set_pid(pid);
  276. unveiled_paths_widget.set_pid(pid);
  277. };
  278. window->show();
  279. window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-system-monitor.png"));
  280. return app->exec();
  281. }
  282. class ProgressBarPaintingDelegate final : public GUI::TableCellPaintingDelegate {
  283. public:
  284. virtual ~ProgressBarPaintingDelegate() override { }
  285. virtual void paint(GUI::Painter& painter, const Gfx::IntRect& a_rect, const Palette& palette, const GUI::ModelIndex& index) override
  286. {
  287. auto rect = a_rect.shrunken(2, 2);
  288. auto percentage = index.data(GUI::ModelRole::Custom).to_i32();
  289. auto data = index.data();
  290. String text;
  291. if (data.is_string())
  292. text = data.as_string();
  293. Gfx::StylePainter::paint_progress_bar(painter, rect, palette, 0, 100, percentage, text);
  294. painter.draw_rect(rect, Color::Black);
  295. }
  296. };
  297. NonnullRefPtr<GUI::Widget> build_file_systems_tab()
  298. {
  299. auto fs_widget = GUI::LazyWidget::construct();
  300. fs_widget->on_first_show = [](GUI::LazyWidget& self) {
  301. self.set_layout<GUI::VerticalBoxLayout>();
  302. self.layout()->set_margins({ 4, 4, 4, 4 });
  303. auto& fs_table_view = self.add<GUI::TableView>();
  304. Vector<GUI::JsonArrayModel::FieldSpec> df_fields;
  305. df_fields.empend("mount_point", "Mount point", Gfx::TextAlignment::CenterLeft);
  306. df_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft);
  307. df_fields.empend("source", "Source", Gfx::TextAlignment::CenterLeft);
  308. df_fields.empend(
  309. "Size", Gfx::TextAlignment::CenterRight,
  310. [](const JsonObject& object) {
  311. StringBuilder size_builder;
  312. size_builder.append(" ");
  313. size_builder.append(human_readable_size(object.get("total_block_count").to_u32() * object.get("block_size").to_u32()));
  314. size_builder.append(" ");
  315. return size_builder.to_string();
  316. },
  317. [](const JsonObject& object) {
  318. return object.get("total_block_count").to_u32() * object.get("block_size").to_u32();
  319. },
  320. [](const JsonObject& object) {
  321. auto total_blocks = object.get("total_block_count").to_u32();
  322. if (total_blocks == 0)
  323. return 0;
  324. auto free_blocks = object.get("free_block_count").to_u32();
  325. auto used_blocks = total_blocks - free_blocks;
  326. int percentage = (int)((float)used_blocks / (float)total_blocks * 100.0f);
  327. return percentage;
  328. });
  329. df_fields.empend(
  330. "Used", Gfx::TextAlignment::CenterRight,
  331. [](const JsonObject& object) {
  332. auto total_blocks = object.get("total_block_count").to_u32();
  333. auto free_blocks = object.get("free_block_count").to_u32();
  334. auto used_blocks = total_blocks - free_blocks;
  335. return human_readable_size(used_blocks * object.get("block_size").to_u32()); },
  336. [](const JsonObject& object) {
  337. auto total_blocks = object.get("total_block_count").to_u32();
  338. auto free_blocks = object.get("free_block_count").to_u32();
  339. auto used_blocks = total_blocks - free_blocks;
  340. return used_blocks * object.get("block_size").to_u32();
  341. });
  342. df_fields.empend(
  343. "Available", Gfx::TextAlignment::CenterRight,
  344. [](const JsonObject& object) {
  345. return human_readable_size(object.get("free_block_count").to_u32() * object.get("block_size").to_u32());
  346. },
  347. [](const JsonObject& object) {
  348. return object.get("free_block_count").to_u32() * object.get("block_size").to_u32();
  349. });
  350. df_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](const JsonObject& object) {
  351. bool readonly = object.get("readonly").to_bool();
  352. int mount_flags = object.get("mount_flags").to_int();
  353. return readonly || (mount_flags & MS_RDONLY) ? "Read-only" : "Read/Write";
  354. });
  355. df_fields.empend("Mount flags", Gfx::TextAlignment::CenterLeft, [](const JsonObject& object) {
  356. int mount_flags = object.get("mount_flags").to_int();
  357. StringBuilder builder;
  358. bool first = true;
  359. auto check = [&](int flag, const char* name) {
  360. if (!(mount_flags & flag))
  361. return;
  362. if (!first)
  363. builder.append(',');
  364. builder.append(name);
  365. first = false;
  366. };
  367. check(MS_NODEV, "nodev");
  368. check(MS_NOEXEC, "noexec");
  369. check(MS_NOSUID, "nosuid");
  370. check(MS_BIND, "bind");
  371. check(MS_RDONLY, "ro");
  372. if (builder.string_view().is_empty())
  373. return String("defaults");
  374. return builder.to_string();
  375. });
  376. df_fields.empend("free_block_count", "Free blocks", Gfx::TextAlignment::CenterRight);
  377. df_fields.empend("total_block_count", "Total blocks", Gfx::TextAlignment::CenterRight);
  378. df_fields.empend("free_inode_count", "Free inodes", Gfx::TextAlignment::CenterRight);
  379. df_fields.empend("total_inode_count", "Total inodes", Gfx::TextAlignment::CenterRight);
  380. df_fields.empend("block_size", "Block size", Gfx::TextAlignment::CenterRight);
  381. fs_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/df", move(df_fields))));
  382. fs_table_view.set_cell_painting_delegate(3, make<ProgressBarPaintingDelegate>());
  383. fs_table_view.model()->update();
  384. };
  385. return fs_widget;
  386. }
  387. NonnullRefPtr<GUI::Widget> build_pci_devices_tab()
  388. {
  389. auto pci_widget = GUI::LazyWidget::construct();
  390. pci_widget->on_first_show = [](GUI::LazyWidget& self) {
  391. self.set_layout<GUI::VerticalBoxLayout>();
  392. self.layout()->set_margins({ 4, 4, 4, 4 });
  393. auto& pci_table_view = self.add<GUI::TableView>();
  394. auto db = PCIDB::Database::open();
  395. Vector<GUI::JsonArrayModel::FieldSpec> pci_fields;
  396. pci_fields.empend(
  397. "Address", Gfx::TextAlignment::CenterLeft,
  398. [](const JsonObject& object) {
  399. auto seg = object.get("seg").to_u32();
  400. auto bus = object.get("bus").to_u32();
  401. auto slot = object.get("slot").to_u32();
  402. auto function = object.get("function").to_u32();
  403. return String::format("%04x:%02x:%02x.%d", seg, bus, slot, function);
  404. });
  405. pci_fields.empend(
  406. "Class", Gfx::TextAlignment::CenterLeft,
  407. [db](const JsonObject& object) {
  408. auto class_id = object.get("class").to_u32();
  409. String class_name = db->get_class(class_id);
  410. return class_name == "" ? String::format("%04x", class_id) : class_name;
  411. });
  412. pci_fields.empend(
  413. "Vendor", Gfx::TextAlignment::CenterLeft,
  414. [db](const JsonObject& object) {
  415. auto vendor_id = object.get("vendor_id").to_u32();
  416. String vendor_name = db->get_vendor(vendor_id);
  417. return vendor_name == "" ? String::format("%02x", vendor_id) : vendor_name;
  418. });
  419. pci_fields.empend(
  420. "Device", Gfx::TextAlignment::CenterLeft,
  421. [db](const JsonObject& object) {
  422. auto vendor_id = object.get("vendor_id").to_u32();
  423. auto device_id = object.get("device_id").to_u32();
  424. String device_name = db->get_device(vendor_id, device_id);
  425. return device_name == "" ? String::format("%02x", device_id) : device_name;
  426. });
  427. pci_fields.empend(
  428. "Revision", Gfx::TextAlignment::CenterRight,
  429. [](const JsonObject& object) {
  430. auto revision_id = object.get("revision_id").to_u32();
  431. return String::format("%02x", revision_id);
  432. });
  433. pci_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/pci", move(pci_fields))));
  434. pci_table_view.model()->update();
  435. };
  436. return pci_widget;
  437. }
  438. NonnullRefPtr<GUI::Widget> build_devices_tab()
  439. {
  440. auto devices_widget = GUI::LazyWidget::construct();
  441. devices_widget->on_first_show = [](GUI::LazyWidget& self) {
  442. self.set_layout<GUI::VerticalBoxLayout>();
  443. self.layout()->set_margins({ 4, 4, 4, 4 });
  444. auto& devices_table_view = self.add<GUI::TableView>();
  445. devices_table_view.set_model(GUI::SortingProxyModel::create(DevicesModel::create()));
  446. devices_table_view.model()->update();
  447. };
  448. return devices_widget;
  449. }
  450. NonnullRefPtr<GUI::Widget> build_graphs_tab()
  451. {
  452. auto graphs_container = GUI::LazyWidget::construct();
  453. graphs_container->on_first_show = [](GUI::LazyWidget& self) {
  454. self.set_fill_with_background_color(true);
  455. self.set_background_role(ColorRole::Button);
  456. self.set_layout<GUI::VerticalBoxLayout>();
  457. self.layout()->set_margins({ 4, 4, 4, 4 });
  458. auto& cpu_graph_group_box = self.add<GUI::GroupBox>("CPU usage");
  459. cpu_graph_group_box.set_layout<GUI::HorizontalBoxLayout>();
  460. cpu_graph_group_box.layout()->set_margins({ 6, 16, 6, 6 });
  461. cpu_graph_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  462. cpu_graph_group_box.set_preferred_size(0, 120);
  463. Vector<GraphWidget*> cpu_graphs;
  464. for (size_t i = 0; i < ProcessModel::the().cpus().size(); i++) {
  465. auto& cpu_graph = cpu_graph_group_box.add<GraphWidget>();
  466. cpu_graph.set_max(100);
  467. cpu_graph.set_text_color(Color::Green);
  468. cpu_graph.set_graph_color(Color::from_rgb(0x00bb00));
  469. cpu_graph.text_formatter = [](int value, int) {
  470. return String::format("%d%%", value);
  471. };
  472. cpu_graphs.append(&cpu_graph);
  473. }
  474. ProcessModel::the().on_cpu_info_change = [cpu_graphs](const NonnullOwnPtrVector<ProcessModel::CpuInfo>& cpus) {
  475. for (size_t i = 0; i < cpus.size(); i++)
  476. cpu_graphs[i]->add_value(cpus[i].total_cpu_percent);
  477. };
  478. auto& memory_graph_group_box = self.add<GUI::GroupBox>("Memory usage");
  479. memory_graph_group_box.set_layout<GUI::VerticalBoxLayout>();
  480. memory_graph_group_box.layout()->set_margins({ 6, 16, 6, 6 });
  481. memory_graph_group_box.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  482. memory_graph_group_box.set_preferred_size(0, 120);
  483. auto& memory_graph = memory_graph_group_box.add<GraphWidget>();
  484. memory_graph.set_text_color(Color::Cyan);
  485. memory_graph.set_graph_color(Color::from_rgb(0x00bbbb));
  486. memory_graph.text_formatter = [](int value, int max) {
  487. return String::format("%d / %d KiB", value, max);
  488. };
  489. self.add<MemoryStatsWidget>(memory_graph);
  490. };
  491. return graphs_container;
  492. }