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

@ -16,7 +16,7 @@ namespace Core {
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
{
if (proc_all_file) {
if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
@ -31,13 +31,14 @@ Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPt
}
}
Vector<Core::ProcessStatistics> processes;
AllProcessesStatistics all_processes_statistics;
auto file_contents = proc_all_file->read_all();
auto json = JsonValue::from_string(file_contents);
if (!json.has_value())
return {};
json.value().as_array().for_each([&](auto& value) {
auto& json_obj = json.value().as_object();
json_obj.get("processes").as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object();
Core::ProcessStatistics process;
@ -92,13 +93,15 @@ Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPt
// and synthetic data last
process.username = username_from_uid(process.uid);
processes.append(move(process));
all_processes_statistics.processes.append(move(process));
});
return processes;
all_processes_statistics.total_ticks_scheduled = json_obj.get("total_ticks").to_u64();
all_processes_statistics.total_ticks_scheduled_kernel = json_obj.get("total_ticks_kernel").to_u64();
return all_processes_statistics;
}
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all()
{
RefPtr<Core::File> proc_all_file;
return get_all(proc_all_file);