From 399b5ffb646ba1930aa36c81522b59a15ebb4d1f Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 7 Oct 2021 22:02:18 +0300 Subject: [PATCH] Kernel: Add the /proc/stat ProcFS component This exposes a small subset of the information exposed by the Linux equivalent, and will be used to optimize applications that would like to know the current CPU usage statistics, but don't want to read all of the unrelated information in /proc/all --- Kernel/GlobalProcessExposed.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Kernel/GlobalProcessExposed.cpp b/Kernel/GlobalProcessExposed.cpp index c3f15c7d08..e1cd1c1029 100644 --- a/Kernel/GlobalProcessExposed.cpp +++ b/Kernel/GlobalProcessExposed.cpp @@ -411,6 +411,29 @@ private: } }; +class ProcFSSystemStatistics final : public ProcFSGlobalInformation { +public: + static NonnullRefPtr must_create(); + +private: + ProcFSSystemStatistics(); + virtual KResult try_generate(KBufferBuilder& builder) override + { + JsonObjectSerializer json { builder }; + auto total_time_scheduled = Scheduler::get_total_time_scheduled(); + json.add("total_time", total_time_scheduled.total); + json.add("kernel_time", total_time_scheduled.total_kernel); + json.add("user_time", total_time_scheduled.total - total_time_scheduled.total_kernel); + u64 idle_time = 0; + Processor::for_each([&](Processor& processor) { + idle_time += processor.time_spent_idle(); + }); + json.add("idle_time", idle_time); + json.finish(); + return KSuccess; + } +}; + class ProcFSOverallProcesses final : public ProcFSGlobalInformation { public: static NonnullRefPtr must_create(); @@ -730,6 +753,10 @@ UNMAP_AFTER_INIT NonnullRefPtr ProcFSMemoryStatus::must_crea { return adopt_ref_if_nonnull(new (nothrow) ProcFSMemoryStatus).release_nonnull(); } +UNMAP_AFTER_INIT NonnullRefPtr ProcFSSystemStatistics::must_create() +{ + return adopt_ref_if_nonnull(new (nothrow) ProcFSSystemStatistics).release_nonnull(); +} UNMAP_AFTER_INIT NonnullRefPtr ProcFSOverallProcesses::must_create() { return adopt_ref_if_nonnull(new (nothrow) ProcFSOverallProcesses).release_nonnull(); @@ -788,6 +815,10 @@ UNMAP_AFTER_INIT ProcFSMemoryStatus::ProcFSMemoryStatus() : ProcFSGlobalInformation("memstat"sv) { } +UNMAP_AFTER_INIT ProcFSSystemStatistics::ProcFSSystemStatistics() + : ProcFSGlobalInformation("stat"sv) +{ +} UNMAP_AFTER_INIT ProcFSOverallProcesses::ProcFSOverallProcesses() : ProcFSGlobalInformation("all"sv) { @@ -854,6 +885,7 @@ UNMAP_AFTER_INIT NonnullRefPtr ProcFSRootDirectory::must_cr directory->m_components.append(ProcFSSelfProcessDirectory::must_create()); directory->m_components.append(ProcFSDiskUsage::must_create()); directory->m_components.append(ProcFSMemoryStatus::must_create()); + directory->m_components.append(ProcFSSystemStatistics::must_create()); directory->m_components.append(ProcFSOverallProcesses::must_create()); directory->m_components.append(ProcFSCPUInformation::must_create()); directory->m_components.append(ProcFSDmesg::must_create());