Added DIAG macros for countdown logging

Also for tracking memory use and for timing.
This commit is contained in:
James Crook 2015-04-12 15:26:04 +01:00
parent 5bd99f94cd
commit fafe3f105a
5 changed files with 183 additions and 0 deletions

91
src/Diags.cpp Normal file
View File

@ -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 <wx/defs.h>
#include <wx/hash.h>
#include <wx/intl.h>
#include <wx/log.h>
#include <wx/stopwatch.h>
#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.
}

82
src/Diags.h Normal file
View File

@ -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

View File

@ -122,6 +122,8 @@ audacity_SOURCES = \
DeviceChange.h \
DeviceManager.cpp \
DeviceManager.h \
Diags.cpp \
Diags.h \
Envelope.cpp \
Envelope.h \
Experimental.h \

View File

@ -246,6 +246,7 @@
<ClCompile Include="..\..\..\src\Dependencies.cpp" />
<ClCompile Include="..\..\..\src\DeviceChange.cpp" />
<ClCompile Include="..\..\..\src\DeviceManager.cpp" />
<ClCompile Include="..\..\..\src\Diags.cpp" />
<ClCompile Include="..\..\..\src\DirManager.cpp" />
<ClCompile Include="..\..\..\src\Dither.cpp" />
<ClCompile Include="..\..\..\src\effects\EffectRack.cpp" />
@ -520,6 +521,7 @@
<ClInclude Include="..\..\..\src\CaptureEvents.h" />
<ClInclude Include="..\..\..\src\commands\OpenSaveCommands.h" />
<ClInclude Include="..\..\..\src\DeviceChange.h" />
<ClInclude Include="..\..\..\src\Diags.h" />
<ClInclude Include="..\..\..\src\effects\EffectRack.h" />
<ClInclude Include="..\..\..\src\effects\NoiseReduction.h" />
<ClInclude Include="..\..\..\src\effects\Phaser.h" />

View File

@ -834,6 +834,9 @@
<ClCompile Include="..\..\..\src\SelectedRegion.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\Diags.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\src\AboutDialog.h">
@ -1667,6 +1670,9 @@
<ClInclude Include="..\..\..\src\SelectedRegion.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\..\src\Diags.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\..\audacity.ico">