diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-07-10 13:56:28 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-07-10 13:56:28 +0200 |
commit | 01216f3c3fb0cd763adbc441fa728ffd5a2c254f (patch) | |
tree | 4fce31bb571190e5ad82faa5676566d9c7f5fb63 /Libraries/LibCore | |
parent | 7083a0104ab73b57494236ac3b7eac78a67f103f (diff) | |
download | serenity-01216f3c3fb0cd763adbc441fa728ffd5a2c254f.zip |
Userland+LibCore: Use CProcessStatisticsReader to implement top.
Also tweaked CProcessStatisticsReader a bit to simplify the API.
Diffstat (limited to 'Libraries/LibCore')
-rw-r--r-- | Libraries/LibCore/CProcessStatisticsReader.cpp | 40 | ||||
-rw-r--r-- | Libraries/LibCore/CProcessStatisticsReader.h | 9 |
2 files changed, 22 insertions, 27 deletions
diff --git a/Libraries/LibCore/CProcessStatisticsReader.cpp b/Libraries/LibCore/CProcessStatisticsReader.cpp index 7f0456d53e..30da9a1f53 100644 --- a/Libraries/LibCore/CProcessStatisticsReader.cpp +++ b/Libraries/LibCore/CProcessStatisticsReader.cpp @@ -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); } + diff --git a/Libraries/LibCore/CProcessStatisticsReader.h b/Libraries/LibCore/CProcessStatisticsReader.h index adb96f39ad..c26312bc89 100644 --- a/Libraries/LibCore/CProcessStatisticsReader.h +++ b/Libraries/LibCore/CProcessStatisticsReader.h @@ -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; }; |