mirror of
https://github.com/RGBCube/serenity
synced 2025-07-10 06:57:35 +00:00
Libraries: Create top level directory for libraries.
Things were getting a little crowded in the project root, so this patch moves the Lib*/ directories into Libraries/.
This commit is contained in:
parent
63814ffebf
commit
04b9dc2d30
328 changed files with 36 additions and 36 deletions
58
Libraries/LibCore/CProcessStatisticsReader.cpp
Normal file
58
Libraries/LibCore/CProcessStatisticsReader.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
#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>
|
||||
|
||||
CProcessStatisticsReader::CProcessStatisticsReader()
|
||||
{
|
||||
setpwent();
|
||||
while (auto* passwd = getpwent())
|
||||
m_usernames.set(passwd->pw_uid, passwd->pw_name);
|
||||
endpwent();
|
||||
}
|
||||
|
||||
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_map()
|
||||
{
|
||||
HashMap<pid_t, CProcessStatistics> res;
|
||||
update_map(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
CProcessStatistics process;
|
||||
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.priority = process_object.get("priority").to_string();
|
||||
process.syscalls = process_object.get("syscall_count").to_u32();
|
||||
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();
|
||||
map.set(process.pid, process);
|
||||
});
|
||||
}
|
||||
|
||||
String CProcessStatisticsReader::get_username_from_uid(const uid_t uid)
|
||||
{
|
||||
auto it = m_usernames.find(uid);
|
||||
if (it != m_usernames.end())
|
||||
return (*it).value;
|
||||
else
|
||||
return String::number(uid);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue