2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-11-15 15:11:21 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2019-06-29 11:12:40 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2019-05-16 19:47:47 +03:00
|
|
|
#include <pwd.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <stdio.h>
|
2019-05-16 19:47:47 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
namespace Core {
|
2019-05-16 19:47:47 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
|
|
|
|
|
|
|
|
HashMap<pid_t, Core::ProcessStatistics> ProcessStatisticsReader::get_all()
|
2019-05-16 19:47:47 +03:00
|
|
|
{
|
2020-02-02 14:34:39 +03:00
|
|
|
auto file = Core::File::construct("/proc/all");
|
|
|
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
2020-03-07 01:37:51 +03:00
|
|
|
fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
|
2019-07-10 14:56:28 +03:00
|
|
|
return {};
|
2019-05-16 19:47:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
HashMap<pid_t, Core::ProcessStatistics> map;
|
2019-07-10 16:32:07 +03:00
|
|
|
|
2019-09-21 21:50:06 +03:00
|
|
|
auto file_contents = file->read_all();
|
2020-03-01 14:35:09 +03:00
|
|
|
auto json = JsonValue::from_string(file_contents);
|
2020-06-11 07:40:27 +03:00
|
|
|
ASSERT(json.has_value());
|
|
|
|
json.value().as_array().for_each([&](auto& value) {
|
2019-06-29 11:12:40 +03:00
|
|
|
const JsonObject& process_object = value.as_object();
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ProcessStatistics process;
|
2019-07-17 23:22:22 +03:00
|
|
|
|
|
|
|
// kernel data first
|
2019-07-03 22:17:35 +03:00
|
|
|
process.pid = process_object.get("pid").to_u32();
|
2019-07-17 23:22:22 +03:00
|
|
|
process.pgid = process_object.get("pgid").to_u32();
|
2019-07-17 22:24:47 +03:00
|
|
|
process.pgp = process_object.get("pgp").to_u32();
|
2019-07-17 23:22:22 +03:00
|
|
|
process.sid = process_object.get("sid").to_u32();
|
2019-07-03 22:17:35 +03:00
|
|
|
process.uid = process_object.get("uid").to_u32();
|
2019-07-17 23:22:22 +03: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();
|
2019-06-29 11:12:40 +03:00
|
|
|
process.name = process_object.get("name").to_string();
|
2019-07-17 23:22:22 +03:00
|
|
|
process.tty = process_object.get("tty").to_string();
|
2020-01-11 23:17:07 +03:00
|
|
|
process.pledge = process_object.get("pledge").to_string();
|
2020-01-21 20:56:23 +03:00
|
|
|
process.veil = process_object.get("veil").to_string();
|
2019-07-17 23:22:22 +03: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 14:28:32 +03:00
|
|
|
process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
|
2019-12-29 14:45:58 +03:00
|
|
|
process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
|
2019-12-09 21:16:58 +03: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-29 08:26:01 +03:00
|
|
|
process.icon_id = process_object.get("icon_id").to_int();
|
2019-07-17 23:22:22 +03:00
|
|
|
|
2019-12-30 16:50:40 +03:00
|
|
|
auto& thread_array = process_object.get_ptr("threads")->as_array();
|
|
|
|
process.threads.ensure_capacity(thread_array.size());
|
2019-11-26 23:25:45 +03:00
|
|
|
thread_array.for_each([&](auto& value) {
|
|
|
|
auto& thread_object = value.as_object();
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ThreadStatistics thread;
|
2019-11-26 23:25:45 +03:00
|
|
|
thread.tid = thread_object.get("tid").to_u32();
|
|
|
|
thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
|
2019-12-07 22:50:30 +03:00
|
|
|
thread.name = thread_object.get("name").to_string();
|
2019-11-26 23:25:45 +03:00
|
|
|
thread.state = thread_object.get("state").to_string();
|
|
|
|
thread.ticks = thread_object.get("ticks").to_u32();
|
2020-06-28 07:36:15 +03:00
|
|
|
thread.cpu = thread_object.get("cpu").to_u32();
|
2019-12-30 20:46:17 +03:00
|
|
|
thread.priority = thread_object.get("priority").to_u32();
|
|
|
|
thread.effective_priority = thread_object.get("effective_priority").to_u32();
|
2019-11-26 23:35:24 +03: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 19:36:06 +03: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 23:25:45 +03:00
|
|
|
process.threads.append(move(thread));
|
|
|
|
});
|
|
|
|
|
2019-07-17 23:22:22 +03:00
|
|
|
// and synthetic data last
|
|
|
|
process.username = username_from_uid(process.uid);
|
2019-05-16 19:47:47 +03:00
|
|
|
map.set(process.pid, process);
|
2019-06-29 11:12:40 +03:00
|
|
|
});
|
2019-07-10 14:56:28 +03:00
|
|
|
|
|
|
|
return map;
|
2019-05-16 19:47:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
String ProcessStatisticsReader::username_from_uid(uid_t uid)
|
2019-05-16 19:47:47 +03:00
|
|
|
{
|
2019-07-10 14:56:28 +03: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 19:47:47 +03:00
|
|
|
return (*it).value;
|
2019-07-10 14:56:28 +03:00
|
|
|
return String::number(uid);
|
2019-05-16 19:47:47 +03:00
|
|
|
}
|
2020-02-02 14:34:39 +03:00
|
|
|
}
|