ladybird/MenuApplets/CPUGraph/main.cpp
Andreas Kling 26a31c7efb Kernel: Add "accept" pledge promise for accepting incoming connections
This patch adds a new "accept" promise that allows you to call accept()
on an already listening socket. This lets programs set up a socket for
for listening and then dropping "inet" and/or "unix" so that only
incoming (and existing) connections are allowed from that point on.
No new outgoing connections or listening server sockets can be created.

In addition to accept() it also allows getsockopt() with SOL_SOCKET
and SO_PEERCRED, which is used to find the PID/UID/GID of the socket
peer. This is used by our IPC library when creating shared buffers that
should only be accessible to a specific peer process.

This allows us to drop "unix" in WindowServer and LookupServer. :^)

It also makes the debugging/introspection RPC sockets in CEventLoop
based programs work again.
2020-01-17 11:19:06 +01:00

96 lines
2.5 KiB
C++

#include <AK/CircularQueue.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <LibDraw/Palette.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <stdio.h>
class GraphWidget final : public GWidget {
C_OBJECT(GraphWidget)
public:
GraphWidget()
: GWidget(nullptr)
{
start_timer(1000);
}
virtual ~GraphWidget() override {}
private:
virtual void timer_event(CTimerEvent&) override
{
unsigned busy;
unsigned idle;
get_cpu_usage(busy, idle);
unsigned busy_diff = busy - m_last_busy;
unsigned idle_diff = idle - m_last_idle;
m_last_busy = busy;
m_last_idle = idle;
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
m_cpu_history.enqueue(cpu);
update();
}
virtual void paint_event(GPaintEvent& event) override
{
GPainter painter(*this);
painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Black);
int i = m_cpu_history.capacity() - m_cpu_history.size();
for (auto cpu_usage : m_cpu_history) {
painter.draw_line(
{ i, rect().bottom() },
{ i, (int)(height() - (cpu_usage * (float)height())) },
palette().menu_selection());
++i;
}
}
static void get_cpu_usage(unsigned& busy, unsigned& idle)
{
busy = 0;
idle = 0;
auto all_processes = CProcessStatisticsReader::get_all();
for (auto& it : all_processes) {
for (auto& jt : it.value.threads) {
if (it.value.pid == 0)
idle += jt.times_scheduled;
else
busy += jt.times_scheduled;
}
}
}
CircularQueue<float, 30> m_cpu_history;
unsigned m_last_busy { 0 };
unsigned m_last_idle { 0 };
};
int main(int argc, char** argv)
{
if (pledge("stdio shared_buffer accept rpath unix cpath fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
GApplication app(argc, argv);
if (pledge("stdio shared_buffer accept rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
auto window = GWindow::construct();
window->set_window_type(GWindowType::MenuApplet);
window->resize(30, 16);
auto widget = GraphWidget::construct();
window->set_main_widget(widget);
window->show();
return app.exec();
}