main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. {
  39. start_timer(1000);
  40. }
  41. virtual ~GraphWidget() override {}
  42. private:
  43. virtual void timer_event(Core::TimerEvent&) override
  44. {
  45. unsigned busy;
  46. unsigned idle;
  47. get_cpu_usage(busy, idle);
  48. unsigned busy_diff = busy - m_last_busy;
  49. unsigned idle_diff = idle - m_last_idle;
  50. m_last_busy = busy;
  51. m_last_idle = idle;
  52. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  53. m_cpu_history.enqueue(cpu);
  54. update();
  55. }
  56. virtual void paint_event(GUI::PaintEvent& event) override
  57. {
  58. GUI::Painter painter(*this);
  59. painter.add_clip_rect(event.rect());
  60. painter.fill_rect(event.rect(), Color::Black);
  61. int i = m_cpu_history.capacity() - m_cpu_history.size();
  62. for (auto cpu_usage : m_cpu_history) {
  63. painter.draw_line(
  64. { i, rect().bottom() },
  65. { i, (int)(height() - (cpu_usage * (float)height())) },
  66. palette().menu_selection());
  67. ++i;
  68. }
  69. }
  70. virtual void mousedown_event(GUI::MouseEvent& event) override
  71. {
  72. if (event.button() != GUI::MouseButton::Left)
  73. return;
  74. pid_t pid = fork();
  75. if (pid < 0) {
  76. perror("fork");
  77. } else if (pid == 0) {
  78. execl("/bin/SystemMonitor", "SystemMonitor", nullptr);
  79. perror("execl");
  80. ASSERT_NOT_REACHED();
  81. }
  82. }
  83. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  84. {
  85. busy = 0;
  86. idle = 0;
  87. auto all_processes = Core::ProcessStatisticsReader::get_all();
  88. for (auto& it : all_processes) {
  89. for (auto& jt : it.value.threads) {
  90. if (it.value.pid == 0)
  91. idle += jt.times_scheduled;
  92. else
  93. busy += jt.times_scheduled;
  94. }
  95. }
  96. }
  97. CircularQueue<float, 30> m_cpu_history;
  98. unsigned m_last_busy { 0 };
  99. unsigned m_last_idle { 0 };
  100. };
  101. int main(int argc, char** argv)
  102. {
  103. if (pledge("stdio shared_buffer accept proc exec rpath unix cpath fattr", nullptr) < 0) {
  104. perror("pledge");
  105. return 1;
  106. }
  107. GUI::Application app(argc, argv);
  108. if (pledge("stdio shared_buffer accept proc exec rpath", nullptr) < 0) {
  109. perror("pledge");
  110. return 1;
  111. }
  112. auto window = GUI::Window::construct();
  113. window->set_title("CPUGraph");
  114. window->set_window_type(GUI::WindowType::MenuApplet);
  115. window->resize(30, 16);
  116. window->set_main_widget<GraphWidget>();
  117. window->show();
  118. if (unveil("/res", "r") < 0) {
  119. perror("unveil");
  120. return 1;
  121. }
  122. // FIXME: This is required by Core::ProcessStatisticsReader.
  123. // It would be good if we didn't depend on that.
  124. if (unveil("/etc/passwd", "r") < 0) {
  125. perror("unveil");
  126. return 1;
  127. }
  128. if (unveil("/proc/all", "r") < 0) {
  129. perror("unveil");
  130. return 1;
  131. }
  132. if (unveil("/bin/SystemMonitor", "x") < 0) {
  133. perror("unveil");
  134. return 1;
  135. }
  136. unveil(nullptr, nullptr);
  137. return app.exec();
  138. }