Commit bc0c89b4 authored by Dominik Inführ's avatar Dominik Inführ Committed by V8 LUCI CQ

[heap] Cache OS commit page size in MemoryAllocator

We can cache this in a static field during V8 initialization such that
we don't need to cache this method's result for each use-case that
might benefit.

Bug: v8:12691
Change-Id: I4391a5e306646465ce96fb7e354be996d9fe8b44
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3506375Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79406}
parent 0215c537
......@@ -521,6 +521,8 @@ void Isolate::InitializeOncePerProcess() {
CHECK(isolate_key_created_.compare_exchange_strong(
expected, true, std::memory_order_relaxed));
per_isolate_thread_data_key_ = base::Thread::CreateThreadLocalKey();
Heap::InitializeOncePerProcess();
}
void Isolate::DisposeOncePerProcess() {
......
......@@ -5835,6 +5835,11 @@ void Heap::InitializeHashSeed() {
0, reinterpret_cast<byte*>(&new_hash_seed), kInt64Size);
}
// static
void Heap::InitializeOncePerProcess() {
MemoryAllocator::InitializeOncePerProcess();
}
void Heap::PrintMaxMarkingLimitReached() {
PrintF("\n### Maximum marking limit reached = %.02lf\n",
max_marking_limit_reached_);
......
......@@ -808,6 +808,9 @@ class Heap {
// (Re-)Initialize hash seed from flag or RNG.
void InitializeHashSeed();
// Invoked once for the process from V8::Initialize.
static void InitializeOncePerProcess();
// Bootstraps the object heap with the core set of objects required to run.
// Returns whether it succeeded.
bool CreateHeapObjects();
......
......@@ -24,6 +24,9 @@ namespace internal {
// MemoryAllocator
//
size_t MemoryAllocator::commit_page_size_ = 0;
size_t MemoryAllocator::commit_page_size_bits_ = 0;
MemoryAllocator::MemoryAllocator(Isolate* isolate,
v8::PageAllocator* code_page_allocator,
size_t capacity)
......@@ -619,18 +622,16 @@ void MemoryAllocator::ZapBlock(Address start, size_t size,
size >> kTaggedSizeLog2);
}
intptr_t MemoryAllocator::GetCommitPageSize() {
if (FLAG_v8_os_page_size != 0) {
DCHECK(base::bits::IsPowerOfTwo(FLAG_v8_os_page_size));
return FLAG_v8_os_page_size * KB;
} else {
return CommitPageSize();
}
void MemoryAllocator::InitializeOncePerProcess() {
commit_page_size_ =
FLAG_v8_os_page_size > 0 ? FLAG_v8_os_page_size * KB : CommitPageSize();
CHECK(base::bits::IsPowerOfTwo(commit_page_size_));
commit_page_size_bits_ = base::bits::WhichPowerOfTwo(commit_page_size_);
}
base::AddressRegion MemoryAllocator::ComputeDiscardMemoryArea(Address addr,
size_t size) {
size_t page_size = MemoryAllocator::GetCommitPageSize();
size_t page_size = GetCommitPageSize();
if (size < page_size + FreeSpace::kSize) {
return base::AddressRegion(0, 0);
}
......
......@@ -153,7 +153,18 @@ class MemoryAllocator {
kConcurrentlyAndPool,
};
V8_EXPORT_PRIVATE static intptr_t GetCommitPageSize();
// Initialize page sizes field in V8::Initialize.
static void InitializeOncePerProcess();
V8_INLINE static intptr_t GetCommitPageSize() {
DCHECK_LT(0, commit_page_size_);
return commit_page_size_;
}
V8_INLINE static intptr_t GetCommitPageSizeBits() {
DCHECK_LT(0, commit_page_size_bits_);
return commit_page_size_bits_;
}
// Computes the memory area of discardable memory within a given memory area
// [addr, addr+size) and returns the result as base::AddressRegion. If the
......@@ -383,6 +394,9 @@ class MemoryAllocator {
base::Mutex executable_memory_mutex_;
#endif // DEBUG
V8_EXPORT_PRIVATE static size_t commit_page_size_;
V8_EXPORT_PRIVATE static size_t commit_page_size_bits_;
friend class heap::TestCodePageAllocatorScope;
friend class heap::TestMemoryAllocatorScope;
......
......@@ -857,14 +857,12 @@ TEST(ReadOnlySpaceMetrics_OnePage) {
faked_space->ShrinkPages();
faked_space->Seal(ReadOnlySpace::SealMode::kDoNotDetachFromHeap);
MemoryAllocator* allocator = heap->memory_allocator();
// Allocated objects size.
CHECK_EQ(faked_space->Size(), 16);
size_t committed_memory = RoundUp(
MemoryChunkLayout::ObjectStartOffsetInDataPage() + faked_space->Size(),
allocator->GetCommitPageSize());
MemoryAllocator::GetCommitPageSize());
// Amount of OS allocated memory.
CHECK_EQ(faked_space->CommittedMemory(), committed_memory);
......@@ -891,10 +889,9 @@ TEST(ReadOnlySpaceMetrics_AlignedAllocations) {
CHECK_EQ(faked_space->CommittedMemory(), 0);
CHECK_EQ(faked_space->CommittedPhysicalMemory(), 0);
MemoryAllocator* allocator = heap->memory_allocator();
// Allocate an object just under an OS page in size.
int object_size =
static_cast<int>(allocator->GetCommitPageSize() - kApiTaggedSize);
static_cast<int>(MemoryAllocator::GetCommitPageSize() - kApiTaggedSize);
// TODO(v8:8875): Pointer compression does not enable aligned memory allocation
// yet.
......@@ -926,7 +923,7 @@ TEST(ReadOnlySpaceMetrics_AlignedAllocations) {
size_t committed_memory = RoundUp(
MemoryChunkLayout::ObjectStartOffsetInDataPage() + faked_space->Size(),
allocator->GetCommitPageSize());
MemoryAllocator::GetCommitPageSize());
CHECK_EQ(faked_space->CommittedMemory(), committed_memory);
CHECK_EQ(faked_space->CommittedPhysicalMemory(), committed_memory);
......@@ -952,8 +949,6 @@ TEST(ReadOnlySpaceMetrics_TwoPages) {
CHECK_EQ(faked_space->CommittedMemory(), 0);
CHECK_EQ(faked_space->CommittedPhysicalMemory(), 0);
MemoryAllocator* allocator = heap->memory_allocator();
// Allocate an object that's too big to have more than one on a page.
int object_size = RoundUp(
......@@ -976,7 +971,7 @@ TEST(ReadOnlySpaceMetrics_TwoPages) {
// Amount of OS allocated memory.
size_t committed_memory_per_page =
RoundUp(MemoryChunkLayout::ObjectStartOffsetInDataPage() + object_size,
allocator->GetCommitPageSize());
MemoryAllocator::GetCommitPageSize());
CHECK_EQ(faked_space->CommittedMemory(), 2 * committed_memory_per_page);
CHECK_EQ(faked_space->CommittedPhysicalMemory(),
2 * committed_memory_per_page);
......@@ -985,7 +980,7 @@ TEST(ReadOnlySpaceMetrics_TwoPages) {
// page headers.
size_t capacity_per_page =
RoundUp(MemoryChunkLayout::ObjectStartOffsetInDataPage() + object_size,
allocator->GetCommitPageSize()) -
MemoryAllocator::GetCommitPageSize()) -
MemoryChunkLayout::ObjectStartOffsetInDataPage();
CHECK_EQ(faked_space->Capacity(), 2 * capacity_per_page);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment