mirror of
https://github.com/RGBCube/serenity
synced 2025-05-15 01:54:57 +00:00
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
This commit is contained in:
parent
ef85c4f747
commit
7e77a2ec40
17 changed files with 132 additions and 83 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue