summaryrefslogtreecommitdiff
path: root/Applications
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-10 15:27:21 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-10 15:27:52 +0200
commit245ae479eb40872cc2dfb76c4c75cd9c6d76b33c (patch)
tree8e33060a8e7fb95e575ad9575161e23b9120c716 /Applications
parent64d9b43734e9d0f950dffc6964af3236f9adacfb (diff)
downloadserenity-245ae479eb40872cc2dfb76c4c75cd9c6d76b33c.zip
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 :^)
Diffstat (limited to 'Applications')
-rw-r--r--Applications/ProcessManager/ProcessModel.cpp68
-rw-r--r--Applications/ProcessManager/ProcessModel.h2
2 files changed, 22 insertions, 48 deletions
diff --git a/Applications/ProcessManager/ProcessModel.cpp b/Applications/ProcessManager/ProcessModel.cpp
index ac1c872305..462fff7daa 100644
--- a/Applications/ProcessManager/ProcessModel.cpp
+++ b/Applications/ProcessManager/ProcessModel.cpp
@@ -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;
diff --git a/Applications/ProcessManager/ProcessModel.h b/Applications/ProcessManager/ProcessModel.h
index 6f74609dc4..4371367a35 100644
--- a/Applications/ProcessManager/ProcessModel.h
+++ b/Applications/ProcessManager/ProcessModel.h
@@ -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;
};