1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/common.h>
#include <aws/common/array_list.h>
#include <aws/common/assert.h>
#include <aws/common/thread.h>
#include <aws/testing/aws_test_harness.h>
#ifdef __MACH__
# include <CoreFoundation/CoreFoundation.h>
#endif
static void *s_test_alloc_acquire(struct aws_allocator *allocator, size_t size) {
(void)allocator;
return (size > 0) ? malloc(size) : NULL;
}
static void s_test_alloc_release(struct aws_allocator *allocator, void *ptr) {
(void)allocator;
free(ptr);
}
static void *s_test_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
(void)allocator;
(void)oldsize;
/* Realloc should ensure that newsize is never 0 */
AWS_FATAL_ASSERT(newsize != 0);
return realloc(ptr, newsize);
}
static void *s_test_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
(void)allocator;
return (num > 0 && size > 0) ? calloc(num, size) : NULL;
}
/**
* Check that we correctly protect against
* https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations
* For now, can only test the realloc case, because it returns NULL on error
* Test the remaining cases once https://github.com/awslabs/aws-c-common/issues/471 is solved
*/
AWS_TEST_CASE(test_alloc_nothing, s_test_alloc_nothing_fn)
static int s_test_alloc_nothing_fn(struct aws_allocator *allocator, void *ctx) {
(void)allocator;
(void)ctx;
struct aws_allocator test_allocator = {.mem_acquire = s_test_alloc_acquire,
.mem_release = s_test_alloc_release,
.mem_realloc = s_test_realloc,
.mem_calloc = s_test_calloc};
/* realloc should handle the case correctly, return null, and free the memory */
void *p = aws_mem_acquire(&test_allocator, 12);
ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &p, 12, 0));
ASSERT_NULL(p);
return 0;
}
/*
* Small Block Allocator tests
*/
static int s_sba_alloc_free_once(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
void *mem = aws_mem_acquire(sba, 42);
ASSERT_NOT_NULL(mem);
const size_t allocated = aws_mem_tracer_bytes(allocator);
ASSERT_TRUE(allocated > 0);
aws_mem_release(sba, mem);
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_alloc_free_once, s_sba_alloc_free_once)
#define NUM_TEST_ALLOCS 10000
#define NUM_TEST_THREADS 8
static int s_sba_random_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
srand(42);
void *allocs[NUM_TEST_ALLOCS];
for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
size_t size = aws_max_size(rand() % 512, 1);
void *alloc = aws_mem_acquire(sba, size);
ASSERT_NOT_NULL(alloc);
allocs[count] = alloc;
}
for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
void *alloc = allocs[count];
aws_mem_release(sba, alloc);
}
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_random_allocs_and_frees, s_sba_random_allocs_and_frees)
static int s_sba_random_reallocs(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
srand(128);
void *alloc = NULL;
size_t size = 0;
for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
size_t old_size = size;
size = rand() % 4096;
ASSERT_SUCCESS(aws_mem_realloc(sba, &alloc, old_size, size));
}
ASSERT_SUCCESS(aws_mem_realloc(sba, &alloc, size, 0));
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_random_reallocs, s_sba_random_reallocs)
struct sba_thread_test_data {
struct aws_allocator *sba;
uint32_t thread_idx;
};
static void s_sba_threaded_alloc_worker(void *user_data) {
struct aws_allocator *sba = ((struct sba_thread_test_data *)user_data)->sba;
void *allocs[NUM_TEST_ALLOCS];
for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
size_t size = aws_max_size(rand() % 512, 1);
void *alloc = aws_mem_acquire(sba, size);
AWS_FATAL_ASSERT(alloc);
allocs[count] = alloc;
}
for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
void *alloc = allocs[count];
aws_mem_release(sba, alloc);
}
}
static void s_sba_thread_test(struct aws_allocator *allocator, void (*thread_fn)(void *), struct aws_allocator *sba) {
const struct aws_thread_options *thread_options = aws_default_thread_options();
struct aws_thread threads[NUM_TEST_THREADS];
struct sba_thread_test_data thread_data[NUM_TEST_THREADS];
AWS_ZERO_ARRAY(threads);
AWS_ZERO_ARRAY(thread_data);
for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) {
struct aws_thread *thread = &threads[thread_idx];
aws_thread_init(thread, allocator);
struct sba_thread_test_data *data = &thread_data[thread_idx];
data->sba = sba;
data->thread_idx = (uint32_t)thread_idx;
aws_thread_launch(thread, thread_fn, data, thread_options);
}
for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) {
struct aws_thread *thread = &threads[thread_idx];
aws_thread_join(thread);
}
}
static int s_sba_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
srand(96);
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true);
s_sba_thread_test(allocator, s_sba_threaded_alloc_worker, sba);
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_threaded_allocs_and_frees, s_sba_threaded_allocs_and_frees)
static void s_sba_threaded_realloc_worker(void *user_data) {
struct sba_thread_test_data *thread_data = user_data;
struct aws_allocator *sba = thread_data->sba;
void *alloc = NULL;
size_t size = 0;
for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
size_t old_size = size;
size = rand() % 1024;
if (old_size) {
AWS_FATAL_ASSERT(0 == memcmp(alloc, &thread_data->thread_idx, 1));
}
AWS_FATAL_ASSERT(0 == aws_mem_realloc(sba, &alloc, old_size, size));
/* If there was a value, make sure it's still there */
if (old_size && size) {
AWS_FATAL_ASSERT(0 == memcmp(alloc, &thread_data->thread_idx, 1));
}
if (size) {
memset(alloc, (int)thread_data->thread_idx, size);
}
}
AWS_FATAL_ASSERT(0 == aws_mem_realloc(sba, &alloc, size, 0));
}
static int s_sba_threaded_reallocs(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
srand(12);
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true);
s_sba_thread_test(allocator, s_sba_threaded_realloc_worker, sba);
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_threaded_reallocs, s_sba_threaded_reallocs)
static int s_sba_churn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
srand(9000);
struct aws_array_list allocs;
aws_array_list_init_dynamic(&allocs, allocator, NUM_TEST_ALLOCS, sizeof(void *));
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
size_t alloc_count = 0;
while (alloc_count++ < NUM_TEST_ALLOCS * 10) {
size_t size = aws_max_size(rand() % (2 * 4096), 1);
void *alloc = aws_mem_acquire(sba, size);
aws_array_list_push_back(&allocs, &alloc);
/* randomly free a previous allocation, simulating the real world a bit */
if ((rand() % allocs.length) > (allocs.length / 2)) {
size_t idx = rand() % allocs.length;
aws_array_list_get_at(&allocs, &alloc, idx);
aws_array_list_erase(&allocs, idx);
aws_mem_release(sba, alloc);
}
}
/* free all remaining allocations */
for (size_t idx = 0; idx < allocs.length; ++idx) {
void *alloc = NULL;
aws_array_list_get_at(&allocs, &alloc, idx);
aws_mem_release(sba, alloc);
}
aws_array_list_clean_up(&allocs);
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_churn, s_sba_churn)
static int s_sba_metrics_test(struct aws_allocator *allocator, void *ctx) {
(void)ctx;
struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
size_t expected_active_size = 0;
void *allocs[512] = {0};
for (int idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
size_t size = idx + 1;
size_t bin_size = 0;
ASSERT_SUCCESS(aws_round_up_to_power_of_two(size, &bin_size));
expected_active_size += bin_size;
allocs[idx] = aws_mem_acquire(sba, size);
ASSERT_TRUE(aws_small_block_allocator_bytes_reserved(sba) > aws_small_block_allocator_bytes_active(sba));
ASSERT_TRUE(expected_active_size <= aws_small_block_allocator_bytes_active(sba));
}
/*
* There are
*
* 32 allocations of size < 32 # (bin 0)
* 32 allocations of 32 < size <= 64 # (bin 1)
* 64 allocations of 64 < size <= 128 # (bin 2)
* 128 allocations of 128 < size <= 256 # (bin 3)
* 256 allocations of 256 < size <= 512 # (bin 4)
*
* If we let actual_page_size = allocated_page_size - sizeof(page_header), then we expect to have reserved
*
* (32 + actual_page_size / 32 - 1) / (actual_page_size / 32) # (bin 0)
* (32 + actual_page_size / 64 - 1) / (actual_page_size / 64) # (bin 1)
* (64 + actual_page_size / 128 - 1) / (actual_page_size / 128) # (bin 2)
* (128 + actual_page_size / 256 - 1) / (actual_page_size / 256) # (bin 3)
* (256 + actual_page_size / 512 - 1) / (actual_page_size / 512) # (bin 4)
*
* total pages during the allocations.
*/
size_t actual_page_size = aws_small_block_allocator_page_size_available(sba);
size_t bin0_pages = (32 + actual_page_size / 32 - 1) / (actual_page_size / 32);
size_t bin1_pages = (32 + actual_page_size / 64 - 1) / (actual_page_size / 64);
size_t bin2_pages = (64 + actual_page_size / 128 - 1) / (actual_page_size / 128);
size_t bin3_pages = (128 + actual_page_size / 256 - 1) / (actual_page_size / 256);
size_t bin4_pages = (256 + actual_page_size / 512 - 1) / (actual_page_size / 512);
size_t expected_page_count = bin0_pages + bin1_pages + bin2_pages + bin3_pages + bin4_pages;
ASSERT_INT_EQUALS(
expected_page_count * aws_small_block_allocator_page_size(sba), aws_small_block_allocator_bytes_reserved(sba));
for (int idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
aws_mem_release(sba, allocs[idx]);
}
ASSERT_INT_EQUALS(0, aws_small_block_allocator_bytes_active(sba));
/* after freeing everything, we should have reliniquished all but one page in each bin */
ASSERT_INT_EQUALS(5 * aws_small_block_allocator_page_size(sba), aws_small_block_allocator_bytes_reserved(sba));
aws_small_block_allocator_destroy(sba);
return 0;
}
AWS_TEST_CASE(sba_metrics, s_sba_metrics_test)
|