main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/CircularQueue.h>
  28. #include <AK/JsonObject.h>
  29. #include <LibCore/ArgsParser.h>
  30. #include <LibCore/File.h>
  31. #include <LibCore/ProcessStatisticsReader.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/Frame.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/Palette.h>
  37. #include <serenity.h>
  38. #include <spawn.h>
  39. #include <stdio.h>
  40. enum class GraphType {
  41. CPU,
  42. Memory,
  43. };
  44. class GraphWidget final : public GUI::Frame {
  45. C_OBJECT(GraphWidget);
  46. public:
  47. static constexpr size_t history_size = 30;
  48. GraphWidget(GraphType graph_type, Optional<Gfx::Color> graph_color)
  49. : m_graph_type(graph_type)
  50. {
  51. set_frame_thickness(1);
  52. m_graph_color = graph_color.value_or(palette().menu_selection());
  53. start_timer(1000);
  54. }
  55. private:
  56. virtual void timer_event(Core::TimerEvent&) override
  57. {
  58. switch (m_graph_type) {
  59. case GraphType::CPU: {
  60. unsigned busy;
  61. unsigned idle;
  62. get_cpu_usage(busy, idle);
  63. unsigned busy_diff = busy - m_last_cpu_busy;
  64. unsigned idle_diff = idle - m_last_cpu_idle;
  65. m_last_cpu_busy = busy;
  66. m_last_cpu_idle = idle;
  67. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  68. m_history.enqueue(cpu);
  69. m_tooltip = String::format("CPU usage: %.1f%%", 100 * cpu);
  70. break;
  71. }
  72. case GraphType::Memory: {
  73. unsigned allocated;
  74. unsigned available;
  75. get_memory_usage(allocated, available);
  76. float total_memory = allocated + available;
  77. float memory = (float)allocated / total_memory;
  78. m_history.enqueue(memory);
  79. m_tooltip = String::format("Memory: %.1f MiB of %.1f MiB in use", (float)allocated / MiB, total_memory / MiB);
  80. break;
  81. }
  82. default:
  83. ASSERT_NOT_REACHED();
  84. }
  85. set_tooltip(m_tooltip);
  86. update();
  87. }
  88. virtual void paint_event(GUI::PaintEvent& event) override
  89. {
  90. GUI::Frame::paint_event(event);
  91. GUI::Painter painter(*this);
  92. painter.add_clip_rect(event.rect());
  93. painter.add_clip_rect(frame_inner_rect());
  94. painter.fill_rect(event.rect(), Color::Black);
  95. int i = m_history.capacity() - m_history.size();
  96. auto rect = frame_inner_rect();
  97. for (auto value : m_history) {
  98. painter.draw_line(
  99. { rect.x() + i, rect.bottom() },
  100. { rect.x() + i, rect.top() + (int)(round(rect.height() - (value * rect.height()))) },
  101. m_graph_color);
  102. ++i;
  103. }
  104. }
  105. virtual void mousedown_event(GUI::MouseEvent& event) override
  106. {
  107. if (event.button() != GUI::MouseButton::Left)
  108. return;
  109. pid_t child_pid;
  110. const char* argv[] = { "SystemMonitor", nullptr };
  111. if ((errno = posix_spawn(&child_pid, "/bin/SystemMonitor", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  112. perror("posix_spawn");
  113. } else {
  114. if (disown(child_pid) < 0)
  115. perror("disown");
  116. }
  117. }
  118. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  119. {
  120. busy = 0;
  121. idle = 0;
  122. auto all_processes = Core::ProcessStatisticsReader::get_all();
  123. for (auto& it : all_processes) {
  124. for (auto& jt : it.value.threads) {
  125. if (it.value.pid == 0)
  126. idle += jt.times_scheduled;
  127. else
  128. busy += jt.times_scheduled;
  129. }
  130. }
  131. }
  132. static void get_memory_usage(unsigned& allocated, unsigned& available)
  133. {
  134. auto proc_memstat = Core::File::construct("/proc/memstat");
  135. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  136. ASSERT_NOT_REACHED();
  137. auto file_contents = proc_memstat->read_all();
  138. auto json = JsonValue::from_string(file_contents);
  139. ASSERT(json.has_value());
  140. unsigned user_physical_allocated = json.value().as_object().get("user_physical_allocated").to_u32();
  141. unsigned user_physical_available = json.value().as_object().get("user_physical_available").to_u32();
  142. allocated = (user_physical_allocated * PAGE_SIZE);
  143. available = (user_physical_available * PAGE_SIZE);
  144. }
  145. GraphType m_graph_type;
  146. Gfx::Color m_graph_color;
  147. CircularQueue<float, history_size> m_history;
  148. unsigned m_last_cpu_busy { 0 };
  149. unsigned m_last_cpu_idle { 0 };
  150. String m_tooltip;
  151. };
  152. int main(int argc, char** argv)
  153. {
  154. if (pledge("stdio shared_buffer accept proc exec rpath unix cpath fattr", nullptr) < 0) {
  155. perror("pledge");
  156. return 1;
  157. }
  158. auto app = GUI::Application::construct(argc, argv);
  159. if (pledge("stdio shared_buffer accept proc exec rpath", nullptr) < 0) {
  160. perror("pledge");
  161. return 1;
  162. }
  163. bool cpu = false;
  164. bool memory = false;
  165. const char* name = nullptr;
  166. const char* color = nullptr;
  167. Core::ArgsParser args_parser;
  168. args_parser.add_option(cpu, "Show CPU usage", "cpu", 'C');
  169. args_parser.add_option(memory, "Show memory usage", "memory", 'M');
  170. args_parser.add_option(name, "Applet name used by WindowServer.ini to set the applet order", "name", 'n', "name");
  171. args_parser.add_option(color, "Graph color", "color", 'c', "color");
  172. args_parser.parse(argc, argv);
  173. if (!cpu && !memory) {
  174. printf("Either --cpu or --memory option must be used");
  175. return 1;
  176. }
  177. if (cpu && memory) {
  178. printf("--cpu and --memory options must not be used together");
  179. return 1;
  180. }
  181. GraphType graph_type;
  182. if (cpu)
  183. graph_type = GraphType::CPU;
  184. if (memory)
  185. graph_type = GraphType::Memory;
  186. if (name == nullptr)
  187. name = "ResourceGraph";
  188. Optional<Gfx::Color> graph_color;
  189. if (color != nullptr)
  190. graph_color = Gfx::Color::from_string(color);
  191. auto window = GUI::Window::construct();
  192. window->set_title(name);
  193. window->set_window_type(GUI::WindowType::MenuApplet);
  194. window->resize(GraphWidget::history_size + 2, 16);
  195. window->set_main_widget<GraphWidget>(graph_type, graph_color);
  196. window->show();
  197. if (unveil("/res", "r") < 0) {
  198. perror("unveil");
  199. return 1;
  200. }
  201. // FIXME: This is required by Core::ProcessStatisticsReader.
  202. // It would be good if we didn't depend on that.
  203. if (unveil("/etc/passwd", "r") < 0) {
  204. perror("unveil");
  205. return 1;
  206. }
  207. if (unveil("/proc/all", "r") < 0) {
  208. perror("unveil");
  209. return 1;
  210. }
  211. if (unveil("/proc/memstat", "r") < 0) {
  212. perror("unveil");
  213. return 1;
  214. }
  215. if (unveil("/bin/SystemMonitor", "x") < 0) {
  216. perror("unveil");
  217. return 1;
  218. }
  219. unveil(nullptr, nullptr);
  220. return app->exec();
  221. }