From 60f5f48dd14cad81ece4837f6517a2dfc4c32476 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 1 Jan 2021 20:15:06 -0700 Subject: [PATCH] Kernel: Allocate profiling memory upfront We need to allocate all pages for the profiler right away so that we don't trigger page faults in the timer interrupt handler to allocate them. Fixes #4734 --- Kernel/KBuffer.h | 12 ++++++------ Kernel/Profiling.cpp | 13 ++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index 6da99d19a6..b6722d71ba 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -106,25 +106,25 @@ private: class KBuffer { public: - static OwnPtr try_create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") + static OwnPtr try_create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) { - auto impl = KBufferImpl::try_create_with_size(size, access, name); + auto impl = KBufferImpl::try_create_with_size(size, access, name, strategy); if (!impl) return nullptr; return adopt_own(*new KBuffer(impl.release_nonnull())); } - static OwnPtr try_create_with_bytes(ReadonlyBytes bytes, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") + static OwnPtr try_create_with_bytes(ReadonlyBytes bytes, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) { - auto impl = KBufferImpl::try_create_with_bytes(bytes, access, name); + auto impl = KBufferImpl::try_create_with_bytes(bytes, access, name, strategy); if (!impl) return nullptr; return adopt_own(*new KBuffer(impl.release_nonnull())); } - static KBuffer create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") + static KBuffer create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve) { - return KBuffer(KBufferImpl::create_with_size(size, access, name)); + return KBuffer(KBufferImpl::create_with_size(size, access, name, strategy)); } static KBuffer copy(const void* data, size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") diff --git a/Kernel/Profiling.cpp b/Kernel/Profiling.cpp index de7fb545ac..08278a5c01 100644 --- a/Kernel/Profiling.cpp +++ b/Kernel/Profiling.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include #include @@ -36,8 +37,13 @@ namespace Kernel { namespace Profiling { -static KBufferImpl* s_profiling_buffer; static size_t s_slot_count; +static AK::Singleton KBuffer* { + auto buffer = KBuffer::try_create_with_size(8 * MiB, Region::Access::Read | Region::Access::Write, "Profiling Buffer", AllocationStrategy::AllocateNow); + s_slot_count = buffer->size() / sizeof(Sample); + return buffer.leak_ptr(); +}> + s_profiling_buffer; static size_t s_next_slot_index; static ProcessID s_pid { -1 }; @@ -62,10 +68,7 @@ void start(Process& process) executable_path() = {}; s_pid = process.pid(); - if (!s_profiling_buffer) { - s_profiling_buffer = RefPtr(KBuffer::create_with_size(8 * MiB).impl()).leak_ref(); - s_slot_count = s_profiling_buffer->size() / sizeof(Sample); - } + s_profiling_buffer.ensure_instance(); s_next_slot_index = 0; }