Skip to content

Commit e20f74e

Browse files
committed
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 [email protected]
1 parent 6e687fa commit e20f74e

File tree

7 files changed

+58
-64
lines changed

7 files changed

+58
-64
lines changed

jerry-core/mem/mem-allocator.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
22
* Copyright 2016 University of Szeged.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -144,22 +144,7 @@ mem_stats_reset_peak (void)
144144
void
145145
mem_stats_print (void)
146146
{
147-
mem_heap_print ();
148-
149-
mem_pools_stats_t stats;
150-
mem_pools_get_stats (&stats);
151-
152-
printf ("Pools stats:\n");
153-
printf (" Chunk size: %zu\n"
154-
" Pool chunks: %zu\n"
155-
" Peak pool chunks: %zu\n"
156-
" Free chunks: %zu\n"
157-
" Pool reuse ratio: %zu.%04zu\n",
158-
MEM_POOL_CHUNK_SIZE,
159-
stats.pools_count,
160-
stats.peak_pools_count,
161-
stats.free_chunks,
162-
stats.reused_count / stats.new_alloc_count,
163-
stats.reused_count % stats.new_alloc_count * 10000 / stats.new_alloc_count);
147+
mem_heap_stats_print ();
148+
mem_pools_stats_print ();
164149
} /* mem_stats_print */
165150
#endif /* MEM_STATS */

jerry-core/mem/mem-heap.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,34 @@ mem_is_heap_pointer (const void *pointer) /**< pointer */
607607
} /* mem_is_heap_pointer */
608608
#endif /* !JERRY_NDEBUG */
609609

610+
#ifdef MEM_STATS
610611
/**
611-
* Print heap
612+
* Get heap memory usage statistics
612613
*/
613614
void
614-
mem_heap_print ()
615+
mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p) /**< [out] heap stats */
616+
{
617+
JERRY_ASSERT (out_heap_stats_p != NULL);
618+
619+
*out_heap_stats_p = mem_heap_stats;
620+
} /* mem_heap_get_stats */
621+
622+
/**
623+
* Reset peak values in memory usage statistics
624+
*/
625+
void
626+
mem_heap_stats_reset_peak (void)
627+
{
628+
mem_heap_stats.peak_allocated_bytes = mem_heap_stats.allocated_bytes;
629+
mem_heap_stats.peak_waste_bytes = mem_heap_stats.waste_bytes;
630+
} /* mem_heap_stats_reset_peak */
631+
632+
/**
633+
* Print heap memory usage statistics
634+
*/
635+
void
636+
mem_heap_stats_print (void)
615637
{
616-
#ifdef MEM_STATS
617638
printf ("Heap stats:\n");
618639
printf (" Heap size = %zu bytes\n"
619640
" Allocated = %zu bytes\n"
@@ -635,28 +656,7 @@ mem_heap_print ()
635656
mem_heap_stats.free_iter_count / mem_heap_stats.free_count,
636657
mem_heap_stats.free_iter_count % mem_heap_stats.free_count * 10000 / mem_heap_stats.free_count);
637658
printf ("\n");
638-
#endif /* !MEM_STATS */
639-
} /* mem_heap_print */
640-
641-
#ifdef MEM_STATS
642-
/**
643-
* Get heap memory usage statistics
644-
*/
645-
void
646-
mem_heap_get_stats (mem_heap_stats_t *out_heap_stats_p) /**< [out] heap stats */
647-
{
648-
*out_heap_stats_p = mem_heap_stats;
649-
} /* mem_heap_get_stats */
650-
651-
/**
652-
* Reset peak values in memory usage statistics
653-
*/
654-
void
655-
mem_heap_stats_reset_peak (void)
656-
{
657-
mem_heap_stats.peak_allocated_bytes = mem_heap_stats.allocated_bytes;
658-
mem_heap_stats.peak_waste_bytes = mem_heap_stats.waste_bytes;
659-
} /* mem_heap_stats_reset_peak */
659+
} /* mem_heap_stats_print */
660660

