mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +00:00
LibCore: Allow caching and reusing the ProcFS file descriptors
Because ProcFS will refresh the data upon seek to 0, we can re-use the same file descriptor. This saves us from having to open it every time, but it also reduces the odds that we are unable to open a new file descriptor due to low memory conditions.
This commit is contained in:
parent
cf89180c35
commit
bc3c0fa936
2 changed files with 21 additions and 6 deletions
|
@ -37,17 +37,24 @@ namespace Core {
|
||||||
|
|
||||||
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
|
HashMap<uid_t, String> ProcessStatisticsReader::s_usernames;
|
||||||
|
|
||||||
Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
|
Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file)
|
||||||
{
|
{
|
||||||
auto file = Core::File::construct("/proc/all");
|
if (proc_all_file) {
|
||||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
if (!proc_all_file->seek(0, Core::File::SeekMode::SetPosition)) {
|
||||||
fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string());
|
fprintf(stderr, "ProcessStatisticsReader: Failed to refresh /proc/all: %s\n", proc_all_file->error_string());
|
||||||
return {};
|
return {};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
proc_all_file = Core::File::construct("/proc/all");
|
||||||
|
if (!proc_all_file->open(Core::IODevice::ReadOnly)) {
|
||||||
|
fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", proc_all_file->error_string());
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMap<pid_t, Core::ProcessStatistics> map;
|
HashMap<pid_t, Core::ProcessStatistics> map;
|
||||||
|
|
||||||
auto file_contents = file->read_all();
|
auto file_contents = proc_all_file->read_all();
|
||||||
auto json = JsonValue::from_string(file_contents);
|
auto json = JsonValue::from_string(file_contents);
|
||||||
if (!json.has_value())
|
if (!json.has_value())
|
||||||
return {};
|
return {};
|
||||||
|
@ -112,6 +119,12 @@ Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_a
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<HashMap<pid_t, Core::ProcessStatistics>> ProcessStatisticsReader::get_all()
|
||||||
|
{
|
||||||
|
RefPtr<Core::File> proc_all_file;
|
||||||
|
return get_all(proc_all_file);
|
||||||
|
}
|
||||||
|
|
||||||
String ProcessStatisticsReader::username_from_uid(uid_t uid)
|
String ProcessStatisticsReader::username_from_uid(uid_t uid)
|
||||||
{
|
{
|
||||||
if (s_usernames.is_empty()) {
|
if (s_usernames.is_empty()) {
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
@ -86,6 +87,7 @@ struct ProcessStatistics {
|
||||||
|
|
||||||
class ProcessStatisticsReader {
|
class ProcessStatisticsReader {
|
||||||
public:
|
public:
|
||||||
|
static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all(RefPtr<Core::File>&);
|
||||||
static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all();
|
static Optional<HashMap<pid_t, Core::ProcessStatistics>> get_all();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue