main.cpp 7.6 KB

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