MCPcopy Create free account
hub / github.com/bytedance/bolt / testMmapMemoryAllocation

Function testMmapMemoryAllocation

bolt/common/memory/tests/MemoryPoolTest.cpp:313–363  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

311}
312
313void testMmapMemoryAllocation(
314 int64_t capacity,
315 MachinePageCount allocPages,
316 size_t allocCount,
317 bool threadSafe) {
318 MemoryManager::Options options;
319 options.allocatorCapacity = capacity;
320 options.useMmapAllocator = true;
321 MemoryManager manager{options};
322 const auto kPageSize = 4 * KB;
323
324 auto root = manager.addRootPool();
325 auto child = root->addLeafChild("elastic_quota", threadSafe);
326
327 std::vector<void*> allocations;
328 uint64_t totalPageAllocated = 0;
329 uint64_t totalPageMapped = 0;
330 auto* mmapAllocator = static_cast<MmapAllocator*>(manager.allocator());
331 const auto pageIncrement = numPagesNeeded(mmapAllocator, allocPages);
332 const auto isSizeClassAlloc =
333 allocPages <= mmapAllocator->sizeClasses().back();
334 const auto byteSize = allocPages * kPageSize;
335 const std::string buffer(byteSize, 'x');
336 for (size_t i = 0; i < allocCount; i++) {
337 void* allocResult = nullptr;
338 ASSERT_NO_THROW(allocResult = child->allocate(byteSize));
339 ASSERT_TRUE(allocResult != nullptr);
340
341 // Write data to let mapped address to be backed by physical memory
342 memcpy(allocResult, buffer.data(), byteSize);
343 allocations.emplace_back(allocResult);
344 totalPageAllocated += pageIncrement;
345 totalPageMapped += pageIncrement;
346 ASSERT_EQ(mmapAllocator->numAllocated(), totalPageAllocated);
347 ASSERT_EQ(
348 isSizeClassAlloc ? mmapAllocator->numMapped()
349 : mmapAllocator->numExternalMapped(),
350 totalPageMapped);
351 }
352 for (size_t i = 0; i < allocCount; i++) {
353 ASSERT_NO_THROW(child->free(allocations[i], byteSize));
354 totalPageAllocated -= pageIncrement;
355 ASSERT_EQ(mmapAllocator->numAllocated(), totalPageAllocated);
356 if (isSizeClassAlloc) {
357 ASSERT_EQ(mmapAllocator->numMapped(), totalPageMapped);
358 } else {
359 totalPageMapped -= pageIncrement;
360 ASSERT_EQ(mmapAllocator->numExternalMapped(), totalPageMapped);
361 }
362 }
363}
364
365TEST_P(MemoryPoolTest, smallMmapMemoryAllocation) {
366 testMmapMemoryAllocation(8 * GB, 6, 100, isLeafThreadSafe_);

Callers 1

TEST_PFunction · 0.85

Calls 12

numPagesNeededFunction · 0.85
memcpyFunction · 0.85
addRootPoolMethod · 0.80
addLeafChildMethod · 0.80
backMethod · 0.80
allocatorMethod · 0.45
allocateMethod · 0.45
dataMethod · 0.45
numAllocatedMethod · 0.45
numMappedMethod · 0.45
numExternalMappedMethod · 0.45
freeMethod · 0.45

Tested by

no test coverage detected