Created
February 5, 2021 18:52
-
-
Save ajkr/0c1aa96937ab892afb35ba2c489eec25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <pthread.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
void* run_allocs(void* arg) { | |
uint64_t kSmallAllocSize = 32 << 10, kLargeAllocSize = 16 << 20; | |
uint64_t kLimit = (uint64_t)1 << 30; | |
for (uint64_t iter = 0;; ++iter) { | |
uint64_t num_large = kLimit / kLargeAllocSize; | |
void** large_allocs = (void**) malloc(sizeof(void*) * num_large); | |
printf("round begin\nlarge allocs\n"); | |
for (uint64_t i = 0; i < num_large; ++i) { | |
large_allocs[i] = malloc(kLargeAllocSize); | |
assert(large_allocs[i] != NULL); | |
memset(large_allocs[i], '\0', kLargeAllocSize); | |
} | |
sleep(10); | |
// split the large allocs into small pieces | |
printf("split allocs\n"); | |
uint64_t num_small_per_large = kLargeAllocSize / kSmallAllocSize; | |
void** small_allocs = (void**) malloc(sizeof(void*) * num_small_per_large * num_large); | |
for (uint64_t i = 0; i < num_large; ++i) { | |
free(large_allocs[i]); | |
for (uint64_t j = 0; j < num_small_per_large; ++j) { | |
small_allocs[num_small_per_large * i + j] = malloc(kSmallAllocSize); | |
assert(small_allocs[num_small_per_large * i + j] != NULL); | |
memset(small_allocs[num_small_per_large * i + j], '\0', kSmallAllocSize); | |
} | |
} | |
free(large_allocs); | |
sleep(10); | |
// free the small allocations | |
for (uint64_t i = 0; i < num_small_per_large * num_large; ++i) { | |
free(small_allocs[i]); | |
} | |
free(small_allocs); | |
printf("round end\n"); | |
} | |
} | |
int main() { | |
const int kNumThreads = 8; | |
pthread_t threads[kNumThreads]; | |
for (int i = 0; i < kNumThreads; ++i) { | |
int ret = pthread_create(&threads[i], NULL, &run_allocs, NULL); | |
assert(ret == 0); | |
} | |
pthread_join(threads[0], NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment