main.cpp 21 KB

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