2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-05-23 09:08:32 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-15 12:11:21 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-29 08:12:40 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2019-05-16 16:47:47 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
2019-05-16 16:47:47 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<uid_t, DeprecatedString> ProcessStatisticsReader::s_usernames;
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2023-01-22 04:09:11 +00:00
|
|
|
ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(SeekableStream& proc_all_file, bool include_usernames)
|
2019-05-16 16:47:47 +00:00
|
|
|
{
|
2023-01-22 04:09:11 +00:00
|
|
|
TRY(proc_all_file.seek(0, SeekMode::SetPosition));
|
2019-05-16 16:47:47 +00:00
|
|
|
|
2021-07-14 18:05:59 +00:00
|
|
|
AllProcessesStatistics all_processes_statistics;
|
2019-07-10 13:32:07 +00:00
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto file_contents = TRY(proc_all_file.read_until_eof());
|
2022-12-08 13:50:31 +00:00
|
|
|
auto json_obj = TRY(JsonValue::from_string(file_contents)).as_object();
|
2022-12-21 16:51:33 +00:00
|
|
|
json_obj.get_array("processes"sv)->for_each([&](auto& value) {
|
2019-06-29 08:12:40 +00:00
|
|
|
const JsonObject& process_object = value.as_object();
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ProcessStatistics process;
|
2019-07-17 20:22:22 +00:00
|
|
|
|
|
|
|
// kernel data first
|
2022-12-21 16:51:33 +00:00
|
|
|
process.pid = process_object.get_u32("pid"sv).value_or(0);
|
|
|
|
process.pgid = process_object.get_u32("pgid"sv).value_or(0);
|
|
|
|
process.pgp = process_object.get_u32("pgp"sv).value_or(0);
|
|
|
|
process.sid = process_object.get_u32("sid"sv).value_or(0);
|
|
|
|
process.uid = process_object.get_u32("uid"sv).value_or(0);
|
|
|
|
process.gid = process_object.get_u32("gid"sv).value_or(0);
|
|
|
|
process.ppid = process_object.get_u32("ppid"sv).value_or(0);
|
|
|
|
process.kernel = process_object.get_bool("kernel"sv).value_or(false);
|
|
|
|
process.name = process_object.get_deprecated_string("name"sv).value_or("");
|
|
|
|
process.executable = process_object.get_deprecated_string("executable"sv).value_or("");
|
|
|
|
process.tty = process_object.get_deprecated_string("tty"sv).value_or("");
|
|
|
|
process.pledge = process_object.get_deprecated_string("pledge"sv).value_or("");
|
|
|
|
process.veil = process_object.get_deprecated_string("veil"sv).value_or("");
|
2023-06-09 16:22:07 +00:00
|
|
|
process.creation_time = UnixDateTime::from_nanoseconds_since_epoch(process_object.get_i64("creation_time"sv).value_or(0));
|
2022-12-21 16:51:33 +00:00
|
|
|
process.amount_virtual = process_object.get_u32("amount_virtual"sv).value_or(0);
|
|
|
|
process.amount_resident = process_object.get_u32("amount_resident"sv).value_or(0);
|
|
|
|
process.amount_shared = process_object.get_u32("amount_shared"sv).value_or(0);
|
|
|
|
process.amount_dirty_private = process_object.get_u32("amount_dirty_private"sv).value_or(0);
|
|
|
|
process.amount_clean_inode = process_object.get_u32("amount_clean_inode"sv).value_or(0);
|
|
|
|
process.amount_purgeable_volatile = process_object.get_u32("amount_purgeable_volatile"sv).value_or(0);
|
|
|
|
process.amount_purgeable_nonvolatile = process_object.get_u32("amount_purgeable_nonvolatile"sv).value_or(0);
|
2019-07-17 20:22:22 +00:00
|
|
|
|
2022-12-21 16:51:33 +00:00
|
|
|
auto& thread_array = process_object.get_array("threads"sv).value();
|
2019-12-30 13:50:40 +00:00
|
|
|
process.threads.ensure_capacity(thread_array.size());
|
2019-11-26 20:25:45 +00:00
|
|
|
thread_array.for_each([&](auto& value) {
|
|
|
|
auto& thread_object = value.as_object();
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ThreadStatistics thread;
|
2022-12-21 16:51:33 +00:00
|
|
|
thread.tid = thread_object.get_u32("tid"sv).value_or(0);
|
|
|
|
thread.times_scheduled = thread_object.get_u32("times_scheduled"sv).value_or(0);
|
|
|
|
thread.name = thread_object.get_deprecated_string("name"sv).value_or("");
|
|
|
|
thread.state = thread_object.get_deprecated_string("state"sv).value_or("");
|
|
|
|
thread.time_user = thread_object.get_u64("time_user"sv).value_or(0);
|
|
|
|
thread.time_kernel = thread_object.get_u64("time_kernel"sv).value_or(0);
|
|
|
|
thread.cpu = thread_object.get_u32("cpu"sv).value_or(0);
|
|
|
|
thread.priority = thread_object.get_u32("priority"sv).value_or(0);
|
|
|
|
thread.syscall_count = thread_object.get_u32("syscall_count"sv).value_or(0);
|
|
|
|
thread.inode_faults = thread_object.get_u32("inode_faults"sv).value_or(0);
|
|
|
|
thread.zero_faults = thread_object.get_u32("zero_faults"sv).value_or(0);
|
|
|
|
thread.cow_faults = thread_object.get_u32("cow_faults"sv).value_or(0);
|
2023-06-10 18:29:48 +00:00
|
|
|
thread.unix_socket_read_bytes = thread_object.get_u64("unix_socket_read_bytes"sv).value_or(0);
|
|
|
|
thread.unix_socket_write_bytes = thread_object.get_u64("unix_socket_write_bytes"sv).value_or(0);
|
|
|
|
thread.ipv4_socket_read_bytes = thread_object.get_u64("ipv4_socket_read_bytes"sv).value_or(0);
|
|
|
|
thread.ipv4_socket_write_bytes = thread_object.get_u64("ipv4_socket_write_bytes"sv).value_or(0);
|
|
|
|
thread.file_read_bytes = thread_object.get_u64("file_read_bytes"sv).value_or(0);
|
|
|
|
thread.file_write_bytes = thread_object.get_u64("file_write_bytes"sv).value_or(0);
|
2019-11-26 20:25:45 +00:00
|
|
|
process.threads.append(move(thread));
|
|
|
|
});
|
|
|
|
|
2019-07-17 20:22:22 +00:00
|
|
|
// and synthetic data last
|
2022-10-03 00:17:24 +00:00
|
|
|
if (include_usernames) {
|
|
|
|
process.username = username_from_uid(process.uid);
|
|
|
|
}
|
2021-07-14 18:05:59 +00:00
|
|
|
all_processes_statistics.processes.append(move(process));
|
2019-06-29 08:12:40 +00:00
|
|
|
});
|
2019-07-10 11:56:28 +00:00
|
|
|
|
2022-12-21 16:51:33 +00:00
|
|
|
all_processes_statistics.total_time_scheduled = json_obj.get_u64("total_time"sv).value_or(0);
|
|
|
|
all_processes_statistics.total_time_scheduled_kernel = json_obj.get_u64("total_time_kernel"sv).value_or(0);
|
2021-07-14 18:05:59 +00:00
|
|
|
return all_processes_statistics;
|
2019-05-16 16:47:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames)
|
2021-01-03 17:08:51 +00:00
|
|
|
{
|
2023-02-09 02:02:46 +00:00
|
|
|
auto proc_all_file = TRY(Core::File::open("/sys/kernel/processes"sv, Core::File::OpenMode::Read));
|
2022-12-08 13:50:31 +00:00
|
|
|
return get_all(*proc_all_file, include_usernames);
|
2021-01-03 17:08:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString ProcessStatisticsReader::username_from_uid(uid_t uid)
|
2019-05-16 16:47:47 +00:00
|
|
|
{
|
2019-07-10 11:56:28 +00:00
|
|
|
if (s_usernames.is_empty()) {
|
|
|
|
setpwent();
|
|
|
|
while (auto* passwd = getpwent())
|
|
|
|
s_usernames.set(passwd->pw_uid, passwd->pw_name);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = s_usernames.find(uid);
|
|
|
|
if (it != s_usernames.end())
|
2019-05-16 16:47:47 +00:00
|
|
|
return (*it).value;
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::number(uid);
|
2019-05-16 16:47:47 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|