From fafe3f105a490d5bd092df43076aa0affecd37fb Mon Sep 17 00:00:00 2001 From: James Crook Date: Sun, 12 Apr 2015 15:26:04 +0100 Subject: [PATCH] Added DIAG macros for countdown logging Also for tracking memory use and for timing. --- src/Diags.cpp | 91 +++++++++++++++++++ src/Diags.h | 82 +++++++++++++++++ src/Makefile.am | 2 + win/Projects/Audacity/Audacity.vcxproj | 2 + .../Audacity/Audacity.vcxproj.filters | 6 ++ 5 files changed, 183 insertions(+) create mode 100644 src/Diags.cpp create mode 100644 src/Diags.h diff --git a/src/Diags.cpp b/src/Diags.cpp new file mode 100644 index 000000000..4533b0914 --- /dev/null +++ b/src/Diags.cpp @@ -0,0 +1,91 @@ + +/********************************************************************** + + Audacity: A Digital Audio Editor + + Diags.cpp + + James Crook + + +********************************************************************//** + +\class Diags +\brief Processing of the macros for recording bad events or performance +monitoring. + +The idea of these macros is that we can include them in release +code at no risk. They + +a) have almost zero performance impact. +b) will not flood the log with events. + +This is achieved by a countdown which stops recording in the log +when the countdown is finished. The countdwon continues to +count down so that we track how many times the event happens. + + +*//********************************************************************/ + + +#include "Audacity.h" +#include +#include +#include +#include + +#include +#include "Diags.h" +#include "Experimental.h" + +static wxStopWatch MasterWatch; +static bool bStopWatchStarted = false; + +void diagnostics_do_diag( t_diag_struct * pDiag ){ + wxLogDebug( wxT("%s"), pDiag->pMessage ); +} +void diagnostics_do_diag_mem( t_diag_struct * pDiag, long amount ){ + wxLogDebug( wxT("%s %l"), pDiag->pMessage, amount ); + pDiag->total += amount; + pDiag->most_recent = amount; + if( pDiag->countdown == (pDiag->initial_count -1 )){ + pDiag->most = amount; + pDiag->least = amount; + } + else if( amount > pDiag->most ) + pDiag->most = amount; + else if( amount < pDiag->least ) + pDiag->least = amount; +} + +void diagnostics_do_perfmon_start( t_diag_struct * pDiag, t_diag_struct ** pRememberMe ){ + if( *pRememberMe == NULL ){ + *pRememberMe = pDiag; + if( !bStopWatchStarted ){ + bStopWatchStarted = true; + MasterWatch.Start(); + } + } + pDiag->most_recent = MasterWatch.Time(); +} + +void diagnostics_do_perfmon_stop( const char * pMessage, t_diag_struct ** ppDiag ){ + t_diag_struct * pDiag = *ppDiag; + *ppDiag = NULL; + long amount = MasterWatch.Time() - pDiag->most_recent; + pDiag->total += amount; + pDiag->most_recent = amount; + if( pDiag->countdown == (pDiag->initial_count -1 )){ + pDiag->most = amount; + pDiag->least = amount; + } + else if( amount > pDiag->most ) + pDiag->most = amount; + else if( amount < pDiag->least ) + pDiag->least = amount; +} + + +void diag_sample_test(){ + DIAG("Flip counter");// Flip counter will show in log ten times, then just count. +} diff --git a/src/Diags.h b/src/Diags.h new file mode 100644 index 000000000..73659dc57 --- /dev/null +++ b/src/Diags.h @@ -0,0 +1,82 @@ +/********************************************************************** + + Audacity: A Digital Audio Editor + + Diags.h + + James Crook + + Provides Macros for recording bad events and performance monitoring. + These macros have such low cost that they can be used in release code. + They will take miniscule processing time after the first ten times. + +**********************************************************************/ + +#ifndef __AUDACITY_DIAGS__ +#define __AUDACITY_DIAGS__ + +typedef long t_diag_timer; + +struct t_diag_struct { + long countdown; + long initial_count; + long total; + long most_recent; + long least; + long most; + const char * pMessage; +}; + + +extern void diagnostics_do_diag( t_diag_struct * pDiag ); +extern void diagnostics_do_diag_mem( t_diag_struct * pDiag, long amount ); +extern void diagnostics_do_perfmon_start( t_diag_struct * pDiag, t_diag_struct ** ppRememberMe ); +extern void diagnostics_do_perfmon_stop( const char * pMessage, t_diag_struct ** ppDiag); + +// A constant that sets the maximum number of times we log the message. +#define DEFAULT_LOG_COUNT (10) + +// USAGE: +// Each of these will do something the first ten times, then just count. +// They can be reactivated by a GUI. +// +// Use DIAG for a simple message. Usually for something bad like an overrun. +// Use TRACK_MEM to track hungry memory usage, RAM or disk. +// Use PERFMON_START and STOP to time an interval. +// For the above two, you will need a MAKE_TIMER( timername ) first. + +// The 'timername' created here is almost free. +// It's a pointer that allows both START and STOP to use the same struct. +#define MAKE_TIMER( timername ) \ + static t_diagstruct * timername = NULL; + +// Note that in all three macros: +// The {} ensure diag name is not visible outside +// static ensures struct is initialised just once. +// No function is called after the countdown is counted out. +#define DIAG( message ) { \ + static t_diag_struct diag{ DEFAULT_LOG_COUNT, DEFAULT_LOG_COUNT, 0,0,0,0,message};\ + if( --diag.countdown >=0 )\ + diagnostics_do_diag( &diag );\ +} + +#define TRACK_MEM( message, amount ) { \ + static t_diag_struct diag{ DEFAULT_LOG_COUNT, DEFAULT_LOG_COUNT, 0,0,0,0,message};\ + if( --diag.countdown >=0 )\ + diagnostics_do_diag_mem( &diag, amount );\ +} + +#define PERFMON_START( message, timername ){ \ + static t_diag_struct diag{ DEFAULT_LOG_COUNT, DEFAULT_LOG_COUNT, 0,0,0,0,message};\ + if( --diag.countdown >=0 )\ + diagnostics_do_perfmon_start( &diag, &pRememberMe );\ +} + +#define PERFMON_STOP( message, timername ){ \ + if( timername != NULL )\ + diagnostics_do_perfmon_stop( message, &timername );\ +} + + + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index baac6eeb9..9a4d4e5a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -122,6 +122,8 @@ audacity_SOURCES = \ DeviceChange.h \ DeviceManager.cpp \ DeviceManager.h \ + Diags.cpp \ + Diags.h \ Envelope.cpp \ Envelope.h \ Experimental.h \ diff --git a/win/Projects/Audacity/Audacity.vcxproj b/win/Projects/Audacity/Audacity.vcxproj index 78a0a8180..caa39487d 100755 --- a/win/Projects/Audacity/Audacity.vcxproj +++ b/win/Projects/Audacity/Audacity.vcxproj @@ -246,6 +246,7 @@ + @@ -520,6 +521,7 @@ + diff --git a/win/Projects/Audacity/Audacity.vcxproj.filters b/win/Projects/Audacity/Audacity.vcxproj.filters index 9a31c3d9c..17d535d85 100755 --- a/win/Projects/Audacity/Audacity.vcxproj.filters +++ b/win/Projects/Audacity/Audacity.vcxproj.filters @@ -834,6 +834,9 @@ src + + src + @@ -1667,6 +1670,9 @@ src + + src +