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>
|
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
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(Core::Stream::SeekableStream& proc_all_file, bool include_usernames)
|
2019-05-16 16:47:47 +00:00
|
|
|
{
|
2022-12-08 13:50:31 +00:00
|
|
|
TRY(proc_all_file.seek(0, Core::Stream::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 11:42:06 +00:00
|
|
|
json_obj.get_deprecated("processes"sv).as_array().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 11:42:06 +00:00
|
|
|
process.pid = process_object.get_deprecated("pid"sv).to_u32();
|
|
|
|
process.pgid = process_object.get_deprecated("pgid"sv).to_u32();
|
|
|
|
process.pgp = process_object.get_deprecated("pgp"sv).to_u32();
|
|
|
|
process.sid = process_object.get_deprecated("sid"sv).to_u32();
|
|
|
|
process.uid = process_object.get_deprecated("uid"sv).to_u32();
|
|
|
|
process.gid = process_object.get_deprecated("gid"sv).to_u32();
|
|
|
|
process.ppid = process_object.get_deprecated("ppid"sv).to_u32();
|
|
|
|
process.nfds = process_object.get_deprecated("nfds"sv).to_u32();
|
|
|
|
process.kernel = process_object.get_deprecated("kernel"sv).to_bool();
|
|
|
|
process.name = process_object.get_deprecated("name"sv).to_deprecated_string();
|
|
|
|
process.executable = process_object.get_deprecated("executable"sv).to_deprecated_string();
|
|
|
|
process.tty = process_object.get_deprecated("tty"sv).to_deprecated_string();
|
|
|
|
process.pledge = process_object.get_deprecated("pledge"sv).to_deprecated_string();
|
|
|
|
process.veil = process_object.get_deprecated("veil"sv).to_deprecated_string();
|
|
|
|
process.amount_virtual = process_object.get_deprecated("amount_virtual"sv).to_u32();
|
|
|
|
process.amount_resident = process_object.get_deprecated("amount_resident"sv).to_u32();
|
|
|
|
process.amount_shared = process_object.get_deprecated("amount_shared"sv).to_u32();
|
|
|
|
process.amount_dirty_private = process_object.get_deprecated("amount_dirty_private"sv).to_u32();
|
|
|
|
process.amount_clean_inode = process_object.get_deprecated("amount_clean_inode"sv).to_u32();
|
|
|
|
process.amount_purgeable_volatile = process_object.get_deprecated("amount_purgeable_volatile"sv).to_u32();
|
|
|
|
process.amount_purgeable_nonvolatile = process_object.get_deprecated("amount_purgeable_nonvolatile"sv).to_u32();
|
2019-07-17 20:22:22 +00:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto& thread_array = process_object.get_ptr("threads"sv)->as_array();
|
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 11:42:06 +00:00
|
|
|
thread.tid = thread_object.get_deprecated("tid"sv).to_u32();
|
|
|
|
thread.times_scheduled = thread_object.get_deprecated("times_scheduled"sv).to_u32();
|
|
|
|
thread.name = thread_object.get_deprecated("name"sv).to_deprecated_string();
|
|
|
|
thread.state = thread_object.get_deprecated("state"sv).to_deprecated_string();
|
|
|
|
thread.time_user = thread_object.get_deprecated("time_user"sv).to_u64();
|
|
|
|
thread.time_kernel = thread_object.get_deprecated("time_kernel"sv).to_u64();
|
|
|
|
thread.cpu = thread_object.get_deprecated("cpu"sv).to_u32();
|
|
|
|
thread.priority = thread_object.get_deprecated("priority"sv).to_u32();
|
|
|
|
thread.syscall_count = thread_object.get_deprecated("syscall_count"sv).to_u32();
|
|
|
|
thread.inode_faults = thread_object.get_deprecated("inode_faults"sv).to_u32();
|
|
|
|
thread.zero_faults = thread_object.get_deprecated("zero_faults"sv).to_u32();
|
|
|
|
thread.cow_faults = thread_object.get_deprecated("cow_faults"sv).to_u32();
|
|
|
|
thread.unix_socket_read_bytes = thread_object.get_deprecated("unix_socket_read_bytes"sv).to_u32();
|
|
|
|
thread.unix_socket_write_bytes = thread_object.get_deprecated("unix_socket_write_bytes"sv).to_u32();
|
|
|
|
thread.ipv4_socket_read_bytes = thread_object.get_deprecated("ipv4_socket_read_bytes"sv).to_u32();
|
|
|
|
thread.ipv4_socket_write_bytes = thread_object.get_deprecated("ipv4_socket_write_bytes"sv).to_u32();
|
|
|
|
thread.file_read_bytes = thread_object.get_deprecated("file_read_bytes"sv).to_u32();
|
|
|
|
thread.file_write_bytes = thread_object.get_deprecated("file_write_bytes"sv).to_u32();
|
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 11:42:06 +00:00
|
|
|
all_processes_statistics.total_time_scheduled = json_obj.get_deprecated("total_time"sv).to_u64();
|
|
|
|
all_processes_statistics.total_time_scheduled_kernel = json_obj.get_deprecated("total_time_kernel"sv).to_u64();
|
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
|
|
|
{
|
2022-12-08 13:50:31 +00:00
|
|
|
auto proc_all_file = TRY(Core::Stream::File::open("/sys/kernel/processes"sv, Core::Stream::OpenMode::Read));
|
|
|
|
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
|
|
|
}
|