1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 21:25:00 +00:00
serenity/Libraries/LibCore/CProcessStatisticsReader.cpp
Andreas Kling 50677bf806 Kernel: Refactor scheduler to use dynamic thread priorities
Threads now have numeric priorities with a base priority in the 1-99
range.

Whenever a runnable thread is *not* scheduled, its effective priority
is incremented by 1. This is tracked in Thread::m_extra_priority.
The effective priority of a thread is m_priority + m_extra_priority.

When a runnable thread *is* scheduled, its m_extra_priority is reset to
zero and the effective priority returns to base.

This means that lower-priority threads will always eventually get
scheduled to run, once its effective priority becomes high enough to
exceed the base priority of threads "above" it.

The previous values for ThreadPriority (Low, Normal and High) are now
replaced as follows:

    Low -> 10
    Normal -> 30
    High -> 50

In other words, it will take 20 ticks for a "Low" priority thread to
get to "Normal" effective priority, and another 20 to reach "High".

This is not perfect, and I've used some quite naive data structures,
but I think the mechanism will allow us to build various new and
interesting optimizations, and we can figure out better data structures
later on. :^)
2019-12-30 18:46:17 +01:00

93 lines
4.4 KiB
C++

#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/CFile.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <pwd.h>
#include <stdio.h>
HashMap<uid_t, String> CProcessStatisticsReader::s_usernames;
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
{
auto file = CFile::construct("/proc/all");
if (!file->open(CIODevice::ReadOnly)) {
fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
return {};
}
HashMap<pid_t, CProcessStatistics> map;
auto file_contents = file->read_all();
auto json = JsonValue::from_string({ file_contents.data(), (size_t)file_contents.size() });
json.as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object();
CProcessStatistics process;
// kernel data first
process.pid = process_object.get("pid").to_u32();
process.pgid = process_object.get("pgid").to_u32();
process.pgp = process_object.get("pgp").to_u32();
process.sid = process_object.get("sid").to_u32();
process.uid = process_object.get("uid").to_u32();
process.gid = process_object.get("gid").to_u32();
process.ppid = process_object.get("ppid").to_u32();
process.nfds = process_object.get("nfds").to_u32();
process.name = process_object.get("name").to_string();
process.tty = process_object.get("tty").to_string();
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();
process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32();
process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32();
process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32();
process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32();
process.icon_id = process_object.get("icon_id").to_int();
auto& thread_array = process_object.get_ptr("threads")->as_array();
process.threads.ensure_capacity(thread_array.size());
thread_array.for_each([&](auto& value) {
auto& thread_object = value.as_object();
CThreadStatistics thread;
thread.tid = thread_object.get("tid").to_u32();
thread.times_scheduled = thread_object.get("times_scheduled").to_u32();
thread.name = thread_object.get("name").to_string();
thread.state = thread_object.get("state").to_string();
thread.ticks = thread_object.get("ticks").to_u32();
thread.priority = thread_object.get("priority").to_u32();
thread.effective_priority = thread_object.get("effective_priority").to_u32();
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();
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();
process.threads.append(move(thread));
});
// and synthetic data last
process.username = username_from_uid(process.uid);
map.set(process.pid, process);
});
return map;
}
String CProcessStatisticsReader::username_from_uid(uid_t uid)
{
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())
return (*it).value;
return String::number(uid);
}