1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 09:27:34 +00:00

WindowServer: Move the CPU usage graph updates to a secondary thread.

This avoids blocking the main thread on filesystem access, which created
noticeable stutters during compilation.
This commit is contained in:
Andreas Kling 2019-03-27 14:56:35 +01:00
parent a2fe5f8517
commit 56f7b392c1
2 changed files with 28 additions and 13 deletions

View file

@ -70,6 +70,8 @@ public:
ConstIterator begin() const { return ConstIterator(*this, m_head); } ConstIterator begin() const { return ConstIterator(*this, m_head); }
ConstIterator end() const { return ConstIterator(*this, size()); } ConstIterator end() const { return ConstIterator(*this, size()); }
int head_index() const { return m_head; }
private: private:
friend class ConstIterator; friend class ConstIterator;
T m_elements[Capacity]; T m_elements[Capacity];

View file

@ -24,6 +24,8 @@
//#define DEBUG_WID_IN_TITLE_BAR //#define DEBUG_WID_IN_TITLE_BAR
//#define RESIZE_DEBUG //#define RESIZE_DEBUG
static void get_cpu_usage(unsigned& busy, unsigned& idle);
static const int window_titlebar_height = 18; static const int window_titlebar_height = 18;
static inline Rect menu_window_rect(const Rect& rect) static inline Rect menu_window_rect(const Rect& rect)
@ -255,12 +257,34 @@ 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) { if (now != last_update_time || m_cpu_history.size() != last_cpu_history_size || m_cpu_history.head_index() != last_cpu_history_head_index) {
tick_clock(); tick_clock();
last_update_time = now; last_update_time = now;
last_cpu_history_head_index = m_cpu_history.head_index();
last_cpu_history_size = m_cpu_history.size();
} }
}); });
@ -292,7 +316,7 @@ const Font& WSWindowManager::app_menu_font() const
return Font::default_bold_font(); return Font::default_bold_font();
} }
static void get_cpu_usage(unsigned& busy, unsigned& idle) void get_cpu_usage(unsigned& busy, unsigned& idle)
{ {
busy = 0; busy = 0;
idle = 0; idle = 0;
@ -327,17 +351,6 @@ static void get_cpu_usage(unsigned& busy, unsigned& idle)
void WSWindowManager::tick_clock() void WSWindowManager::tick_clock()
{ {
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);
m_cpu_history.enqueue(cpu);
invalidate(menubar_rect()); invalidate(menubar_rect());
} }