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:
Tom 2021-07-14 12:05:59 -06:00 committed by Andreas Kling
parent ef85c4f747
commit 7e77a2ec40
Notes: sideshowbarker 2024-07-18 08:46:58 +09:00
17 changed files with 132 additions and 83 deletions

View file

@ -406,10 +406,10 @@ private:
ProcFSOverallProcesses();
virtual bool output(KBufferBuilder& builder) override
{
JsonArraySerializer array { builder };
JsonObjectSerializer<KBufferBuilder> json { builder };
// Keep this in sync with CProcessStatistics.
auto build_process = [&](const Process& process) {
auto build_process = [&](JsonArraySerializer<KBufferBuilder>& array, const Process& process) {
auto process_object = array.add_object();
if (process.is_user_process()) {
@ -488,11 +488,19 @@ private:
};
ScopedSpinLock lock(g_scheduler_lock);
auto processes = Process::all_processes();
build_process(*Scheduler::colonel());
for (auto& process : processes)
build_process(process);
array.finish();
{
{
auto array = json.add_array("processes");
auto processes = Process::all_processes();
build_process(array, *Scheduler::colonel());
for (auto& process : processes)
build_process(array, process);
}
auto total_ticks_scheduled = Scheduler::get_total_ticks_scheduled();
json.add("total_ticks", total_ticks_scheduled.total);
json.add("total_ticks_kernel", total_ticks_scheduled.total_kernel);
}
return true;
}
};