main.cpp 7.8 KB

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