From 9b256b9bc9d4cc80af4097565c2ba7c46172f8a0 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Fri, 24 Apr 2015 15:56:31 +0300 Subject: [PATCH] Renaming --mem-stats-at-exit option to --mem-stats; adding --mem-stats-separate option for dumping memory statistics and resetting memory usage peak values after parse. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan r.ayrapetyan@samsung.com --- jerry-core/jerry.cpp | 32 ++++++++++++++++++++++----- jerry-core/jerry.h | 15 +++++++------ jerry-core/mem/mem-allocator.cpp | 38 ++++++++++++++++++++++++++++++++ jerry-core/mem/mem-allocator.h | 5 +++++ main-linux.cpp | 8 +++++-- main-nuttx.cpp | 8 +++++-- 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/jerry-core/jerry.cpp b/jerry-core/jerry.cpp index 8ece823a8..ccfee573a 100644 --- a/jerry-core/jerry.cpp +++ b/jerry-core/jerry.cpp @@ -27,6 +27,8 @@ #include "ecma-objects-general.h" #include "jerry.h" #include "jrt.h" +#include "mem-heap.h" +#include "mem-poolman.h" #include "parser.h" #include "serializer.h" #include "vm.h" @@ -901,14 +903,24 @@ jerry_api_eval (const char *source_p, /**< source code */ void jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */ { - jerry_flags = flags; - -#ifndef MEM_STATS - if (flags & (JERRY_FLAG_MEM_STATS_AT_EXIT | JERRY_FLAG_MEM_STATS_PER_OPCODE)) + if (flags & (JERRY_FLAG_MEM_STATS)) { +#ifndef MEM_STATS + flags &= ~(JERRY_FLAG_MEM_STATS + | JERRY_FLAG_MEM_STATS_PER_OPCODE + | JERRY_FLAG_MEM_STATS_SEPARATE); + printf ("Ignoring memory statistics option because of '!MEM_STATS' build configuration.\n"); - } #endif /* !MEM_STATS */ + } + else if (flags & (JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE)) + { + flags &= ~(JERRY_FLAG_MEM_STATS_PER_OPCODE | JERRY_FLAG_MEM_STATS_SEPARATE); + + printf ("Ignoring detailed memory statistics options because memory statistics dump mode is not enabled.\n"); + } + + jerry_flags = flags; mem_init (); serializer_init (); @@ -921,7 +933,7 @@ jerry_init (jerry_flag_t flags) /**< combination of Jerry flags */ void jerry_cleanup (void) { - bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS_AT_EXIT) != 0); + bool is_show_mem_stats = ((jerry_flags & JERRY_FLAG_MEM_STATS) != 0); ecma_finalize (); serializer_free (); @@ -966,6 +978,14 @@ jerry_parse (const char* source_p, /**< script source */ serializer_print_opcodes (); parser_free (); +#ifdef MEM_STATS + if (jerry_flags & JERRY_FLAG_MEM_STATS_SEPARATE) + { + mem_stats_print (); + mem_stats_reset_peak (); + } +#endif /* MEM_STATS */ + bool is_show_mem_stats_per_opcode = ((jerry_flags & JERRY_FLAG_MEM_STATS_PER_OPCODE) != 0); init_int (opcodes, is_show_mem_stats_per_opcode); diff --git a/jerry-core/jerry.h b/jerry-core/jerry.h index 7df750a31..45c60e5e5 100644 --- a/jerry-core/jerry.h +++ b/jerry-core/jerry.h @@ -31,13 +31,14 @@ */ typedef uint32_t jerry_flag_t; -#define JERRY_FLAG_EMPTY (0) /**< empty flag set */ -#define JERRY_FLAG_SHOW_OPCODES (1 << 0) /**< dump opcodes to stdout after parse */ -#define JERRY_FLAG_MEM_STATS_AT_EXIT (1 << 1) /**< dump per-opcode memory statistics during execution - * (in the mode full GC is performed after each opcode handler) */ -#define JERRY_FLAG_MEM_STATS_PER_OPCODE (1 << 2) /**< dump peak memory statistics before exit */ -#define JERRY_FLAG_PARSE_ONLY (1 << 3) /**< parse only, prevents script execution (only for testing) - * FIXME: Remove. */ +#define JERRY_FLAG_EMPTY (0u) /**< empty flag set */ +#define JERRY_FLAG_SHOW_OPCODES (1u << 0) /**< dump opcodes to stdout after parse */ +#define JERRY_FLAG_MEM_STATS (1u << 1) /**< dump memory statistics */ +#define JERRY_FLAG_MEM_STATS_PER_OPCODE (1u << 2) /**< dump per-opcode memory statistics during execution + * (in the mode full GC is performed after each opcode handler) */ +#define JERRY_FLAG_MEM_STATS_SEPARATE (1u << 3) /**< dump memory statistics and reset peak values after parse */ +#define JERRY_FLAG_PARSE_ONLY (1u << 4) /**< parse only, prevents script execution (only for testing) + * FIXME: Remove. */ /** * Error codes diff --git a/jerry-core/mem/mem-allocator.cpp b/jerry-core/mem/mem-allocator.cpp index b1f650dc1..0adb83a1b 100644 --- a/jerry-core/mem/mem-allocator.cpp +++ b/jerry-core/mem/mem-allocator.cpp @@ -185,3 +185,41 @@ mem_is_heap_pointer (void *pointer) /**< pointer */ return (uint8_pointer >= mem_heap_area && uint8_pointer <= (mem_heap_area + MEM_HEAP_AREA_SIZE)); } /* mem_is_heap_pointer */ #endif /* !JERRY_NDEBUG */ + +#ifdef MEM_STATS +/** + * Reset peak values in memory usage statistics + */ +void +mem_stats_reset_peak (void) +{ + mem_heap_stats_reset_peak (); + mem_pools_stats_reset_peak (); +} /* mem_stats_reset_peak */ + +/** + * Print memory usage statistics + */ +void +mem_stats_print (void) +{ + mem_heap_print (false, false, true); + + mem_pools_stats_t stats; + mem_pools_get_stats (&stats); + + printf ("Pools stats:\n"); + printf (" Chunk size: %zu\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_stats_print */ +#endif /* MEM_STATS */ diff --git a/jerry-core/mem/mem-allocator.h b/jerry-core/mem/mem-allocator.h index cb872ca0a..a7e45d4a7 100644 --- a/jerry-core/mem/mem-allocator.h +++ b/jerry-core/mem/mem-allocator.h @@ -93,6 +93,11 @@ extern void mem_unregister_a_try_give_memory_back_callback (mem_try_give_memory_ extern bool mem_is_heap_pointer (void *pointer); #endif /* !JERRY_NDEBUG */ +#ifdef MEM_STATS +extern void mem_stats_reset_peak (void); +extern void mem_stats_print (void); +#endif /* MEM_STATS */ + #endif /* !JERRY_MEM_ALLOCATOR_H */ /** diff --git a/main-linux.cpp b/main-linux.cpp index de0a2f478..26430470f 100644 --- a/main-linux.cpp +++ b/main-linux.cpp @@ -139,14 +139,18 @@ main (int argc, printf ("Branch name:\t%s\n", jerry_branch_name); printf ("\n"); } - else if (!strcmp ("--mem-stats-at-exit", argv[i])) + else if (!strcmp ("--mem-stats", argv[i])) { - flags |= JERRY_FLAG_MEM_STATS_AT_EXIT; + flags |= JERRY_FLAG_MEM_STATS; } else if (!strcmp ("--mem-stats-per-opcode", argv[i])) { flags |= JERRY_FLAG_MEM_STATS_PER_OPCODE; } + else if (!strcmp ("--mem-stats-separate", argv[i])) + { + flags |= JERRY_FLAG_MEM_STATS_SEPARATE; + } else if (!strcmp ("--parse-only", argv[i])) { flags |= JERRY_FLAG_PARSE_ONLY; diff --git a/main-nuttx.cpp b/main-nuttx.cpp index 9e2dc342a..bfc0661b1 100644 --- a/main-nuttx.cpp +++ b/main-nuttx.cpp @@ -146,14 +146,18 @@ int jerry_main (int argc, char *argv[]) printf ("Commit hash:\t%s\n", jerry_commit_hash); printf ("Branch name:\t%s\n", jerry_branch_name); } - else if (!strcmp ("--mem-stats-at-exit", argv[i])) + else if (!strcmp ("--mem-stats", argv[i])) { - flags |= JERRY_FLAG_MEM_STATS_AT_EXIT; + flags |= JERRY_FLAG_MEM_STATS; } else if (!strcmp ("--mem-stats-per-opcode", argv[i])) { flags |= JERRY_FLAG_MEM_STATS_PER_OPCODE; } + else if (!strcmp ("--mem-stats-separate", argv[i])) + { + flags |= JERRY_FLAG_MEM_STATS_SEPARATE; + } else if (!strcmp ("--parse-only", argv[i])) { flags |= JERRY_FLAG_PARSE_ONLY;