mirror of
https://github.com/RGBCube/serenity
synced 2025-06-13 21:52:07 +00:00
LibCore: Port CProcessStatisticsReader to the JSON formatted /proc/all.
This commit is contained in:
parent
14b51253c0
commit
c9826bb429
1 changed files with 20 additions and 62 deletions
|
@ -1,6 +1,8 @@
|
||||||
#include "CProcessStatisticsReader.h"
|
#include <AK/JsonArray.h>
|
||||||
#include "CFile.h"
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
|
#include <LibCore/CFile.h>
|
||||||
|
#include <LibCore/CProcessStatisticsReader.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
@ -27,67 +29,23 @@ void CProcessStatisticsReader::update_map(HashMap<pid_t, CProcessStatistics>& ma
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
auto file_contents = file.read_all();
|
||||||
auto line = file.read_line(1024);
|
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
|
||||||
|
json.as_array().for_each([&](auto& value) {
|
||||||
if (line.is_empty())
|
const JsonObject& process_object = value.as_object();
|
||||||
break;
|
|
||||||
|
|
||||||
auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp);
|
|
||||||
auto parts = chomped.split_view(',');
|
|
||||||
|
|
||||||
if (parts.size() < 18)
|
|
||||||
break;
|
|
||||||
|
|
||||||
bool ok = false;
|
|
||||||
CProcessStatistics process;
|
CProcessStatistics process;
|
||||||
|
process.pid = process_object.get("pid").to_dword();
|
||||||
process.pid = parts[0].to_uint(ok);
|
process.nsched = process_object.get("times_scheduled").to_dword();
|
||||||
if (!ok) {
|
process.uid = process_object.get("uid").to_dword();
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid pid\n", parts[0].characters());
|
process.username = get_username_from_uid(process.uid);
|
||||||
return;
|
process.priority = process_object.get("priority").to_string();
|
||||||
}
|
process.syscalls = process_object.get("syscall_count").to_dword();
|
||||||
|
process.state = process_object.get("state").to_string();
|
||||||
process.nsched = parts[1].to_uint(ok);
|
process.name = process_object.get("name").to_string();
|
||||||
if (!ok) {
|
process.virtual_size = process_object.get("amount_virtual").to_dword();
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid nsched value\n", parts[1].characters());
|
process.physical_size = process_object.get("amount_resident").to_dword();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uid_t uid = parts[5].to_uint(ok);
|
|
||||||
if (!ok) {
|
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid uid value\n", parts[5].characters());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
process.uid = uid;
|
|
||||||
process.username = get_username_from_uid(uid);
|
|
||||||
|
|
||||||
process.priority = parts[16];
|
|
||||||
|
|
||||||
process.syscalls = parts[17].to_uint(ok);
|
|
||||||
if (!ok) {
|
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid syscalls count value\n", parts[17].characters());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
process.state = parts[7];
|
|
||||||
|
|
||||||
process.name = parts[11];
|
|
||||||
process.virtual_size = parts[12].to_uint(ok);
|
|
||||||
if (!ok) {
|
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid amount of virtual address space used\n", parts[12].characters());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
process.physical_size = parts[13].to_uint(ok);
|
|
||||||
if (!ok) {
|
|
||||||
fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid amount of physical address space used\n", parts[13].characters());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
map.set(process.pid, process);
|
map.set(process.pid, process);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
String CProcessStatisticsReader::get_username_from_uid(const uid_t uid)
|
String CProcessStatisticsReader::get_username_from_uid(const uid_t uid)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue