2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-05-23 11:08:32 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-11-15 13:11:21 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-29 10:12:40 +02:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2019-05-16 18:47:47 +02:00
|
|
|
#include <pwd.h>
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
2019-05-16 18:47:47 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
|
|
|
|
|
2021-05-23 11:08:32 +02:00
|
|
|
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
|
2019-05-16 18:47:47 +02:00
|
|
|
{
|
2021-01-03 10:08:51 -07:00
|
|
|
if (proc_all_file) {
|
2021-05-12 14:01:34 +04:30
|
|
|
if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) {
|
2021-05-31 15:00:38 +01:00
|
|
|
warnln("ProcessStatisticsReader: Failed to refresh /proc/all: {}", proc_all_file->error_string());
|
2021-01-03 10:08:51 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
proc_all_file = Core::File::construct("/proc/all");
|
2021-05-12 13:56:43 +04:30
|
|
|
if (!proc_all_file->open(Core::OpenMode::ReadOnly)) {
|
2021-05-31 15:00:38 +01:00
|
|
|
warnln("ProcessStatisticsReader: Failed to open /proc/all: {}", proc_all_file->error_string());
|
2021-01-03 10:08:51 -07:00
|
|
|
return {};
|
|
|
|
}
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 11:08:32 +02:00
|
|
|
Vector<Core::ProcessStatistics> processes;
|
2019-07-10 15:32:07 +02:00
|
|
|
|
2021-01-03 10:08:51 -07:00
|
|
|
auto file_contents = proc_all_file->read_all();
|
2020-03-01 12:35:09 +01:00
|
|
|
auto json = JsonValue::from_string(file_contents);
|
2021-01-01 20:58:47 -07:00
|
|
|
if (!json.has_value())
|
|
|
|
return {};
|
2020-06-10 21:40:27 -07:00
|
|
|
json.value().as_array().for_each([&](auto& value) {
|
2019-06-29 10:12:40 +02:00
|
|
|
const JsonObject& process_object = value.as_object();
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ProcessStatistics process;
|
2019-07-17 22:22:22 +02:00
|
|
|
|
|
|
|
// kernel data first
|
2019-07-03 21:17:35 +02:00
|
|
|
process.pid = process_object.get("pid").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.pgid = process_object.get("pgid").to_u32();
|
2019-07-17 21:24:47 +02:00
|
|
|
process.pgp = process_object.get("pgp").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.sid = process_object.get("sid").to_u32();
|
2019-07-03 21:17:35 +02:00
|
|
|
process.uid = process_object.get("uid").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.gid = process_object.get("gid").to_u32();
|
|
|
|
process.ppid = process_object.get("ppid").to_u32();
|
|
|
|
process.nfds = process_object.get("nfds").to_u32();
|
2021-04-06 17:17:53 +02:00
|
|
|
process.kernel = process_object.get("kernel").to_bool();
|
2019-06-29 10:12:40 +02:00
|
|
|
process.name = process_object.get("name").to_string();
|
2020-12-27 00:54:13 +01:00
|
|
|
process.executable = process_object.get("executable").to_string();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.tty = process_object.get("tty").to_string();
|
2020-01-11 21:17:07 +01:00
|
|
|
process.pledge = process_object.get("pledge").to_string();
|
2020-01-21 18:56:23 +01:00
|
|
|
process.veil = process_object.get("veil").to_string();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.amount_virtual = process_object.get("amount_virtual").to_u32();
|
|
|
|
process.amount_resident = process_object.get("amount_resident").to_u32();
|
|
|
|
process.amount_shared = process_object.get("amount_shared").to_u32();
|
2019-12-29 12:28:32 +01:00
|
|
|
process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
|
2019-12-29 12:45:58 +01:00
|
|
|
process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
|
2019-12-09 19:16:58 +01:00
|
|
|
process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
|
|
|
|
process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
|
2019-12-30 14:50:40 +01:00
|
|
|
auto& thread_array = process_object.get_ptr("threads")->as_array();
|
|
|
|
process.threads.ensure_capacity(thread_array.size());
|
2019-11-26 21:25:45 +01:00
|
|
|
thread_array.for_each([&](auto& value) {
|
|
|
|
auto& thread_object = value.as_object();
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ThreadStatistics thread;
|
2019-11-26 21:25:45 +01:00
|
|
|
thread.tid = thread_object.get("tid").to_u32();
|
|
|
|
thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
|
2019-12-07 12:50:30 -07:00
|
|
|
thread.name = thread_object.get("name").to_string();
|
2019-11-26 21:25:45 +01:00
|
|
|
thread.state = thread_object.get("state").to_string();
|
2020-12-03 22:12:50 -07:00
|
|
|
thread.ticks_user = thread_object.get("ticks_user").to_u32();
|
|
|
|
thread.ticks_kernel = thread_object.get("ticks_kernel").to_u32();
|
2020-06-27 22:36:15 -06:00
|
|
|
thread.cpu = thread_object.get("cpu").to_u32();
|
2019-12-30 18:46:17 +01:00
|
|
|
thread.priority = thread_object.get("priority").to_u32();
|
2019-11-26 21:35:24 +01:00
|
|
|
thread.syscall_count = thread_object.get("syscall_count").to_u32();
|
|
|
|
thread.inode_faults = thread_object.get("inode_faults").to_u32();
|
|
|
|
thread.zero_faults = thread_object.get("zero_faults").to_u32();
|
|
|
|
thread.cow_faults = thread_object.get("cow_faults").to_u32();
|
2019-12-01 17:36:06 +01:00
|
|
|
thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32();
|
|
|
|
thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32();
|
|
|
|
thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32();
|
|
|
|
thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32();
|
|
|
|
thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32();
|
|
|
|
thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32();
|
2019-11-26 21:25:45 +01:00
|
|
|
process.threads.append(move(thread));
|
|
|
|
});
|
|
|
|
|
2019-07-17 22:22:22 +02:00
|
|
|
// and synthetic data last
|
|
|
|
process.username = username_from_uid(process.uid);
|
2021-05-23 11:08:32 +02:00
|
|
|
processes.append(move(process));
|
2019-06-29 10:12:40 +02:00
|
|
|
});
|
2019-07-10 13:56:28 +02:00
|
|
|
|
2021-05-23 11:08:32 +02:00
|
|
|
return processes;
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 11:08:32 +02:00
|
|
|
Optional<Vector<Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
|
2021-01-03 10:08:51 -07:00
|
|
|
{
|
|
|
|
RefPtr<Core::File> proc_all_file;
|
|
|
|
return get_all(proc_all_file);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
String ProcessStatisticsReader::username_from_uid(uid_t uid)
|
2019-05-16 18:47:47 +02:00
|
|
|
{
|
2019-07-10 13:56:28 +02: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 18:47:47 +02:00
|
|
|
return (*it).value;
|
2019-07-10 13:56:28 +02:00
|
|
|
return String::number(uid);
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|