ladybird/Userland/Libraries/LibCore/ProcessStatisticsReader.h
Tom 7e77a2ec40 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
2021-07-18 22:08:26 +02:00

83 lines
1.8 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <LibCore/File.h>
#include <unistd.h>
namespace Core {
struct ThreadStatistics {
pid_t tid;
unsigned times_scheduled;
unsigned ticks_user;
unsigned ticks_kernel;
unsigned syscall_count;
unsigned inode_faults;
unsigned zero_faults;
unsigned cow_faults;
unsigned unix_socket_read_bytes;
unsigned unix_socket_write_bytes;
unsigned ipv4_socket_read_bytes;
unsigned ipv4_socket_write_bytes;
unsigned file_read_bytes;
unsigned file_write_bytes;
String state;
u32 cpu;
u32 priority;
String name;
};
struct ProcessStatistics {
// Keep this in sync with /proc/all.
// From the kernel side:
pid_t pid;
pid_t pgid;
pid_t pgp;
pid_t sid;
uid_t uid;
gid_t gid;
pid_t ppid;
unsigned nfds;
bool kernel;
String name;
String executable;
String tty;
String pledge;
String veil;
size_t amount_virtual;
size_t amount_resident;
size_t amount_shared;
size_t amount_dirty_private;
size_t amount_clean_inode;
size_t amount_purgeable_volatile;
size_t amount_purgeable_nonvolatile;
Vector<Core::ThreadStatistics> threads;
// synthetic
String username;
};
struct AllProcessesStatistics {
Vector<ProcessStatistics> processes;
u64 total_ticks_scheduled;
u64 total_ticks_scheduled_kernel;
};
class ProcessStatisticsReader {
public:
static Optional<AllProcessesStatistics> get_all(RefPtr<Core::File>&);
static Optional<AllProcessesStatistics> get_all();
private:
static String username_from_uid(uid_t);
static HashMap<uid_t, String> s_usernames;
};
}