661661
/**
662662
* Initalize heap memory usage statistics account structure

jerry-core/mem/mem-heap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extern void mem_heap_free_block_size_stored (void *);
3939
extern uintptr_t mem_heap_compress_pointer (const void *);
4040
extern void *mem_heap_decompress_pointer (uintptr_t);
4141
extern bool mem_is_heap_pointer (const void *);
42-
extern void mem_heap_print ();
4342

4443
#ifdef MEM_STATS
4544
/**

jerry-core/mem/mem-poolman.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ mem_pools_stats_reset_peak (void)
217217
mem_pools_stats.peak_pools_count = mem_pools_stats.pools_count;
218218
} /* mem_pools_stats_reset_peak */
219219

220+
/**
221+
* Print pools memory usage statistics
222+
*/
223+
void
224+
mem_pools_stats_print (void)
225+
{
226+
printf ("Pools stats:\n");
227+
printf (" Chunk size: %zu\n"
228+
" Pool chunks: %zu\n"
229+
" Peak pool chunks: %zu\n"
230+
" Free chunks: %zu\n"
231+
" Pool reuse ratio: %zu.%04zu\n",
232+
MEM_POOL_CHUNK_SIZE,
233+
mem_pools_stats.pools_count,
234+
mem_pools_stats.peak_pools_count,
235+
mem_pools_stats.free_chunks,
236+
mem_pools_stats.reused_count / mem_pools_stats.new_alloc_count,
237+
mem_pools_stats.reused_count % mem_pools_stats.new_alloc_count * 10000 / mem_pools_stats.new_alloc_count);
238+
} /* mem_pools_stats_print */
239+
220240
/**
221241
* Initalize pools' memory usage statistics account structure
222242
*/

jerry-core/mem/mem-poolman.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct
6363

6464
extern void mem_pools_get_stats (mem_pools_stats_t *);
6565
extern void mem_pools_stats_reset_peak (void);
66+
extern void mem_pools_stats_print (void);
6667
#endif /* MEM_STATS */
6768

6869
#endif /* !MEM_POOLMAN_H */

tests/unit/test-heap.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
22
* Copyright 2016 University of Szeged.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -78,7 +78,9 @@ main (int __attr_unused___ argc,
7878

7979
mem_register_a_try_give_memory_back_callback (test_heap_give_some_memory_back);
8080

81-
mem_heap_print ();
81+
#ifdef MEM_STATS
82+
mem_heap_stats_print ();
83+
#endif /* MEM_STATS */
8284

8385
for (uint32_t i = 0; i < test_iters; i++)
8486
{
@@ -110,7 +112,9 @@ main (int __attr_unused___ argc,
110112
}
111113
}
112114

113-
mem_heap_print ();
115+
#ifdef MEM_STATS
116+
mem_heap_stats_print ();
117+
#endif /* MEM_STATS */
114118

115119
return 0;
116120
} /* main */

tests/unit/test-poolman.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
1+
/* Copyright 2014-2016 Samsung Electronics Co., Ltd.
22
* Copyright 2016 University of Szeged.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -80,22 +80,7 @@ main (int __attr_unused___ argc,
8080
}
8181

8282
#ifdef MEM_STATS
83-
mem_pools_stats_t stats;
84-
mem_pools_get_stats (&stats);
85-
86-
printf ("Pools stats:\n");
87-
printf (" Chunk size: %u\n"
88-
" Pools: %zu\n"
89-
" Allocated chunks: %zu\n"
90-
" Free chunks: %zu\n"
91-
" Peak pools: %zu\n"
92-
" Peak allocated chunks: %zu\n\n",
93-
MEM_POOL_CHUNK_SIZE,
94-
stats.pools_count,
95-
stats.allocated_chunks,
96-
stats.free_chunks,
97-
stats.peak_pools_count,
98-
stats.peak_allocated_chunks);
83+
mem_pools_stats_print ();
9984
#endif /* MEM_STATS */
10085

10186
mem_finalize (false);

0 commit comments

Comments
 (0)