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#ifdef __serenity__
115 if (pledge("stdio shared_buffer accept proc exec rpath unix cpath fattr", nullptr) < 0) {
116 perror("pledge");
117 return 1;
118 }
119#endif
120
121 GUI::Application app(argc, argv);
122
123#ifdef __serenity__
124 if (pledge("stdio shared_buffer accept proc exec rpath", nullptr) < 0) {
125 perror("pledge");
126 return 1;
127 }
128#endif
129
130 auto window = GUI::Window::construct();
131 window->set_title("CPUGraph");
132 window->set_window_type(GUI::WindowType::MenuApplet);
133 window->resize(30, 16);
134
135 window->set_main_widget<GraphWidget>();
136 window->show();
137
138 if (unveil("/res", "r") < 0) {
139 perror("unveil");
140 return 1;
141 }
142
143 // FIXME: This is required by Core::ProcessStatisticsReader.
144 // It would be good if we didn't depend on that.
145 if (unveil("/etc/passwd", "r") < 0) {
146 perror("unveil");
147 return 1;
148 }
149
150#ifdef __serenity__
151 if (unveil("/proc/all", "r") < 0) {
152 perror("unveil");
153 return 1;
154 }
155#endif
156
157 if (unveil("/bin/SystemMonitor", "x") < 0) {
158 perror("unveil");
159 return 1;
160 }
161
162 unveil(nullptr, nullptr);
163
164 return app.exec();
165}