Userland+LibCore: Use CProcessStatisticsReader to implement top.

Also tweaked CProcessStatisticsReader a bit to simplify the API.
This commit is contained in:
Andreas Kling 2019-07-10 13:56:28 +02:00
parent 7083a0104a
commit 01216f3c3f
Notes: sideshowbarker 2024-07-19 13:21:10 +09:00
5 changed files with 52 additions and 72 deletions

View file

@ -6,27 +6,16 @@
#include <pwd.h>
#include <stdio.h>
CProcessStatisticsReader::CProcessStatisticsReader()
{
setpwent();
while (auto* passwd = getpwent())
m_usernames.set(passwd->pw_uid, passwd->pw_name);
endpwent();
}
HashMap<uid_t, String> CProcessStatisticsReader::s_usernames;
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_map()
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
{
HashMap<pid_t, CProcessStatistics> res;
update_map(res);
return res;
}
HashMap<pid_t, CProcessStatistics> map;
void CProcessStatisticsReader::update_map(HashMap<pid_t, CProcessStatistics>& map)
{
CFile file("/proc/all");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "CProcessHelper : failed to open /proc/all: %s\n", file.error_string());
return;
return {};
}
auto file_contents = file.read_all();
@ -37,7 +26,7 @@ void CProcessStatisticsReader::update_map(HashMap<pid_t, CProcessStatistics>& ma
process.pid = process_object.get("pid").to_u32();
process.nsched = process_object.get("times_scheduled").to_u32();
process.uid = process_object.get("uid").to_u32();
process.username = get_username_from_uid(process.uid);
process.username = username_from_uid(process.uid);
process.priority = process_object.get("priority").to_string();
process.syscalls = process_object.get("syscall_count").to_u32();
process.state = process_object.get("state").to_string();
@ -46,13 +35,22 @@ void CProcessStatisticsReader::update_map(HashMap<pid_t, CProcessStatistics>& ma
process.physical_size = process_object.get("amount_resident").to_u32();
map.set(process.pid, process);
});
return map;
}
String CProcessStatisticsReader::get_username_from_uid(const uid_t uid)
String CProcessStatisticsReader::username_from_uid(uid_t uid)
{
auto it = m_usernames.find(uid);
if (it != m_usernames.end())
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;
else
return String::number(uid);
return String::number(uid);
}

View file

@ -18,12 +18,9 @@ struct CProcessStatistics {
class CProcessStatisticsReader {
public:
CProcessStatisticsReader();
HashMap<pid_t, CProcessStatistics> get_map();
static HashMap<pid_t, CProcessStatistics> get_all();
private:
void update_map(HashMap<pid_t, CProcessStatistics>& map);
String get_username_from_uid(const uid_t uid);
HashMap<uid_t, String> m_usernames;
static String username_from_uid(uid_t);
static HashMap<uid_t, String> s_usernames;
};

View file

@ -13,7 +13,7 @@ static void print_usage_and_exit()
static int kill_all(const String& process_name, const unsigned signum)
{
HashMap<pid_t, CProcessStatistics> processes = CProcessStatisticsReader().get_map();
auto processes = CProcessStatisticsReader().get_all();
for (auto& it : processes) {
if (it.value.name == process_name) {

View file

@ -12,7 +12,7 @@ static int pid_of(const String& process_name, bool single_shot, bool omit_pid, p
{
bool displayed_at_least_one = false;
HashMap<pid_t, CProcessStatistics> processes = CProcessStatisticsReader().get_map();
auto processes = CProcessStatisticsReader().get_all();
for (auto& it : processes) {
if (it.value.name == process_name) {

View file

@ -6,6 +6,7 @@
#include <AK/QuickSort.h>
#include <AK/Vector.h>
#include <LibCore/CFile.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
@ -14,22 +15,15 @@
static HashMap<unsigned, String>* s_usernames;
struct Process {
pid_t pid;
unsigned nsched;
String name;
String state;
String user;
String priority;
unsigned virtual_size;
unsigned physical_size;
unsigned nsched_since_prev;
unsigned cpu_percent;
unsigned cpu_percent_decimal;
struct ProcessData {
CProcessStatistics stats;
unsigned nsched_since_prev { 0 };
unsigned cpu_percent { 0 };
unsigned cpu_percent_decimal { 0 };
};
struct Snapshot {
HashMap<unsigned, Process> map;
HashMap<unsigned, ProcessData> map;
u32 sum_nsched { 0 };
};
@ -43,25 +37,16 @@ static Snapshot get_snapshot()
Snapshot snapshot;
auto file_contents = file.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();
snapshot.sum_nsched += nsched;
Process process;
process.pid = pid;
process.nsched = nsched;
unsigned uid = process_object.get("uid").to_u32();
process.user = s_usernames->get(uid);
process.priority = process_object.get("priority").to_string();
process.state = process_object.get("state").to_string();
process.name = process_object.get("name").to_string();
process.virtual_size = process_object.get("amount_virtual").to_u32();
process.physical_size = process_object.get("amount_resident").to_u32();
snapshot.map.set(pid, move(process));
});
auto all_processes = CProcessStatisticsReader::get_all();
for (auto& it : all_processes) {
auto& stats = it.value;
snapshot.sum_nsched += stats.nsched;
ProcessData process_data;
process_data.stats = stats;
snapshot.map.set(stats.pid, move(process_data));
}
return snapshot;
}
@ -73,7 +58,7 @@ int main(int, char**)
s_usernames->set(passwd->pw_uid, passwd->pw_name);
endpwent();
Vector<Process*> processes;
Vector<ProcessData*> processes;
auto prev = get_snapshot();
usleep(10000);
for (;;) {
@ -94,11 +79,11 @@ int main(int, char**)
pid_t pid = it.key;
if (pid == 0)
continue;
u32 nsched_now = it.value.nsched;
u32 nsched_now = it.value.stats.nsched;
auto jt = prev.map.find(pid);
if (jt == prev.map.end())
continue;
u32 nsched_before = (*jt).value.nsched;
u32 nsched_before = (*jt).value.stats.nsched;
u32 nsched_diff = nsched_now - nsched_before;
it.value.nsched_since_prev = nsched_diff;
it.value.cpu_percent = ((nsched_diff * 100) / sum_diff);
@ -111,16 +96,16 @@ int main(int, char**)
});
for (auto* process : processes) {
printf("%6d %c %-8s %-8s %6u %6u %2u.%1u %s\n",
process->pid,
process->priority[0],
process->user.characters(),
process->state.characters(),
process->virtual_size / 1024,
process->physical_size / 1024,
printf("%6d %c %-8s %-8s %6zu %6zu %2u.%1u %s\n",
process->stats.pid,
process->stats.priority[0],
process->stats.username.characters(),
process->stats.state.characters(),
process->stats.virtual_size / 1024,
process->stats.physical_size / 1024,
process->cpu_percent,
process->cpu_percent_decimal,
process->name.characters());
process->stats.name.characters());
}
processes.clear_with_capacity();
prev = move(current);