Serenity Operating System
1/*
2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/Vector.h>
11#include <unistd.h>
12
13namespace Core {
14
15struct ThreadStatistics {
16 pid_t tid;
17 unsigned times_scheduled;
18 u64 time_user;
19 u64 time_kernel;
20 unsigned syscall_count;
21 unsigned inode_faults;
22 unsigned zero_faults;
23 unsigned cow_faults;
24 unsigned unix_socket_read_bytes;
25 unsigned unix_socket_write_bytes;
26 unsigned ipv4_socket_read_bytes;
27 unsigned ipv4_socket_write_bytes;
28 unsigned file_read_bytes;
29 unsigned file_write_bytes;
30 DeprecatedString state;
31 u32 cpu;
32 u32 priority;
33 DeprecatedString name;
34};
35
36struct ProcessStatistics {
37 // Keep this in sync with /sys/kernel/processes.
38 // From the kernel side:
39 pid_t pid;
40 pid_t pgid;
41 pid_t pgp;
42 pid_t sid;
43 uid_t uid;
44 gid_t gid;
45 pid_t ppid;
46 unsigned nfds;
47 bool kernel;
48 DeprecatedString name;
49 DeprecatedString executable;
50 DeprecatedString tty;
51 DeprecatedString pledge;
52 DeprecatedString veil;
53 size_t amount_virtual;
54 size_t amount_resident;
55 size_t amount_shared;
56 size_t amount_dirty_private;
57 size_t amount_clean_inode;
58 size_t amount_purgeable_volatile;
59 size_t amount_purgeable_nonvolatile;
60
61 Vector<Core::ThreadStatistics> threads;
62
63 // synthetic
64 DeprecatedString username;
65};
66
67struct AllProcessesStatistics {
68 Vector<ProcessStatistics> processes;
69 u64 total_time_scheduled;
70 u64 total_time_scheduled_kernel;
71};
72
73class ProcessStatisticsReader {
74public:
75 static ErrorOr<AllProcessesStatistics> get_all(SeekableStream&, bool include_usernames = true);
76 static ErrorOr<AllProcessesStatistics> get_all(bool include_usernames = true);
77
78private:
79 static DeprecatedString username_from_uid(uid_t);
80 static HashMap<uid_t, DeprecatedString> s_usernames;
81};
82
83}