| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/s3/private/s3_default_buffer_pool.h> |
| 7 |
#include <aws/s3/private/s3_util.h> |
| 8 |
|
| 9 |
#include <aws/common/process.h> |
| 10 |
#include <aws/common/thread.h> |
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
#define NUM_TEST_ALLOCS 100 |
| 14 |
#define NUM_TEST_THREADS 8 |
| 15 |
|
| 16 |
struct pool_thread_test_data { |
| 17 |
struct aws_s3_buffer_pool *pool; |
| 18 |
uint32_t thread_idx; |
| 19 |
}; |
| 20 |
|
| 21 |
static void s_thread_test(struct aws_allocator *allocator, void (*thread_fn)(void *), struct aws_s3_buffer_pool *pool) { |
| 22 |
const struct aws_thread_options *thread_options = aws_default_thread_options(); |
| 23 |
struct aws_thread threads[NUM_TEST_THREADS]; |
| 24 |
struct pool_thread_test_data thread_data[NUM_TEST_THREADS]; |
| 25 |
AWS_ZERO_ARRAY(threads); |
| 26 |
AWS_ZERO_ARRAY(thread_data); |
| 27 |
for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) { |
| 28 |
struct aws_thread *thread = &threads[thread_idx]; |
| 29 |
aws_thread_init(thread, allocator); |
| 30 |
struct pool_thread_test_data *data = &thread_data[thread_idx]; |
| 31 |
data->pool = pool; |
| 32 |
data->thread_idx = (uint32_t)thread_idx; |
| 33 |
aws_thread_launch(thread, thread_fn, data, thread_options); |
| 34 |
} |
| 35 |
|
| 36 |
for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) { |
| 37 |
struct aws_thread *thread = &threads[thread_idx]; |
| 38 |
aws_thread_join(thread); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void s_threaded_alloc_worker(void *user_data) { |
| 43 |
struct aws_s3_buffer_pool *pool = ((struct pool_thread_test_data *)user_data)->pool; |
| 44 |
|
| 45 |
struct aws_s3_buffer_ticket *tickets[NUM_TEST_ALLOCS]; |
| 46 |
for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) { |
| 47 |
struct aws_future_s3_buffer_ticket *future = |
| 48 |
aws_s3_default_buffer_pool_reserve(pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 49 |
AWS_FATAL_ASSERT(future != NULL); |
| 50 |
AWS_FATAL_ASSERT(aws_future_s3_buffer_ticket_is_done(future)); |
| 51 |
AWS_FATAL_ASSERT(aws_future_s3_buffer_ticket_get_error(future) == AWS_OP_SUCCESS); |
| 52 |
struct aws_s3_buffer_ticket *ticket = aws_future_s3_buffer_ticket_get_result_by_move(future); |
| 53 |
|
| 54 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(ticket); |
| 55 |
AWS_FATAL_ASSERT(buf.buffer); |
| 56 |
memset(buf.buffer, 0, buf.capacity); |
| 57 |
tickets[count] = ticket; |
| 58 |
aws_future_s3_buffer_ticket_release(future); |
| 59 |
} |
| 60 |
|
| 61 |
for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) { |
| 62 |
aws_s3_buffer_ticket_release(tickets[count]); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static int s_test_s3_buffer_pool_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) { |
| 67 |
(void)allocator; |
| 68 |
(void)ctx; |
| 69 |
|
| 70 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 71 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(2)}); |
| 72 |
|
| 73 |
s_thread_test(allocator, s_threaded_alloc_worker, buffer_pool); |
| 74 |
|
| 75 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
AWS_TEST_CASE(test_s3_buffer_pool_threaded_allocs_and_frees, s_test_s3_buffer_pool_threaded_allocs_and_frees) |
| 80 |
|
| 81 |
static int s_test_s3_buffer_pool_large_chunk_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) { |
| 82 |
(void)allocator; |
| 83 |
(void)ctx; |
| 84 |
|
| 85 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 86 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(65), .memory_limit = GB_TO_BYTES(2)}); |
| 87 |
|
| 88 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 89 |
ASSERT_INT_EQUALS(0, stats.primary_cutoff); |
| 90 |
|
| 91 |
s_thread_test(allocator, s_threaded_alloc_worker, buffer_pool); |
| 92 |
|
| 93 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
AWS_TEST_CASE( |
| 98 |
test_s3_buffer_pool_large_chunk_threaded_allocs_and_frees, |
| 99 |
s_test_s3_buffer_pool_large_chunk_threaded_allocs_and_frees) |
| 100 |
|
| 101 |
static int s_test_s3_buffer_pool_limits(struct aws_allocator *allocator, void *ctx) { |
| 102 |
(void)allocator; |
| 103 |
(void)ctx; |
| 104 |
|
| 105 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 106 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 107 |
|
| 108 |
struct aws_s3_buffer_pool_reserve_meta meta = {.size = MB_TO_BYTES(64)}; |
| 109 |
struct aws_future_s3_buffer_ticket *future1 = aws_s3_default_buffer_pool_reserve(buffer_pool, meta); |
| 110 |
ASSERT_NOT_NULL(future1); |
| 111 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(future1)); |
| 112 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(future1), AWS_OP_SUCCESS); |
| 113 |
struct aws_s3_buffer_ticket *ticket1 = aws_future_s3_buffer_ticket_get_result_by_move(future1); |
| 114 |
ASSERT_NOT_NULL(ticket1); |
| 115 |
struct aws_byte_buf buf1 = aws_s3_buffer_ticket_claim(ticket1); |
| 116 |
ASSERT_NOT_NULL(buf1.buffer); |
| 117 |
|
| 118 |
struct aws_s3_buffer_ticket *tickets[6]; |
| 119 |
struct aws_future_s3_buffer_ticket *ticket_futures[6]; |
| 120 |
for (size_t i = 0; i < 6; ++i) { |
| 121 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 122 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(128)}); |
| 123 |
ASSERT_NOT_NULL(ticket_futures[i]); |
| 124 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 125 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 126 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 127 |
ASSERT_NOT_NULL(tickets[i]); |
| 128 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 129 |
ASSERT_NOT_NULL(buf.buffer); |
| 130 |
} |
| 131 |
|
| 132 |
struct aws_future_s3_buffer_ticket *overallocated_future = aws_s3_default_buffer_pool_reserve( |
| 133 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(128)}); |
| 134 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(overallocated_future)); |
| 135 |
|
| 136 |
struct aws_future_s3_buffer_ticket *overallocated_future2 = aws_s3_default_buffer_pool_reserve( |
| 137 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(96)}); |
| 138 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(overallocated_future2)); |
| 139 |
|
| 140 |
struct aws_future_s3_buffer_ticket *future2 = aws_s3_default_buffer_pool_reserve( |
| 141 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(32)}); |
| 142 |
|
| 143 |
ASSERT_NOT_NULL(future2); |
| 144 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(future2)); |
| 145 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(future2), AWS_OP_SUCCESS); |
| 146 |
struct aws_s3_buffer_ticket *ticket2 = aws_future_s3_buffer_ticket_get_result_by_move(future2); |
| 147 |
ASSERT_NOT_NULL(ticket2); |
| 148 |
struct aws_byte_buf buf2 = aws_s3_buffer_ticket_claim(ticket2); |
| 149 |
ASSERT_NOT_NULL(buf2.buffer); |
| 150 |
|
| 151 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 152 |
ASSERT_UINT_EQUALS(0, stats.primary_allocated); |
| 153 |
|
| 154 |
for (size_t i = 0; i < 6; ++i) { |
| 155 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 156 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 157 |
} |
| 158 |
|
| 159 |
aws_s3_buffer_ticket_release(ticket1); |
| 160 |
aws_future_s3_buffer_ticket_release(future1); |
| 161 |
aws_s3_buffer_ticket_release(ticket2); |
| 162 |
aws_future_s3_buffer_ticket_release(future2); |
| 163 |
|
| 164 |
aws_future_s3_buffer_ticket_release(overallocated_future); |
| 165 |
aws_future_s3_buffer_ticket_release(overallocated_future2); |
| 166 |
|
| 167 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 168 |
|
| 169 |
return 0; |
| 170 |
} |
| 171 |
AWS_TEST_CASE(test_s3_buffer_pool_limits, s_test_s3_buffer_pool_limits) |
| 172 |
|
| 173 |
static int s_test_s3_buffer_pool_trim(struct aws_allocator *allocator, void *ctx) { |
| 174 |
(void)allocator; |
| 175 |
(void)ctx; |
| 176 |
|
| 177 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 178 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 179 |
|
| 180 |
struct aws_s3_buffer_ticket *tickets[40]; |
| 181 |
struct aws_future_s3_buffer_ticket *ticket_futures[40]; |
| 182 |
for (size_t i = 0; i < 40; ++i) { |
| 183 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 184 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 185 |
ASSERT_NOT_NULL(ticket_futures[i]); |
| 186 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 187 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 188 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 189 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 190 |
ASSERT_NOT_NULL(buf.buffer); |
| 191 |
} |
| 192 |
|
| 193 |
struct aws_s3_default_buffer_pool_usage_stats stats_before = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 194 |
|
| 195 |
for (size_t i = 0; i < 20; ++i) { |
| 196 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 197 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 198 |
} |
| 199 |
|
| 200 |
aws_s3_default_buffer_pool_trim(buffer_pool); |
| 201 |
|
| 202 |
struct aws_s3_default_buffer_pool_usage_stats stats_after = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 203 |
|
| 204 |
ASSERT_TRUE(stats_before.primary_num_blocks > stats_after.primary_num_blocks); |
| 205 |
ASSERT_TRUE(stats_before.primary_allocated > stats_after.primary_allocated); |
| 206 |
|
| 207 |
for (size_t i = 20; i < 40; ++i) { |
| 208 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 209 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 210 |
} |
| 211 |
|
| 212 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 213 |
|
| 214 |
return 0; |
| 215 |
}; |
| 216 |
AWS_TEST_CASE(test_s3_buffer_pool_trim, s_test_s3_buffer_pool_trim) |
| 217 |
|
| 218 |
struct s_reserve_state { |
| 219 |
struct aws_future_s3_buffer_ticket *future; |
| 220 |
struct aws_s3_buffer_ticket *ticket; |
| 221 |
}; |
| 222 |
|
| 223 |
static void s_on_pool_buffer_reserved(void *user_data) { |
| 224 |
struct s_reserve_state *state = user_data; |
| 225 |
|
| 226 |
if (aws_future_s3_buffer_ticket_get_error(state->future) == AWS_OP_SUCCESS) { |
| 227 |
state->ticket = aws_future_s3_buffer_ticket_get_result_by_move(state->future); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
static int s_test_s3_buffer_pool_reserve_over_limit(struct aws_allocator *allocator, void *ctx) { |
| 232 |
(void)allocator; |
| 233 |
(void)ctx; |
| 234 |
|
| 235 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 236 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 237 |
|
| 238 |
struct aws_s3_buffer_ticket *tickets[112]; |
| 239 |
struct aws_future_s3_buffer_ticket *ticket_futures[112]; |
| 240 |
for (size_t i = 0; i < 112; ++i) { |
| 241 |
AWS_LOGF_DEBUG(0, "reserving buffer %zu", i + 1); |
| 242 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 243 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 244 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 245 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 246 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 247 |
ASSERT_INT_EQUALS( |
| 248 |
aws_s3_default_buffer_pool_get_ticket_reserved_from(tickets[i]->impl), |
| 249 |
AWS_S3_BUFFER_POOL_RESERVED_FROM_PRIMARY); |
| 250 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 251 |
ASSERT_NOT_NULL(buf.buffer); |
| 252 |
} |
| 253 |
|
| 254 |
struct aws_future_s3_buffer_ticket *over_future = aws_s3_default_buffer_pool_reserve( |
| 255 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 256 |
|
| 257 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(over_future)); |
| 258 |
|
| 259 |
struct s_reserve_state state = {.future = over_future}; |
| 260 |
|
| 261 |
aws_future_s3_buffer_ticket_register_callback(over_future, s_on_pool_buffer_reserved, &state); |
| 262 |
|
| 263 |
for (size_t i = 0; i < 112; ++i) { |
| 264 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 265 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 266 |
} |
| 267 |
|
| 268 |
ASSERT_NOT_NULL(state.ticket); |
| 269 |
ASSERT_INT_EQUALS( |
| 270 |
aws_s3_default_buffer_pool_get_ticket_reserved_from(state.ticket->impl), |
| 271 |
AWS_S3_BUFFER_POOL_RESERVED_FROM_PRIMARY); |
| 272 |
|
| 273 |
aws_s3_buffer_ticket_release(state.ticket); |
| 274 |
aws_future_s3_buffer_ticket_release(over_future); |
| 275 |
|
| 276 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 277 |
|
| 278 |
return 0; |
| 279 |
}; |
| 280 |
AWS_TEST_CASE(test_s3_buffer_pool_reserve_over_limit, s_test_s3_buffer_pool_reserve_over_limit) |
| 281 |
|
| 282 |
|
| 283 |
static int s_test_s3_buffer_pool_reserve_over_limit_multi(struct aws_allocator *allocator, void *ctx) { |
| 284 |
(void)allocator; |
| 285 |
(void)ctx; |
| 286 |
|
| 287 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 288 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 289 |
|
| 290 |
struct aws_s3_buffer_ticket *tickets[109]; |
| 291 |
struct aws_future_s3_buffer_ticket *ticket_futures[109]; |
| 292 |
|
| 293 |
ticket_futures[0] = aws_s3_default_buffer_pool_reserve( |
| 294 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(32)}); |
| 295 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[0])); |
| 296 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[0]), AWS_OP_SUCCESS); |
| 297 |
tickets[0] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[0]); |
| 298 |
struct aws_byte_buf buf0 = aws_s3_buffer_ticket_claim(tickets[0]); |
| 299 |
ASSERT_NOT_NULL(buf0.buffer); |
| 300 |
|
| 301 |
for (size_t i = 1; i < 109; ++i) { |
| 302 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 303 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 304 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 305 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 306 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 307 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 308 |
ASSERT_NOT_NULL(buf.buffer); |
| 309 |
} |
| 310 |
|
| 311 |
struct aws_future_s3_buffer_ticket *over_future1 = aws_s3_default_buffer_pool_reserve( |
| 312 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 313 |
|
| 314 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(over_future1)); |
| 315 |
struct s_reserve_state state1 = {.future = over_future1}; |
| 316 |
aws_future_s3_buffer_ticket_register_callback(over_future1, s_on_pool_buffer_reserved, &state1); |
| 317 |
|
| 318 |
struct aws_future_s3_buffer_ticket *over_future2 = aws_s3_default_buffer_pool_reserve( |
| 319 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 320 |
|
| 321 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(over_future2)); |
| 322 |
struct s_reserve_state state2 = {.future = over_future2}; |
| 323 |
aws_future_s3_buffer_ticket_register_callback(over_future2, s_on_pool_buffer_reserved, &state2); |
| 324 |
|
| 325 |
|
| 326 |
aws_s3_buffer_ticket_release(tickets[0]); |
| 327 |
aws_future_s3_buffer_ticket_release(ticket_futures[0]); |
| 328 |
|
| 329 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(over_future1)); |
| 330 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(over_future2)); |
| 331 |
ASSERT_NOT_NULL(state1.ticket); |
| 332 |
ASSERT_NOT_NULL(state2.ticket); |
| 333 |
|
| 334 |
for (size_t i = 1; i < 109; ++i) { |
| 335 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 336 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 337 |
} |
| 338 |
|
| 339 |
aws_s3_buffer_ticket_release(state1.ticket); |
| 340 |
aws_s3_buffer_ticket_release(state2.ticket); |
| 341 |
aws_future_s3_buffer_ticket_release(over_future1); |
| 342 |
aws_future_s3_buffer_ticket_release(over_future2); |
| 343 |
|
| 344 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 345 |
|
| 346 |
return 0; |
| 347 |
}; |
| 348 |
AWS_TEST_CASE(test_s3_buffer_pool_reserve_over_limit_multi, s_test_s3_buffer_pool_reserve_over_limit_multi) |
| 349 |
|
| 350 |
static void s_on_pool_buffer_reserved_instant_release(void *user_data) { |
| 351 |
struct s_reserve_state *state = user_data; |
| 352 |
|
| 353 |
if (aws_future_s3_buffer_ticket_get_error(state->future) == AWS_OP_SUCCESS) { |
| 354 |
state->ticket = aws_future_s3_buffer_ticket_get_result_by_move(state->future); |
| 355 |
} |
| 356 |
|
| 357 |
state->future = aws_future_s3_buffer_ticket_release(state->future); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
static int s_test_s3_buffer_pool_reserve_over_limit_instant_release(struct aws_allocator *allocator, void *ctx) { |
| 362 |
(void)allocator; |
| 363 |
(void)ctx; |
| 364 |
|
| 365 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 366 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 367 |
|
| 368 |
struct aws_s3_buffer_ticket *tickets[112]; |
| 369 |
struct aws_future_s3_buffer_ticket *ticket_futures[112]; |
| 370 |
for (size_t i = 0; i < 112; ++i) { |
| 371 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 372 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 373 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 374 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 375 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 376 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 377 |
ASSERT_NOT_NULL(buf.buffer); |
| 378 |
} |
| 379 |
|
| 380 |
struct aws_future_s3_buffer_ticket *over_future = aws_s3_default_buffer_pool_reserve( |
| 381 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 382 |
|
| 383 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(over_future)); |
| 384 |
|
| 385 |
struct s_reserve_state state = {.future = over_future}; |
| 386 |
|
| 387 |
aws_future_s3_buffer_ticket_register_callback(over_future, s_on_pool_buffer_reserved_instant_release, &state); |
| 388 |
|
| 389 |
for (size_t i = 0; i < 112; ++i) { |
| 390 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 391 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 392 |
} |
| 393 |
|
| 394 |
ASSERT_NOT_NULL(state.ticket); |
| 395 |
|
| 396 |
aws_s3_buffer_ticket_release(state.ticket); |
| 397 |
|
| 398 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 399 |
|
| 400 |
return 0; |
| 401 |
}; |
| 402 |
AWS_TEST_CASE( |
| 403 |
test_s3_buffer_pool_reserve_over_limit_instant_release, |
| 404 |
s_test_s3_buffer_pool_reserve_over_limit_instant_release) |
| 405 |
|
| 406 |
static void s_on_pool_buffer_reserved_cancel(void *user_data) { |
| 407 |
struct s_reserve_state *state = user_data; |
| 408 |
|
| 409 |
if (aws_future_s3_buffer_ticket_get_error(state->future) == AWS_OP_SUCCESS) { |
| 410 |
state->ticket = aws_future_s3_buffer_ticket_get_result_by_move(state->future); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
static int s_test_s3_buffer_pool_reserve_over_limit_cancel(struct aws_allocator *allocator, void *ctx) { |
| 416 |
(void)allocator; |
| 417 |
(void)ctx; |
| 418 |
|
| 419 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 420 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = GB_TO_BYTES(1)}); |
| 421 |
|
| 422 |
struct aws_s3_buffer_ticket *tickets[112]; |
| 423 |
struct aws_future_s3_buffer_ticket *ticket_futures[112]; |
| 424 |
for (size_t i = 0; i < 112; ++i) { |
| 425 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 426 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 427 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 428 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 429 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 430 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 431 |
ASSERT_NOT_NULL(buf.buffer); |
| 432 |
} |
| 433 |
|
| 434 |
struct aws_future_s3_buffer_ticket *over_future = aws_s3_default_buffer_pool_reserve( |
| 435 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 436 |
|
| 437 |
ASSERT_FALSE(aws_future_s3_buffer_ticket_is_done(over_future)); |
| 438 |
|
| 439 |
struct s_reserve_state state = {.future = over_future}; |
| 440 |
|
| 441 |
aws_future_s3_buffer_ticket_register_callback(over_future, s_on_pool_buffer_reserved_cancel, &state); |
| 442 |
|
| 443 |
|
| 444 |
aws_future_s3_buffer_ticket_set_error(over_future, AWS_ERROR_S3_CANCELED); |
| 445 |
|
| 446 |
for (size_t i = 0; i < 112; ++i) { |
| 447 |
|
| 448 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 449 |
|
| 450 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
ASSERT_NULL(state.ticket); |
| 455 |
|
| 456 |
aws_s3_buffer_ticket_release(state.ticket); |
| 457 |
|
| 458 |
aws_future_s3_buffer_ticket_release(over_future); |
| 459 |
|
| 460 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 461 |
|
| 462 |
return 0; |
| 463 |
}; |
| 464 |
AWS_TEST_CASE(test_s3_buffer_pool_reserve_over_limit_cancel, s_test_s3_buffer_pool_reserve_over_limit_cancel) |
| 465 |
|
| 466 |
static int s_test_s3_buffer_pool_too_small(struct aws_allocator *allocator, void *ctx) { |
| 467 |
(void)allocator; |
| 468 |
(void)ctx; |
| 469 |
|
| 470 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 471 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = MB_TO_BYTES(8), .memory_limit = MB_TO_BYTES(512)}); |
| 472 |
ASSERT_NULL(buffer_pool); |
| 473 |
ASSERT_INT_EQUALS(AWS_ERROR_S3_INVALID_MEMORY_LIMIT_CONFIG, aws_last_error()); |
| 474 |
|
| 475 |
return 0; |
| 476 |
}; |
| 477 |
AWS_TEST_CASE(test_s3_buffer_pool_too_small, s_test_s3_buffer_pool_too_small) |
| 478 |
|
| 479 |
|
| 480 |
static int s_test_s3_buffer_pool_forced_buffer(struct aws_allocator *allocator, void *ctx) { |
| 481 |
(void)ctx; |
| 482 |
const size_t chunk_size = MB_TO_BYTES(8); |
| 483 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 484 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = chunk_size, .memory_limit = GB_TO_BYTES(1)}); |
| 485 |
|
| 486 |
{ |
| 487 |
size_t acquire_size = chunk_size; |
| 488 |
struct aws_s3_buffer_ticket *forced_ticket = NULL; |
| 489 |
struct aws_future_s3_buffer_ticket *forced_future = aws_s3_default_buffer_pool_reserve( |
| 490 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = acquire_size, .can_block = true}); |
| 491 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(forced_future)); |
| 492 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(forced_future), AWS_OP_SUCCESS); |
| 493 |
forced_ticket = aws_future_s3_buffer_ticket_get_result_by_move(forced_future); |
| 494 |
struct aws_byte_buf forced_buf = aws_s3_buffer_ticket_claim(forced_ticket); |
| 495 |
ASSERT_NOT_NULL(forced_buf.buffer); |
| 496 |
ASSERT_UINT_EQUALS(acquire_size, forced_buf.capacity); |
| 497 |
ASSERT_UINT_EQUALS(0, forced_buf.len); |
| 498 |
|
| 499 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 500 |
ASSERT_UINT_EQUALS(acquire_size, stats.forced_used); |
| 501 |
ASSERT_UINT_EQUALS(acquire_size, stats.primary_used); |
| 502 |
ASSERT_UINT_EQUALS(0, stats.primary_reserved); |
| 503 |
aws_s3_buffer_ticket_release(forced_ticket); |
| 504 |
aws_future_s3_buffer_ticket_release(forced_future); |
| 505 |
} |
| 506 |
|
| 507 |
{ |
| 508 |
size_t acquire_size = aws_s3_default_buffer_pool_get_usage(buffer_pool).primary_cutoff + 1; |
| 509 |
struct aws_s3_buffer_ticket *forced_ticket = NULL; |
| 510 |
struct aws_future_s3_buffer_ticket *forced_future = aws_s3_default_buffer_pool_reserve( |
| 511 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = acquire_size, .can_block = true}); |
| 512 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(forced_future)); |
| 513 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(forced_future), AWS_OP_SUCCESS); |
| 514 |
forced_ticket = aws_future_s3_buffer_ticket_get_result_by_move(forced_future); |
| 515 |
struct aws_byte_buf forced_buf = aws_s3_buffer_ticket_claim(forced_ticket); |
| 516 |
ASSERT_NOT_NULL(forced_buf.buffer); |
| 517 |
ASSERT_UINT_EQUALS(acquire_size, forced_buf.capacity); |
| 518 |
ASSERT_UINT_EQUALS(0, forced_buf.len); |
| 519 |
|
| 520 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 521 |
ASSERT_UINT_EQUALS(acquire_size, stats.forced_used); |
| 522 |
ASSERT_UINT_EQUALS(acquire_size, stats.secondary_used); |
| 523 |
ASSERT_UINT_EQUALS(0, stats.secondary_reserved); |
| 524 |
aws_s3_buffer_ticket_release(forced_ticket); |
| 525 |
aws_future_s3_buffer_ticket_release(forced_future); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 530 |
ASSERT_UINT_EQUALS(0, stats.forced_used); |
| 531 |
ASSERT_UINT_EQUALS(0, stats.primary_used); |
| 532 |
ASSERT_UINT_EQUALS(0, stats.secondary_used); |
| 533 |
|
| 534 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 535 |
return 0; |
| 536 |
} |
| 537 |
AWS_TEST_CASE(test_s3_buffer_pool_forced_buffer, s_test_s3_buffer_pool_forced_buffer) |
| 538 |
|
| 539 |
|
| 540 |
static int s_test_s3_buffer_pool_forced_buffer_after_limit_hit(struct aws_allocator *allocator, void *ctx) { |
| 541 |
(void)ctx; |
| 542 |
const size_t chunk_size = MB_TO_BYTES(8); |
| 543 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 544 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = chunk_size, .memory_limit = GB_TO_BYTES(1)}); |
| 545 |
|
| 546 |
|
| 547 |
struct aws_s3_buffer_ticket *tickets[112]; |
| 548 |
struct aws_future_s3_buffer_ticket *ticket_futures[112]; |
| 549 |
for (size_t i = 0; i < 112; ++i) { |
| 550 |
ticket_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 551 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8)}); |
| 552 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(ticket_futures[i])); |
| 553 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(ticket_futures[i]), AWS_OP_SUCCESS); |
| 554 |
tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(ticket_futures[i]); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
struct aws_s3_buffer_ticket *forced_ticket_1 = NULL; |
| 559 |
struct aws_future_s3_buffer_ticket *forced_future_1 = aws_s3_default_buffer_pool_reserve( |
| 560 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8), .can_block = true}); |
| 561 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(forced_future_1)); |
| 562 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(forced_future_1), AWS_OP_SUCCESS); |
| 563 |
forced_ticket_1 = aws_future_s3_buffer_ticket_get_result_by_move(forced_future_1); |
| 564 |
ASSERT_NOT_NULL(forced_ticket_1); |
| 565 |
struct aws_byte_buf forced_buf_1 = aws_s3_buffer_ticket_claim(forced_ticket_1); |
| 566 |
ASSERT_UINT_EQUALS(chunk_size, forced_buf_1.capacity); |
| 567 |
|
| 568 |
|
| 569 |
for (size_t i = 0; i < 112; ++i) { |
| 570 |
struct aws_byte_buf normal_buf = aws_s3_buffer_ticket_claim(tickets[i]); |
| 571 |
ASSERT_UINT_EQUALS(chunk_size, normal_buf.capacity); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
struct aws_s3_buffer_ticket *forced_ticket_2 = NULL; |
| 576 |
struct aws_future_s3_buffer_ticket *forced_future_2 = aws_s3_default_buffer_pool_reserve( |
| 577 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = MB_TO_BYTES(8), .can_block = true}); |
| 578 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(forced_future_2)); |
| 579 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(forced_future_2), AWS_OP_SUCCESS); |
| 580 |
forced_ticket_2 = aws_future_s3_buffer_ticket_get_result_by_move(forced_future_2); |
| 581 |
ASSERT_NOT_NULL(forced_ticket_2); |
| 582 |
struct aws_byte_buf forced_buf_2 = aws_s3_buffer_ticket_claim(forced_ticket_2); |
| 583 |
ASSERT_UINT_EQUALS(chunk_size, forced_buf_2.capacity); |
| 584 |
|
| 585 |
|
| 586 |
for (size_t i = 0; i < 112; ++i) { |
| 587 |
aws_s3_buffer_ticket_release(tickets[i]); |
| 588 |
aws_future_s3_buffer_ticket_release(ticket_futures[i]); |
| 589 |
} |
| 590 |
|
| 591 |
aws_s3_buffer_ticket_release(forced_ticket_1); |
| 592 |
aws_future_s3_buffer_ticket_release(forced_future_1); |
| 593 |
aws_s3_buffer_ticket_release(forced_ticket_2); |
| 594 |
aws_future_s3_buffer_ticket_release(forced_future_2); |
| 595 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 596 |
return 0; |
| 597 |
} |
| 598 |
AWS_TEST_CASE(test_s3_buffer_pool_forced_buffer_after_limit_hit, s_test_s3_buffer_pool_forced_buffer_after_limit_hit) |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
static int s_test_s3_buffer_pool_forced_buffer_wont_stop_reservations(struct aws_allocator *allocator, void *ctx) { |
| 603 |
(void)ctx; |
| 604 |
const size_t chunk_size = MB_TO_BYTES(8); |
| 605 |
const size_t mem_limit = GB_TO_BYTES(1); |
| 606 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 607 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = chunk_size, .memory_limit = mem_limit}); |
| 608 |
|
| 609 |
|
| 610 |
void *try_large_alloc = malloc(mem_limit); |
| 611 |
if (try_large_alloc == NULL) { |
| 612 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 613 |
return AWS_OP_SKIP; |
| 614 |
} |
| 615 |
free(try_large_alloc); |
| 616 |
|
| 617 |
|
| 618 |
struct aws_s3_buffer_ticket *forced_ticket = NULL; |
| 619 |
struct aws_future_s3_buffer_ticket *forced_future = aws_s3_default_buffer_pool_reserve( |
| 620 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = chunk_size, .can_block = true}); |
| 621 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(forced_future)); |
| 622 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(forced_future), AWS_OP_SUCCESS); |
| 623 |
forced_ticket = aws_future_s3_buffer_ticket_get_result_by_move(forced_future); |
| 624 |
struct aws_byte_buf forced_buf = aws_s3_buffer_ticket_claim(forced_ticket); |
| 625 |
ASSERT_NOT_NULL(forced_ticket); |
| 626 |
ASSERT_UINT_EQUALS(chunk_size, forced_buf.capacity); |
| 627 |
|
| 628 |
|
| 629 |
struct aws_s3_buffer_ticket *normal_ticket = NULL; |
| 630 |
struct aws_future_s3_buffer_ticket *normal_future = |
| 631 |
aws_s3_default_buffer_pool_reserve(buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = chunk_size}); |
| 632 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(normal_future)); |
| 633 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(normal_future), AWS_OP_SUCCESS); |
| 634 |
normal_ticket = aws_future_s3_buffer_ticket_get_result_by_move(normal_future); |
| 635 |
struct aws_byte_buf normal_buf = aws_s3_buffer_ticket_claim(normal_ticket); |
| 636 |
ASSERT_UINT_EQUALS(chunk_size, normal_buf.capacity); |
| 637 |
aws_s3_buffer_ticket_release(normal_ticket); |
| 638 |
aws_future_s3_buffer_ticket_release(normal_future); |
| 639 |
|
| 640 |
|
| 641 |
aws_s3_buffer_ticket_release(forced_ticket); |
| 642 |
aws_future_s3_buffer_ticket_release(forced_future); |
| 643 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 644 |
return 0; |
| 645 |
} |
| 646 |
AWS_TEST_CASE( |
| 647 |
test_s3_buffer_pool_forced_buffer_wont_stop_reservations, |
| 648 |
s_test_s3_buffer_pool_forced_buffer_wont_stop_reservations) |
| 649 |
|
| 650 |
|
| 651 |
static int s_test_s3_buffer_pool_reserve_tiny_chunks(struct aws_allocator *allocator, void *ctx) { |
| 652 |
(void)ctx; |
| 653 |
const size_t chunk_size = MB_TO_BYTES(8); |
| 654 |
const size_t small_size = KB_TO_BYTES(128); |
| 655 |
const size_t mem_limit = GB_TO_BYTES(1); |
| 656 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 657 |
allocator, (struct aws_s3_buffer_pool_config){.part_size = chunk_size, .memory_limit = mem_limit}); |
| 658 |
|
| 659 |
struct aws_s3_buffer_ticket *ticket = NULL; |
| 660 |
struct aws_future_s3_buffer_ticket *future = |
| 661 |
aws_s3_default_buffer_pool_reserve(buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = small_size}); |
| 662 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(future)); |
| 663 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(future), AWS_OP_SUCCESS); |
| 664 |
ticket = aws_future_s3_buffer_ticket_get_result_by_move(future); |
| 665 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(ticket); |
| 666 |
ASSERT_UINT_EQUALS(small_size, buf.capacity); |
| 667 |
|
| 668 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 669 |
ASSERT_INT_EQUALS(small_size, stats.secondary_used); |
| 670 |
|
| 671 |
aws_s3_buffer_ticket_release(ticket); |
| 672 |
aws_future_s3_buffer_ticket_release(future); |
| 673 |
|
| 674 |
stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 675 |
ASSERT_INT_EQUALS(0, stats.secondary_used); |
| 676 |
|
| 677 |
|
| 678 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 679 |
return 0; |
| 680 |
} |
| 681 |
AWS_TEST_CASE(test_s3_buffer_pool_reserve_tiny_chunks, s_test_s3_buffer_pool_reserve_tiny_chunks) |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
static int s_test_s3_buffer_pool_trim_reserved_but_unallocated(struct aws_allocator *allocator, void *ctx) { |
| 692 |
(void)ctx; |
| 693 |
const size_t chunk_size = MB_TO_BYTES(8); |
| 694 |
const size_t mem_limit = GB_TO_BYTES(1); |
| 695 |
struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new( |
| 696 |
allocator, |
| 697 |
(struct aws_s3_buffer_pool_config){ |
| 698 |
.part_size = chunk_size, |
| 699 |
.memory_limit = mem_limit, |
| 700 |
}); |
| 701 |
ASSERT_NOT_NULL(buffer_pool); |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
const size_t num_reserved = 50; |
| 706 |
struct aws_future_s3_buffer_ticket *reserved_futures[50]; |
| 707 |
struct aws_s3_buffer_ticket *reserved_tickets[50]; |
| 708 |
for (size_t i = 0; i < num_reserved; ++i) { |
| 709 |
reserved_futures[i] = aws_s3_default_buffer_pool_reserve( |
| 710 |
buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = chunk_size}); |
| 711 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(reserved_futures[i])); |
| 712 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(reserved_futures[i]), AWS_OP_SUCCESS); |
| 713 |
reserved_tickets[i] = aws_future_s3_buffer_ticket_get_result_by_move(reserved_futures[i]); |
| 714 |
ASSERT_NOT_NULL(reserved_tickets[i]); |
| 715 |
|
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
struct aws_s3_default_buffer_pool_usage_stats stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 720 |
ASSERT_TRUE(stats.primary_reserved > 0); |
| 721 |
ASSERT_UINT_EQUALS(0, stats.primary_allocated); |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
struct aws_future_s3_buffer_ticket *secondary_future = aws_s3_default_buffer_pool_reserve( |
| 727 |
buffer_pool, |
| 728 |
(struct aws_s3_buffer_pool_reserve_meta){ |
| 729 |
.size = MB_TO_BYTES(800), |
| 730 |
}); |
| 731 |
ASSERT_NOT_NULL(secondary_future); |
| 732 |
|
| 733 |
for (size_t i = 0; i < num_reserved; ++i) { |
| 734 |
aws_s3_buffer_ticket_release(reserved_tickets[i]); |
| 735 |
aws_future_s3_buffer_ticket_release(reserved_futures[i]); |
| 736 |
} |
| 737 |
ASSERT_TRUE(aws_future_s3_buffer_ticket_is_done(secondary_future)); |
| 738 |
ASSERT_INT_EQUALS(aws_future_s3_buffer_ticket_get_error(secondary_future), AWS_OP_SUCCESS); |
| 739 |
struct aws_s3_buffer_ticket *secondary_ticket = aws_future_s3_buffer_ticket_get_result_by_move(secondary_future); |
| 740 |
ASSERT_NOT_NULL(secondary_ticket); |
| 741 |
struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(secondary_ticket); |
| 742 |
ASSERT_NOT_NULL(buf.buffer); |
| 743 |
|
| 744 |
|
| 745 |
aws_s3_buffer_ticket_release(secondary_ticket); |
| 746 |
aws_future_s3_buffer_ticket_release(secondary_future); |
| 747 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 748 |
return 0; |
| 749 |
} |
| 750 |
AWS_TEST_CASE(test_s3_buffer_pool_trim_reserved_but_unallocated, s_test_s3_buffer_pool_trim_reserved_but_unallocated) |