| 1 | /* Copyright (c) 2016, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify it under |
| 4 | the terms of the GNU General Public License, version 2.0, as published by the |
| 5 | Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 18 | for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License along with |
| 21 | this program; if not, write to the Free Software Foundation, Inc., |
| 22 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <array> |
| 26 | |
| 27 | #include "storage/temptable/include/temptable/allocator.h" |
| 28 | #include "storage/temptable/include/temptable/block.h" |
| 29 | #include "storage/temptable/include/temptable/constants.h" |
| 30 | |
| 31 | namespace temptable_allocator_unittest { |
| 32 | |
| 33 | // Needed for making it possible to use user-defined literals (e.g. 1_MiB) when |
| 34 | // instantiating (generating) test-cases below |
| 35 | using temptable::operator""_KiB; |
| 36 | using temptable::operator""_MiB; |
| 37 | |
| 38 | /** GoogleTest macros for testing exceptions (EXPECT_THROW, EXPECT_NO_THROW, |
| 39 | * EXPECT_ANY_THROW, etc.) do not provide direct means to inspect the value of |
| 40 | * exception that has been thrown. |
| 41 | * |
| 42 | * Indirectly, it is possible to inspect the value but the test-body becomes |
| 43 | * unnecessarily verbose, longer and more prone to introducing errors. E.g. |
| 44 | * |
| 45 | * try { |
| 46 | * Foo foo; |
| 47 | * foo.bar(); |
| 48 | * FAIL() << "We must not reach here. Expected std::out_of_range"; |
| 49 | * } |
| 50 | * catch (const std::out_of_range& ex) { |
| 51 | * EXPECT_EQ(ex.what(), std::string("Out of range")); |
| 52 | * } |
| 53 | * catch (...) { |
| 54 | * FAIL() << "We must not reach here. Expected std::out_of_range"; |
| 55 | * } |
| 56 | * |
| 57 | * Following set of macros extend GoogleTest set of macros and enables |
| 58 | * us to express our intent in much more cleaner way. E.g. |
| 59 | * |
| 60 | * Foo foo; |
| 61 | * EXPECT_THROW_WITH_VALUE_STR(foo.bar(), std::out_of_range, "Out of range"); |
| 62 | * |
| 63 | * EXPECT_THROW_WITH_VALUE macro is for catching exceptions which are not |
| 64 | * derived from std::exception and whose value can be inspected by a mere usage |
| 65 | * of operator==. |
| 66 | * |
| 67 | * EXPECT_THROW_WITH_VALUE_STR macro is for catching exceptions which provide |
| 68 | * ::what() interface to inspect the value, such as all exceptions which |
| 69 | * are derived from std::exception. |
| 70 | * */ |
| 71 | #define EXPECT_THROW_WITH_VALUE(stmt, etype, value) \ |
| 72 | EXPECT_THROW( \ |
| 73 | try { stmt; } catch (const etype &ex) { \ |
| 74 | EXPECT_EQ(ex, value); \ |
| 75 | throw; \ |
| 76 | }, \ |
| 77 | etype) |
| 78 | #define EXPECT_THROW_WITH_VALUE_STR(stmt, etype, str) \ |
| 79 | EXPECT_THROW( \ |
| 80 | try { stmt; } catch (const etype &ex) { \ |
| 81 | EXPECT_EQ(std::string(ex.what()), str); \ |
| 82 | throw; \ |
| 83 | }, \ |
| 84 | etype) |
| 85 | |
| 86 | // A "probe" which gains us read-only access to temptable::MemoryMonitor. |
| 87 | // Necessary for implementing certain unit-tests. |
| 88 | struct MemoryMonitorReadOnlyProbe : public temptable::MemoryMonitor { |
| 89 | static size_t ram_consumption() { |
| 90 | return temptable::MemoryMonitor::RAM::consumption(); |
| 91 | } |
| 92 | static size_t ram_threshold() { |
| 93 | return temptable::MemoryMonitor::RAM::threshold(); |
| 94 | } |
| 95 | static bool mmap_enabled() { return temptable_use_mmap; } |
| 96 | static size_t mmap_consumption() { |
| 97 | return temptable::MemoryMonitor::MMAP::consumption(); |
| 98 | } |
| 99 | static size_t mmap_threshold() { |
| 100 | return temptable::MemoryMonitor::MMAP::threshold(); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | // A "probe" which enables us to hijack the temptable::MemoryMonitor. |
| 105 | // Necessary for implementing certain unit-tests. |
| 106 | struct MemoryMonitorHijackProbe : public MemoryMonitorReadOnlyProbe { |
| 107 | static size_t ram_consumption_reset() { |
| 108 | auto current_consumption = temptable::MemoryMonitor::RAM::consumption(); |
| 109 | return temptable::MemoryMonitor::RAM::decrease(current_consumption); |
| 110 | } |
| 111 | static size_t ram_consumption_set(size_t consumption) { |
| 112 | MemoryMonitorHijackProbe::ram_consumption_reset(); |
| 113 | return temptable::MemoryMonitor::RAM::increase(consumption); |
| 114 | } |
| 115 | static size_t mmap_consumption_reset() { |
| 116 | auto current_consumption = temptable::MemoryMonitor::MMAP::consumption(); |
| 117 | return temptable::MemoryMonitor::MMAP::decrease(current_consumption); |
| 118 | } |
| 119 | static void mmap_enable() { temptable_use_mmap = true; } |
| 120 | static void mmap_disable() { temptable_use_mmap = false; } |
| 121 | static void max_ram_set(size_t new_max_ram) { |
| 122 | temptable_max_ram = new_max_ram; |
| 123 | } |
| 124 | static void max_mmap_set(size_t new_max_mmap) { |
| 125 | temptable_max_mmap = new_max_mmap; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | class TempTableAllocator : public ::testing::Test { |
| 130 | protected: |
| 131 | void SetUp() override { |
| 132 | // Enable MMAP by default. We need to set it first, so the mmap_threshold() |
| 133 | // is not zero. |
| 134 | MemoryMonitorHijackProbe::mmap_enable(); |
| 135 | |
| 136 | // Store the default thresholds of RAM and MMAP so we can restore them to |
| 137 | // the original values prior to starting a new test |
| 138 | m_default_ram_threshold = MemoryMonitorHijackProbe::ram_threshold(); |
| 139 | m_default_mmap_threshold = MemoryMonitorHijackProbe::mmap_threshold(); |
| 140 | |
| 141 | // Reset the RAM and MMAP consumption counters to zero |
| 142 | EXPECT_EQ(MemoryMonitorHijackProbe::ram_consumption_reset(), 0); |
| 143 | EXPECT_EQ(MemoryMonitorHijackProbe::mmap_consumption_reset(), 0); |
| 144 | } |
| 145 | |
| 146 | void TearDown() override { |
| 147 | // Check all memory was released. |
| 148 | EXPECT_EQ(MemoryMonitorHijackProbe::ram_consumption(), 0); |
| 149 | EXPECT_EQ(MemoryMonitorHijackProbe::mmap_consumption(), 0); |
| 150 | |
| 151 | // Restore the original RAM and MMAP thresholds |
| 152 | MemoryMonitorHijackProbe::max_ram_set(m_default_ram_threshold); |
| 153 | MemoryMonitorHijackProbe::max_mmap_set(m_default_mmap_threshold); |
| 154 | } |
| 155 | |
| 156 | size_t m_default_ram_threshold; |
| 157 | size_t m_default_mmap_threshold; |
| 158 | }; |
| 159 | |
| 160 | TEST_F(TempTableAllocator, basic) { |
| 161 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 162 | temptable::Block shared_block; |
| 163 | EXPECT_TRUE(shared_block.is_empty()); |
| 164 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 165 | table_resource_monitor); |
| 166 | |
| 167 | constexpr size_t n_allocate = 128; |
| 168 | std::array<uint8_t *, n_allocate> a; |
| 169 | constexpr size_t n_elements = 16; |
| 170 | |
| 171 | for (size_t i = 0; i < n_allocate; ++i) { |
| 172 | a[i] = allocator.allocate(n_elements); |
| 173 | |
| 174 | for (size_t j = 0; j < n_elements; ++j) { |
| 175 | a[i][j] = 0xB; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | EXPECT_FALSE(shared_block.is_empty()); |
| 180 | |
| 181 | for (size_t i = 0; i < n_allocate; ++i) { |
| 182 | allocator.deallocate(a[i], n_elements); |
| 183 | } |
| 184 | |
| 185 | // Physically deallocate the shared-block (allocator keeps it alive |
| 186 | // intentionally) |
| 187 | EXPECT_FALSE(shared_block.is_empty()); |
| 188 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 189 | shared_block.type()); |
| 190 | shared_block.destroy(); |
| 191 | EXPECT_TRUE(shared_block.is_empty()); |
| 192 | } |
| 193 | |
| 194 | TEST_F(TempTableAllocator, |
| 195 | allocation_successful_when_shared_block_is_not_available) { |
| 196 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 197 | // No shared-block is available to be used by the allocator |
| 198 | temptable::Allocator<uint8_t> allocator(nullptr, table_resource_monitor); |
| 199 | uint32_t n_elements = 16; |
| 200 | |
| 201 | // Trigger the allocation |
| 202 | uint8_t *chunk = nullptr; |
| 203 | EXPECT_NO_THROW(chunk = allocator.allocate(n_elements)); |
| 204 | EXPECT_NE(chunk, nullptr); |
| 205 | |
| 206 | // Clean-up |
| 207 | allocator.deallocate(chunk, n_elements); |
| 208 | } |
| 209 | |
| 210 | TEST_F(TempTableAllocator, shared_block_is_kept_after_last_deallocation) { |
| 211 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 212 | temptable::Block shared_block; |
| 213 | EXPECT_TRUE(shared_block.is_empty()); |
| 214 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 215 | table_resource_monitor); |
| 216 | |
| 217 | uint8_t *ptr = allocator.allocate(16); |
| 218 | EXPECT_FALSE(shared_block.is_empty()); |
| 219 | |
| 220 | allocator.deallocate(ptr, 16); |
| 221 | |
| 222 | // Physically deallocate the shared-block (allocator keeps it alive |
| 223 | // intentionally) |
| 224 | EXPECT_FALSE(shared_block.is_empty()); |
| 225 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 226 | shared_block.type()); |
| 227 | shared_block.destroy(); |
| 228 | EXPECT_TRUE(shared_block.is_empty()); |
| 229 | } |
| 230 | |
| 231 | TEST_F(TempTableAllocator, rightmost_chunk_deallocated_reused_for_allocation) { |
| 232 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 233 | temptable::Block shared_block; |
| 234 | EXPECT_TRUE(shared_block.is_empty()); |
| 235 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 236 | table_resource_monitor); |
| 237 | |
| 238 | // Allocate first Chunk which is less than the 1MB |
| 239 | size_t first_chunk_size = 512 * 1024; |
| 240 | uint8_t *first_chunk = allocator.allocate(first_chunk_size); |
| 241 | |
| 242 | // Calculate and allocate second chunk in such a way that |
| 243 | // it lies within the block and fills it |
| 244 | size_t first_chunk_actual_size = |
| 245 | temptable::Chunk::size_hint(first_chunk_size); |
| 246 | size_t space_left_in_block = |
| 247 | shared_block.size() - |
| 248 | temptable::Block::size_hint(first_chunk_actual_size); |
| 249 | size_t second_chunk_size = |
| 250 | space_left_in_block - (first_chunk_actual_size - first_chunk_size); |
| 251 | uint8_t *second_chunk = allocator.allocate(second_chunk_size); |
| 252 | |
| 253 | // Make sure that pointers (Chunk's) are from same blocks |
| 254 | EXPECT_EQ(temptable::Block(temptable::Chunk(first_chunk)), |
| 255 | temptable::Block(temptable::Chunk(second_chunk))); |
| 256 | |
| 257 | EXPECT_FALSE(shared_block.can_accommodate(1)); |
| 258 | |
| 259 | // Deallocate Second Chunk |
| 260 | allocator.deallocate(second_chunk, second_chunk_size); |
| 261 | |
| 262 | // Allocate Second Chunk again |
| 263 | second_chunk = allocator.allocate(second_chunk_size); |
| 264 | |
| 265 | // Make sure that pointers (Chunk's) are from same blocks |
| 266 | EXPECT_EQ(temptable::Block(temptable::Chunk(first_chunk)), |
| 267 | temptable::Block(temptable::Chunk(second_chunk))); |
| 268 | |
| 269 | // Deallocate Second Chunk |
| 270 | allocator.deallocate(second_chunk, second_chunk_size); |
| 271 | |
| 272 | // Deallocate First Chunk |
| 273 | allocator.deallocate(first_chunk, first_chunk_size); |
| 274 | |
| 275 | // Physically deallocate the shared-block (allocator keeps it alive |
| 276 | // intentionally) |
| 277 | EXPECT_FALSE(shared_block.is_empty()); |
| 278 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 279 | shared_block.type()); |
| 280 | shared_block.destroy(); |
| 281 | EXPECT_TRUE(shared_block.is_empty()); |
| 282 | } |
| 283 | |
| 284 | TEST_F(TempTableAllocator, |
| 285 | will_increment_ram_consumption_when_shared_block_is_allocated) { |
| 286 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 287 | temptable::Block shared_block; |
| 288 | EXPECT_TRUE(shared_block.is_empty()); |
| 289 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 290 | table_resource_monitor); |
| 291 | |
| 292 | // RAM consumption is 0 at the start |
| 293 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 294 | |
| 295 | // First allocation is fed from shared-block |
| 296 | size_t shared_block_n_elements = 1024 * 1024; |
| 297 | uint8_t *shared_block_ptr = allocator.allocate(shared_block_n_elements); |
| 298 | EXPECT_FALSE(shared_block.is_empty()); |
| 299 | |
| 300 | // RAM consumption should be greater or equal than |
| 301 | // shared_block_n_elements bytes at this point |
| 302 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 303 | shared_block_n_elements); |
| 304 | |
| 305 | // Deallocate the shared-block |
| 306 | allocator.deallocate(shared_block_ptr, shared_block_n_elements); |
| 307 | |
| 308 | // Physically deallocate the shared-block (allocator keeps it alive |
| 309 | // intentionally) |
| 310 | EXPECT_FALSE(shared_block.is_empty()); |
| 311 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 312 | shared_block.type()); |
| 313 | shared_block.destroy(); |
| 314 | EXPECT_TRUE(shared_block.is_empty()); |
| 315 | } |
| 316 | |
| 317 | TEST_F(TempTableAllocator, |
| 318 | will_not_decrement_ram_consumption_when_shared_block_is_deallocated) { |
| 319 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 320 | temptable::Block shared_block; |
| 321 | EXPECT_TRUE(shared_block.is_empty()); |
| 322 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 323 | table_resource_monitor); |
| 324 | |
| 325 | // RAM consumption is 0 at the start |
| 326 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 327 | |
| 328 | // First allocation is fed from shared-block |
| 329 | size_t shared_block_n_elements = 1024 * 1024; |
| 330 | uint8_t *shared_block_ptr = allocator.allocate(shared_block_n_elements); |
| 331 | EXPECT_FALSE(shared_block.is_empty()); |
| 332 | |
| 333 | // RAM consumption should be greater or equal than |
| 334 | // shared_block_n_elements bytes at this point |
| 335 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 336 | shared_block_n_elements); |
| 337 | |
| 338 | // Deallocate the shared-block |
| 339 | allocator.deallocate(shared_block_ptr, shared_block_n_elements); |
| 340 | |
| 341 | // Physically deallocate the shared-block (allocator keeps it alive |
| 342 | // intentionally) |
| 343 | EXPECT_FALSE(shared_block.is_empty()); |
| 344 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 345 | shared_block.type()); |
| 346 | shared_block.destroy(); |
| 347 | EXPECT_TRUE(shared_block.is_empty()); |
| 348 | |
| 349 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 350 | } |
| 351 | |
| 352 | TEST_F( |
| 353 | TempTableAllocator, |
| 354 | ram_consumption_does_not_drop_to_zero_when_last_non_shared_block_is_destroyed) { |
| 355 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 356 | temptable::Block shared_block; |
| 357 | EXPECT_TRUE(shared_block.is_empty()); |
| 358 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 359 | table_resource_monitor); |
| 360 | |
| 361 | // RAM consumption should be greater or equal than |
| 362 | // shared_block_n_elements bytes at this point |
| 363 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 364 | |
| 365 | // Make sure we fill up the shared_block first |
| 366 | // nr of elements must be >= 1MiB in size |
| 367 | size_t shared_block_n_elements = 1024 * 1024 + 256; |
| 368 | uint8_t *shared_block_ptr = allocator.allocate(shared_block_n_elements); |
| 369 | EXPECT_FALSE(shared_block.is_empty()); |
| 370 | |
| 371 | // Not even 1-byte should be able to fit anymore |
| 372 | EXPECT_FALSE(shared_block.can_accommodate(1)); |
| 373 | |
| 374 | // Now our next allocation should result in new block allocation ... |
| 375 | size_t non_shared_block_n_elements = 2 * 1024; |
| 376 | uint8_t *non_shared_block_ptr = |
| 377 | allocator.allocate(non_shared_block_n_elements); |
| 378 | |
| 379 | // Make sure that pointers (Chunk's) are from different blocks |
| 380 | EXPECT_NE(temptable::Block(temptable::Chunk(non_shared_block_ptr)), |
| 381 | temptable::Block(temptable::Chunk(shared_block_ptr))); |
| 382 | |
| 383 | // RAM consumption should be greater or equal than |
| 384 | // non_shared_block_n_elements bytes at this point |
| 385 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 386 | non_shared_block_n_elements); |
| 387 | |
| 388 | // Deallocate the non-shared block |
| 389 | allocator.deallocate(non_shared_block_ptr, non_shared_block_n_elements); |
| 390 | |
| 391 | // RAM consumption should be greater or equal than |
| 392 | // shared_block_n_elements bytes at this point |
| 393 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 394 | shared_block_n_elements); |
| 395 | |
| 396 | // Deallocate the shared-block |
| 397 | allocator.deallocate(shared_block_ptr, shared_block_n_elements); |
| 398 | |
| 399 | // Physically deallocate the shared-block (allocator keeps it alive |
| 400 | // intentionally) |
| 401 | EXPECT_FALSE(shared_block.is_empty()); |
| 402 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 403 | shared_block.type()); |
| 404 | shared_block.destroy(); |
| 405 | EXPECT_TRUE(shared_block.is_empty()); |
| 406 | } |
| 407 | |
| 408 | TEST_F( |
| 409 | TempTableAllocator, |
| 410 | shared_block_allocated_from_ram_when_ram_threshold_is_not_hit_for_given_block_size) { |
| 411 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 412 | temptable::Block shared_block; |
| 413 | EXPECT_TRUE(shared_block.is_empty()); |
| 414 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 415 | table_resource_monitor); |
| 416 | |
| 417 | // Size of the shared_block we will request must fit (not hit the |
| 418 | // threshold) |
| 419 | size_t shared_block_n_elements = 1024; |
| 420 | EXPECT_LE(MemoryMonitorReadOnlyProbe::ram_consumption() + |
| 421 | temptable::Block::size_hint(shared_block_n_elements), |
| 422 | MemoryMonitorReadOnlyProbe::ram_threshold()); |
| 423 | |
| 424 | // First allocation is fed from shared-block |
| 425 | uint8_t *shared_block_ptr = allocator.allocate(shared_block_n_elements); |
| 426 | EXPECT_FALSE(shared_block.is_empty()); |
| 427 | |
| 428 | // RAM consumption should be greater or equal than |
| 429 | // shared_block_n_elements bytes at this point |
| 430 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 431 | shared_block_n_elements); |
| 432 | |
| 433 | // Deallocate the shared-block |
| 434 | allocator.deallocate(shared_block_ptr, shared_block_n_elements); |
| 435 | |
| 436 | // Physically deallocate the shared-block (allocator keeps it alive |
| 437 | // intentionally) |
| 438 | EXPECT_FALSE(shared_block.is_empty()); |
| 439 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 440 | shared_block.type()); |
| 441 | shared_block.destroy(); |
| 442 | EXPECT_TRUE(shared_block.is_empty()); |
| 443 | } |
| 444 | |
| 445 | TEST_F( |
| 446 | TempTableAllocator, |
| 447 | shared_block_allocated_from_mmap_when_ram_threshold_is_hit_for_given_block_size) { |
| 448 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 449 | temptable::Block shared_block; |
| 450 | EXPECT_TRUE(shared_block.is_empty()); |
| 451 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 452 | table_resource_monitor); |
| 453 | |
| 454 | // Set some artificially low RAM threshold |
| 455 | MemoryMonitorHijackProbe::max_ram_set(128); |
| 456 | |
| 457 | // Size of the shared_block we will request must exceed the RAM threshold |
| 458 | size_t shared_block_n_elements = 1024; |
| 459 | EXPECT_GT(MemoryMonitorReadOnlyProbe::ram_consumption() + |
| 460 | temptable::Block::size_hint(shared_block_n_elements), |
| 461 | MemoryMonitorReadOnlyProbe::ram_threshold()); |
| 462 | |
| 463 | // First allocation is fed from shared-block |
| 464 | uint8_t *shared_block_ptr = allocator.allocate(shared_block_n_elements); |
| 465 | EXPECT_FALSE(shared_block.is_empty()); |
| 466 | |
| 467 | // As we have no means to track MMAP consumption yet, we will have to deduce |
| 468 | // information that shared_block was allocated from MMAP by checking if |
| 469 | // RAM consumption remained the same (zero) |
| 470 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 471 | // Similarly we can check that we didn't get nullptr block |
| 472 | EXPECT_TRUE(shared_block_ptr != nullptr); |
| 473 | |
| 474 | // Deallocate the shared-block |
| 475 | allocator.deallocate(shared_block_ptr, shared_block_n_elements); |
| 476 | |
| 477 | // Physically deallocate the shared-block (allocator keeps it alive |
| 478 | // intentionally) |
| 479 | EXPECT_FALSE(shared_block.is_empty()); |
| 480 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 481 | shared_block.type()); |
| 482 | shared_block.destroy(); |
| 483 | EXPECT_TRUE(shared_block.is_empty()); |
| 484 | } |
| 485 | |
| 486 | TEST_F(TempTableAllocator, zero_size_allocation_returns_nullptr) { |
| 487 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 488 | temptable::Allocator<uint8_t> allocator(nullptr, table_resource_monitor); |
| 489 | EXPECT_EQ(nullptr, allocator.allocate(0)); |
| 490 | } |
| 491 | |
| 492 | TEST_F(TempTableAllocator, block_size_cap) { |
| 493 | temptable::Block shared_block; |
| 494 | EXPECT_TRUE(shared_block.is_empty()); |
| 495 | temptable::TableResourceMonitor table_resource_monitor( |
| 496 | std::numeric_limits<size_t>::max()); |
| 497 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 498 | table_resource_monitor); |
| 499 | |
| 500 | using namespace temptable; |
| 501 | |
| 502 | constexpr size_t alloc_size = 1_MiB; |
| 503 | constexpr size_t n_allocate = ALLOCATOR_MAX_BLOCK_BYTES / alloc_size + 10; |
| 504 | std::array<uint8_t *, n_allocate> a; |
| 505 | |
| 506 | for (size_t i = 0; i < n_allocate; ++i) { |
| 507 | a[i] = allocator.allocate(alloc_size); |
| 508 | } |
| 509 | |
| 510 | EXPECT_FALSE(shared_block.is_empty()); |
| 511 | |
| 512 | for (size_t i = 0; i < n_allocate; ++i) { |
| 513 | allocator.deallocate(a[i], alloc_size); |
| 514 | } |
| 515 | |
| 516 | // Physically deallocate the shared-block (allocator keeps it alive |
| 517 | // intentionally) |
| 518 | EXPECT_FALSE(shared_block.is_empty()); |
| 519 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 520 | shared_block.type()); |
| 521 | shared_block.destroy(); |
| 522 | EXPECT_TRUE(shared_block.is_empty()); |
| 523 | } |
| 524 | |
| 525 | TEST_F( |
| 526 | TempTableAllocator, |
| 527 | table_resource_monitor_increases_then_drops_to_0_when_allocation_is_backed_by_shared_block) { |
| 528 | temptable::TableResourceMonitor table_resource_monitor(16_MiB); |
| 529 | temptable::Block shared_block; |
| 530 | EXPECT_TRUE(shared_block.is_empty()); |
| 531 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 532 | table_resource_monitor); |
| 533 | |
| 534 | // Make sure table resource monitor is set |
| 535 | EXPECT_EQ(table_resource_monitor.consumption(), 0); |
| 536 | EXPECT_EQ(table_resource_monitor.threshold(), 16_MiB); |
| 537 | |
| 538 | // Allocate a chunk |
| 539 | auto chunk_from_shared_block = allocator.allocate(5_KiB); |
| 540 | |
| 541 | // Make sure that the chunk is fed by the shared_block |
| 542 | temptable::Block block = |
| 543 | temptable::Block(temptable::Chunk(chunk_from_shared_block)); |
| 544 | EXPECT_EQ(block, shared_block); |
| 545 | EXPECT_EQ(block.size(), shared_block.size()); |
| 546 | |
| 547 | // Check that the table resource monitor increased accordingly |
| 548 | EXPECT_EQ(table_resource_monitor.consumption(), 5_KiB); |
| 549 | |
| 550 | // Deallocate and check that the table resource monitor decreased accordingly |
| 551 | allocator.deallocate(chunk_from_shared_block, 5_KiB); |
| 552 | EXPECT_EQ(table_resource_monitor.consumption(), 0_KiB); |
| 553 | |
| 554 | // Physically deallocate the shared-block (allocator keeps it alive |
| 555 | // intentionally) |
| 556 | EXPECT_FALSE(shared_block.is_empty()); |
| 557 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 558 | shared_block.type()); |
| 559 | shared_block.destroy(); |
| 560 | EXPECT_TRUE(shared_block.is_empty()); |
| 561 | } |
| 562 | |
| 563 | TEST_F( |
| 564 | TempTableAllocator, |
| 565 | table_resource_monitor_increases_then_drops_to_0_when_allocation_is_not_backed_by_shared_block) { |
| 566 | temptable::TableResourceMonitor table_resource_monitor(16_MiB); |
| 567 | temptable::Allocator<uint8_t> allocator(nullptr, table_resource_monitor); |
| 568 | |
| 569 | // Make sure table resource monitor is set |
| 570 | EXPECT_EQ(table_resource_monitor.consumption(), 0); |
| 571 | EXPECT_EQ(table_resource_monitor.threshold(), 16_MiB); |
| 572 | |
| 573 | // Allocate a chunk |
| 574 | auto chunk = allocator.allocate(5_KiB); |
| 575 | |
| 576 | // Check that the table resource monitor increased accordingly |
| 577 | EXPECT_EQ(table_resource_monitor.consumption(), 5_KiB); |
| 578 | |
| 579 | // Deallocate and check that the table resource monitor decreased accordingly |
| 580 | allocator.deallocate(chunk, 5_KiB); |
| 581 | EXPECT_EQ(table_resource_monitor.consumption(), 0_KiB); |
| 582 | } |
| 583 | |
| 584 | TEST_F( |
| 585 | TempTableAllocator, |
| 586 | table_resource_monitor_increases_then_drops_to_0_when_there_are_multitude_of_allocations) { |
| 587 | temptable::TableResourceMonitor table_resource_monitor(16_MiB); |
| 588 | temptable::Block shared_block; |
| 589 | EXPECT_TRUE(shared_block.is_empty()); |
| 590 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 591 | table_resource_monitor); |
| 592 | |
| 593 | // Make sure table resource monitor is set |
| 594 | EXPECT_EQ(table_resource_monitor.consumption(), 0); |
| 595 | EXPECT_EQ(table_resource_monitor.threshold(), 16_MiB); |
| 596 | |
| 597 | // Allocate a chunk |
| 598 | auto chunk1 = allocator.allocate(5_KiB); |
| 599 | |
| 600 | // Check that the table resource monitor increased accordingly |
| 601 | EXPECT_EQ(table_resource_monitor.consumption(), 5_KiB); |
| 602 | |
| 603 | // Allocate another chunk |
| 604 | auto chunk2 = allocator.allocate(10_KiB); |
| 605 | |
| 606 | // Check that the table resource monitor increased accordingly |
| 607 | EXPECT_EQ(table_resource_monitor.consumption(), 15_KiB); |
| 608 | |
| 609 | // Deallocate the first chunk and check that the table resource monitor |
| 610 | // decreased accordingly |
| 611 | allocator.deallocate(chunk1, 5_KiB); |
| 612 | EXPECT_EQ(table_resource_monitor.consumption(), 10_KiB); |
| 613 | |
| 614 | // Allocate another chunk |
| 615 | auto chunk3 = allocator.allocate(50_KiB); |
| 616 | |
| 617 | // Check that the table resource monitor increased accordingly |
| 618 | EXPECT_EQ(table_resource_monitor.consumption(), 60_KiB); |
| 619 | |
| 620 | // Deallocate the second chunk and check that the table resource monitor |
| 621 | // decreased accordingly |
| 622 | allocator.deallocate(chunk2, 10_KiB); |
| 623 | EXPECT_EQ(table_resource_monitor.consumption(), 50_KiB); |
| 624 | |
| 625 | // Deallocate the third chunk and check that the table resource monitor |
| 626 | // decreased accordingly |
| 627 | allocator.deallocate(chunk3, 50_KiB); |
| 628 | EXPECT_EQ(table_resource_monitor.consumption(), 0_KiB); |
| 629 | |
| 630 | // Physically deallocate the shared-block (allocator keeps it alive |
| 631 | // intentionally) |
| 632 | EXPECT_FALSE(shared_block.is_empty()); |
| 633 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 634 | shared_block.type()); |
| 635 | shared_block.destroy(); |
| 636 | EXPECT_TRUE(shared_block.is_empty()); |
| 637 | } |
| 638 | |
| 639 | TEST_F( |
| 640 | TempTableAllocator, |
| 641 | table_resource_monitor_limit_is_respected_and_record_file_full_is_thrown) { |
| 642 | temptable::TableResourceMonitor table_resource_monitor(2_MiB); |
| 643 | temptable::Block shared_block; |
| 644 | EXPECT_TRUE(shared_block.is_empty()); |
| 645 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 646 | table_resource_monitor); |
| 647 | |
| 648 | // Make sure table resource monitor is set |
| 649 | EXPECT_EQ(table_resource_monitor.consumption(), 0); |
| 650 | EXPECT_EQ(table_resource_monitor.threshold(), 2_MiB); |
| 651 | |
| 652 | // Allocate a chunk |
| 653 | auto chunk1 = allocator.allocate(792_KiB); |
| 654 | |
| 655 | // Check that the table resource monitor increased accordingly |
| 656 | EXPECT_EQ(table_resource_monitor.consumption(), 792_KiB); |
| 657 | |
| 658 | // Allocate another chunk |
| 659 | auto chunk2 = allocator.allocate(512_KiB); |
| 660 | |
| 661 | // Check that the table resource monitor increased accordingly |
| 662 | EXPECT_EQ(table_resource_monitor.consumption(), 792_KiB + 512_KiB); |
| 663 | |
| 664 | try { |
| 665 | // Allocate another chunk |
| 666 | auto chunk3 = allocator.allocate(520_KiB); |
| 667 | (void)chunk3; |
| 668 | } catch (std::exception &) { |
| 669 | EXPECT_TRUE(false); |
| 670 | } catch (temptable::Result r) { |
| 671 | EXPECT_EQ(r, temptable::Result::RECORD_FILE_FULL); |
| 672 | } |
| 673 | |
| 674 | // Deallocate the second chunk and check that the table resource monitor |
| 675 | // decreased accordingly |
| 676 | allocator.deallocate(chunk2, 512_KiB); |
| 677 | EXPECT_EQ(table_resource_monitor.consumption(), 792_KiB); |
| 678 | |
| 679 | // Deallocate the third chunk and check that the table resource monitor |
| 680 | // decreased accordingly |
| 681 | allocator.deallocate(chunk1, 792_KiB); |
| 682 | EXPECT_EQ(table_resource_monitor.consumption(), 0_KiB); |
| 683 | |
| 684 | // Physically deallocate the shared-block (allocator keeps it alive |
| 685 | // intentionally) |
| 686 | EXPECT_FALSE(shared_block.is_empty()); |
| 687 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 688 | shared_block.type()); |
| 689 | shared_block.destroy(); |
| 690 | EXPECT_TRUE(shared_block.is_empty()); |
| 691 | } |
| 692 | |
| 693 | TEST_F(TempTableAllocator, |
| 694 | shared_block_utilization_shall_not_impact_the_block_size_growth_policy) { |
| 695 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 696 | temptable::Block shared_block; |
| 697 | temptable::Allocator<uint8_t> a1(&shared_block, table_resource_monitor); |
| 698 | temptable::Allocator<uint8_t> a2(&shared_block, table_resource_monitor); |
| 699 | auto r11 = a1.allocate(512_KiB); |
| 700 | temptable::Block b11 = temptable::Block(temptable::Chunk(r11)); |
| 701 | EXPECT_EQ(b11, shared_block); |
| 702 | EXPECT_EQ(b11.size(), shared_block.size()); |
| 703 | EXPECT_EQ(b11.size(), 1_MiB); |
| 704 | // ^^ |
| 705 | // 1. Allocator detects that shared_block is empty |
| 706 | // 2. It uses the block-size growth policy to compute the block-size |
| 707 | // 3. It allocates the block of 1MiB of size. Our shared_block is now 1MiB of |
| 708 | // size big. |
| 709 | // 4. Returns a pointer from shared_block. |
| 710 | |
| 711 | auto r12 = a1.allocate(256_KiB); |
| 712 | temptable::Block b12 = temptable::Block(temptable::Chunk(r12)); |
| 713 | EXPECT_EQ(b12, shared_block); |
| 714 | EXPECT_EQ(b12.size(), shared_block.size()); |
| 715 | // ^^ |
| 716 | // 1. Allocator detects that shared_block is not empty |
| 717 | // 2. It detects that shared_block has enough space left (1MiB - 512KiB = |
| 718 | // 512KiB) to accomodate the 256KiB request. |
| 719 | // 3. Returns a pointer from shared_block. |
| 720 | |
| 721 | auto r13 = a1.allocate(512_KiB); |
| 722 | temptable::Block b13 = temptable::Block(temptable::Chunk(r13)); |
| 723 | EXPECT_NE(b13, shared_block); |
| 724 | EXPECT_NE(b13, b12); |
| 725 | EXPECT_EQ(b13.size(), 1_MiB); |
| 726 | // ^^ |
| 727 | // 1. Allocator detects that shared_block is not empty |
| 728 | // 2. It detects that shared_block does not have enough space left (1MiB - |
| 729 | // 512KiB - 256KiB = 256KiB) to accomodate the 512KiB request. |
| 730 | // 3. It uses the block-size growth policy to compute the block-size. |
| 731 | // 4. It allocates the block of 2MiB of size. |
| 732 | // 5. Returns a pointer from new block. |
| 733 | |
| 734 | auto r21 = a2.allocate(512_KiB); |
| 735 | temptable::Block b21 = temptable::Block(temptable::Chunk(r21)); |
| 736 | EXPECT_NE(b21, shared_block); |
| 737 | EXPECT_EQ(b21.size(), 1_MiB); |
| 738 | // ^^^^ |
| 739 | // 1. Allocator detects that shared_block is not empty. |
| 740 | // 2. It detects that shared_block does not have enough space left (1MiB - |
| 741 | // 512KiB - 256KiB = 256KiB) to accomodate the 512KiB request. |
| 742 | // 3. It uses the block-size growth policy to compute the block-size. |
| 743 | // 4. It allocates the block of 1MiB of size. |
| 744 | // 5. Returns a pointer from new block. |
| 745 | |
| 746 | auto r14 = a1.allocate(128_KiB); |
| 747 | temptable::Block b14 = temptable::Block(temptable::Chunk(r14)); |
| 748 | EXPECT_EQ(b14, shared_block); |
| 749 | EXPECT_EQ(b14.size(), shared_block.size()); |
| 750 | // ^^ |
| 751 | // 1. Allocator detects that shared_block is not empty |
| 752 | // 2. It detects that shared_block has enough space left (1MiB - 512KiB - |
| 753 | // 256KiB = 256KiB) to accomodate the 128KiB request. |
| 754 | // 3. Returns a pointer from shared_block. |
| 755 | |
| 756 | auto r15 = a1.allocate(1_MiB - 512_KiB); |
| 757 | temptable::Block b15 = temptable::Block(temptable::Chunk(r15)); |
| 758 | EXPECT_NE(b15, shared_block); |
| 759 | EXPECT_EQ(b15.size(), 2_MiB); |
| 760 | // ^^ |
| 761 | // 1. Allocator detects that shared_block is not empty. |
| 762 | // 2. It detects that shared_block does not have enough space left (1MiB - |
| 763 | // 512KiB - 256KiB - 128KiB = 128KiB) to accomodate the 1.5MiB request. |
| 764 | // 3. It also checks if there is enough space left in secondly instantiated |
| 765 | // 1MiB block (see (B)) to accomodate the 1.5MiB. It does not. |
| 766 | // 4. It allocates the block of 2MiB of size. |
| 767 | // 3. Returns a pointer from new block. |
| 768 | |
| 769 | auto r22 = a2.allocate(1_MiB); |
| 770 | temptable::Block b22 = temptable::Block(temptable::Chunk(r22)); |
| 771 | EXPECT_NE(b22, shared_block); |
| 772 | EXPECT_EQ(b22.size(), 2_MiB); |
| 773 | // 1. Allocator detects that shared_block is not empty. |
| 774 | // 2. It detects that shared_block does not have enough space left (1MiB - |
| 775 | // 512KiB - 256KiB - 128KiB = 128KiB) to accomodate the 1MiB request. |
| 776 | // 3. It uses the block-size growth policy to compute the block-size. |
| 777 | // 4. It allocates the block of 2MiB of size. |
| 778 | // 5. Returns a pointer from new block. |
| 779 | |
| 780 | a1.deallocate(r11, 512_KiB); |
| 781 | a1.deallocate(r12, 256_KiB); |
| 782 | a1.deallocate(r13, 512_KiB); |
| 783 | a1.deallocate(r14, 128_KiB); |
| 784 | a1.deallocate(r15, 1_MiB - 512_KiB); |
| 785 | a2.deallocate(r21, 512_KiB); |
| 786 | a2.deallocate(r22, 1_MiB); |
| 787 | |
| 788 | // Physically deallocate the shared-block (allocator keeps it alive |
| 789 | // intentionally) |
| 790 | EXPECT_FALSE(shared_block.is_empty()); |
| 791 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 792 | shared_block.type()); |
| 793 | shared_block.destroy(); |
| 794 | EXPECT_TRUE(shared_block.is_empty()); |
| 795 | } |
| 796 | |
| 797 | TEST_F( |
| 798 | TempTableAllocator, |
| 799 | repeated_allocation_followed_by_deallocation_does_not_create_new_blocks) { |
| 800 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 801 | { |
| 802 | temptable::Block shared_block; |
| 803 | ; |
| 804 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 805 | table_resource_monitor); |
| 806 | // RAM consumption is 0 at the start |
| 807 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 808 | |
| 809 | auto r1 = allocator.allocate(800_KiB); |
| 810 | temptable::Block b1 = temptable::Block(temptable::Chunk(r1)); |
| 811 | EXPECT_EQ(b1, shared_block); |
| 812 | EXPECT_EQ(b1.size(), shared_block.size()); |
| 813 | EXPECT_EQ(b1.size(), 1_MiB); |
| 814 | // ^^ |
| 815 | // 1. Allocator detects that shared_block is empty |
| 816 | // 2. It uses the block-size growth policy to compute the block-size |
| 817 | // 3. It allocates the block of 1MiB of size. Our shared_block is now 1MiB |
| 818 | // of size big. |
| 819 | // 4. Returns a pointer from shared_block. |
| 820 | |
| 821 | auto r2 = allocator.allocate(800_KiB); |
| 822 | temptable::Block b2 = temptable::Block(temptable::Chunk(r2)); |
| 823 | EXPECT_NE(b2, shared_block); |
| 824 | EXPECT_EQ(b2.size(), 1_MiB); |
| 825 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 2_MiB); |
| 826 | // ^^ |
| 827 | // 1. Allocator detects that shared_block is not empty, but it can't use it |
| 828 | // to allocate new chunk. |
| 829 | // 2. It allocates a new block of 1MiB of size. |
| 830 | // 3. Returns a pointer from a new block. |
| 831 | |
| 832 | { |
| 833 | auto r3 = allocator.allocate(800_KiB); |
| 834 | temptable::Block b3 = temptable::Block(temptable::Chunk(r3)); |
| 835 | EXPECT_NE(b3, shared_block); |
| 836 | EXPECT_EQ(b3.size(), 2_MiB); |
| 837 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 4_MiB); |
| 838 | // ^^ |
| 839 | // 1. Allocator detects that shared_block is not empty, but it can't use |
| 840 | // it to allocate new chunk. |
| 841 | // 2. Neither the current block can be used. |
| 842 | // 3. It allocates a new block of 1MiB of size. |
| 843 | // 4. Returns a pointer from a new block. |
| 844 | |
| 845 | allocator.deallocate(r3, 800_KiB); |
| 846 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 4_MiB); |
| 847 | // ^^ |
| 848 | // 1. Allocator removes the chunk from the current block. |
| 849 | // 2. It sees it is now empty, but caches it and does not deallocate it. |
| 850 | // 3. The consumption stays at 4MiB. |
| 851 | } |
| 852 | |
| 853 | { |
| 854 | auto r3 = allocator.allocate(800_KiB); |
| 855 | temptable::Block b3 = temptable::Block(temptable::Chunk(r3)); |
| 856 | EXPECT_NE(b3, shared_block); |
| 857 | EXPECT_EQ(b3.size(), 2_MiB); |
| 858 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 4_MiB); |
| 859 | // ^^ |
| 860 | // 1. Allocator detects that shared_block is not empty, but it can't use |
| 861 | // it to allocate new chunk. |
| 862 | // 2. The current block can be used as it empty now. |
| 863 | // 3. It allocates a new block of 1MiB of size. |
| 864 | // 4. Returns a pointer from a new block. |
| 865 | |
| 866 | allocator.deallocate(r3, 800_KiB); |
| 867 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 4_MiB); |
| 868 | // ^^ |
| 869 | // 1. Allocator removes the chunk from the current block. |
| 870 | // 2. It sees it is now empty, but caches it and does not deallocate it. |
| 871 | // 3. The consumption stays at 4MiB. |
| 872 | } |
| 873 | allocator.deallocate(r2, 800_KiB); |
| 874 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 3_MiB); |
| 875 | // ^^ |
| 876 | // 1. Allocator removes the chunk from old block. |
| 877 | // 2. It sees it is now empty, and is not the current one and deallocates |
| 878 | // it. |
| 879 | // 3. The consumption drops to 2MiB. |
| 880 | allocator.deallocate(r1, 800_KiB); |
| 881 | |
| 882 | // Physically deallocate the shared-block (allocator keeps it alive |
| 883 | // intentionally) |
| 884 | EXPECT_FALSE(shared_block.is_empty()); |
| 885 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(shared_block.size(), |
| 886 | shared_block.type()); |
| 887 | shared_block.destroy(); |
| 888 | EXPECT_TRUE(shared_block.is_empty()); |
| 889 | |
| 890 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 2_MiB); |
| 891 | // ^^ |
| 892 | // 1. Shared block is deallocated. |
| 893 | // 2. Allocator still holds the current block alive. |
| 894 | // 3. The consumption should drop by the 1MiB used by the shared block. |
| 895 | } |
| 896 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0_MiB); |
| 897 | // ^^ |
| 898 | // 1. Allocator is destroyed. |
| 899 | // 2. It sees it has an empty current block and deallocates it. |
| 900 | // 3. The consumption should drop by the 2MiB used by the current block. |
| 901 | } |
| 902 | |
| 903 | // Create some aliases to make our life easier when generating the test-cases |
| 904 | // down below. |
| 905 | using max_ram = decltype(temptable_max_ram); |
| 906 | using max_mmap = decltype(temptable_max_mmap); |
| 907 | using use_mmap = decltype(temptable_use_mmap); |
| 908 | using n_elements = uint32_t; |
| 909 | using is_ram_expected_to_be_increased = bool; |
| 910 | using is_mmap_expected_to_be_increased = bool; |
| 911 | |
| 912 | // Parametrized test for testing successful allocation patterns |
| 913 | class AllocatesSuccessfully |
| 914 | : public TempTableAllocator, |
| 915 | public ::testing::WithParamInterface<std::tuple< |
| 916 | max_ram, max_mmap, use_mmap, n_elements, |
| 917 | is_ram_expected_to_be_increased, is_mmap_expected_to_be_increased>> { |
| 918 | }; |
| 919 | |
| 920 | // Parametrized test for testing allocation patterns which should yield |
| 921 | // RecordFileFull exception |
| 922 | class ThrowsRecordFileFull |
| 923 | : public TempTableAllocator, |
| 924 | public ::testing::WithParamInterface< |
| 925 | std::tuple<max_ram, max_mmap, use_mmap, n_elements>> {}; |
| 926 | |
| 927 | // Implementation of parametrized test-cases which tests successful allocation |
| 928 | // patterns |
| 929 | TEST_P(AllocatesSuccessfully, |
| 930 | for_various_allocation_patterns_and_configurations) { |
| 931 | auto max_ram = std::get<0>(GetParam()); |
| 932 | auto max_mmap = std::get<1>(GetParam()); |
| 933 | auto mmap_enabled = std::get<2>(GetParam()); |
| 934 | auto n_elements = std::get<3>(GetParam()); |
| 935 | auto ram_expected_to_be_increased = std::get<4>(GetParam()); |
| 936 | auto mmap_expected_to_be_increased = std::get<5>(GetParam()); |
| 937 | |
| 938 | MemoryMonitorHijackProbe::max_ram_set(max_ram); |
| 939 | MemoryMonitorHijackProbe::max_mmap_set(max_mmap); |
| 940 | mmap_enabled ? MemoryMonitorHijackProbe::mmap_enable() |
| 941 | : MemoryMonitorHijackProbe::mmap_disable(); |
| 942 | |
| 943 | // Trigger the allocation |
| 944 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 945 | temptable::Allocator<uint8_t> allocator(nullptr, table_resource_monitor); |
| 946 | uint8_t *chunk = nullptr; |
| 947 | EXPECT_NO_THROW(chunk = allocator.allocate(n_elements)); |
| 948 | EXPECT_NE(chunk, nullptr); |
| 949 | |
| 950 | // After successful allocation, and depending on the use-case, RAM and MMAP |
| 951 | // consumption should increase or stay at the same level accordingly |
| 952 | if (ram_expected_to_be_increased) { |
| 953 | EXPECT_GE(MemoryMonitorReadOnlyProbe::ram_consumption(), |
| 954 | n_elements * sizeof(uint8_t)); |
| 955 | } else { |
| 956 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 957 | } |
| 958 | if (mmap_expected_to_be_increased) { |
| 959 | EXPECT_GE(MemoryMonitorReadOnlyProbe::mmap_consumption(), |
| 960 | n_elements * sizeof(uint8_t)); |
| 961 | } else { |
| 962 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::mmap_consumption(), 0); |
| 963 | } |
| 964 | |
| 965 | // Clean-up |
| 966 | allocator.deallocate(chunk, n_elements); |
| 967 | } |
| 968 | |
| 969 | // Implementation of parametrized test-cases which tests allocation patterns |
| 970 | // yielding RecordFileFull exception |
| 971 | TEST_P(ThrowsRecordFileFull, |
| 972 | for_various_allocation_patterns_and_configurations) { |
| 973 | auto max_ram = std::get<0>(GetParam()); |
| 974 | auto max_mmap = std::get<1>(GetParam()); |
| 975 | auto mmap_enabled = std::get<2>(GetParam()); |
| 976 | auto n_elements = std::get<3>(GetParam()); |
| 977 | |
| 978 | MemoryMonitorHijackProbe::max_ram_set(max_ram); |
| 979 | MemoryMonitorHijackProbe::max_mmap_set(max_mmap); |
| 980 | mmap_enabled ? MemoryMonitorHijackProbe::mmap_enable() |
| 981 | : MemoryMonitorHijackProbe::mmap_disable(); |
| 982 | |
| 983 | // Trigger the allocation |
| 984 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 985 | temptable::Allocator<uint8_t> allocator(nullptr, table_resource_monitor); |
| 986 | uint8_t *chunk = nullptr; |
| 987 | EXPECT_THROW_WITH_VALUE(chunk = allocator.allocate(n_elements), |
| 988 | temptable::Result, |
| 989 | temptable::Result::RECORD_FILE_FULL); |
| 990 | EXPECT_EQ(chunk, nullptr); |
| 991 | // After allocation failure, RAM consumption must remain intact (zero) |
| 992 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::ram_consumption(), 0); |
| 993 | // Ditto for MMAP |
| 994 | EXPECT_EQ(MemoryMonitorReadOnlyProbe::mmap_consumption(), 0); |
| 995 | } |
| 996 | |
| 997 | // Generate tests for all of the test-case scenarios which should yield |
| 998 | // RecordFileFull exception |
| 999 | INSTANTIATE_TEST_SUITE_P( |
| 1000 | TempTableAllocator, ThrowsRecordFileFull, |
| 1001 | ::testing::Values( |
| 1002 | // ram threshold reached, mmap threshold not reached, mmap disabled |
| 1003 | std::make_tuple(1_MiB, 2_MiB, false, 1_MiB + 1), |
| 1004 | // ram threshold reached, mmap threshold reached, mmap disabled |
| 1005 | std::make_tuple(1_MiB, 1_MiB, false, 2_MiB), |
| 1006 | // ram threshold reached, mmap threshold reached, mmap enabled |
| 1007 | std::make_tuple(1_MiB, 1_MiB, true, 2_MiB), |
| 1008 | // ram threshold reached, mmap threshold reached (but set to 0), mmap |
| 1009 | // disabled |
| 1010 | std::make_tuple(1_MiB, 0_MiB, false, 2_MiB), |
| 1011 | // ram threshold reached, mmap threshold reached (but set to 0), mmap |
| 1012 | // enabled |
| 1013 | std::make_tuple(1_MiB, 0_MiB, true, 2_MiB))); |
| 1014 | |
| 1015 | // Generate tests for all of the test-case scenarios which should result with a |
| 1016 | // successful allocation |
| 1017 | INSTANTIATE_TEST_SUITE_P( |
| 1018 | TempTableAllocator, AllocatesSuccessfully, |
| 1019 | ::testing::Values( |
| 1020 | // ram threshold not reached, mmap threshold not reached (but set to 0), |
| 1021 | // mmap disabled |
| 1022 | std::make_tuple(1_MiB, 0_MiB, false, 2_KiB, true, false), |
| 1023 | // ram threshold not reached, mmap threshold not reached (but set to 0), |
| 1024 | // mmap enabled |
| 1025 | std::make_tuple(1_MiB, 0_MiB, true, 2_KiB, true, false), |
| 1026 | // ram threshold not reached, mmap threshold not reached, mmap disabled |
| 1027 | std::make_tuple(1_MiB, 1_MiB, true, 2_KiB, true, false), |
| 1028 | // ram threshold not reached, mmap threshold not reached, mmap enabled |
| 1029 | std::make_tuple(1_MiB, 1_MiB, true, 2_KiB, true, false), |
| 1030 | // ram threshold reached, mmap threshold not reached, mmap enabled |
| 1031 | std::make_tuple(1_MiB, 4_MiB, true, 2_MiB, false, true))); |
| 1032 | |
| 1033 | // Create some aliases to make our life easier when generating the test-cases |
| 1034 | // down below. |
| 1035 | using block_size_expected = size_t; |
| 1036 | using block_size = uint32_t; |
| 1037 | using number_of_blocks = size_t; |
| 1038 | using n_bytes_requested = size_t; |
| 1039 | using ram_consumption = uint32_t; |
| 1040 | using ram_threshold = uint32_t; |
| 1041 | using mmap_threshold = uint32_t; |
| 1042 | using exception_will_be_thrown = bool; |
| 1043 | using expected_source = temptable::Source; |
| 1044 | |
| 1045 | // Parameterized test for testing Exponential_policy behavior in cases when |
| 1046 | // requested block size is smaller than the power to the two number which |
| 1047 | // will be internally picked up by the policy. |
| 1048 | class ExponentialPolicyReturnsPowerToTheTwoBlockSize |
| 1049 | : public TempTableAllocator, |
| 1050 | public ::testing::WithParamInterface<std::tuple< |
| 1051 | number_of_blocks, n_bytes_requested, block_size_expected>> {}; |
| 1052 | |
| 1053 | // Implementation of parameterized test-cases which test for correct block sizes |
| 1054 | // returned by policy. |
| 1055 | TEST_P(ExponentialPolicyReturnsPowerToTheTwoBlockSize, |
| 1056 | when_actual_block_size_is_smaller_than_that_power_to_the_two_number) { |
| 1057 | auto number_of_blocks = std::get<0>(GetParam()); |
| 1058 | auto n_bytes_requested = std::get<1>(GetParam()); |
| 1059 | auto block_size_expected = std::get<2>(GetParam()); |
| 1060 | |
| 1061 | EXPECT_EQ(block_size_expected, temptable::Exponential_policy::block_size( |
| 1062 | number_of_blocks, n_bytes_requested)); |
| 1063 | } |
| 1064 | |
| 1065 | // Generate the test-case scenarios. |
| 1066 | INSTANTIATE_TEST_SUITE_P( |
| 1067 | TempTableBlockSizePolicy1, ExponentialPolicyReturnsPowerToTheTwoBlockSize, |
| 1068 | ::testing::Values( |
| 1069 | // First and smallest block size returned is always 1 MiB |
| 1070 | // (unless requested size is larger than 1 MiB) |
| 1071 | std::make_tuple(0, 1_KiB, 1_MiB), |
| 1072 | // ... |
| 1073 | std::make_tuple(0, 5_KiB, 1_MiB), |
| 1074 | // ... |
| 1075 | std::make_tuple(0, 128_KiB, 1_MiB), |
| 1076 | // ... |
| 1077 | std::make_tuple(0, 512_KiB, 1_MiB), |
| 1078 | // ... |
| 1079 | std::make_tuple(0, 786_KiB, 1_MiB), |
| 1080 | // Block size returned will grow exponentially if we continue |
| 1081 | // increasing number of blocks (first) parameter |
| 1082 | std::make_tuple(1, 1_KiB, 2_MiB), |
| 1083 | // ... |
| 1084 | std::make_tuple(2, 1_KiB, 4_MiB), |
| 1085 | // ... |
| 1086 | std::make_tuple(3, 1_KiB, 8_MiB), |
| 1087 | // ... |
| 1088 | std::make_tuple(4, 1_KiB, 16_MiB), |
| 1089 | // ... |
| 1090 | std::make_tuple(5, 1_KiB, 32_MiB), |
| 1091 | // ... |
| 1092 | std::make_tuple(6, 1_KiB, 64_MiB), |
| 1093 | // ... |
| 1094 | std::make_tuple(7, 1_KiB, 128_MiB), |
| 1095 | // ... |
| 1096 | std::make_tuple(8, 1_KiB, 256_MiB), |
| 1097 | // Once number of blocks hits the |
| 1098 | // temptable::ALLOCATOR_MAX_BLOCK_MB_EXP threshold, block size of |
| 1099 | // temptable::ALLOCATOR_MAX_BLOCK_BYTES will be returned if |
| 1100 | // requested size is not bigger than that |
| 1101 | std::make_tuple(temptable::ALLOCATOR_MAX_BLOCK_MB_EXP, 1_MiB, |
| 1102 | temptable::ALLOCATOR_MAX_BLOCK_BYTES))); |
| 1103 | |
| 1104 | // Parameterized test for testing Exponential_policy behavior in cases when |
| 1105 | // requested block size is larger than the power to the two number which |
| 1106 | // would be otherwise internally picked up by the policy. |
| 1107 | class ExponentialPolicyReturnsExactBlockSize |
| 1108 | : public TempTableAllocator, |
| 1109 | public ::testing::WithParamInterface<std::tuple< |
| 1110 | number_of_blocks, n_bytes_requested, block_size_expected>> {}; |
| 1111 | |
| 1112 | // Implementation of parameterized test-cases which test for correct block sizes |
| 1113 | // returned by policy. |
| 1114 | TEST_P( |
| 1115 | ExponentialPolicyReturnsExactBlockSize, |
| 1116 | when_actual_block_size_is_larger_than_the_power_to_the_two_number_which_would_be_otherwise_used) { |
| 1117 | auto number_of_blocks = std::get<0>(GetParam()); |
| 1118 | auto n_bytes_requested = std::get<1>(GetParam()); |
| 1119 | auto block_size_expected = std::get<2>(GetParam()); |
| 1120 | |
| 1121 | EXPECT_EQ(block_size_expected, temptable::Exponential_policy::block_size( |
| 1122 | number_of_blocks, n_bytes_requested)); |
| 1123 | } |
| 1124 | |
| 1125 | // Generate the test-case scenarios. |
| 1126 | INSTANTIATE_TEST_SUITE_P( |
| 1127 | TempTableBlockSizePolicy2, ExponentialPolicyReturnsExactBlockSize, |
| 1128 | ::testing::Values( |
| 1129 | // If requested size is larger than 1 MiB, then returned size must match |
| 1130 | // the expected size and not the number which is power to the 2 |
| 1131 | std::make_tuple(0, 1_MiB, temptable::Block::size_hint(1_MiB)), |
| 1132 | // Same for any other combination of number of blocks input |
| 1133 | std::make_tuple(4, 32_MiB, temptable::Block::size_hint(32_MiB)), |
| 1134 | // ... |
| 1135 | std::make_tuple(6, 256_MiB, temptable::Block::size_hint(256_MiB)), |
| 1136 | // Once number of blocks hits the |
| 1137 | // temptable::ALLOCATOR_MAX_BLOCK_MB_EXP threshold, and requested |
| 1138 | // block size is larger than temptable::ALLOCATOR_MAX_BLOCK_BYTES, |
| 1139 | // big enough block size shall be returned. |
| 1140 | std::make_tuple(temptable::ALLOCATOR_MAX_BLOCK_MB_EXP, |
| 1141 | temptable::ALLOCATOR_MAX_BLOCK_BYTES, |
| 1142 | temptable::Block::size_hint( |
| 1143 | temptable::ALLOCATOR_MAX_BLOCK_BYTES)))); |
| 1144 | |
| 1145 | // Parameterized test for testing Prefer_RAM_over_MMAP_policy behavior. |
| 1146 | class PreferRamOverMmapPolicy |
| 1147 | : public TempTableAllocator, |
| 1148 | public ::testing::WithParamInterface< |
| 1149 | std::tuple<block_size, ram_consumption, ram_threshold, mmap_threshold, |
| 1150 | exception_will_be_thrown, expected_source>> {}; |
| 1151 | |
| 1152 | // Implementation of parameterized test-cases which test for correct block |
| 1153 | // source returned by policy. |
| 1154 | TEST_P( |
| 1155 | PreferRamOverMmapPolicy, |
| 1156 | selects_ram_or_mmap_when_requested_block_size_fits_otherwise_throws_an_exception) { |
| 1157 | auto block_size = std::get<0>(GetParam()); |
| 1158 | auto ram_consumption = std::get<1>(GetParam()); |
| 1159 | auto ram_threshold = std::get<2>(GetParam()); |
| 1160 | auto mmap_threshold = std::get<3>(GetParam()); |
| 1161 | auto exception_will_be_thrown = std::get<4>(GetParam()); |
| 1162 | auto source_expected = std::get<5>(GetParam()); |
| 1163 | |
| 1164 | MemoryMonitorHijackProbe::max_ram_set(ram_threshold); |
| 1165 | MemoryMonitorHijackProbe::max_mmap_set(mmap_threshold); |
| 1166 | MemoryMonitorHijackProbe::ram_consumption_set(ram_consumption); |
| 1167 | |
| 1168 | if (exception_will_be_thrown) { |
| 1169 | EXPECT_THROW_WITH_VALUE( |
| 1170 | temptable::Prefer_RAM_over_MMAP_policy::block_source(block_size), |
| 1171 | temptable::Result, temptable::Result::RECORD_FILE_FULL); |
| 1172 | } else { |
| 1173 | EXPECT_EQ(source_expected, |
| 1174 | temptable::Prefer_RAM_over_MMAP_policy::block_source(block_size)); |
| 1175 | // A block source was successfully provisioned, and the usage was recorded. |
| 1176 | // Test the accounting when the block is freed. |
| 1177 | temptable::Prefer_RAM_over_MMAP_policy::block_freed(block_size, |
| 1178 | source_expected); |
| 1179 | } |
| 1180 | |
| 1181 | // Reset the usage and check it is back to where we started. |
| 1182 | EXPECT_EQ(temptable::MemoryMonitor::RAM::consumption(), ram_consumption); |
| 1183 | MemoryMonitorHijackProbe::ram_consumption_reset(); |
| 1184 | } |
| 1185 | |
| 1186 | // Generate the test-case scenarios. |
| 1187 | INSTANTIATE_TEST_SUITE_P( |
| 1188 | TempTableBlockSourcePolicy, PreferRamOverMmapPolicy, |
| 1189 | ::testing::Values( |
| 1190 | // RAM threshold not reached, block size will fit, source is RAM |
| 1191 | std::make_tuple(1_MiB, 1_MiB, 2_MiB, 2_MiB, false, |
| 1192 | temptable::Source::RAM), |
| 1193 | // RAM threshold not reached, block size will hit the threshold (by only |
| 1194 | // 1 byte), source is MMAP |
| 1195 | std::make_tuple(1_MiB + 1, 1_MiB, 2_MiB, 2_MiB, false, |
| 1196 | temptable::Source::MMAP_FILE), |
| 1197 | // RAM threshold not reached, block size will hit the threshold (by |
| 1198 | // 1 MiB), source is MMAP |
| 1199 | std::make_tuple(2_MiB, 1_MiB, 2_MiB, 2_MiB, false, |
| 1200 | temptable::Source::MMAP_FILE), |
| 1201 | // block does not fit nor in RAM or MMAP, exception will be thrown |
| 1202 | std::make_tuple(3_MiB, 1_MiB, 2_MiB, 2_MiB, true, |
| 1203 | temptable::Source::MMAP_FILE))); |
| 1204 | |
| 1205 | } /* namespace temptable_allocator_unittest */ |