From 66c94b7d9b9120db1607e3fb58fdbea8d0231afd Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Fri, 11 Mar 2016 00:12:22 +0100 Subject: [PATCH] Refactor the printing of memory usage statistics Make the internal heap and pools memory usage statistics APIs more similar: how the print functions are named, where they are implemented, and which parts of them are guarded by `MEM_STATS`. Also, adapt unit tests to the changes. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu --- jerry-core/mem/mem-allocator.c | 21 ++---------- jerry-core/mem/mem-heap.c | 58 +++++++++++++++++----------------- jerry-core/mem/mem-heap.h | 1 - jerry-core/mem/mem-poolman.c | 20 ++++++++++++ jerry-core/mem/mem-poolman.h | 1 + tests/unit/test-heap.c | 10 ++++-- tests/unit/test-poolman.c | 19 ++--------- 7 files changed, 62 insertions(+), 68 deletions(-) diff --git a/jerry-core/mem/mem-allocator.c b/jerry-core/mem/mem-allocator.c index 22eac93ff6..5278a3c903 100644 --- a/jerry-core/mem/mem-allocator.c +++ b/jerry-core/mem/mem-allocator.c @@ -1,4 +1,4 @@ -/* Copyright 2014-2015 Samsung Electronics Co., Ltd. +/* Copyright 2014-2016 Samsung Electronics Co., Ltd. * Copyright 2016 University of Szeged. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -144,22 +144,7 @@ mem_stats_reset_peak (void) void mem_stats_print (void) { - mem_heap_print (); - - mem_pools_stats_t stats; - mem_pools_get_stats (&stats); - - printf ("Pools stats:\n"); - printf (" Chunk size: %zu\n" - " Pool chunks: %zu\n" - " Peak pool chunks: %zu\n" - " Free chunks: %zu\n" - " Pool reuse ratio: %zu.%04zu\n", - MEM_POOL_CHUNK_SIZE, - stats.pools_count, - stats.peak_pools_count, - stats.free_chunks, - stats.reused_count / stats.new_alloc_count, - stats.reused_count % stats.new_alloc_count * 10000 / stats.new_alloc_count); + mem_heap_stats_print (); + mem_pools_stats_print (); } /* mem_stats_print */ #endif /* MEM_STATS */ diff --git a/jerry-core/mem/mem-heap.c b/jerry-core/mem/mem-heap.c index db651c97cc..0d2b907bfc 100644 --- a/jerry-core/mem/mem-heap.c +++ b/jerry-core/mem/mem-heap.c @@ -607,22 +607,44 @@ mem_is_heap_pointer (const void *pointer) /**< pointer */ } /* mem_is_heap_pointer */ #endif /* !JERRY_NDEBUG */ +#ifdef MEM_STATS /** - * Print heap + * Get heap memory usage statistics */ void -mem_heap_print () +mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p) /**< [out] heap stats */ { -#ifdef MEM_STATS - printf ("Heap stats:\n"); - printf (" Heap size = %zu bytes\n" + JERRY_ASSERT (out_heap_stats_p != NULL); + + *out_heap_stats_p = mem_heap_stats; +} /* mem_heap_get_stats */ + +/** + * Reset peak values in memory usage statistics + */ +void +mem_heap_stats_reset_peak (void) +{ + mem_heap_stats.peak_allocated_bytes = mem_heap_stats.allocated_bytes; + mem_heap_stats.peak_waste_bytes = mem_heap_stats.waste_bytes; +} /* mem_heap_stats_reset_peak */ + +/** + * Print heap memory usage statistics + */ +void +mem_heap_stats_print (void) +{ + printf ("Heap stats:\n" + " Heap size = %zu bytes\n" " Allocated = %zu bytes\n" " Waste = %zu bytes\n" " Peak allocated = %zu bytes\n" " Peak waste = %zu bytes\n" " Skip-ahead ratio = %zu.%04zu\n" " Average alloc iteration = %zu.%04zu\n" - " Average free iteration = %zu.%04zu\n", + " Average free iteration = %zu.%04zu\n" + "\n", mem_heap_stats.size, mem_heap_stats.allocated_bytes, mem_heap_stats.waste_bytes, @@ -634,29 +656,7 @@ mem_heap_print () mem_heap_stats.alloc_iter_count % mem_heap_stats.alloc_count * 10000 / mem_heap_stats.alloc_count, mem_heap_stats.free_iter_count / mem_heap_stats.free_count, mem_heap_stats.free_iter_count % mem_heap_stats.free_count * 10000 / mem_heap_stats.free_count); - printf ("\n"); -#endif /* !MEM_STATS */ -} /* mem_heap_print */ - -#ifdef MEM_STATS -/** - * Get heap memory usage statistics - */ -void -mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p) /**< [out] heap stats */ -{ - *out_heap_stats_p = mem_heap_stats; -} /* mem_heap_get_stats */ - -/** - * Reset peak values in memory usage statistics - */ -void -mem_heap_stats_reset_peak (void) -{ - mem_heap_stats.peak_allocated_bytes = mem_heap_stats.allocated_bytes; - mem_heap_stats.peak_waste_bytes = mem_heap_stats.waste_bytes; -} /* mem_heap_stats_reset_peak */ +} /* mem_heap_stats_print */ /** * Initalize heap memory usage statistics account structure diff --git a/jerry-core/mem/mem-heap.h b/jerry-core/mem/mem-heap.h index 4b820d6822..e87c3183fe 100644 --- a/jerry-core/mem/mem-heap.h +++ b/jerry-core/mem/mem-heap.h @@ -39,7 +39,6 @@ extern void mem_heap_free_block_size_stored (void *); extern uintptr_t mem_heap_compress_pointer (const void *); extern void *mem_heap_decompress_pointer (uintptr_t); extern bool mem_is_heap_pointer (const void *); -extern void mem_heap_print (); #ifdef MEM_STATS /** diff --git a/jerry-core/mem/mem-poolman.c b/jerry-core/mem/mem-poolman.c index 0a66cda441..6a9b05d68d 100644 --- a/jerry-core/mem/mem-poolman.c +++ b/jerry-core/mem/mem-poolman.c @@ -217,6 +217,26 @@ mem_pools_stats_reset_peak (void) mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count; } /* mem_pools_stats_reset_peak */ +/** + * Print pools memory usage statistics + */ +void +mem_pools_stats_print (void) +{ + printf ("Pools stats:\n" + " Chunk size: %zu\n" + " Pool chunks: %zu\n" + " Peak pool chunks: %zu\n" + " Free chunks: %zu\n" + " Pool reuse ratio: %zu.%04zu\n", + MEM_POOL_CHUNK_SIZE, + mem_pools_stats.pools_count, + mem_pools_stats.peak_pools_count, + mem_pools_stats.free_chunks, + mem_pools_stats.reused_count / mem_pools_stats.new_alloc_count, + mem_pools_stats.reused_count % mem_pools_stats.new_alloc_count * 10000 / mem_pools_stats.new_alloc_count); +} /* mem_pools_stats_print */ + /** * Initalize pools' memory usage statistics account structure */ diff --git a/jerry-core/mem/mem-poolman.h b/jerry-core/mem/mem-poolman.h index 35a5b14bd0..2ee8d7c7ef 100644 --- a/jerry-core/mem/mem-poolman.h +++ b/jerry-core/mem/mem-poolman.h @@ -63,6 +63,7 @@ typedef struct extern void mem_pools_get_stats (mem_pools_stats_t *); extern void mem_pools_stats_reset_peak (void); +extern void mem_pools_stats_print (void); #endif /* MEM_STATS */ #endif /* !MEM_POOLMAN_H */ diff --git a/tests/unit/test-heap.c b/tests/unit/test-heap.c index f08ddfce01..710ee59b08 100644 --- a/tests/unit/test-heap.c +++ b/tests/unit/test-heap.c @@ -1,4 +1,4 @@ -/* Copyright 2014-2015 Samsung Electronics Co., Ltd. +/* Copyright 2014-2016 Samsung Electronics Co., Ltd. * Copyright 2016 University of Szeged. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -78,7 +78,9 @@ main (int __attr_unused___ argc, mem_register_a_try_give_memory_back_callback (test_heap_give_some_memory_back); - mem_heap_print (); +#ifdef MEM_STATS + mem_heap_stats_print (); +#endif /* MEM_STATS */ for (uint32_t i = 0; i < test_iters; i++) { @@ -110,7 +112,9 @@ main (int __attr_unused___ argc, } } - mem_heap_print (); +#ifdef MEM_STATS + mem_heap_stats_print (); +#endif /* MEM_STATS */ return 0; } /* main */ diff --git a/tests/unit/test-poolman.c b/tests/unit/test-poolman.c index 0a9ffa8c4a..6a6045113b 100644 --- a/tests/unit/test-poolman.c +++ b/tests/unit/test-poolman.c @@ -1,4 +1,4 @@ -/* Copyright 2014-2015 Samsung Electronics Co., Ltd. +/* Copyright 2014-2016 Samsung Electronics Co., Ltd. * Copyright 2016 University of Szeged. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -80,22 +80,7 @@ main (int __attr_unused___ argc, } #ifdef MEM_STATS - mem_pools_stats_t stats; - mem_pools_get_stats (&stats); - - printf ("Pools stats:\n"); - printf (" Chunk size: %u\n" - " Pools: %zu\n" - " Allocated chunks: %zu\n" - " Free chunks: %zu\n" - " Peak pools: %zu\n" - " Peak allocated chunks: %zu\n\n", - MEM_POOL_CHUNK_SIZE, - stats.pools_count, - stats.allocated_chunks, - stats.free_chunks, - stats.peak_pools_count, - stats.peak_allocated_chunks); + mem_pools_stats_print (); #endif /* MEM_STATS */ mem_finalize (false);