1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

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.
This commit is contained in:
Tom 2021-01-03 10:11:44 -07:00 committed by Andreas Kling
parent bc3c0fa936
commit 1d33765e1c

View file

@ -141,12 +141,12 @@ private:
} }
} }
static bool get_cpu_usage(unsigned& busy, unsigned& idle) bool get_cpu_usage(unsigned& busy, unsigned& idle)
{ {
busy = 0; busy = 0;
idle = 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()) if (!all_processes.has_value() || all_processes.value().is_empty())
return false; return false;
@ -161,13 +161,20 @@ private:
return true; 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 (m_proc_mem) {
if (!proc_memstat->open(Core::IODevice::OpenMode::ReadOnly)) // Seeking to the beginning causes a data refresh!
return false; 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); auto json = JsonValue::from_string(file_contents);
ASSERT(json.has_value()); ASSERT(json.has_value());
unsigned user_physical_allocated = json.value().as_object().get("user_physical_allocated").to_u32(); 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_busy { 0 };
unsigned m_last_cpu_idle { 0 }; unsigned m_last_cpu_idle { 0 };
String m_tooltip; String m_tooltip;
RefPtr<Core::File> m_proc_all;
RefPtr<Core::File> m_proc_mem;
}; };
int main(int argc, char** argv) int main(int argc, char** argv)