ProcessManager: Use CProcessStatisticsReader to get process data.

It was silly to duplicate this functionality in so many places. Now everyone
uses CProcessStatisticsReader to parse /proc/all :^)
This commit is contained in:
Andreas Kling 2019-07-10 15:27:21 +02:00
parent 64d9b43734
commit 245ae479eb
2 changed files with 22 additions and 48 deletions

View file

@ -3,25 +3,13 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibCore/CFile.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
ProcessModel::ProcessModel(GraphWidget& graph)
: m_graph(graph)
, m_proc_all("/proc/all")
{
if (!m_proc_all.open(CIODevice::ReadOnly)) {
fprintf(stderr, "ProcessManager: Failed to open /proc/all: %s\n", m_proc_all.error_string());
exit(1);
}
setpwent();
while (auto* passwd = getpwent())
m_usernames.set(passwd->pw_uid, passwd->pw_name);
endpwent();
m_generic_process_icon = GraphicsBitmap::load_from_file("/res/icons/gear16.png");
m_high_priority_icon = GraphicsBitmap::load_from_file("/res/icons/highpriority16.png");
m_low_priority_icon = GraphicsBitmap::load_from_file("/res/icons/lowpriority16.png");
@ -186,7 +174,7 @@ GVariant ProcessModel::data(const GModelIndex& index, Role role) const
void ProcessModel::update()
{
m_proc_all.seek(0);
auto all_processes = CProcessStatisticsReader::get_all();
unsigned last_sum_nsched = 0;
for (auto& it : m_processes)
@ -194,42 +182,30 @@ void ProcessModel::update()
HashTable<pid_t> live_pids;
unsigned sum_nsched = 0;
auto file_contents = m_proc_all.read_all();
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
json.as_array().for_each([&](auto& value) {
const JsonObject& process_object = value.as_object();
pid_t pid = process_object.get("pid").to_u32();
unsigned nsched = process_object.get("times_scheduled").to_u32();
for (auto& it : all_processes) {
ProcessState state;
state.pid = pid;
state.nsched = nsched;
unsigned uid = process_object.get("uid").to_u32();
state.pid = it.value.pid;
state.nsched = it.value.nsched;
state.user = it.value.username;
state.priority = it.value.priority;
state.syscalls = it.value.syscalls;
state.state = it.value.state;
state.name = it.value.name;
state.virtual_size = it.value.virtual_size;
state.physical_size = it.value.physical_size;
sum_nsched += it.value.nsched;
{
auto it = m_usernames.find((uid_t)uid);
if (it != m_usernames.end())
state.user = (*it).value;
else
state.user = String::number(uid);
auto pit = m_processes.find(it.value.pid);
if (pit == m_processes.end())
m_processes.set(it.value.pid, make<Process>());
}
state.priority = process_object.get("priority").to_string();
state.syscalls = process_object.get("syscall_count").to_u32();
state.state = process_object.get("state").to_string();
state.name = process_object.get("name").to_string();
state.virtual_size = process_object.get("amount_virtual").to_u32();
state.physical_size = process_object.get("amount_resident").to_u32();
sum_nsched += nsched;
{
auto it = m_processes.find(pid);
if (it == m_processes.end())
m_processes.set(pid, make<Process>());
}
auto it = m_processes.find(pid);
ASSERT(it != m_processes.end());
(*it).value->previous_state = (*it).value->current_state;
(*it).value->current_state = state;
auto pit = m_processes.find(it.value.pid);
ASSERT(pit != m_processes.end());
(*pit).value->previous_state = (*pit).value->current_state;
(*pit).value->current_state = state;
live_pids.set(pid);
});
live_pids.set(it.value.pid);
}
m_pids.clear();
float total_cpu_percent = 0;

View file

@ -3,7 +3,6 @@
#include <AK/AKString.h>
#include <AK/HashMap.h>
#include <AK/Vector.h>
#include <LibCore/CFile.h>
#include <LibGUI/GModel.h>
#include <unistd.h>
@ -65,5 +64,4 @@ private:
RefPtr<GraphicsBitmap> m_high_priority_icon;
RefPtr<GraphicsBitmap> m_low_priority_icon;
RefPtr<GraphicsBitmap> m_normal_priority_icon;
CFile m_proc_all;
};