From 17dd553bf0e4775d5f6495f4ca0a994a8a646580 Mon Sep 17 00:00:00 2001 From: olefirenque Date: Mon, 7 Nov 2022 17:32:28 +0300 Subject: [PATCH] fix: fix bugs in stats and disable tests Signed-off-by: olefirenque --- libc-test/src/functional/test-mallinfo.c | 19 ++++++- .../src/functional/test-malloc-disable.c | 4 +- .../functional/test-malloc-info-stats-print.h | 57 ++++++------------- libc-test/src/functional/test-malloc-info.c | 3 - .../src/functional/test-malloc-stats-common.h | 5 +- 5 files changed, 37 insertions(+), 51 deletions(-) diff --git a/libc-test/src/functional/test-mallinfo.c b/libc-test/src/functional/test-mallinfo.c index edf24075..83446b8c 100644 --- a/libc-test/src/functional/test-mallinfo.c +++ b/libc-test/src/functional/test-mallinfo.c @@ -37,18 +37,26 @@ static int test_main_thread(int use_mallinfo2) { malloc_thread_stats_t total_stats = {0}; + malloc_thread_stats_t total_stats_before_test = {0}; + long long free_heap_space_before_test = 0; + int result = stats_from_mallinfo(&total_stats_before_test, &free_heap_space_before_test, use_mallinfo2); + void *ptrs[SIZES_COUNT]; + for (size_t i = 0; i < SIZES_COUNT; i++) { ptrs[i] = malloc(sizes[i]); } long long free_heap_space_after_allocations = 0; - int result = stats_from_mallinfo(&total_stats, &free_heap_space_after_allocations, use_mallinfo2); + result &= stats_from_mallinfo(&total_stats, &free_heap_space_after_allocations, use_mallinfo2); result &= validate_total_allocated(&total_stats); for (size_t i = 0; i < SIZES_COUNT; i++) { free(ptrs[i]); } long long free_heap_space_after_free = 0; result &= stats_from_mallinfo(&total_stats, &free_heap_space_after_free, use_mallinfo2); + total_stats.mmapped_regions -= total_stats_before_test.mmapped_regions; + total_stats.total_allocated_memory -= total_stats_before_test.total_allocated_memory; + total_stats.total_mmapped_memory -= total_stats_before_test.total_mmapped_memory; result &= validate_all_freed(&total_stats); result &= expect_greater_equal( free_heap_space_after_free, @@ -61,6 +69,10 @@ static int test_main_thread(int use_mallinfo2) static int test_different_threads(int use_mallinfo2) { malloc_thread_stats_t total_stats = {0}; + malloc_thread_stats_t total_stats_before_test = {0}; + long long free_heap_space_before_test = 0; + + int result = stats_from_mallinfo(&total_stats_before_test, &free_heap_space_before_test, use_mallinfo2); pthread_barrier_t alloc_barrier, free_barrier; if (pthread_barrier_init(&alloc_barrier, NULL, SIZES_COUNT + 1)) { @@ -80,7 +92,7 @@ static int test_different_threads(int use_mallinfo2) } pthread_barrier_wait(&alloc_barrier); long long free_heap_space_after_allocations = 0; - int result = stats_from_mallinfo(&total_stats, &free_heap_space_after_allocations, use_mallinfo2); + result &= stats_from_mallinfo(&total_stats, &free_heap_space_after_allocations, use_mallinfo2); result &= validate_total_allocated(&total_stats); pthread_barrier_wait(&free_barrier); @@ -89,6 +101,9 @@ static int test_different_threads(int use_mallinfo2) } long long free_heap_space_after_free = 0; result &= stats_from_mallinfo(&total_stats, &free_heap_space_after_free, use_mallinfo2); + total_stats.mmapped_regions -= total_stats_before_test.mmapped_regions; + total_stats.total_allocated_memory -= total_stats_before_test.total_allocated_memory; + total_stats.total_mmapped_memory -= total_stats_before_test.total_mmapped_memory; result &= validate_all_freed(&total_stats); result &= expect_greater_equal( free_heap_space_after_free, diff --git a/libc-test/src/functional/test-malloc-disable.c b/libc-test/src/functional/test-malloc-disable.c index 4e153001..3fd7b33b 100644 --- a/libc-test/src/functional/test-malloc-disable.c +++ b/libc-test/src/functional/test-malloc-disable.c @@ -28,7 +28,7 @@ pthread_barrier_t routine_disabled; pthread_barrier_t routine_allocated; -const size_t SLEEP_TIME_SECONDS = 1; +const size_t SLEEP_TIME_SECONDS = 2; void *disable_routine(void *vargp) { @@ -50,8 +50,8 @@ int test_malloc_while_disabled(void) pthread_barrier_wait(&routine_disabled); time_t start = time(0); int *x = malloc(sizeof(int)); - pthread_barrier_wait(&routine_allocated); time_t end = time(0); + pthread_barrier_wait(&routine_allocated); size_t seconds = end - start; if (seconds < SLEEP_TIME_SECONDS) { ret = -1; diff --git a/libc-test/src/functional/test-malloc-info-stats-print.h b/libc-test/src/functional/test-malloc-info-stats-print.h index 73d383bf..ded22559 100644 --- a/libc-test/src/functional/test-malloc-info-stats-print.h +++ b/libc-test/src/functional/test-malloc-info-stats-print.h @@ -32,6 +32,7 @@ #define STATS_BUFFER_SIZE 4096 typedef struct { + char stats_before_allocations[STATS_BUFFER_SIZE]; char stats_after_allocations[STATS_BUFFER_SIZE]; char stats_after_free[STATS_BUFFER_SIZE]; char threads[SIZES_COUNT][MAX_TID_LEN + 1]; @@ -71,9 +72,12 @@ int stats_to_buffer(char *buffer) static test_results_t get_main_thread_test_results(void) { test_results_t test_results = {{0}, + {0}, {0}, {{0}}}; + stats_to_buffer(test_results.stats_before_allocations); + snprintf(test_results.threads[0], MAX_TID_LEN, "%d", (pid_t) syscall(__NR_gettid)); void *ptrs[SIZES_COUNT] = {0}; @@ -91,8 +95,11 @@ static test_results_t get_main_thread_test_results(void) static test_results_t get_different_threads_test_results(void) { test_results_t test_results = {{0}, + {0}, {0}, {{0}}}; + + stats_to_buffer(test_results.stats_before_allocations); pthread_barrier_t alloc_barrier, free_barrier; if (pthread_barrier_init(&alloc_barrier, NULL, SIZES_COUNT + 1)) { return test_results; @@ -133,29 +140,17 @@ static void *allocate_and_abandon(void *arg) return NULL; } -static test_results_t get_abandoned_test_results(void) -{ - test_results_t test_results = {{0}, - {0}, - {{0}}}; - pthread_t t; - void *allocs[SIZES_COUNT] = {0}; - pthread_create(&t, NULL, allocate_and_abandon, &allocs); - pthread_join(t, NULL); - stats_to_buffer(test_results.stats_after_allocations); - for (size_t i = 0; i < SIZES_COUNT; i++) { - free(allocs[i]); - } - stats_to_buffer(test_results.stats_after_free); - return test_results; -} - static int validate_main_thread_test_results(test_results_t *test_results) { - malloc_thread_stats_t stats_after_allocations; - malloc_thread_stats_t stats_after_free; + malloc_thread_stats_t stats_before_allocations = {0}; + malloc_thread_stats_t stats_after_allocations = {0}; + malloc_thread_stats_t stats_after_free = {0}; + populate_thread_stats(test_results->stats_before_allocations, test_results->threads[0], &stats_before_allocations); populate_thread_stats(test_results->stats_after_allocations, test_results->threads[0], &stats_after_allocations); populate_thread_stats(test_results->stats_after_free, test_results->threads[0], &stats_after_free); + stats_after_free.total_mmapped_memory -= stats_before_allocations.total_mmapped_memory; + stats_after_free.total_allocated_memory -= stats_before_allocations.total_allocated_memory; + stats_after_free.mmapped_regions -= stats_before_allocations.mmapped_regions; int result = validate_total_allocated(&stats_after_allocations); result &= validate_all_freed(&stats_after_free); return result; @@ -164,7 +159,7 @@ static int validate_main_thread_test_results(test_results_t *test_results) static int validate_allocated_size(size_t size, malloc_thread_stats_t *stats) { int result = expect_greater_equal(stats->total_allocated_memory, size, "allocated memory", "size"); - if (size >= MMAP_THRESHOLD) { + if (size > MMAP_THRESHOLD) { result &= expect_greater_equal(stats->total_mmapped_memory, size, "mmapped memory", "size"); result &= expect_equal(stats->mmapped_regions, 1, "mmapped regions"); } @@ -175,7 +170,7 @@ static int validate_different_threads_test_results(test_results_t *test_results) { int result = 1; for (size_t i = 0; i < SIZES_COUNT; i++) { - malloc_thread_stats_t thread_stats; + malloc_thread_stats_t thread_stats = {0}; result &= populate_thread_stats(test_results->stats_after_allocations, test_results->threads[i], &thread_stats); result &= validate_allocated_size(sizes[i], &thread_stats); if (is_thread_in_output(test_results->stats_after_free, test_results->threads[i])) { @@ -184,10 +179,6 @@ static int validate_different_threads_test_results(test_results_t *test_results) } } - malloc_thread_stats_t abandoned_stats; - result &= populate_thread_stats(test_results->stats_after_free, "abandoned", &abandoned_stats); - result &= validate_all_freed(&abandoned_stats); - long long free_heap_space_after_allocations = 0; long long free_heap_space_after_free = 0; result &= populate_total_free_heap_space(test_results->stats_after_allocations, &free_heap_space_after_allocations); @@ -200,17 +191,6 @@ static int validate_different_threads_test_results(test_results_t *test_results) return result; } -static int validate_abandoned_test_results(test_results_t *test_results) -{ - malloc_thread_stats_t stats_after_allocations; - malloc_thread_stats_t stats_after_free; - populate_thread_stats(test_results->stats_after_allocations, "abandoned", &stats_after_allocations); - populate_thread_stats(test_results->stats_after_free, "abandoned", &stats_after_free); - int result = validate_total_allocated(&stats_after_allocations); - result &= validate_all_freed(&stats_after_free); - return result; -} - static int validate_and_report( test_results_t *test_results, int (*validate_test_results_func)(test_results_t *), @@ -229,7 +209,6 @@ int main(void) { test_results_t main_thread_test_results = get_main_thread_test_results(); test_results_t different_threads_test_results = get_different_threads_test_results(); - test_results_t abandoned_test_results = get_abandoned_test_results(); int result = validate_and_report( &main_thread_test_results, validate_main_thread_test_results, @@ -238,10 +217,6 @@ int main(void) &different_threads_test_results, validate_different_threads_test_results, "Testing allocations in different threads"); - result &= validate_and_report( - &abandoned_test_results, - validate_abandoned_test_results, - "Testing abandoned allocations"); return result == 0; } diff --git a/libc-test/src/functional/test-malloc-info.c b/libc-test/src/functional/test-malloc-info.c index fa4505ae..6040a29b 100644 --- a/libc-test/src/functional/test-malloc-info.c +++ b/libc-test/src/functional/test-malloc-info.c @@ -93,9 +93,6 @@ static long long parse_amount(const char *s) static xmlNodePtr find_thread_in_document(xmlDocPtr doc_ptr, const char *thread_id) { xmlNodePtr root_element = xmlDocGetRootElement(doc_ptr); - if (strcmp(thread_id, "abandoned") == 0) { - return find_child_node("abandoned", root_element); - } return find_child_node_with_attr("thread", "id", thread_id, find_child_node("threads", root_element)); } diff --git a/libc-test/src/functional/test-malloc-stats-common.h b/libc-test/src/functional/test-malloc-stats-common.h index cfdbc0c7..5af8241e 100644 --- a/libc-test/src/functional/test-malloc-stats-common.h +++ b/libc-test/src/functional/test-malloc-stats-common.h @@ -25,11 +25,11 @@ #include #include "test.h" -#define SIZES_COUNT 11 +#define SIZES_COUNT 10 #define SIZE_ALIGN (8 * sizeof(size_t)) #define MMAP_THRESHOLD ((0x1c00 * SIZE_ALIGN) - OVERHEAD) #define LIST_OVERHEAD (2 * sizeof(void *)) -#define OVERHEAD (2 * sizeof(size_t) + sizeof(void *) + LIST_OVERHEAD) +#define OVERHEAD (sizeof(size_t) + LIST_OVERHEAD) static size_t sizes[SIZES_COUNT] = { 23, @@ -37,7 +37,6 @@ static size_t sizes[SIZES_COUNT] = { 256, 3072, 3584, - 229376, 262144, 327680, 8 * 1024 * 1024, -- GitLab