| 311 | } |
| 312 | |
| 313 | void 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 | |
| 365 | TEST_P(MemoryPoolTest, smallMmapMemoryAllocation) { |
| 366 | testMmapMemoryAllocation(8 * GB, 6, 100, isLeafThreadSafe_); |
no test coverage detected