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