summaryrefslogtreecommitdiff
path: root/Userland/Applications
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2021-07-14 12:05:59 -0600
committerAndreas Kling <kling@serenityos.org>2021-07-18 22:08:26 +0200
commit7e77a2ec40bc339f41e75aa9fdc6744df449612c (patch)
tree374e613b57185d92b6ce7903d8c5c6cd907f7b7a /Userland/Applications
parentef85c4f7473e50c936a67a762c158833b9f0160a (diff)
downloadserenity-7e77a2ec40bc339f41e75aa9fdc6744df449612c.zip
Everywhere: Improve CPU usage calculation
As threads come and go, we can't simply account for how many time slices the threads at any given point may have been using. We need to also account for threads that have since disappeared. This means we also need to track how many time slices we have expired globally. However, because this doesn't account for context switches outside of the system timer tick values may still be under-reported. To solve this we will need to track more accurate time information on each context switch. This also fixes top's cpu usage calculation which was still based on the number of context switches. Fixes #6473
Diffstat (limited to 'Userland/Applications')
-rw-r--r--Userland/Applications/SystemMonitor/ProcessModel.cpp25
-rw-r--r--Userland/Applications/SystemMonitor/ProcessModel.h3
2 files changed, 17 insertions, 11 deletions
diff --git a/Userland/Applications/SystemMonitor/ProcessModel.cpp b/Userland/Applications/SystemMonitor/ProcessModel.cpp
index d0a1b55ced..5f91c24449 100644
--- a/Userland/Applications/SystemMonitor/ProcessModel.cpp
+++ b/Userland/Applications/SystemMonitor/ProcessModel.cpp
@@ -318,17 +318,18 @@ void ProcessModel::update()
auto previous_tid_count = m_tids.size();
auto all_processes = Core::ProcessStatisticsReader::get_all(m_proc_all);
- u64 last_sum_ticks_scheduled = 0, last_sum_ticks_scheduled_kernel = 0;
- for (auto& it : m_threads) {
- auto& current_state = it.value->current_state;
- last_sum_ticks_scheduled += current_state.ticks_user + current_state.ticks_kernel;
- last_sum_ticks_scheduled_kernel += current_state.ticks_kernel;
- }
-
HashTable<int> live_tids;
u64 sum_ticks_scheduled = 0, sum_ticks_scheduled_kernel = 0;
+ u64 total_ticks_scheduled_diff = 0;
if (all_processes.has_value()) {
- for (auto& process : all_processes.value()) {
+ if (m_has_total_ticks)
+ total_ticks_scheduled_diff = all_processes->total_ticks_scheduled - m_total_ticks_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;
+
+ for (auto& process : all_processes.value().processes) {
for (auto& thread : process.threads) {
ThreadState state;
state.kernel = process.kernel;
@@ -388,6 +389,7 @@ void ProcessModel::update()
c.total_cpu_percent = 0.0;
c.total_cpu_percent_kernel = 0.0;
}
+
Vector<int, 16> tids_to_remove;
for (auto& it : m_threads) {
if (!live_tids.contains(it.key)) {
@@ -398,8 +400,8 @@ void ProcessModel::update()
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 = ((float)ticks_scheduled_diff * 100) / (float)(sum_ticks_scheduled - last_sum_ticks_scheduled);
- thread.current_state.cpu_percent_kernel = ((float)ticks_scheduled_diff_kernel * 100) / (float)(sum_ticks_scheduled - last_sum_ticks_scheduled);
+ 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;
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;
@@ -407,6 +409,7 @@ void ProcessModel::update()
m_tids.append(it.key);
}
}
+
for (auto tid : tids_to_remove)
m_threads.remove(tid);
@@ -414,7 +417,7 @@ void ProcessModel::update()
on_cpu_info_change(m_cpus);
if (on_state_update)
- on_state_update(all_processes->size(), m_threads.size());
+ on_state_update(all_processes.has_value() ? all_processes->processes.size() : 0, m_threads.size());
// FIXME: This is a rather hackish way of invalidating indices.
// It would be good if GUI::Model had a way to orchestrate removal/insertion while preserving indices.
diff --git a/Userland/Applications/SystemMonitor/ProcessModel.h b/Userland/Applications/SystemMonitor/ProcessModel.h
index fe43e43bc6..13993ff257 100644
--- a/Userland/Applications/SystemMonitor/ProcessModel.h
+++ b/Userland/Applications/SystemMonitor/ProcessModel.h
@@ -129,4 +129,7 @@ private:
Vector<int> m_tids;
RefPtr<Core::File> 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 };
};