diff options
author | Tom <tomut@yahoo.com> | 2021-01-03 10:11:44 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-03 22:12:19 +0100 |
commit | 1d33765e1cd0d00466ce5cf13e7ae642268a5348 (patch) | |
tree | 9a632eb202c9c24187c905af3fefb0d210d3a1e2 | |
parent | bc3c0fa93622e498d07cd01f6f40e3d5a9120c05 (diff) | |
download | serenity-1d33765e1cd0d00466ce5cf13e7ae642268a5348.zip |
ResourceGraph: Re-use the ProcFS file descriptors when updating stats
This makes it more likely to be able to get statistics when resources
are scarce.
-rw-r--r-- | MenuApplets/ResourceGraph/main.cpp | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/MenuApplets/ResourceGraph/main.cpp b/MenuApplets/ResourceGraph/main.cpp index f6cdd183ce..d03db75552 100644 --- a/MenuApplets/ResourceGraph/main.cpp +++ b/MenuApplets/ResourceGraph/main.cpp @@ -141,12 +141,12 @@ private: } } - static bool get_cpu_usage(unsigned& busy, unsigned& idle) + bool get_cpu_usage(unsigned& busy, unsigned& idle) { busy = 0; idle = 0; - auto all_processes = Core::ProcessStatisticsReader::get_all(); + auto all_processes = Core::ProcessStatisticsReader::get_all(m_proc_all); if (!all_processes.has_value() || all_processes.value().is_empty()) return false; @@ -161,13 +161,20 @@ private: return true; } - static bool get_memory_usage(unsigned& allocated, unsigned& available) + bool get_memory_usage(unsigned& allocated, unsigned& available) { - auto proc_memstat = Core::File::construct("/proc/memstat"); - if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly)) - return false; + if (m_proc_mem) { + // Seeking to the beginning causes a data refresh! + if (!m_proc_mem->seek(0, Core::File::SeekMode::SetPosition)) + return false; + } else { + auto proc_memstat = Core::File::construct("/proc/memstat"); + if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly)) + return false; + m_proc_mem = move(proc_memstat); + } - auto file_contents = proc_memstat->read_all(); + auto file_contents = m_proc_mem->read_all(); auto json = JsonValue::from_string(file_contents); ASSERT(json.has_value()); unsigned user_physical_allocated = json.value().as_object().get("user_physical_allocated").to_u32(); @@ -184,6 +191,8 @@ private: unsigned m_last_cpu_busy { 0 }; unsigned m_last_cpu_idle { 0 }; String m_tooltip; + RefPtr<Core::File> m_proc_all; + RefPtr<Core::File> m_proc_mem; }; int main(int argc, char** argv) |