main.cpp 7.6 KB

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