main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/ByteBuffer.h>
  28. #include <AK/CircularQueue.h>
  29. #include <AK/JsonObject.h>
  30. #include <LibCore/ArgsParser.h>
  31. #include <LibCore/File.h>
  32. #include <LibCore/ProcessStatisticsReader.h>
  33. #include <LibGUI/Application.h>
  34. #include <LibGUI/Frame.h>
  35. #include <LibGUI/Painter.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/Palette.h>
  38. #include <serenity.h>
  39. #include <spawn.h>
  40. #include <stdio.h>
  41. enum class GraphType {
  42. CPU,
  43. Memory,
  44. };
  45. class GraphWidget final : public GUI::Frame {
  46. C_OBJECT(GraphWidget);
  47. public:
  48. static constexpr size_t history_size = 30;
  49. GraphWidget(GraphType graph_type, Optional<Gfx::Color> graph_color)
  50. : m_graph_type(graph_type)
  51. {
  52. set_frame_thickness(1);
  53. m_graph_color = graph_color.value_or(palette().menu_selection());
  54. start_timer(1000);
  55. }
  56. private:
  57. virtual void timer_event(Core::TimerEvent&) override
  58. {
  59. switch (m_graph_type) {
  60. case GraphType::CPU: {
  61. unsigned busy;
  62. unsigned idle;
  63. get_cpu_usage(busy, idle);
  64. unsigned busy_diff = busy - m_last_cpu_busy;
  65. unsigned idle_diff = idle - m_last_cpu_idle;
  66. m_last_cpu_busy = busy;
  67. m_last_cpu_idle = idle;
  68. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  69. m_history.enqueue(cpu);
  70. m_tooltip = String::format("CPU usage: %.1f%%", 100 * cpu);
  71. break;
  72. }
  73. case GraphType::Memory: {
  74. unsigned allocated;
  75. unsigned available;
  76. get_memory_usage(allocated, available);
  77. float total_memory = allocated + available;
  78. float memory = (float)allocated / total_memory;
  79. m_history.enqueue(memory);
  80. m_tooltip = String::format("Memory: %.1f MiB of %.1f MiB in use", (float)allocated / MiB, total_memory / MiB);
  81. break;
  82. }
  83. default:
  84. ASSERT_NOT_REACHED();
  85. }
  86. set_tooltip(m_tooltip);
  87. update();
  88. }
  89. virtual void paint_event(GUI::PaintEvent& event) override
  90. {
  91. GUI::Frame::paint_event(event);
  92. GUI::Painter painter(*this);
  93. painter.add_clip_rect(event.rect());
  94. painter.add_clip_rect(frame_inner_rect());
  95. painter.fill_rect(event.rect(), Color::Black);
  96. int i = m_history.capacity() - m_history.size();
  97. auto rect = frame_inner_rect();
  98. for (auto value : m_history) {
  99. painter.draw_line(
  100. { rect.x() + i, rect.bottom() },
  101. { rect.x() + i, rect.top() + (int)(round(rect.height() - (value * rect.height()))) },
  102. m_graph_color);
  103. ++i;
  104. }
  105. }
  106. virtual void mousedown_event(GUI::MouseEvent& event) override
  107. {
  108. if (event.button() != GUI::MouseButton::Left)
  109. return;
  110. pid_t child_pid;
  111. const char* argv[] = { "SystemMonitor", nullptr };
  112. if ((errno = posix_spawn(&child_pid, "/bin/SystemMonitor", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  113. perror("posix_spawn");
  114. } else {
  115. if (disown(child_pid) < 0)
  116. perror("disown");
  117. }
  118. }
  119. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  120. {
  121. busy = 0;
  122. idle = 0;
  123. auto all_processes = Core::ProcessStatisticsReader::get_all();
  124. for (auto& it : all_processes) {
  125. for (auto& jt : it.value.threads) {
  126. if (it.value.pid == 0)
  127. idle += jt.times_scheduled;
  128. else
  129. busy += jt.times_scheduled;
  130. }
  131. }
  132. }
  133. static void get_memory_usage(unsigned& allocated, unsigned& available)
  134. {
  135. auto proc_memstat = Core::File::construct("/proc/memstat");
  136. if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly))
  137. ASSERT_NOT_REACHED();
  138. auto file_contents = proc_memstat->read_all();
  139. auto json = JsonValue::from_string(file_contents);
  140. ASSERT(json.has_value());
  141. unsigned user_physical_allocated = json.value().as_object().get("user_physical_allocated").to_u32();
  142. unsigned user_physical_available = json.value().as_object().get("user_physical_available").to_u32();
  143. allocated = (user_physical_allocated * PAGE_SIZE);
  144. available = (user_physical_available * PAGE_SIZE);
  145. }
  146. GraphType m_graph_type;
  147. Gfx::Color m_graph_color;
  148. CircularQueue<float, history_size> m_history;
  149. unsigned m_last_cpu_busy { 0 };
  150. unsigned m_last_cpu_idle { 0 };
  151. String m_tooltip;
  152. };
  153. int main(int argc, char** argv)
  154. {
  155. if (pledge("stdio shared_buffer accept proc exec rpath unix cpath fattr", nullptr) < 0) {
  156. perror("pledge");
  157. return 1;
  158. }
  159. auto app = GUI::Application::construct(argc, argv);
  160. if (pledge("stdio shared_buffer accept proc exec rpath", nullptr) < 0) {
  161. perror("pledge");
  162. return 1;
  163. }
  164. bool cpu = false;
  165. bool memory = false;
  166. const char* name = nullptr;
  167. const char* color = nullptr;
  168. Core::ArgsParser args_parser;
  169. args_parser.add_option(cpu, "Show CPU usage", "cpu", 'C');
  170. args_parser.add_option(memory, "Show memory usage", "memory", 'M');
  171. args_parser.add_option(name, "Applet name used by WindowServer.ini to set the applet order", "name", 'n', "name");
  172. args_parser.add_option(color, "Graph color", "color", 'c', "color");
  173. args_parser.parse(argc, argv);
  174. if (!cpu && !memory) {
  175. printf("Either --cpu or --memory option must be used");
  176. return 1;
  177. }
  178. if (cpu && memory) {
  179. printf("--cpu and --memory options must not be used together");
  180. return 1;
  181. }
  182. GraphType graph_type;
  183. if (cpu)
  184. graph_type = GraphType::CPU;
  185. if (memory)
  186. graph_type = GraphType::Memory;
  187. if (name == nullptr)
  188. name = "ResourceGraph";
  189. Optional<Gfx::Color> graph_color;
  190. if (color != nullptr)
  191. graph_color = Gfx::Color::from_string(color);
  192. auto window = GUI::Window::construct();
  193. window->set_title(name);
  194. window->set_window_type(GUI::WindowType::MenuApplet);
  195. window->resize(GraphWidget::history_size + 2, 16);
  196. window->set_main_widget<GraphWidget>(graph_type, graph_color);
  197. window->show();
  198. if (unveil("/res", "r") < 0) {
  199. perror("unveil");
  200. return 1;
  201. }
  202. // FIXME: This is required by Core::ProcessStatisticsReader.
  203. // It would be good if we didn't depend on that.
  204. if (unveil("/etc/passwd", "r") < 0) {
  205. perror("unveil");
  206. return 1;
  207. }
  208. if (unveil("/proc/all", "r") < 0) {
  209. perror("unveil");
  210. return 1;
  211. }
  212. if (unveil("/proc/memstat", "r") < 0) {
  213. perror("unveil");
  214. return 1;
  215. }
  216. if (unveil("/bin/SystemMonitor", "x") < 0) {
  217. perror("unveil");
  218. return 1;
  219. }
  220. unveil(nullptr, nullptr);
  221. return app->exec();
  222. }