main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/CircularQueue.h>
  27. #include <LibCore/ProcessStatisticsReader.h>
  28. #include <LibGUI/Application.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGUI/Widget.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/Palette.h>
  33. #include <stdio.h>
  34. class GraphWidget final : public GUI::Widget {
  35. C_OBJECT(GraphWidget)
  36. public:
  37. GraphWidget()
  38. : GUI::Widget(nullptr)
  39. {
  40. start_timer(1000);
  41. }
  42. virtual ~GraphWidget() override {}
  43. private:
  44. virtual void timer_event(Core::TimerEvent&) override
  45. {
  46. unsigned busy;
  47. unsigned idle;
  48. get_cpu_usage(busy, idle);
  49. unsigned busy_diff = busy - m_last_busy;
  50. unsigned idle_diff = idle - m_last_idle;
  51. m_last_busy = busy;
  52. m_last_idle = idle;
  53. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  54. m_cpu_history.enqueue(cpu);
  55. update();
  56. }
  57. virtual void paint_event(GUI::PaintEvent& event) override
  58. {
  59. GUI::Painter painter(*this);
  60. painter.add_clip_rect(event.rect());
  61. painter.fill_rect(event.rect(), Color::Black);
  62. int i = m_cpu_history.capacity() - m_cpu_history.size();
  63. for (auto cpu_usage : m_cpu_history) {
  64. painter.draw_line(
  65. { i, rect().bottom() },
  66. { i, (int)(height() - (cpu_usage * (float)height())) },
  67. palette().menu_selection());
  68. ++i;
  69. }
  70. }
  71. virtual void mousedown_event(GUI::MouseEvent& event) override
  72. {
  73. if (event.button() != GUI::MouseButton::Left)
  74. return;
  75. pid_t pid = fork();
  76. if (pid < 0) {
  77. perror("fork");
  78. } else if (pid == 0) {
  79. execl("/bin/SystemMonitor", "SystemMonitor", nullptr);
  80. perror("execl");
  81. ASSERT_NOT_REACHED();
  82. }
  83. }
  84. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  85. {
  86. busy = 0;
  87. idle = 0;
  88. auto all_processes = Core::ProcessStatisticsReader::get_all();
  89. for (auto& it : all_processes) {
  90. for (auto& jt : it.value.threads) {
  91. if (it.value.pid == 0)
  92. idle += jt.times_scheduled;
  93. else
  94. busy += jt.times_scheduled;
  95. }
  96. }
  97. }
  98. CircularQueue<float, 30> m_cpu_history;
  99. unsigned m_last_busy { 0 };
  100. unsigned m_last_idle { 0 };
  101. };
  102. int main(int argc, char** argv)
  103. {
  104. if (pledge("stdio shared_buffer accept proc exec rpath unix cpath fattr", nullptr) < 0) {
  105. perror("pledge");
  106. return 1;
  107. }
  108. GUI::Application app(argc, argv);
  109. if (pledge("stdio shared_buffer accept proc exec rpath", nullptr) < 0) {
  110. perror("pledge");
  111. return 1;
  112. }
  113. auto window = GUI::Window::construct();
  114. window->set_title("CPUGraph");
  115. window->set_window_type(GUI::WindowType::MenuApplet);
  116. window->resize(30, 16);
  117. auto widget = GraphWidget::construct();
  118. window->set_main_widget(widget);
  119. window->show();
  120. if (unveil("/res", "r") < 0) {
  121. perror("unveil");
  122. return 1;
  123. }
  124. // FIXME: This is required by Core::ProcessStatisticsReader.
  125. // It would be good if we didn't depend on that.
  126. if (unveil("/etc/passwd", "r") < 0) {
  127. perror("unveil");
  128. return 1;
  129. }
  130. if (unveil("/proc/all", "r") < 0) {
  131. perror("unveil");
  132. return 1;
  133. }
  134. if (unveil("/bin/SystemMonitor", "x") < 0) {
  135. perror("unveil");
  136. return 1;
  137. }
  138. unveil(nullptr, nullptr);
  139. return app.exec();
  140. }