From a635ff4e600729fb7d12fdd16ac6ffbaa455bb30 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 14 Jul 2021 21:46:32 -0600 Subject: Everywhere: Make tracking cpu usage independent from system ticks This switches tracking CPU usage to more accurately measure time in user and kernel land using either the TSC or another time source. This will also come in handy when implementing a tickless kernel mode. --- .../Applications/SystemMonitor/ProcessModel.cpp | 32 +++++++++++----------- Userland/Applications/SystemMonitor/ProcessModel.h | 10 +++---- 2 files changed, 21 insertions(+), 21 deletions(-) (limited to 'Userland/Applications') diff --git a/Userland/Applications/SystemMonitor/ProcessModel.cpp b/Userland/Applications/SystemMonitor/ProcessModel.cpp index 5f91c24449..65364ce2a3 100644 --- a/Userland/Applications/SystemMonitor/ProcessModel.cpp +++ b/Userland/Applications/SystemMonitor/ProcessModel.cpp @@ -319,15 +319,15 @@ void ProcessModel::update() auto all_processes = Core::ProcessStatisticsReader::get_all(m_proc_all); HashTable live_tids; - u64 sum_ticks_scheduled = 0, sum_ticks_scheduled_kernel = 0; - u64 total_ticks_scheduled_diff = 0; + u64 sum_time_scheduled = 0, sum_time_scheduled_kernel = 0; + u64 total_time_scheduled_diff = 0; if (all_processes.has_value()) { - if (m_has_total_ticks) - total_ticks_scheduled_diff = all_processes->total_ticks_scheduled - m_total_ticks_scheduled; + if (m_has_total_scheduled_time) + total_time_scheduled_diff = all_processes->total_time_scheduled - m_total_time_scheduled; - m_total_ticks_scheduled = all_processes->total_ticks_scheduled; - m_total_ticks_scheduled_kernel = all_processes->total_ticks_scheduled_kernel; - m_has_total_ticks = true; + m_total_time_scheduled = all_processes->total_time_scheduled; + m_total_time_scheduled_kernel = all_processes->total_time_scheduled_kernel; + m_has_total_scheduled_time = true; for (auto& process : all_processes.value().processes) { for (auto& thread : process.threads) { @@ -361,14 +361,14 @@ void ProcessModel::update() state.tid = thread.tid; state.pgid = process.pgid; state.sid = process.sid; - state.ticks_user = thread.ticks_user; - state.ticks_kernel = thread.ticks_kernel; + state.time_user = thread.time_user; + state.time_kernel = thread.time_kernel; state.cpu = thread.cpu; state.cpu_percent = 0; state.priority = thread.priority; state.state = thread.state; - sum_ticks_scheduled += thread.ticks_user + thread.ticks_kernel; - sum_ticks_scheduled_kernel += thread.ticks_kernel; + sum_time_scheduled += thread.time_user + thread.time_kernel; + sum_time_scheduled_kernel += thread.time_kernel; { auto pit = m_threads.find(thread.tid); if (pit == m_threads.end()) @@ -397,11 +397,11 @@ void ProcessModel::update() continue; } auto& thread = *it.value; - u32 ticks_scheduled_diff = (thread.current_state.ticks_user + thread.current_state.ticks_kernel) - - (thread.previous_state.ticks_user + thread.previous_state.ticks_kernel); - u32 ticks_scheduled_diff_kernel = thread.current_state.ticks_kernel - thread.previous_state.ticks_kernel; - thread.current_state.cpu_percent = total_ticks_scheduled_diff > 0 ? ((float)ticks_scheduled_diff * 100) / (float)total_ticks_scheduled_diff : 0; - thread.current_state.cpu_percent_kernel = total_ticks_scheduled_diff > 0 ? ((float)ticks_scheduled_diff_kernel * 100) / (float)total_ticks_scheduled_diff : 0; + u32 time_scheduled_diff = (thread.current_state.time_user + thread.current_state.time_kernel) + - (thread.previous_state.time_user + thread.previous_state.time_kernel); + u32 time_scheduled_diff_kernel = thread.current_state.time_kernel - thread.previous_state.time_kernel; + thread.current_state.cpu_percent = total_time_scheduled_diff > 0 ? ((float)time_scheduled_diff * 100) / (float)total_time_scheduled_diff : 0; + thread.current_state.cpu_percent_kernel = total_time_scheduled_diff > 0 ? ((float)time_scheduled_diff_kernel * 100) / (float)total_time_scheduled_diff : 0; if (it.value->current_state.pid != 0) { auto& cpu_info = m_cpus[thread.current_state.cpu]; cpu_info.total_cpu_percent += thread.current_state.cpu_percent; diff --git a/Userland/Applications/SystemMonitor/ProcessModel.h b/Userland/Applications/SystemMonitor/ProcessModel.h index 13993ff257..c679e51d75 100644 --- a/Userland/Applications/SystemMonitor/ProcessModel.h +++ b/Userland/Applications/SystemMonitor/ProcessModel.h @@ -88,8 +88,8 @@ private: pid_t ppid; pid_t pgid; pid_t sid; - unsigned ticks_user; - unsigned ticks_kernel; + u64 time_user; + u64 time_kernel; bool kernel; String executable; String name; @@ -129,7 +129,7 @@ private: Vector m_tids; RefPtr m_proc_all; GUI::Icon m_kernel_process_icon; - u64 m_total_ticks_scheduled { 0 }; - u64 m_total_ticks_scheduled_kernel { 0 }; - bool m_has_total_ticks { false }; + u64 m_total_time_scheduled { 0 }; + u64 m_total_time_scheduled_kernel { 0 }; + bool m_has_total_scheduled_time { false }; }; -- cgit v1.2.3