⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmperftracking.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
          $Archive:  $

         $Revision:  $

      Last Checkin:
             $Date:  $
                By:
           $Author:  $

 Last Modification:
          $ModTime:   $

       Description:   simple means of tracing total number of calls and total
                      tick time spent in any named section of code.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: 5 $ : $JustDate:  1/25/02 $";
/*****************************************************************************/

#include "../../../stdafx.h"
#include "VMWatcherOutput.h"
#include "VMPerfTracking.h"
#include "VMLargeIntMath.h"
#include "VMHighPerfCounter.h"

#define CAPTURE_PERF_STATS


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfStats::GetInstance

       DESCRIPTION:  Get the counter manager

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  pointer to the single counter manager instance
*/
VMPerfStats* VMPerfStats::GetInstance( void )
{
  static VMPerfStats gpoSingleInstance;
  return &gpoSingleInstance;
}
/* End of function "VMPerfStats::GetInstance"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfStats::GetCounter

       DESCRIPTION:  returns the counter associated with the given name. If
                     counter is not present, then creates one and stores it

             INPUT:  pchName - name of the counter
            OUTPUT:  none

           RETURNS:  pointer to count container
*/
P_PERF_COUNTER_DATA VMPerfStats::GetCounter( const char* pchName )
{
  P_PERF_COUNTER_DATA pxResult;

  EnterCriticalSection( &m_xCriticalSection );

  COUNTER_MAP_ITER oIter = m_oCounters.find( pchName );
  if ( oIter != m_oCounters.end() )
  {
    pxResult = (*oIter).second;
  }
  else
  {
    P_PERF_COUNTER_DATA pxCounter = new PERF_COUNTER_DATA;
    memset( pxCounter, 0, sizeof( PERF_COUNTER_DATA ) );
    char* pchKey = new char [ strlen( pchName ) + 1 ];
    strcpy( pchKey, pchName );
    InitializeCriticalSection( &pxCounter->m_xCriticalSection );
    m_oCounters.insert( COUNTER_MAP::value_type( pchKey, pxCounter ) );
    pxResult = pxCounter;
  }

  LeaveCriticalSection( &m_xCriticalSection );

	return( pxResult );
}
/* End of function "VMPerfStats::GetCounter"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfStats::VMPerfStats

       DESCRIPTION:  ctor. called once. inits the high performance counters

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  none
*/
VMPerfStats::VMPerfStats( void )
{
  InitializeCriticalSection( &m_xCriticalSection );
  VMHighPerfCounterInit();
}
/* End of function "VMPerfStats::VMPerfStats"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfStats::~VMPerfStats

       DESCRIPTION:  dumps statistics when this goes out of scope

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  none
*/
VMPerfStats::~VMPerfStats( void )
{
	DeleteCriticalSection( &m_xCriticalSection );
  DumpAllStats();

  for ( COUNTER_MAP_ITER oIter = m_oCounters.begin(); oIter != m_oCounters.end(); oIter++ )
  {
     delete [] const_cast<char*>( (*oIter).first );
     DeleteCriticalSection( &(*oIter).second->m_xCriticalSection ); 
     delete    (*oIter).second;
  }

  m_oCounters.clear();
}
/* End of function "VMPerfStats::~VMPerfStats"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfTimer::DumpAllStats

       DESCRIPTION:  dumps all statistics to the debug window or to the trace
                     program

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
void VMPerfStats::DumpAllStats( void )
{
  COUNTER_MAP_ITER oEnd = m_oCounters.end();

#ifdef CAPTURE_PERF_STATS
  
  if ( g_bVMTraceEnabled )
  {
    DEBUGOUT( "\r\n\r\n" );
    DEBUGOUT( "Performance Statistics:\r\n\r\n" );
  }


	EnterCriticalSection( &m_xCriticalSection );
  
  for ( COUNTER_MAP_ITER oIter = m_oCounters.begin(); oIter != oEnd; oIter++ )
  {
    if ( g_bVMTraceEnabled )
    {
      char achBuffer[ 67 ];
      DEBUGOUT( "%s was called %d times for a total execution time of %s milli-secs.\r\n", 
                (*oIter).first, 
                (*oIter).second->m_dwCalledCounter, 
                VMLargeIntAsString( achBuffer, VMLargeIntDivide( (*oIter).second->m_xTotalExecTime, 
							                                             VMGetTickCountByOffset( VMHPC1USEC ) ) ) );
    }
  }

  LeaveCriticalSection( &m_xCriticalSection );

  if ( g_bVMTraceEnabled )
  {
    DEBUGOUT( "\r\n\r\nEnd Performance Statistics.\r\n\r\n" );
  }
  else
  {
    OutputDebugString( "\r\n\r\nEnd Performance Statistics.\r\n\r\n" );
  }
#endif
}
/* End of function "VMPerfStats::~DumpAllStats"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfTimer::VMPerfTimer

       DESCRIPTION:  code block counter wrapper. Looks up a counter from the
                     global counter manager for later manipulation and also
                     captures the entry tick count

             INPUT:  pchKey - name of the counter to update
                     bAutoStart - true if the timer should start immediately
            OUTPUT:  none

           RETURNS:  none
*/
VMPerfTimer::VMPerfTimer( const char* pchKey, bool bAutoStart )
{
#ifdef CAPTURE_PERF_STATS
  VMPerfStats* poPerfs = VMPerfStats::GetInstance();
  m_pxPerfData = poPerfs->GetCounter( pchKey );

  // get tick count after map look ups so tick time is not
  // skewed by the overhead of the look up of the counter
  //
  m_bTimerRunning = bAutoStart;
  if ( bAutoStart )
  {
    m_xEnterTime = VMGetHighPerfCounterTickCount();
  }
  InterlockedIncrement( &m_pxPerfData->m_dwCalledCounter );
#endif
}
/* End of function "VMPerfTimer::VMPerfTimer"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfTimer::StartTimer

       DESCRIPTION:  starts the timer by capturing 'now'

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  void  
*/
void VMPerfTimer::StartTimer( void )
{
#ifdef CAPTURE_PERF_STATS
  m_bTimerRunning = true;
  m_xEnterTime = VMGetHighPerfCounterTickCount();
#endif
}
/* End of function "VMPerfTimer::StartTimer"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfTimer::StopTimer

       DESCRIPTION:  stops the timer and captures / updates the total time 
                     spent in this timer

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  void 
*/
void VMPerfTimer::StopTimer( void )
{
#ifdef CAPTURE_PERF_STATS
  if ( m_bTimerRunning )
  {
    EnterCriticalSection( &m_pxPerfData->m_xCriticalSection );

    m_pxPerfData->m_xTotalExecTime = VMLargeIntAdd( m_pxPerfData->m_xTotalExecTime, 
                                                    VMLargeIntSubtract( VMGetHighPerfCounterTickCount(), m_xEnterTime ) );

    LeaveCriticalSection( &m_pxPerfData->m_xCriticalSection );
    m_bTimerRunning = false;
    m_xEnterTime    = m_pxPerfData->m_xTotalExecTime;
  }  
#endif
}
/* End of function "VMPerfTimer::StopTimer"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMPerfTimer::~VMPerfTimer

       DESCRIPTION:  code block leaving scope, so update total run time for
                     the counter instance held in this

             INPUT:  void  
            OUTPUT:  none

           RETURNS:  none
*/
VMPerfTimer::~VMPerfTimer( void )
{
#ifdef CAPTURE_PERF_STATS
  if ( m_bTimerRunning )
  {
    StopTimer();
  }
#endif
}
/* End of function "VMPerfTimer::~VMPerfTimer"
/*****************************************************************************/


/*****************************************************************************/
/* Check-in history 
   $WorkFile:   $
    $Archive:   $

 *$Log:   $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -