From 56bd6d8791ce1284fed7f4b6e10d7377c8eff5b5 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 25 Oct 2022 14:56:59 +0200 Subject: [PATCH 1/7] Added code for analyzing allocated memory in MATLAB --- apps/decoder.c | 6 ++++++ apps/encoder.c | 3 +++ lib_com/options.h | 1 + lib_debug/mem_count.c | 24 ++++++++++++++++++++++++ lib_debug/mem_count.h | 4 ++++ mem_analysis.m | 30 ++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 mem_analysis.m diff --git a/apps/decoder.c b/apps/decoder.c index 968dc79ec2..0b4074319e 100644 --- a/apps/decoder.c +++ b/apps/decoder.c @@ -1480,6 +1480,9 @@ static ivas_error decodeG192( } #ifdef WMOPS update_wmops(); +#endif +#ifdef RAM_ANALYSIS + mem_analyze(); #endif } @@ -1836,6 +1839,9 @@ static ivas_error decodeVoIP( #ifdef WMOPS update_wmops(); +#endif +#ifdef RAM_ANALYSIS + mem_analyze(); #endif } diff --git a/apps/encoder.c b/apps/encoder.c index b1abc36d2f..4790019dcc 100644 --- a/apps/encoder.c +++ b/apps/encoder.c @@ -742,6 +742,9 @@ int main( #ifdef WMOPS update_wmops(); +#endif +#ifdef RAM_ANALYSIS + mem_analyze(); #endif } diff --git a/lib_com/options.h b/lib_com/options.h index b988fcb581..b17c271a86 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -42,6 +42,7 @@ #define SUPPORT_JBM_TRACEFILE /* support for JBM tracefile, which is needed for 3GPP objective/subjective testing, but not relevant for real-world implementations */ #define RAM_COUNTING_TOOL /* tool to automatically count the dynamically alocated static memory consumption */ +#define RAM_ANALYSIS /* #################### End compiler switches ######################### */ diff --git a/lib_debug/mem_count.c b/lib_debug/mem_count.c index b12639ae3f..5f0ef86d8b 100644 --- a/lib_debug/mem_count.c +++ b/lib_debug/mem_count.c @@ -178,6 +178,10 @@ static allocation_list Missing_Allocations; static unsigned int Num_Records_Missing_Alloc_Warnings = 0; #endif +#ifdef RAM_ANALYSIS +static FILE *mem_analysis_file = NULL; +#endif + /*-------------------------------------------------------------------* * LOCAL CONST DATA *-------------------------------------------------------------------*/ @@ -903,4 +907,24 @@ size_t mem_count_summary( Counting_Size cnt_size ) return size; } +#ifdef RAM_ANALYSIS +void mem_analyze() +{ + unsigned int i; + + if ( mem_analysis_file == NULL ) + { + mem_analysis_file = fopen( "mem_analysis.csv", "w" ); + } + for ( i = 0; i < Num_Records_Cur_RAM; i++ ) + { + fprintf( mem_analysis_file, "%s:%d,%d;", Current_Allocations[i].name, Current_Allocations[i].lineno, Current_Allocations[i].block_size ); + } + fprintf( mem_analysis_file, "\n" ); + + return; +} +#endif + + #endif diff --git a/lib_debug/mem_count.h b/lib_debug/mem_count.h index d30fc3ab1a..e196c799ec 100644 --- a/lib_debug/mem_count.h +++ b/lib_debug/mem_count.h @@ -95,6 +95,10 @@ extern "C" extern void *mem_alloc( const char *func_name, int func_lineno, size_t size, char *alloc_str ); extern void mem_free( const char *func_name, int func_lineno, void *ptr ); +#ifdef RAM_ANALYSIS + extern void mem_analyze(); +#endif + #ifdef __cplusplus } #endif diff --git a/mem_analysis.m b/mem_analysis.m new file mode 100644 index 0000000000..c31f7547ef --- /dev/null +++ b/mem_analysis.m @@ -0,0 +1,30 @@ +%% + +lines = readlines('mem_analysis.csv'); + +names = {}; +Nframes = length(lines); + +mem = zeros(1,Nframes); + +for i = 1:Nframes + entries = split(lines(i),';'); + for j = 1:length(entries) + a = split(entries(j),','); + if(length(a)==2) + name = a{1}; + num = str2double(a{2}); + I = find(strcmp(names,name)); + if isempty(I) + names{end+1} = name; + I = length(names); + end + mem(I,i) = num; + end + end +end + +bar(mem',1,'stacked') +legend(names,'location','eastoutside','interpreter','none') +xlabel('frame') +ylabel('memory size') \ No newline at end of file -- GitLab From 5864afcd7018729e91edd87fc48eba8b3409811c Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Wed, 2 Nov 2022 14:27:05 +0100 Subject: [PATCH 2/7] Minor fix using dbgwrite instead of fprintf and a file pointer --- lib_debug/mem_count.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib_debug/mem_count.c b/lib_debug/mem_count.c index 5f0ef86d8b..7bb3f2680b 100644 --- a/lib_debug/mem_count.c +++ b/lib_debug/mem_count.c @@ -49,6 +49,10 @@ typedef INT64 int64_t; #define FALSE 0 #endif +#ifdef RAM_ANALYSIS +#include "debug.h" +#endif + /* How to use the tool notes ========================= @@ -178,10 +182,6 @@ static allocation_list Missing_Allocations; static unsigned int Num_Records_Missing_Alloc_Warnings = 0; #endif -#ifdef RAM_ANALYSIS -static FILE *mem_analysis_file = NULL; -#endif - /*-------------------------------------------------------------------* * LOCAL CONST DATA *-------------------------------------------------------------------*/ @@ -911,16 +911,15 @@ size_t mem_count_summary( Counting_Size cnt_size ) void mem_analyze() { unsigned int i; + char buffer[1024]; - if ( mem_analysis_file == NULL ) - { - mem_analysis_file = fopen( "mem_analysis.csv", "w" ); - } for ( i = 0; i < Num_Records_Cur_RAM; i++ ) { - fprintf( mem_analysis_file, "%s:%d,%d;", Current_Allocations[i].name, Current_Allocations[i].lineno, Current_Allocations[i].block_size ); + sprintf( buffer, "%s:%d,%d;", Current_Allocations[i].name, Current_Allocations[i].lineno, Current_Allocations[i].block_size ); + dbgwrite( buffer, sizeof( char ), strlen( buffer ), 1, "mem_analysis.csv" ); } - fprintf( mem_analysis_file, "\n" ); + sprintf( buffer, "\n" ); + dbgwrite( buffer, sizeof( char ), strlen( buffer ), 1, "mem_analysis.csv" ); return; } -- GitLab From ccee19fac4c176fbd7e299c12447e0eb0ed3ce3d Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Wed, 2 Nov 2022 14:45:42 +0100 Subject: [PATCH 3/7] Reorder mem analysis matrix to put all constant buffers at the bottom. This emphasizes which buffers are actually constant --- mem_analysis.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mem_analysis.m b/mem_analysis.m index c31f7547ef..b259b2ebdf 100644 --- a/mem_analysis.m +++ b/mem_analysis.m @@ -1,12 +1,13 @@ %% -lines = readlines('mem_analysis.csv'); +lines = readlines('mem_analysis.csv','EmptyLineRule','skip'); names = {}; Nframes = length(lines); mem = zeros(1,Nframes); +% Load mem_analysis.csv into matrix for i = 1:Nframes entries = split(lines(i),';'); for j = 1:length(entries) @@ -24,6 +25,13 @@ for i = 1:Nframes end end +% Reorder the matrix such that the fixed allocations are at the bottom: +% --> Find all rows which are constant (sum of abs diff is zero). The >0 is +% to limit the reordering to constant values only. +[~,indx] = sort(sum(abs(diff(mem,[],2)),2)>0); +mem = mem(indx,:); +names = names(indx); + bar(mem',1,'stacked') legend(names,'location','eastoutside','interpreter','none') xlabel('frame') -- GitLab From 545d796d56039bd09b945f3268893e09fcc09856 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 22 Nov 2022 08:31:45 +0100 Subject: [PATCH 4/7] Moved mem_analysis.m to scripts folder --- mem_analysis.m => scripts/mem_analysis.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename mem_analysis.m => scripts/mem_analysis.m (93%) diff --git a/mem_analysis.m b/scripts/mem_analysis.m similarity index 93% rename from mem_analysis.m rename to scripts/mem_analysis.m index b259b2ebdf..ad75efa1c7 100644 --- a/mem_analysis.m +++ b/scripts/mem_analysis.m @@ -1,6 +1,6 @@ %% -lines = readlines('mem_analysis.csv','EmptyLineRule','skip'); +lines = readlines('../mem_analysis.csv','EmptyLineRule','skip'); names = {}; Nframes = length(lines); -- GitLab From a1b1037b5fe8eda56e0b2804a6830f17805ac574 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 22 Nov 2022 10:58:36 +0100 Subject: [PATCH 5/7] Fixes for linux build. --- lib_debug/mem_count.c | 4 ++-- lib_debug/mem_count.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_debug/mem_count.c b/lib_debug/mem_count.c index 2a43d56ff1..ba7db459cb 100644 --- a/lib_debug/mem_count.c +++ b/lib_debug/mem_count.c @@ -911,14 +911,14 @@ size_t mem_count_summary( Counting_Size cnt_size ) } #ifdef RAM_ANALYSIS -void mem_analyze() +void mem_analyze(void) { unsigned int i; char buffer[1024]; for ( i = 0; i < Num_Records_Cur_RAM; i++ ) { - sprintf( buffer, "%s:%d,%d;", Current_Allocations[i].name, Current_Allocations[i].lineno, Current_Allocations[i].block_size ); + sprintf( buffer, "%s:%d,%ld;", Current_Allocations[i].name, Current_Allocations[i].lineno, Current_Allocations[i].block_size ); dbgwrite( buffer, sizeof( char ), strlen( buffer ), 1, "mem_analysis.csv" ); } sprintf( buffer, "\n" ); diff --git a/lib_debug/mem_count.h b/lib_debug/mem_count.h index e196c799ec..36f7d8f79f 100644 --- a/lib_debug/mem_count.h +++ b/lib_debug/mem_count.h @@ -96,7 +96,7 @@ extern "C" extern void mem_free( const char *func_name, int func_lineno, void *ptr ); #ifdef RAM_ANALYSIS - extern void mem_analyze(); + extern void mem_analyze(void); #endif #ifdef __cplusplus -- GitLab From 3fa54099cb269c08ec1a4d803f9cac8b1393f8bf Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 22 Nov 2022 11:12:55 +0100 Subject: [PATCH 6/7] Put MEM_ANALYSIS within DEBUGGING since it uses dbgwrite. Move switch to debugging section in options.h and disable by default. --- apps/decoder.c | 4 ++++ apps/encoder.c | 2 ++ lib_com/options.h | 2 +- lib_debug/mem_count.c | 3 ++- lib_debug/mem_count.h | 2 ++ 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/decoder.c b/apps/decoder.c index c54758de6c..6f73723af1 100644 --- a/apps/decoder.c +++ b/apps/decoder.c @@ -1485,8 +1485,10 @@ static ivas_error decodeG192( #ifdef WMOPS update_wmops(); #endif +#ifdef DEBUGGING #ifdef RAM_ANALYSIS mem_analyze(); +#endif #endif } @@ -1844,8 +1846,10 @@ static ivas_error decodeVoIP( #ifdef WMOPS update_wmops(); #endif +#ifdef DEBUGGING #ifdef RAM_ANALYSIS mem_analyze(); +#endif #endif } diff --git a/apps/encoder.c b/apps/encoder.c index ea0374c7ec..0bad34878b 100644 --- a/apps/encoder.c +++ b/apps/encoder.c @@ -766,8 +766,10 @@ int main( #ifdef WMOPS update_wmops(); #endif +#ifdef DEBUGGING #ifdef RAM_ANALYSIS mem_analyze(); +#endif #endif } diff --git a/lib_com/options.h b/lib_com/options.h index ee032d9cbd..68e801b352 100755 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -42,7 +42,6 @@ #define SUPPORT_JBM_TRACEFILE /* support for JBM tracefile, which is needed for 3GPP objective/subjective testing, but not relevant for real-world implementations */ #define RAM_COUNTING_TOOL /* tool to automatically count the dynamically alocated static memory consumption */ -#define RAM_ANALYSIS /* #################### End compiler switches ######################### */ @@ -56,6 +55,7 @@ /*#define WMOPS_PER_FRAME*/ /* Output complexity in WMOPS per frame to the file "res/wmops" (one float value per frame) */ /*#define WMOPS_DETAIL*/ /* Activate complexity detail printout for every function. Increases runtime overhead */ /*#define WMOPS_WC_FRAME_ANALYSIS*/ /* Output WMOPS analysis for worst case frame */ +/*#define RAM_ANALYSIS*/ /* Output memory allocated with count_malloc each frame. Can be parsed and plotted with scripts/mem_analysis.m */ #ifdef DEBUGGING diff --git a/lib_debug/mem_count.c b/lib_debug/mem_count.c index ba7db459cb..8bb85e1a6b 100644 --- a/lib_debug/mem_count.c +++ b/lib_debug/mem_count.c @@ -910,6 +910,7 @@ size_t mem_count_summary( Counting_Size cnt_size ) return size; } +#ifdef DEBUGGING #ifdef RAM_ANALYSIS void mem_analyze(void) { @@ -927,6 +928,6 @@ void mem_analyze(void) return; } #endif - +#endif #endif diff --git a/lib_debug/mem_count.h b/lib_debug/mem_count.h index 36f7d8f79f..dfcd77c4bb 100644 --- a/lib_debug/mem_count.h +++ b/lib_debug/mem_count.h @@ -95,9 +95,11 @@ extern "C" extern void *mem_alloc( const char *func_name, int func_lineno, size_t size, char *alloc_str ); extern void mem_free( const char *func_name, int func_lineno, void *ptr ); +#ifdef DEBUGGING #ifdef RAM_ANALYSIS extern void mem_analyze(void); #endif +#endif #ifdef __cplusplus } -- GitLab From 78bdd79a4beed77196f8faddbf993f1ac9a9126a Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 22 Nov 2022 11:26:57 +0100 Subject: [PATCH 7/7] Clang format --- lib_debug/mem_count.c | 2 +- lib_debug/mem_count.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib_debug/mem_count.c b/lib_debug/mem_count.c index 8bb85e1a6b..278126d35e 100644 --- a/lib_debug/mem_count.c +++ b/lib_debug/mem_count.c @@ -912,7 +912,7 @@ size_t mem_count_summary( Counting_Size cnt_size ) #ifdef DEBUGGING #ifdef RAM_ANALYSIS -void mem_analyze(void) +void mem_analyze( void ) { unsigned int i; char buffer[1024]; diff --git a/lib_debug/mem_count.h b/lib_debug/mem_count.h index dfcd77c4bb..38ccb0bac6 100644 --- a/lib_debug/mem_count.h +++ b/lib_debug/mem_count.h @@ -97,7 +97,7 @@ extern "C" #ifdef DEBUGGING #ifdef RAM_ANALYSIS - extern void mem_analyze(void); + extern void mem_analyze( void ); #endif #endif -- GitLab