mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
WindowServer: Move the CPU monitor thingy to its own class.
This commit is contained in:
parent
f1b58d8d8c
commit
c2093ad994
6 changed files with 106 additions and 83 deletions
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Assertions.h"
|
#include <AK/Assertions.h>
|
||||||
#include "Types.h"
|
#include <AK/Types.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -19,15 +19,6 @@ public:
|
||||||
|
|
||||||
int capacity() const { return Capacity; }
|
int capacity() const { return Capacity; }
|
||||||
|
|
||||||
void dump() const
|
|
||||||
{
|
|
||||||
kprintf("CircularQueue<%zu>:\n", Capacity);
|
|
||||||
kprintf(" size: %zu\n", m_size);
|
|
||||||
for (int i = 0; i < Capacity; ++i) {
|
|
||||||
kprintf(" [%zu] %d %c\n", i, m_elements[i], i == m_head ? '*' : ' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void enqueue(const T& t)
|
void enqueue(const T& t)
|
||||||
{
|
{
|
||||||
m_elements[(m_head + m_size) % Capacity] = t;
|
m_elements[(m_head + m_size) % Capacity] = t;
|
||||||
|
|
|
@ -24,6 +24,7 @@ WINDOWSERVER_OBJS = \
|
||||||
WSCursor.o \
|
WSCursor.o \
|
||||||
WSWindowFrame.o \
|
WSWindowFrame.o \
|
||||||
WSButton.o \
|
WSButton.o \
|
||||||
|
WSCPUMonitor.o \
|
||||||
main.o
|
main.o
|
||||||
|
|
||||||
APP = WindowServer
|
APP = WindowServer
|
||||||
|
|
74
Servers/WindowServer/WSCPUMonitor.cpp
Normal file
74
Servers/WindowServer/WSCPUMonitor.cpp
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
#include <WindowServer/WSCPUMonitor.h>
|
||||||
|
#include <WindowServer/WSMessageLoop.h>
|
||||||
|
#include <WindowServer/WSWindowManager.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
WSCPUMonitor::WSCPUMonitor()
|
||||||
|
{
|
||||||
|
create_thread([] (void* context) -> int {
|
||||||
|
auto& monitor = *(WSCPUMonitor*)context;
|
||||||
|
for (;;) {
|
||||||
|
static unsigned last_busy;
|
||||||
|
static unsigned last_idle;
|
||||||
|
unsigned busy;
|
||||||
|
unsigned idle;
|
||||||
|
monitor.get_cpu_usage(busy, idle);
|
||||||
|
unsigned busy_diff = busy - last_busy;
|
||||||
|
unsigned idle_diff = idle - last_idle;
|
||||||
|
last_busy = busy;
|
||||||
|
last_idle = idle;
|
||||||
|
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
|
||||||
|
monitor.m_cpu_history.enqueue(cpu);
|
||||||
|
monitor.m_dirty = true;
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
}, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)
|
||||||
|
{
|
||||||
|
busy = 0;
|
||||||
|
idle = 0;
|
||||||
|
|
||||||
|
FILE* fp = fopen("/proc/all", "r");
|
||||||
|
if (!fp) {
|
||||||
|
perror("failed to open /proc/all");
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
}
|
||||||
|
for (;;) {
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
char* ptr = fgets(buf, sizeof(buf), fp);
|
||||||
|
if (!ptr)
|
||||||
|
break;
|
||||||
|
auto parts = String(buf, Chomp).split(',');
|
||||||
|
if (parts.size() < 17)
|
||||||
|
break;
|
||||||
|
bool ok;
|
||||||
|
pid_t pid = parts[0].to_uint(ok);
|
||||||
|
ASSERT(ok);
|
||||||
|
unsigned nsched = parts[1].to_uint(ok);
|
||||||
|
ASSERT(ok);
|
||||||
|
|
||||||
|
if (pid == 0)
|
||||||
|
idle += nsched;
|
||||||
|
else
|
||||||
|
busy += nsched;
|
||||||
|
}
|
||||||
|
int rc = fclose(fp);
|
||||||
|
ASSERT(rc == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSCPUMonitor::paint(Painter& painter, const Rect& rect)
|
||||||
|
{
|
||||||
|
painter.fill_rect(rect, Color::Black);
|
||||||
|
int i = m_cpu_history.capacity() - m_cpu_history.size();
|
||||||
|
for (auto cpu_usage : m_cpu_history) {
|
||||||
|
painter.draw_line(
|
||||||
|
{ rect.x() + i, rect.bottom() },
|
||||||
|
{ rect.x() + i, (int)(rect.y() + (rect.height() - (cpu_usage * (float)rect.height()))) },
|
||||||
|
Color::from_rgb(0xaa6d4b)
|
||||||
|
);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
22
Servers/WindowServer/WSCPUMonitor.h
Normal file
22
Servers/WindowServer/WSCPUMonitor.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/CircularQueue.h>
|
||||||
|
|
||||||
|
class Painter;
|
||||||
|
class Rect;
|
||||||
|
|
||||||
|
class WSCPUMonitor {
|
||||||
|
public:
|
||||||
|
WSCPUMonitor();
|
||||||
|
|
||||||
|
bool is_dirty() const { return m_dirty; }
|
||||||
|
void set_dirty(bool dirty) { m_dirty = dirty; }
|
||||||
|
int capacity() const { return m_cpu_history.capacity(); }
|
||||||
|
void paint(Painter&, const Rect&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void get_cpu_usage(unsigned& busy, unsigned& idle);
|
||||||
|
|
||||||
|
CircularQueue<float, 30> m_cpu_history;
|
||||||
|
bool m_dirty { false };
|
||||||
|
};
|
|
@ -22,8 +22,6 @@
|
||||||
//#define DEBUG_COUNTERS
|
//#define DEBUG_COUNTERS
|
||||||
//#define RESIZE_DEBUG
|
//#define RESIZE_DEBUG
|
||||||
|
|
||||||
static void get_cpu_usage(unsigned& busy, unsigned& idle);
|
|
||||||
|
|
||||||
static WSWindowManager* s_the;
|
static WSWindowManager* s_the;
|
||||||
|
|
||||||
WSWindowManager& WSWindowManager::the()
|
WSWindowManager& WSWindowManager::the()
|
||||||
|
@ -143,34 +141,13 @@ WSWindowManager::WSWindowManager()
|
||||||
// NOTE: This ensures that the system menu has the correct dimensions.
|
// NOTE: This ensures that the system menu has the correct dimensions.
|
||||||
set_current_menubar(nullptr);
|
set_current_menubar(nullptr);
|
||||||
|
|
||||||
create_thread([] (void* context) -> int {
|
|
||||||
auto& wm = *(WSWindowManager*)context;
|
|
||||||
for (;;) {
|
|
||||||
static unsigned last_busy;
|
|
||||||
static unsigned last_idle;
|
|
||||||
unsigned busy;
|
|
||||||
unsigned idle;
|
|
||||||
get_cpu_usage(busy, idle);
|
|
||||||
unsigned busy_diff = busy - last_busy;
|
|
||||||
unsigned idle_diff = idle - last_idle;
|
|
||||||
last_busy = busy;
|
|
||||||
last_idle = idle;
|
|
||||||
float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
|
|
||||||
wm.m_cpu_history.enqueue(cpu);
|
|
||||||
sleep(1);
|
|
||||||
}
|
|
||||||
}, this);
|
|
||||||
|
|
||||||
WSMessageLoop::the().start_timer(300, [this] {
|
WSMessageLoop::the().start_timer(300, [this] {
|
||||||
static time_t last_update_time;
|
static time_t last_update_time;
|
||||||
static int last_cpu_history_size = 0;
|
|
||||||
static int last_cpu_history_head_index = 0;
|
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
if (now != last_update_time || m_cpu_history.size() != last_cpu_history_size || m_cpu_history.head_index() != last_cpu_history_head_index) {
|
if (now != last_update_time || m_cpu_monitor.is_dirty()) {
|
||||||
tick_clock();
|
tick_clock();
|
||||||
last_update_time = now;
|
last_update_time = now;
|
||||||
last_cpu_history_head_index = m_cpu_history.head_index();
|
m_cpu_monitor.set_dirty(false);
|
||||||
last_cpu_history_size = m_cpu_history.size();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -202,39 +179,6 @@ const Font& WSWindowManager::app_menu_font() const
|
||||||
return Font::default_bold_font();
|
return Font::default_bold_font();
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_cpu_usage(unsigned& busy, unsigned& idle)
|
|
||||||
{
|
|
||||||
busy = 0;
|
|
||||||
idle = 0;
|
|
||||||
|
|
||||||
FILE* fp = fopen("/proc/all", "r");
|
|
||||||
if (!fp) {
|
|
||||||
perror("failed to open /proc/all");
|
|
||||||
ASSERT_NOT_REACHED();
|
|
||||||
}
|
|
||||||
for (;;) {
|
|
||||||
char buf[BUFSIZ];
|
|
||||||
char* ptr = fgets(buf, sizeof(buf), fp);
|
|
||||||
if (!ptr)
|
|
||||||
break;
|
|
||||||
auto parts = String(buf, Chomp).split(',');
|
|
||||||
if (parts.size() < 17)
|
|
||||||
break;
|
|
||||||
bool ok;
|
|
||||||
pid_t pid = parts[0].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
unsigned nsched = parts[1].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
|
|
||||||
if (pid == 0)
|
|
||||||
idle += nsched;
|
|
||||||
else
|
|
||||||
busy += nsched;
|
|
||||||
}
|
|
||||||
int rc = fclose(fp);
|
|
||||||
ASSERT(rc == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WSWindowManager::tick_clock()
|
void WSWindowManager::tick_clock()
|
||||||
{
|
{
|
||||||
invalidate(menubar_rect());
|
invalidate(menubar_rect());
|
||||||
|
@ -926,17 +870,8 @@ void WSWindowManager::draw_menubar()
|
||||||
|
|
||||||
m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
|
m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
|
||||||
|
|
||||||
Rect cpu_rect { time_rect.right() - font().width(time_text) - (int)m_cpu_history.capacity() - 10, time_rect.y() + 1, (int)m_cpu_history.capacity(), time_rect.height() - 2 };
|
Rect cpu_rect { time_rect.right() - font().width(time_text) - m_cpu_monitor.capacity() - 10, time_rect.y() + 1, m_cpu_monitor.capacity(), time_rect.height() - 2 };
|
||||||
m_back_painter->fill_rect(cpu_rect, Color::Black);
|
m_cpu_monitor.paint(*m_back_painter, cpu_rect);
|
||||||
int i = m_cpu_history.capacity() - m_cpu_history.size();
|
|
||||||
for (auto cpu_usage : m_cpu_history) {
|
|
||||||
m_back_painter->draw_line(
|
|
||||||
{ cpu_rect.x() + i, cpu_rect.bottom() },
|
|
||||||
{ cpu_rect.x() + i, (int)(cpu_rect.y() + (cpu_rect.height() - (cpu_usage * (float)cpu_rect.height()))) },
|
|
||||||
Color::from_rgb(0xaa6d4b)
|
|
||||||
);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSWindowManager::draw_window_switcher()
|
void WSWindowManager::draw_window_switcher()
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
#include <WindowServer/WSWindow.h>
|
#include <WindowServer/WSWindow.h>
|
||||||
#include <WindowServer/WSCursor.h>
|
#include <WindowServer/WSCursor.h>
|
||||||
#include <WindowServer/WSMessage.h>
|
#include <WindowServer/WSMessage.h>
|
||||||
#include <AK/CircularQueue.h>
|
#include <WindowServer/WSCPUMonitor.h>
|
||||||
|
|
||||||
class WSAPIClientRequest;
|
class WSAPIClientRequest;
|
||||||
class WSScreen;
|
class WSScreen;
|
||||||
|
@ -216,11 +216,11 @@ private:
|
||||||
|
|
||||||
WSWindowSwitcher m_switcher;
|
WSWindowSwitcher m_switcher;
|
||||||
|
|
||||||
CircularQueue<float, 30> m_cpu_history;
|
|
||||||
|
|
||||||
String m_username;
|
String m_username;
|
||||||
WeakPtr<WSButton> m_cursor_tracking_button;
|
WeakPtr<WSButton> m_cursor_tracking_button;
|
||||||
WeakPtr<WSButton> m_hovered_button;
|
WeakPtr<WSButton> m_hovered_button;
|
||||||
|
|
||||||
|
WSCPUMonitor m_cpu_monitor;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue