diff --git a/include/malloc.h b/include/malloc.h index 98ba01000134eb592f0ef800c4ba995b51482a46..f771196a84cc29e6e2a0927708879ddd36c63930 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -18,33 +18,23 @@ void *memalign(size_t, size_t); size_t malloc_usable_size(void *); -struct mallinfo { - int arena; - int ordblks; - int smblks; - int hblks; - int hblkhd; - int usmblks; - int fsmblks; - int uordblks; - int fordblks; - int keepcost; -}; +#define __MALLINFO_BODY \ + size_t arena; \ + size_t ordblks; \ + size_t smblks; \ + size_t hblks; \ + size_t hblkhd; \ + size_t usmblks; \ + size_t fsmblks; \ + size_t uordblks; \ + size_t fordblks; \ + size_t keepcost; + +struct mallinfo { __MALLINFO_BODY }; struct mallinfo mallinfo(void); -struct mallinfo2 { - size_t arena; - size_t ordblks; - size_t smblks; - size_t hblks; - size_t hblkhd; - size_t usmblks; - size_t fsmblks; - size_t uordblks; - size_t fordblks; - size_t keepcost; -}; +struct mallinfo2 { __MALLINFO_BODY }; struct mallinfo2 mallinfo2(void); diff --git a/libc-test/src/functional/BUILD.gn b/libc-test/src/functional/BUILD.gn index 902ea97968d12b052ffe8916f8a18227c83b3431..77bf4678de3adeab64d32c3ac919d7fb04ea5dd9 100644 --- a/libc-test/src/functional/BUILD.gn +++ b/libc-test/src/functional/BUILD.gn @@ -1,6 +1,10 @@ import("../../test_template.gni") import("test_src_functional.gni") +if (is_standard_system) { + functional_list += malloc_stats_list +} + foreach(s, functional_list) { test_unittest(s) { target_dir = "functional" diff --git a/libc-test/src/functional/test-iterate.c b/libc-test/src/functional/test-iterate.c new file mode 100644 index 0000000000000000000000000000000000000000..d64bee06a6b4c65d1c3e11b007eb376735a56c50 --- /dev/null +++ b/libc-test/src/functional/test-iterate.c @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include +#include + +#include "test-malloc-api-common.h" + +#define BARRIER_HEIGHT 2 +#define ALLOCATIONS_NUMBER 8 +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + +typedef struct iterate_arg_s { + uintptr_t allocs[ALLOCATIONS_NUMBER]; + size_t allocs_reported_number[ALLOCATIONS_NUMBER]; + size_t allocs_actual_sizes[ALLOCATIONS_NUMBER]; + size_t reported_sizes[ALLOCATIONS_NUMBER]; +} iterate_arg_t; + +typedef struct { + uintptr_t *base; + size_t size; +} allocations_info_t; + +static const size_t allocs_sizes[ALLOCATIONS_NUMBER] = { + 8, + 2 * 1024, + 64 * 1024, + 512 * 1024, + 2 * 1024 * 1024, + 8 * 1024 * 1024, + 16 * 1024 * 1024, + 32 * 1024 * 1024 +}; + +void iterate_callback(void *base, size_t size, void *data) +{ + iterate_arg_t *iterate_arg = (iterate_arg_t *) data; + uintptr_t end; + if (__builtin_add_overflow((uintptr_t) base, size, &end)) { + return; + } + + for (size_t i = 0; i < ALLOCATIONS_NUMBER; ++i) { + if (iterate_arg->allocs[i] >= (uintptr_t) base && iterate_arg->allocs[i] < end) { + iterate_arg->allocs_reported_number[i]++; + uintptr_t max_size = end - iterate_arg->allocs[i]; + iterate_arg->reported_sizes[i] = MIN(size, max_size); + } + } +} + +void fill_allocations_info(const iterate_arg_t *iterate_arg, allocations_info_t *allocations_info) +{ + size_t min_idx, max_idx; + uintptr_t min_val = UINTPTR_MAX, max_val = 0; + + const uintptr_t *allocs = iterate_arg->allocs; + + for (size_t i = 0; i < ALLOCATIONS_NUMBER; ++i) { + if (allocs[i] > max_val) { + max_val = allocs[i]; + max_idx = i; + } + if (allocs[i] < min_val) { + min_val = allocs[i]; + min_idx = i; + } + } + + allocations_info->base = (void *) allocs[min_idx]; + allocations_info->size = allocs[max_idx] - allocs[min_idx] + allocs_sizes[max_idx]; +} + +void make_allocations(iterate_arg_t *iterate_arg) +{ + uintptr_t *allocs = iterate_arg->allocs; + size_t *allocs_actual_sizes = iterate_arg->allocs_actual_sizes; + + for (size_t i = 0; i < ALLOCATIONS_NUMBER; ++i) { + allocs[i] = (uintptr_t) malloc(allocs_sizes[i]); + allocs_actual_sizes[i] = malloc_usable_size((void *) allocs[i]); + } +} + +void free_allocations(iterate_arg_t *iterate_arg) +{ + uintptr_t *allocs = iterate_arg->allocs; + + for (size_t i = 0; i < ALLOCATIONS_NUMBER; ++i) { + free((void *) allocs[i]); + } +} + +int iterate_wrapper(iterate_arg_t *iterate_arg) +{ + int ret = 0; + allocations_info_t allocations_info; + fill_allocations_info(iterate_arg, &allocations_info); + malloc_iterate(allocations_info.base, allocations_info.size, iterate_callback, iterate_arg); + + for (size_t i = 0; i < ALLOCATIONS_NUMBER; ++i) { + if (iterate_arg->allocs_reported_number[i] != 1) { + ret = -1; + } + } + return ret; +} + +pthread_barrier_t routine_allocated; +pthread_barrier_t routine_iterated; + +void *allocate_routine(void *vargp) +{ + iterate_arg_t *iterate_arg = (iterate_arg_t *) vargp; + make_allocations(iterate_arg); + pthread_barrier_wait(&routine_allocated); + pthread_barrier_wait(&routine_iterated); + return NULL; +} + +void *abandoned_allocate_routine(void *vargp) +{ + iterate_arg_t *iterate_arg = (iterate_arg_t *) vargp; + make_allocations(iterate_arg); + return NULL; +} + +int test_iterate_main_thread(void) +{ + int ret; + iterate_arg_t iterate_arg = {{0}, {0}, {0}, {0}}; + make_allocations(&iterate_arg); + ret = iterate_wrapper(&iterate_arg); + free_allocations(&iterate_arg); + return ret; +} + +int test_iterate_another_thread(void) +{ + int ret; + iterate_arg_t iterate_arg_routine = {{0}, {0}, {0}, {0}}; + pthread_barrier_init(&routine_allocated, NULL, BARRIER_HEIGHT); + pthread_barrier_init(&routine_iterated, NULL, BARRIER_HEIGHT); + pthread_t thread_id; + pthread_create(&thread_id, NULL, allocate_routine, (void *) &iterate_arg_routine); + pthread_barrier_wait(&routine_allocated); + ret = iterate_wrapper(&iterate_arg_routine); + free_allocations(&iterate_arg_routine); + pthread_barrier_wait(&routine_iterated); + return ret; +} + +int test_iterate_over_abandoned_allocs(void) +{ + int ret; + iterate_arg_t iterate_arg_routine = {{0}, {0}, {0}, {0}}; + pthread_t thread_id; + pthread_create(&thread_id, NULL, abandoned_allocate_routine, (void *) &iterate_arg_routine); + pthread_join(thread_id, NULL); + ret = iterate_wrapper(&iterate_arg_routine); + free_allocations(&iterate_arg_routine); + return ret; +} + +int main() +{ + int ret = 0; + + ret = check_and_report("Testing iterate main thread", test_iterate_main_thread); + + ret = -(ret || check_and_report("Testing iterate another thread", test_iterate_another_thread)); + + ret = -(ret || check_and_report("Testing iterate over abandoned allocations", test_iterate_over_abandoned_allocs)); + + return ret; +} diff --git a/libc-test/src/functional/test-mallinfo.c b/libc-test/src/functional/test-mallinfo.c new file mode 100644 index 0000000000000000000000000000000000000000..83446b8c2e7b9f1d4a58056ec5300d5328afdb74 --- /dev/null +++ b/libc-test/src/functional/test-mallinfo.c @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test-malloc-stats-common.h" + +static int stats_from_mallinfo(malloc_thread_stats_t *stats, long long *total_free_heap_space, int use_mallinfo2) +{ + if (use_mallinfo2) { + struct mallinfo2 info = mallinfo2(); + *stats = (malloc_thread_stats_t) {info.hblks, info.hblkhd, info.uordblks}; + *total_free_heap_space = info.fordblks; + return 1; + } + struct mallinfo info = mallinfo(); + if (info.hblks < 0 || info.hblkhd < 0 || info.uordblks < 0 || info.fordblks < 0) { + t_error("struct mallinfo contains negative numbers\n"); + return 0; + } + *stats = (malloc_thread_stats_t) {info.hblks, info.hblkhd, info.uordblks}; + *total_free_heap_space = info.fordblks; + return 1; +} + +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; + 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, + free_heap_space_after_allocations, + "free heap space after free", + "free heap space after allocations"); + return result; +} + +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)) { + return 0; + } + if (pthread_barrier_init(&free_barrier, NULL, SIZES_COUNT + 1)) { + return 0; + } + + thread_data_t thread_data[SIZES_COUNT]; + for (size_t i = 0; i < SIZES_COUNT; i++) { + thread_data[i] = (thread_data_t) {sizes[i], &alloc_barrier, &free_barrier, 0}; + } + pthread_t threads[SIZES_COUNT]; + for (size_t i = 0; i < SIZES_COUNT; i++) { + pthread_create(&threads[i], NULL, allocate_wait_free, &thread_data[i]); + } + pthread_barrier_wait(&alloc_barrier); + long long free_heap_space_after_allocations = 0; + result &= stats_from_mallinfo(&total_stats, &free_heap_space_after_allocations, use_mallinfo2); + result &= validate_total_allocated(&total_stats); + + pthread_barrier_wait(&free_barrier); + for (size_t i = 0; i < SIZES_COUNT; i++) { + pthread_join(threads[i], NULL); + } + 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, + free_heap_space_after_allocations, + "free heap space after free", + "free heap space after allocations"); + return result; +} + +static int test_and_report( + int (*test_func)(int), + int test_func_arg, + const char *message) +{ + t_printf("%s...", message); + if (!test_func(test_func_arg)) { + t_error("Failed!\n"); + return 0; + } + t_printf("Success\n"); + return 1; +} + +int main(void) +{ + int result = test_and_report(test_main_thread, 0, "Testing mallinfo main thread"); + result &= test_and_report(test_main_thread, 1, "Testing mallinfo2 main thread"); + result &= test_and_report(test_different_threads, 0, "Testing mallinfo different threads"); + result &= test_and_report(test_different_threads, 1, "Testing mallinfo2 different threads"); + return result == 0; +} \ No newline at end of file diff --git a/libc-test/src/functional/test-malloc-api-common.h b/libc-test/src/functional/test-malloc-api-common.h new file mode 100644 index 0000000000000000000000000000000000000000..0fac128440a11550820fa402ac61ccbe2d0fab51 --- /dev/null +++ b/libc-test/src/functional/test-malloc-api-common.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_ITERATE_DISABLE_COMMON_H +#define TEST_ITERATE_DISABLE_COMMON_H + +#include "test.h" + +int check_and_report(const char *message, int (*check_func)(void)) +{ + t_printf("%s...", message); + int ret = check_func(); + if (ret == 0) { + t_printf("Success\n"); + } else { + t_error("Failed\n"); + } + return ret; +} + +#endif // TEST_ITERATE_DISABLE_COMMON_H \ No newline at end of file diff --git a/libc-test/src/functional/test-malloc-backtrace.c b/libc-test/src/functional/test-malloc-backtrace.c new file mode 100644 index 0000000000000000000000000000000000000000..e8802e9f0bd57d464c6f0079c6e6aed3885d2d9a --- /dev/null +++ b/libc-test/src/functional/test-malloc-backtrace.c @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test-malloc-api-common.h" + +static int test_backtrace() +{ + return malloc_backtrace(NULL, NULL, 0) != 0; +} + +int main() +{ + return check_and_report("Testing malloc_backtrace stub", test_backtrace); +} \ No newline at end of file diff --git a/libc-test/src/functional/test-malloc-disable.c b/libc-test/src/functional/test-malloc-disable.c new file mode 100644 index 0000000000000000000000000000000000000000..3fd7b33bc4e7c52dda0eac168093727efc6f2988 --- /dev/null +++ b/libc-test/src/functional/test-malloc-disable.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "test-malloc-api-common.h" + +#define BARRIER_HEIGHT 2 + +pthread_barrier_t routine_disabled; +pthread_barrier_t routine_allocated; + +const size_t SLEEP_TIME_SECONDS = 2; + +void *disable_routine(void *vargp) +{ + malloc_disable(); + pthread_barrier_wait(&routine_disabled); + sleep(SLEEP_TIME_SECONDS); + malloc_enable(); + pthread_barrier_wait(&routine_allocated); + return NULL; +} + +int test_malloc_while_disabled(void) +{ + int ret = 0; + pthread_barrier_init(&routine_disabled, NULL, BARRIER_HEIGHT); + pthread_barrier_init(&routine_allocated, NULL, BARRIER_HEIGHT); + pthread_t thread_id; + pthread_create(&thread_id, NULL, disable_routine, NULL); + pthread_barrier_wait(&routine_disabled); + time_t start = time(0); + int *x = malloc(sizeof(int)); + time_t end = time(0); + pthread_barrier_wait(&routine_allocated); + size_t seconds = end - start; + if (seconds < SLEEP_TIME_SECONDS) { + ret = -1; + } + free(x); + pthread_join(thread_id, NULL); + + return ret; +} + +int main() +{ + int ret = 0; + + ret = check_and_report("Testing malloc while disabled", test_malloc_while_disabled); + + return ret; +} diff --git a/libc-test/src/functional/test-malloc-info-stats-print.h b/libc-test/src/functional/test-malloc-info-stats-print.h new file mode 100644 index 0000000000000000000000000000000000000000..ded225597d3c7ae9acccc8394208385707ecc8bc --- /dev/null +++ b/libc-test/src/functional/test-malloc-info-stats-print.h @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_MALLOC_STATS_H +#define TEST_MALLOC_STATS_H + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include "test-malloc-stats-common.h" +#include "test.h" + +#define MAX_TID_LEN 32 +#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]; +} test_results_t; + +static void stderr_stats_cb(void); + +static int populate_thread_stats(const char *output, const char *thread_id, malloc_thread_stats_t *stats); + +static int populate_total_free_heap_space(const char *output, long long *total_free_heap_space); + +static int is_thread_in_output(const char *output, const char *thread_id); + +static void print_to_file(void *fp, const char *s) +{ + fputs(s, fp); +} + +int stats_to_buffer(char *buffer) +{ + fflush(stderr); + int err_pipe[2]; + int saved_stderr = dup(STDERR_FILENO); + if (pipe(err_pipe) != 0) { + perror("Can't create pipe"); + return 0; + } + dup2(err_pipe[1], STDERR_FILENO); + close(err_pipe[1]); + stderr_stats_cb(); + fflush(stderr); + read(err_pipe[0], buffer, STATS_BUFFER_SIZE); + dup2(saved_stderr, STDERR_FILENO); + return 1; +} + +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}; + for (size_t i = 0; i < SIZES_COUNT; i++) { + ptrs[i] = malloc(sizes[i]); + } + stats_to_buffer(test_results.stats_after_allocations); + for (size_t i = 0; i < SIZES_COUNT; i++) { + free(ptrs[i]); + } + stats_to_buffer(test_results.stats_after_free); + return test_results; +} + +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; + } + if (pthread_barrier_init(&free_barrier, NULL, SIZES_COUNT + 1)) { + return test_results; + } + + thread_data_t thread_data[SIZES_COUNT]; + for (size_t i = 0; i < SIZES_COUNT; i++) { + thread_data[i] = (thread_data_t) {sizes[i], &alloc_barrier, &free_barrier, 0}; + } + pthread_t threads[SIZES_COUNT]; + for (size_t i = 0; i < SIZES_COUNT; i++) { + pthread_create(&threads[i], NULL, allocate_wait_free, &thread_data[i]); + } + pthread_barrier_wait(&alloc_barrier); + + for (size_t i = 0; i < SIZES_COUNT; i++) { + snprintf(test_results.threads[i], MAX_TID_LEN, "%d", thread_data[i].self_id); + } + stats_to_buffer(test_results.stats_after_allocations); + + pthread_barrier_wait(&free_barrier); + for (size_t i = 0; i < SIZES_COUNT; i++) { + pthread_join(threads[i], NULL); + } + stats_to_buffer(test_results.stats_after_free); + return test_results; +} + +static void *allocate_and_abandon(void *arg) +{ + void **allocs = arg; + for (size_t i = 0; i < SIZES_COUNT; i++) { + allocs[i] = malloc(sizes[i]); + } + return NULL; +} + +static int validate_main_thread_test_results(test_results_t *test_results) +{ + 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; +} + +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) { + result &= expect_greater_equal(stats->total_mmapped_memory, size, "mmapped memory", "size"); + result &= expect_equal(stats->mmapped_regions, 1, "mmapped regions"); + } + return result; +} + +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 = {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])) { + t_error("Thread %s did not disappear from output\n", test_results->threads[i]); + result = 0; + } + } + + 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); + result &= populate_total_free_heap_space(test_results->stats_after_free, &free_heap_space_after_free); + result &= expect_greater_equal( + free_heap_space_after_free, + free_heap_space_after_allocations, + "free heap space after free", + "free heap space after allocations"); + return result; +} + +static int validate_and_report( + test_results_t *test_results, + int (*validate_test_results_func)(test_results_t *), + const char *message) +{ + t_printf("%s...", message); + if (!validate_test_results_func(test_results)) { + t_error("Failed!\n"); + return 0; + } + t_printf("Success\n"); + return 1; +} + +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(); + int result = validate_and_report( + &main_thread_test_results, + validate_main_thread_test_results, + "Testing allocations in main thread"); + result &= validate_and_report( + &different_threads_test_results, + validate_different_threads_test_results, + "Testing allocations in different threads"); + return result == 0; +} + +#endif // TEST_MALLOC_STATS_H diff --git a/libc-test/src/functional/test-malloc-info.c b/libc-test/src/functional/test-malloc-info.c new file mode 100644 index 0000000000000000000000000000000000000000..6040a29b3b789a76d9c774c4f0f818102ab0d246 --- /dev/null +++ b/libc-test/src/functional/test-malloc-info.c @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "test-malloc-info-stats-print.h" +#include "libxml/parser.h" + +static const xmlChar *get_text_from_children(xmlNodePtr children) +{ + for (xmlNodePtr child_node = children; child_node != NULL; child_node = child_node->next) { + if (child_node->type == XML_TEXT_NODE) { + return child_node->content; + } + } + return NULL; +} + +static const xmlChar *get_attribute(const char *attr_name, xmlNodePtr node) +{ + for (xmlAttrPtr curr_attr = node->properties; curr_attr != NULL; curr_attr = curr_attr->next) { + if (xmlStrEqual(curr_attr->name, (const xmlChar *) attr_name)) { + return get_text_from_children(curr_attr->children); + } + } + return NULL; +} + +static xmlNodePtr find_child_node_with_attr(const char *name, const char *attr_name, const char *attr_value, xmlNodePtr parent) +{ + if (parent == NULL) { + return NULL; + } + for (xmlNodePtr curr_node = parent->children; curr_node != NULL; curr_node = curr_node->next) { + if (curr_node->type == XML_ELEMENT_NODE && xmlStrEqual(curr_node->name, (xmlChar *) name)) { + if (attr_name == NULL) { + return curr_node; + } + if (xmlStrEqual(get_attribute(attr_name, curr_node), (const xmlChar *) attr_value)) { + return curr_node; + } + } + } + return NULL; +} + +static xmlNodePtr find_child_node(const char *name, xmlNodePtr parent) +{ + return find_child_node_with_attr(name, NULL, NULL, parent); +} + +static const char *get_node_text(xmlNodePtr node_ptr) +{ + if (node_ptr == NULL) { + return NULL; + } + return (const char *) get_text_from_children(node_ptr->children); +} + +static void stderr_stats_cb(void) +{ + malloc_info(0, stderr); +} + +static long long parse_amount(const char *s) +{ + if (s == NULL) { + return -1; + } + char *end_ptr; + long long result = strtoll(s, &end_ptr, 10); + if (end_ptr != s + strlen(s)) { + return -1; + } + if (result < 0) { + return -1; + } + return result; +} + +static xmlNodePtr find_thread_in_document(xmlDocPtr doc_ptr, const char *thread_id) +{ + xmlNodePtr root_element = xmlDocGetRootElement(doc_ptr); + return find_child_node_with_attr("thread", "id", thread_id, find_child_node("threads", root_element)); +} + +static int populate_thread_stats(const char *output, const char *thread_id, malloc_thread_stats_t *stats) +{ + xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *) output); + if (doc_ptr == NULL) { + return 0; + } + xmlNodePtr thread_root = find_thread_in_document(doc_ptr, thread_id); + long long total_allocated_memory = + parse_amount(get_node_text(find_child_node("total_allocated_memory", thread_root))); + long long total_mmapped_memory = + parse_amount(get_node_text(find_child_node("total_mmapped_memory", thread_root))); + long long mmapped_regions = + parse_amount(get_node_text(find_child_node("mmapped_regions", thread_root))); + xmlFreeDoc(doc_ptr); + + if (total_allocated_memory == -1 || total_mmapped_memory == -1 || mmapped_regions == -1) { + return 0; + } + stats->total_allocated_memory = total_allocated_memory; + stats->total_mmapped_memory = total_mmapped_memory; + stats->mmapped_regions = mmapped_regions; + return 1; +} + +static int populate_total_free_heap_space(const char *output, long long *total_free_heap_space) +{ + xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *) output); + if (doc_ptr == NULL) { + return 0; + } + xmlNodePtr heap_space_root = find_child_node("total_free_heap_space", xmlDocGetRootElement(doc_ptr)); + long long total_free_heap_space_parsed = parse_amount(get_node_text(heap_space_root)); + xmlFreeDoc(doc_ptr); + + if (total_free_heap_space_parsed == -1) { + return 0; + } + *total_free_heap_space = total_free_heap_space_parsed; + return 1; +} + +static int is_thread_in_output(const char *output, const char *thread_id) +{ + xmlDocPtr doc_ptr = xmlParseDoc((const xmlChar *) output); + if (doc_ptr == NULL) { + return 0; + } + int result = find_thread_in_document(doc_ptr, thread_id) != NULL; + xmlFreeDoc(doc_ptr); + return result; +} diff --git a/libc-test/src/functional/test-malloc-stats-common.h b/libc-test/src/functional/test-malloc-stats-common.h new file mode 100644 index 0000000000000000000000000000000000000000..5af8241e80158ca05401e4411f4f3c3e5f4671d7 --- /dev/null +++ b/libc-test/src/functional/test-malloc-stats-common.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TEST_MALLOC_STATS_COMMON_H +#define TEST_MALLOC_STATS_COMMON_H + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include "test.h" + +#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 (sizeof(size_t) + LIST_OVERHEAD) + +static size_t sizes[SIZES_COUNT] = { + 23, + 32, + 256, + 3072, + 3584, + 262144, + 327680, + 8 * 1024 * 1024, + 16 * 1024 * 1024, + 32 * 1024 * 1024 +}; + +typedef struct { + long long mmapped_regions; + long long total_mmapped_memory; + long long total_allocated_memory; +} malloc_thread_stats_t; + +typedef struct { + size_t alloc_size; + pthread_barrier_t *alloc_barrier; + pthread_barrier_t *free_barrier; + pid_t self_id; +} thread_data_t; + +static malloc_thread_stats_t get_total_from_test_sizes() +{ + malloc_thread_stats_t total_stats = {0}; + for (size_t i = 0; i < SIZES_COUNT; i++) { + if (sizes[i] > MMAP_THRESHOLD) { + total_stats.total_mmapped_memory += sizes[i]; + total_stats.mmapped_regions++; + } + total_stats.total_allocated_memory += sizes[i]; + } + return total_stats; +} + +static int expect_greater_equal(long long amt1, long long amt2, const char *amt1_name, const char *amt2_name) +{ + if (amt1 >= amt2) { + return 1; + } + t_error("Expected %s(value: %lld) to be >= %s(value: %lld)\n", amt1_name, amt1, amt2_name, amt2); + return 0; +} + +static int expect_equal(long long amt, long long value, const char *amt_name) +{ + if (amt == value) { + return 1; + } + t_error("Expected %s(value: %lld) to be %lld\n", amt_name, amt, value); + return 0; +} + +static int validate_total_allocated(malloc_thread_stats_t *total_stats) +{ + malloc_thread_stats_t total_from_test_sizes = get_total_from_test_sizes(); + + int result = expect_greater_equal( + total_stats->total_allocated_memory, + total_from_test_sizes.total_allocated_memory, + "allocated memory", + "total memory from test sizes"); + result &= expect_greater_equal( + total_stats->total_mmapped_memory, + total_from_test_sizes.total_mmapped_memory, + "mmapped memory", + "total large memory from test sizes"); + result &= expect_equal( + total_stats->mmapped_regions, + total_from_test_sizes.mmapped_regions, + "mmapped regions"); + return result; +} + +static int validate_all_freed(malloc_thread_stats_t *total_stats) +{ + int result = expect_equal(total_stats->total_allocated_memory, 0, "allocated memory"); + result &= expect_equal(total_stats->total_mmapped_memory, 0, "mmapped memory"); + result &= expect_equal(total_stats->mmapped_regions, 0, "mmapped regions"); + return result; +} + +static void *allocate_wait_free(void *arg) +{ + thread_data_t *thread_data = arg; + thread_data->self_id = syscall(__NR_gettid); + void *alloc = malloc(thread_data->alloc_size); + pthread_barrier_wait(thread_data->alloc_barrier); + pthread_barrier_wait(thread_data->free_barrier); + free(alloc); + return NULL; +} + +#endif // TEST_MALLOC_STATS_COMMON_H diff --git a/libc-test/src/functional/test-malloc-stats-print.c b/libc-test/src/functional/test-malloc-stats-print.c new file mode 100644 index 0000000000000000000000000000000000000000..a93ea40688fdaf488e0e1ea75460e7078f46a31d --- /dev/null +++ b/libc-test/src/functional/test-malloc-stats-print.c @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test-malloc-info-stats-print.h" + +#define THREAD_DATA_REGEX_LEN (MAX_TID_LEN + 43) +#define REGEX_NMATCH 1 + +static void stderr_stats_cb(void) +{ + malloc_stats_print(print_to_file, stderr, ""); +} + +static int parse_amount(const char **s, long long *destination) +{ + char *end_ptr = NULL; + long long result = strtoll(*s, &end_ptr, 10); + if (end_ptr == *s) { + return 0; + } + *s = end_ptr; + if ((!isspace(*end_ptr) && *end_ptr != '\n' && *end_ptr != '\0') || result < 0) { + return 0; + } + *destination = result; + return 1; +} + +static const char *find_thread_in_output(const char *output, const char *thread_id) +{ + char thread_data_regex_s[THREAD_DATA_REGEX_LEN + 1]; + snprintf(thread_data_regex_s, THREAD_DATA_REGEX_LEN, "^%s([[:space:]]+[[:digit:]]+){3}[[:space:]]*$", thread_id); + regex_t thread_data_regex; + if (regcomp(&thread_data_regex, thread_data_regex_s, REG_EXTENDED | REG_NEWLINE) != 0) { + t_error("Failed to compile regex %s", thread_data_regex_s); + return NULL; + } + + regmatch_t pmatch[REGEX_NMATCH]; + int match_result = regexec(&thread_data_regex, output, REGEX_NMATCH, pmatch, 0); + regfree(&thread_data_regex); + if (match_result != 0) { + return NULL; + } + return output + pmatch[0].rm_so; +} + +static int populate_thread_stats(const char *output, const char *thread_id, malloc_thread_stats_t *stats) +{ + const char *thread_data_start = find_thread_in_output(output, thread_id); + if (thread_data_start == NULL) { + t_error("Failed to find thread id %s in output", thread_id); + return 0; + } + + thread_data_start += strlen(thread_id); + int result = 1; + result &= parse_amount(&thread_data_start, &stats->total_allocated_memory); + result &= parse_amount(&thread_data_start, &stats->total_mmapped_memory); + result &= parse_amount(&thread_data_start, &stats->mmapped_regions); + + return result; +} + +static int populate_total_free_heap_space(const char *output, long long *total_free_heap_space) +{ + const char *free_heap_space_start = strstr(output, "total free heap space:"); + if (free_heap_space_start == NULL) { + return 0; + } + free_heap_space_start += strlen("total free heap space:"); + return parse_amount(&free_heap_space_start, total_free_heap_space); +} + +static int is_thread_in_output(const char *output, const char *thread_id) +{ + return find_thread_in_output(output, thread_id) != NULL; +} \ No newline at end of file diff --git a/libc-test/src/functional/test-mallopt.c b/libc-test/src/functional/test-mallopt.c new file mode 100644 index 0000000000000000000000000000000000000000..4a9e5c4289ec27f6bc2da6527a2e4a7047749640 --- /dev/null +++ b/libc-test/src/functional/test-mallopt.c @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test-malloc-api-common.h" + +static int test_mallopt() +{ + return mallopt(0, 0) != 0; +} + +int main() +{ + return check_and_report("Testing mallopt stub", test_mallopt); +} \ No newline at end of file diff --git a/libc-test/src/functional/test_src_functional.gni b/libc-test/src/functional/test_src_functional.gni index fdd9e5532d46b7a0b58cddb12fcf8b6c0290eb2b..b293423f08f5e7f1f89eccd64f71cef92a7dd53b 100644 --- a/libc-test/src/functional/test_src_functional.gni +++ b/libc-test/src/functional/test_src_functional.gni @@ -80,3 +80,13 @@ functional_list = [ "wcstol", "dlclose_reset", ] + +malloc_stats_list = [ + "test-malloc-stats-print", + "test-malloc-info", + "test-mallinfo", + "test-iterate", + "test-malloc-disable", + "test-malloc-backtrace", + "test-mallopt", +] diff --git a/libc-test/test_template.gni b/libc-test/test_template.gni index e186d216920da50fd4d0d4405c5bff3784ae1817..d809d74ea589ec590f7ee6da4493b779931a868f 100644 --- a/libc-test/test_template.gni +++ b/libc-test/test_template.gni @@ -130,6 +130,10 @@ template("test_unittest") { ldflags += [ "-Wl,-rpath=./" ] libs += [ "//${root_out_dir}/${test_lib_dir}/libtls_init_dso.so" ] } + + if (target_name == "test-malloc-info") { + deps = [ "//third_party/libxml2:xml2" ] + } } if (target_dir == "functionalext/fortify") { diff --git a/musl_config.gni b/musl_config.gni index 94eff68b34ed99b1d2806a379d99fc14b433fc33..4ecbf7857f97148b2d7e1526130afcbf1c14e7d4 100644 --- a/musl_config.gni +++ b/musl_config.gni @@ -57,6 +57,7 @@ declare_args() { if (!is_standard_system) { enable_musl_log = false } + musl_iterate_and_stats_api = true musl_secure_level = 1 } diff --git a/musl_src.gni b/musl_src.gni index 8c5328eb3ac4cca385643039ce91d5dff0241038..b4a644e6cdd8ca0544de19526802446eb5bd1605 100644 --- a/musl_src.gni +++ b/musl_src.gni @@ -515,7 +515,7 @@ musl_src_file = [ "src/malloc/malloc.c", "src/malloc/malloc_random.c", "src/malloc/malloc_usable_size.c", - "src/malloc/mallocng/mallinfo.c", + "src/malloc/stats.c", "src/malloc/memalign.c", "src/malloc/posix_memalign.c", "src/math/__cos.c", @@ -2018,6 +2018,7 @@ musl_src_porting_file = [ "include/info/application_target_sdk_version.h", "include/info/device_api_version.h", "include/info/fatal_message.h", + "include/malloc.h", "include/pthread.h", "include/fcntl.h", "include/poll.h", @@ -2067,6 +2068,8 @@ musl_src_porting_file = [ "src/linux/reboot.c", "src/linux/tgkill.c", "src/malloc/malloc.c", + "src/malloc/memalign.c", + "src/malloc/stats.c", "src/malloc/malloc_random.c", "src/multibyte/wcsnrtombs.c", "src/network/inet_legacy.c", diff --git a/musl_template.gni b/musl_template.gni index 2ee1e448ae5d0552ff9b26139b58a6f20c6686c5..42b8f011c4ac0b537db69b758b3a793f70363f55 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -367,6 +367,10 @@ template("musl_libs") { defines += [ "MALLOC_SECURE_ALL" ] } + if (musl_iterate_and_stats_api) { + defines += [ "MUSL_ITERATE_AND_STATS_API" ] + } + foreach(s, sources_orig) { sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ] } @@ -542,6 +546,11 @@ template("musl_libs") { "src/env/__stack_chk_fail.c", ] + defines = [] + if (musl_iterate_and_stats_api) { + defines += [ "MUSL_ITERATE_AND_STATS_API" ] + } + if (musl_arch == "arm") { sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.c" ] } else if (musl_arch == "aarch64") { diff --git a/ndk_musl_include/malloc.h b/ndk_musl_include/malloc.h index 09c9f22506be0f0ec07281fbdea02a20ef795523..e3abf69950fa6fc01fa2db2bd6ff578aa97eaf11 100644 --- a/ndk_musl_include/malloc.h +++ b/ndk_musl_include/malloc.h @@ -18,31 +18,21 @@ void *memalign(size_t, size_t); size_t malloc_usable_size(void *); -struct mallinfo { - int arena; - int ordblks; - int smblks; - int hblks; - int hblkhd; - int usmblks; - int fsmblks; - int uordblks; - int fordblks; - int keepcost; -}; - -struct mallinfo2 { - size_t arena; - size_t ordblks; - size_t smblks; - size_t hblks; - size_t hblkhd; - size_t usmblks; - size_t fsmblks; - size_t uordblks; - size_t fordblks; +#define __MALLINFO_BODY \ + size_t arena; \ + size_t ordblks; \ + size_t smblks; \ + size_t hblks; \ + size_t hblkhd; \ + size_t usmblks; \ + size_t fsmblks; \ + size_t uordblks; \ + size_t fordblks; \ size_t keepcost; -}; + +struct mallinfo { __MALLINFO_BODY }; + +struct mallinfo2 { __MALLINFO_BODY }; #ifdef __cplusplus } diff --git a/porting/linux/user/include/malloc.h b/porting/linux/user/include/malloc.h new file mode 100644 index 0000000000000000000000000000000000000000..e4b64dd6b5f9eee425059b4a68bc4c139ecf3410 --- /dev/null +++ b/porting/linux/user/include/malloc.h @@ -0,0 +1,59 @@ +#ifndef _MALLOC_H +#define _MALLOC_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define __NEED_size_t +#define __NEED_ssize_t +#define __NEED_uintptr_t + +#include + +void *malloc (size_t); +void *calloc (size_t, size_t); +void *realloc (void *, size_t); +void free (void *); +void *valloc (size_t); +void *memalign(size_t, size_t); + +size_t malloc_usable_size(void *); + +#define __MALLINFO_BODY \ + size_t arena; \ + size_t ordblks; \ + size_t smblks; \ + size_t hblks; \ + size_t hblkhd; \ + size_t usmblks; \ + size_t fsmblks; \ + size_t uordblks; \ + size_t fordblks; \ + size_t keepcost; + +struct mallinfo { __MALLINFO_BODY }; + +struct mallinfo mallinfo(void); + +struct mallinfo2 { __MALLINFO_BODY }; + +struct mallinfo2 mallinfo2(void); + +int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg); +void malloc_disable(void); +void malloc_enable(void); + +int malloc_info(int options, FILE* fp); +void malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts); + +int mallopt(int param, int value); +ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/porting/linux/user/src/hook/musl_malloc.h b/porting/linux/user/src/hook/musl_malloc.h index ff8bd641121357175e4c7fe7b52ca3d7880f9670..9bcc1de56e579022faea00b0626ff3b99c5bdf19 100644 --- a/porting/linux/user/src/hook/musl_malloc.h +++ b/porting/linux/user/src/hook/musl_malloc.h @@ -1,6 +1,8 @@ #ifndef _MUSL_MALLOC_H #define _MUSL_MALLOC_H +#include "malloc.h" + #ifdef __cplusplus extern "C" { #endif @@ -21,6 +23,19 @@ void *__libc_valloc(size_t); void *__libc_memalign(size_t, size_t); size_t __libc_malloc_usable_size(void *); +struct mallinfo2 __libc_mallinfo2(void); + +int __libc_malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg); +void __libc_malloc_disable(void); +void __libc_malloc_enable(void); + +int __libc_malloc_info(int options, FILE* fp); +void __libc_malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts); + +int __libc_mallopt(int param, int value); +ssize_t __libc_malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count); + + #ifdef __cplusplus } #endif diff --git a/porting/linux/user/src/hook/musl_malloc_dispatch.h b/porting/linux/user/src/hook/musl_malloc_dispatch.h index bbabb19d9830964397bca049940cf73839db0417..2e612d964a9a3742c8657cde9bd3f9b91845deea 100644 --- a/porting/linux/user/src/hook/musl_malloc_dispatch.h +++ b/porting/linux/user/src/hook/musl_malloc_dispatch.h @@ -18,6 +18,17 @@ typedef void* (*MallocVallocType)(size_t); typedef void (*MallocFreeType)(void*); typedef void* (*MallocMemalignType)(size_t, size_t); typedef size_t (*MallocMallocUsableSizeType)(void*); + +typedef struct mallinfo (*MallinfoType)(void); +typedef struct mallinfo2 (*Mallinfo2Type)(void); +typedef int (*MallocIterateType)(void*, size_t, void (*callback)(void*, size_t, void*), void*); +typedef void (*MallocDisableType)(void); +typedef void (*MallocEnableType)(void); +typedef int (*MallocInfoType)(int, FILE*); +typedef void (*MallocStatsPrintType)(void (*) (void *, const char *), void *, const char *); +typedef int (*MalloptType)(int, int); +typedef ssize_t (*MallocBacktraceType)(void*, uintptr_t*, size_t); + typedef bool (*GetHookFlagType)(); typedef bool (*SetHookFlagType)(bool); @@ -31,6 +42,15 @@ struct MallocDispatchType { MallocFreeType free; MallocMemalignType memalign; MallocMallocUsableSizeType malloc_usable_size; + MallinfoType mallinfo; + Mallinfo2Type mallinfo2; + MallocIterateType malloc_iterate; + MallocDisableType malloc_disable; + MallocEnableType malloc_enable; + MallocInfoType malloc_info; + MallocStatsPrintType malloc_stats_print; + MalloptType mallopt; + MallocBacktraceType malloc_backtrace; GetHookFlagType get_hook_flag; SetHookFlagType set_hook_flag; }; diff --git a/porting/linux/user/src/internal/malloc_impl.h b/porting/linux/user/src/internal/malloc_impl.h index 3f6d0ddf225dedc0130218c7b4733c86331bb8c2..0a9817a34ba6e9bfb2cb07e700d8135ee0b2e8de 100644 --- a/porting/linux/user/src/internal/malloc_impl.h +++ b/porting/linux/user/src/internal/malloc_impl.h @@ -10,11 +10,20 @@ hidden void __malloc_donate(char *, char *); hidden void *__memalign(size_t, size_t); +typedef struct occupied_bin_s { + struct chunk *head, *tail; + volatile int lock[2]; +} occupied_bin_t; + struct chunk { size_t psize, csize; #ifdef MALLOC_RED_ZONE size_t usize; size_t state; +#endif +#ifdef MUSL_ITERATE_AND_STATS_API + size_t thread_id; + struct chunk *next_occupied, *prev_occupied; #endif struct chunk *next, *prev; }; @@ -28,17 +37,41 @@ struct bin { #endif }; +#ifdef MUSL_ITERATE_AND_STATS_API +typedef void (*malloc_iterate_callback)(void* base, size_t size, void* arg); + +hidden occupied_bin_t *__get_occupied_bin_by_idx(size_t bin_index); +hidden occupied_bin_t *__get_occupied_bin(struct chunk *c); +hidden occupied_bin_t *__get_current_occupied_bin(); +hidden void __push_chunk(struct chunk *c); +hidden void __pop_chunk(struct chunk *c); +hidden size_t __get_total_heap_space(void); +#endif + +#define BINS_COUNT 64 + #define SIZE_MASK (-SIZE_ALIGN) -#ifndef MALLOC_RED_ZONE -#define SIZE_ALIGN (4*sizeof(size_t)) -#define OVERHEAD (2*sizeof(size_t)) + +#ifdef MUSL_ITERATE_AND_STATS_API +#define OCCUPIED_BIN_COUNT 64 +#define OCCUPIED_LIST_OVERHEAD (2*sizeof(void*)) +#define ITERATE_AND_STATS_OVERHEAD (sizeof(size_t) + OCCUPIED_LIST_OVERHEAD) #else +#define ITERATE_AND_STATS_OVERHEAD (0) +#endif + +#ifndef MALLOC_RED_ZONE #define SIZE_ALIGN (8*sizeof(size_t)) -#define OVERHEAD (4*sizeof(size_t)) +#define OVERHEAD (2*sizeof(size_t) + ITERATE_AND_STATS_OVERHEAD) +#else +#define SIZE_ALIGN (16*sizeof(size_t)) +#define OVERHEAD (4*sizeof(size_t) + ITERATE_AND_STATS_OVERHEAD) #endif + + #define MMAP_THRESHOLD (0x1c00*SIZE_ALIGN) #ifndef MALLOC_RED_ZONE -#define DONTCARE 16 +#define DONTCARE OVERHEAD #else #define DONTCARE OVERHEAD #define POINTER_USAGE (2*sizeof(void *)) diff --git a/porting/linux/user/src/malloc/malloc.c b/porting/linux/user/src/malloc/malloc.c index 14dc7f1dbbcd81b22a568345dbf9ee5ef2e0a94b..5e906c71cc0cacd5ccb18fd4cb8d8b2f48eb6b2d 100644 --- a/porting/linux/user/src/malloc/malloc.c +++ b/porting/linux/user/src/malloc/malloc.c @@ -1,16 +1,15 @@ #define _GNU_SOURCE #include -#include #include #include #include #include +#include #include "libc.h" #include "atomic.h" #include "pthread_impl.h" #include "malloc_impl.h" #include "malloc_random.h" -#include #ifdef USE_JEMALLOC #include @@ -31,14 +30,17 @@ void __libc_free(void *p); static struct { volatile uint64_t binmap; - struct bin bins[64]; + struct bin bins[BINS_COUNT]; volatile int free_lock[2]; +#ifdef MUSL_ITERATE_AND_STATS_API + occupied_bin_t occupied_bins[OCCUPIED_BIN_COUNT]; +#endif #ifdef MALLOC_FREELIST_QUARANTINE struct bin quarantine[QUARANTINE_NUM]; size_t quarantined_count[QUARANTINE_NUM]; size_t quarantined_size[QUARANTINE_NUM]; #ifdef MALLOC_RED_ZONE - char poison[64]; + char poison[BINS_COUNT]; volatile int poison_lock[2]; int poison_count_down; #endif @@ -47,6 +49,45 @@ static struct { int __malloc_replaced; +#ifdef MUSL_ITERATE_AND_STATS_API + +/* Usable memory only, excluding overhead for chunks */ +size_t total_heap_space = 0; +volatile int total_heap_space_inc_lock[2]; + + +size_t __get_total_heap_space(void) { + return total_heap_space; +} + +static void occupied_bin_destructor(void *occupied_bin) +{ + internal_free(occupied_bin); +} + +occupied_bin_t *__get_occupied_bin_by_idx(size_t bin_index) +{ + return &mal.occupied_bins[bin_index]; +} + +static inline size_t get_occupied_bin_index(int thread_id) +{ + return (size_t) ((size_t)thread_id % OCCUPIED_BIN_COUNT); +} + +occupied_bin_t *__get_occupied_bin(struct chunk *c) +{ + size_t bin_index = get_occupied_bin_index(c->thread_id); + return __get_occupied_bin_by_idx(bin_index); +} + +occupied_bin_t *__get_current_occupied_bin() +{ + size_t bin_index = get_occupied_bin_index(__pthread_self()->tid); + return &mal.occupied_bins[bin_index]; +} +#endif + /* Synchronization tools */ static inline struct chunk *encode_chunk(struct chunk *ptr, void *key) @@ -73,6 +114,117 @@ static inline void unlock(volatile int *lk) } } +#ifdef MUSL_ITERATE_AND_STATS_API +void __push_chunk(struct chunk *c) +{ + c->prev_occupied = c->next_occupied = NULL; + c->thread_id = 0; + + occupied_bin_t *occupied_bin = __get_current_occupied_bin(); + lock(occupied_bin->lock); + + if (occupied_bin->head != NULL) { + occupied_bin->head->prev_occupied = c; + c->next_occupied = occupied_bin->head; + } else { + occupied_bin->tail = c; + } + occupied_bin->head = c; + c->thread_id = __pthread_self()->tid; + + unlock(occupied_bin->lock); +} + +void __pop_chunk(struct chunk *c) +{ + if (!c->thread_id) { + return; + } + occupied_bin_t *occupied_bin = __get_occupied_bin(c); + lock(occupied_bin->lock); + + if (c == occupied_bin->head) { + occupied_bin->head = c->next_occupied; + } else { + c->prev_occupied->next_occupied = c->next_occupied; + } + if (c == occupied_bin->tail) { + occupied_bin->tail = c->prev_occupied; + } else { + c->next_occupied->prev_occupied = c->prev_occupied; + } + unlock(occupied_bin->lock); +} +#endif + +void malloc_disable(void) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + lock(mal.free_lock); + lock(total_heap_space_inc_lock); + for (size_t i = 0; i < BINS_COUNT; ++i) { + lock(mal.bins[i].lock); + } + for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) { + lock(mal.occupied_bins[i].lock); + } +#endif +} + +void malloc_enable(void) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) { + unlock(mal.occupied_bins[i].lock); + } + for (size_t i = 0; i < BINS_COUNT; ++i) { + unlock(mal.bins[i].lock); + } + unlock(total_heap_space_inc_lock); + unlock(mal.free_lock); +#endif +} + +#ifdef MUSL_ITERATE_AND_STATS_API +typedef struct iterate_info_s { + uintptr_t start_ptr; + uintptr_t end_ptr; + malloc_iterate_callback callback; + void *arg; +} iterate_info_t; + +static void malloc_iterate_visitor(void *block, size_t block_size, void *arg) +{ + iterate_info_t *iterate_info = (iterate_info_t *)arg; + if ((uintptr_t)block >= iterate_info->start_ptr && (uintptr_t)block < iterate_info->end_ptr) { + iterate_info->callback(block, block_size, iterate_info->arg); + } +} + +static void malloc_iterate_occupied_bin(occupied_bin_t *occupied_bin, iterate_info_t *iterate_info) +{ + for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) { + malloc_iterate_visitor(CHUNK_TO_MEM(c), CHUNK_SIZE(c) - OVERHEAD, iterate_info); + } +} +#endif + +int malloc_iterate(void* base, size_t size, void (*callback)(void* base, size_t size, void* arg), void* arg) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + uintptr_t ptr = (uintptr_t)base; + uintptr_t end_ptr = ptr + size; + iterate_info_t iterate_info = {ptr, end_ptr, callback, arg}; + + for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) { + occupied_bin_t *occupied_bin = &mal.occupied_bins[i]; + malloc_iterate_occupied_bin(occupied_bin, &iterate_info); + } + +#endif + return 0; +} + static inline void lock_bin(int i) { lock(mal.bins[i].lock); @@ -236,7 +388,7 @@ void __dump_heap(int x) c, CHUNK_SIZE(c), bin_index(CHUNK_SIZE(c)), c->csize & 15, NEXT_CHUNK(c)->psize & 15); - for (i=0; i<64; i++) { + for (i=0; istate &= ~M_RZ_POISON; } chunk_checksum_set(c); +#endif +#ifdef MUSL_ITERATE_AND_STATS_API + __push_chunk(c); #endif return CHUNK_TO_MEM(c); } @@ -657,10 +827,16 @@ void *realloc(void *p, size_t n) } newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; if (oldlen == newlen) return p; +#ifndef MUSL_ITERATE_AND_STATS_API base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); +#else + base = __mremap(base, oldlen, newlen, 0); +#endif if (base == (void *)-1) goto copy_realloc; +#ifndef MUSL_ITERATE_AND_STATS_API self = (void *)(base + extra); +#endif self->csize = newlen - extra; #ifdef MALLOC_RED_ZONE self->usize = user_size; @@ -1072,6 +1248,9 @@ void internal_free(void *p) if (!p) return; struct chunk *self = MEM_TO_CHUNK(p); +#ifdef MUSL_ITERATE_AND_STATS_API + __pop_chunk(self); +#endif #ifdef MALLOC_RED_ZONE /* This is not a valid chunk for freeing */ @@ -1112,7 +1291,24 @@ void __malloc_donate(char *start, char *end) c->usize = POINTER_USAGE; c->state = M_STATE_BRK; chunk_checksum_set(c); +#endif +#ifdef MUSL_ITERATE_AND_STATS_API + lock(total_heap_space_inc_lock); + total_heap_space += CHUNK_SIZE(c) - OVERHEAD; #endif __bin_chunk(c); +#ifdef MUSL_ITERATE_AND_STATS_API + unlock(total_heap_space_inc_lock); +#endif #endif } + +int mallopt(int param, int value) +{ + return 0; +} + +ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) +{ + return 0; +} diff --git a/porting/linux/user/src/malloc/memalign.c b/porting/linux/user/src/malloc/memalign.c index 7327ab4b8e07951df839cc57fb37af4e2fe0baaa..2e888c538a823d24f228fdfa003ce9024d8f483d 100644 --- a/porting/linux/user/src/malloc/memalign.c +++ b/porting/linux/user/src/malloc/memalign.c @@ -38,6 +38,11 @@ void *__memalign(size_t align, size_t len) struct chunk *c = MEM_TO_CHUNK(mem); struct chunk *n = MEM_TO_CHUNK(new); +#ifdef MUSL_ITERATE_AND_STATS_API + __pop_chunk(c); + __push_chunk(n); +#endif + if (IS_MMAPPED(c)) { /* Apply difference between aligned and original * address to the "extra" field of mmapped chunk. diff --git a/porting/linux/user/src/malloc/stats.c b/porting/linux/user/src/malloc/stats.c new file mode 100644 index 0000000000000000000000000000000000000000..c113c10d1cf2e3acd7767c6a8fb84b9ceb12146a --- /dev/null +++ b/porting/linux/user/src/malloc/stats.c @@ -0,0 +1,284 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "pthread_impl.h" +#include "malloc_impl.h" + +#ifdef MUSL_ITERATE_AND_STATS_API +#define STAT_PRINTF_MAX_LEN 255 +#define ALLOCATOR_VERSION 1 +#define SEPARATOR_REPEATS 7 + +typedef void (write_cb_fun)(void *, const char *); + +typedef enum { + TABLE, XML +} print_mode; + +typedef struct { + size_t mmapped_regions; + size_t total_mmapped_memory; + size_t total_allocated_memory; + size_t total_allocated_heap_space; +} malloc_stats_t; + +static void stat_printf(write_cb_fun *write_cb, void *write_cb_arg, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + char buf[STAT_PRINTF_MAX_LEN + 1]; + if (vsnprintf(buf, STAT_PRINTF_MAX_LEN, fmt, args)) { + if (write_cb != NULL) { + write_cb(write_cb_arg, buf); + } else { + printf(buf); + } + } else { + fprintf(stderr, "Error writing to buffer"); + } + va_end(args); +} + +static void print_thread_stats_table( + write_cb_fun *write_cb, + void *write_cb_arg, + int tid, + malloc_stats_t *stats +) +{ + stat_printf( + write_cb, + write_cb_arg, + "%-11d %-23zu %-20zu %-20zu\n", + tid, + stats->total_allocated_memory, + stats->total_mmapped_memory, + stats->mmapped_regions + ); +} + +static void print_amount_xml(write_cb_fun *write_cb, void *write_cb_arg, const char *name, size_t value) +{ + stat_printf(write_cb, write_cb_arg, "<%s>%zu\n", name, value, name); +} + +static void print_thread_specific_amounts_xml(write_cb_fun *write_cb, void *write_cb_arg, malloc_stats_t *stats) +{ + print_amount_xml(write_cb, write_cb_arg, "total_allocated_memory", stats->total_allocated_memory); + print_amount_xml(write_cb, write_cb_arg, "total_mmapped_memory", stats->total_mmapped_memory); + print_amount_xml(write_cb, write_cb_arg, "mmapped_regions", stats->mmapped_regions); +} + +static void print_thread_stats_xml( + write_cb_fun *write_cb, + void *write_cb_arg, + int tid, + malloc_stats_t *stats +) +{ + stat_printf(write_cb, write_cb_arg, "\n", tid); + print_thread_specific_amounts_xml(write_cb, write_cb_arg, stats); + stat_printf(write_cb, write_cb_arg, "\n"); +} + +static malloc_stats_t add_up_chunks(occupied_bin_t *occupied_bin) +{ + malloc_stats_t stats = {0, 0, 0, 0}; + for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) { + size_t chunk_memory = CHUNK_SIZE(c) - OVERHEAD; + stats.total_allocated_memory += chunk_memory; + if (IS_MMAPPED(c)) { + stats.mmapped_regions++; + stats.total_mmapped_memory += chunk_memory; + } else { + stats.total_allocated_heap_space += chunk_memory; + } + } + return stats; +} + +static malloc_stats_t add_up_chunks_by_threads(occupied_bin_t *occupied_bin, int tid) +{ + malloc_stats_t stats = {0, 0, 0, 0}; + for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) { + if (c->thread_id == tid) { + size_t chunk_memory = CHUNK_SIZE(c) - OVERHEAD; + stats.total_allocated_memory += chunk_memory; + if (IS_MMAPPED(c)) { + stats.mmapped_regions++; + stats.total_mmapped_memory += chunk_memory; + } else { + stats.total_allocated_heap_space += chunk_memory; + } + } + } + return stats; +} + +static size_t print_threads(write_cb_fun *write_cb, void *write_cb_arg, print_mode mode) +{ + size_t total_allocated_heap_space = 0; + + for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) { + occupied_bin_t *occupied_bin = __get_occupied_bin_by_idx(i); + int min_id = 0; + int found; + do { + found = 0; + for (struct chunk *c = occupied_bin->head; c != NULL; c = c->next_occupied) { + if (c->thread_id > min_id) { + min_id = c->thread_id; + found = 1; + } + } + if (found) { + malloc_stats_t stats = add_up_chunks_by_threads(occupied_bin, min_id); + total_allocated_heap_space += stats.total_allocated_heap_space; + + if (mode == TABLE) { + print_thread_stats_table(write_cb, write_cb_arg, min_id, &stats); + } else { + print_thread_stats_xml(write_cb, write_cb_arg, min_id, &stats); + } + } + } while (found); + } + + return total_allocated_heap_space; +} + +static void print_total_free_heap_space( + write_cb_fun *write_cb, + void *write_cb_arg, + size_t total_allocated_heap_space, + print_mode mode +) +{ + if (mode == TABLE) { + stat_printf(write_cb, write_cb_arg, "\n"); + for (size_t i = 0; i < SEPARATOR_REPEATS; i++) { + stat_printf( + write_cb, + write_cb_arg, + "-----------" + ); + } + stat_printf( + write_cb, + write_cb_arg, + "\ntotal free heap space: %zu\n", + __get_total_heap_space() - total_allocated_heap_space + ); + } else { + print_amount_xml( + write_cb, + write_cb_arg, + "total_free_heap_space", + __get_total_heap_space() - total_allocated_heap_space + ); + } +} + +static void print_to_file(void *fp, const char *s) +{ + if (fputs(s, fp) == EOF) { + fprintf(stderr, "Error writing to file stream: %s", strerror(errno)); + } +} + +static void add_stats(malloc_stats_t *destination, const malloc_stats_t *source) +{ + destination->total_allocated_memory += source->total_allocated_memory; + destination->total_mmapped_memory += source->total_mmapped_memory; + destination->mmapped_regions += source->mmapped_regions; + destination->total_allocated_heap_space += source->total_allocated_heap_space; +} +#endif + +int malloc_info(int options, FILE* fp) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + if (options != 0) { + errno = EINVAL; + return -1; + } + malloc_disable(); + stat_printf(print_to_file, fp, "\n"); + stat_printf(print_to_file, fp, "\n", ALLOCATOR_VERSION); + stat_printf(print_to_file, fp, "\n"); + size_t total_allocated_heap_space = print_threads(print_to_file, fp, XML); + stat_printf(print_to_file, fp, "\n"); + print_total_free_heap_space(print_to_file, fp, total_allocated_heap_space, XML); + stat_printf(print_to_file, fp, "\n"); + malloc_enable(); +#endif + return 0; +} + +void malloc_stats_print(void (*write_cb) (void *, const char *), void *cbopaque, const char *opts) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + malloc_disable(); + stat_printf( + write_cb, + cbopaque, + "%-11s %-23s %-20s %-20s\n", + "thread_id", + "total_allocated_memory", + "total_mmapped_memory", + "mmapped_regions" + ); + size_t total_allocated_heap_space = print_threads(write_cb, cbopaque, TABLE); + print_total_free_heap_space(write_cb, cbopaque, total_allocated_heap_space, TABLE); + malloc_enable(); +#endif +} + +struct mallinfo2 mallinfo2(void) +{ +#ifdef MUSL_ITERATE_AND_STATS_API + malloc_disable(); + malloc_stats_t shared_stats = {0, 0, 0, 0}; + for (size_t i = 0; i < OCCUPIED_BIN_COUNT; ++i) { + malloc_stats_t stats = add_up_chunks(__get_occupied_bin_by_idx(i)); + add_stats(&shared_stats, &stats); + } + + struct mallinfo2 res = { + .hblks = shared_stats.mmapped_regions, + .hblkhd = shared_stats.total_mmapped_memory, + .uordblks = shared_stats.total_allocated_memory, + .fordblks = __get_total_heap_space() - shared_stats.total_allocated_heap_space + }; + malloc_enable(); + return res; +#endif + return (struct mallinfo2){}; +} + +struct mallinfo mallinfo(void) +{ + struct mallinfo2 mallinfo2_res = mallinfo2(); + return (struct mallinfo) { + .hblks = mallinfo2_res.hblks, + .hblkhd = mallinfo2_res.hblkhd, + .uordblks = mallinfo2_res.uordblks, + .fordblks = mallinfo2_res.fordblks, + }; +}