LCOV - code coverage report
Current view: top level - src/mesquite/Misc - MsqTimer.hpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 5 10 50.0 %
Date: 2020-07-18 00:09:26 Functions: 3 6 50.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* *****************************************************************
       2                 :            :     MESQUITE -- The Mesh Quality Improvement Toolkit
       3                 :            : 
       4                 :            :     Copyright 2004 Sandia Corporation and Argonne National
       5                 :            :     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
       6                 :            :     with Sandia Corporation, the U.S. Government retains certain
       7                 :            :     rights in this software.
       8                 :            : 
       9                 :            :     This library is free software; you can redistribute it and/or
      10                 :            :     modify it under the terms of the GNU Lesser General Public
      11                 :            :     License as published by the Free Software Foundation; either
      12                 :            :     version 2.1 of the License, or (at your option) any later version.
      13                 :            : 
      14                 :            :     This library is distributed in the hope that it will be useful,
      15                 :            :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :     Lesser General Public License for more details.
      18                 :            : 
      19                 :            :     You should have received a copy of the GNU Lesser General Public License
      20                 :            :     (lgpl.txt) along with this library; if not, write to the Free Software
      21                 :            :     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      22                 :            : 
      23                 :            :     [email protected], [email protected], [email protected],
      24                 :            :     [email protected], [email protected], [email protected]
      25                 :            : 
      26                 :            :   ***************************************************************** */
      27                 :            : #ifndef MESQUITE_TIMER_HPP
      28                 :            : #define MESQUITE_TIMER_HPP
      29                 :            : 
      30                 :            : #ifdef WIN32
      31                 :            : #pragma warning( 4 : 4786 )
      32                 :            : #endif
      33                 :            : 
      34                 :            : #include "Mesquite.hpp"
      35                 :            : #include "MsqDebug.hpp"
      36                 :            : 
      37                 :            : #include <vector>
      38                 :            : #include <utility>
      39                 :            : #include <string>
      40                 :            : #include <iosfwd>
      41                 :            : 
      42                 :            : namespace MBMesquite
      43                 :            : {
      44                 :            : class MESQUITE_EXPORT Timer
      45                 :            : {
      46                 :            :   public:
      47                 :            :     Timer();
      48                 :            : 
      49                 :            :     void reset();  // resets the timer as if it were just created
      50                 :            : 
      51                 :            :     double since_last_check();  //- return time in seconds since last
      52                 :            :                                 //- call to since_last_check().  Note that
      53                 :            :                                 //- calling since_birth() doesn't count
      54                 :            :                                 //- as a check.  The first time this function
      55                 :            :                                 //- is called, it returns the time since birth.
      56                 :            : 
      57                 :            :     double since_birth() const;  //- return time in seconds since
      58                 :            :                                  //- object was created.
      59                 :            : 
      60                 :            :   private:
      61                 :            :     double atBirth;      //- Time at birth
      62                 :            :     double atLastCheck;  //- Time at last call to since_last_check()
      63                 :            : };
      64                 :            : 
      65                 :            : class MESQUITE_EXPORT StopWatch
      66                 :            : {
      67                 :            :   public:
      68                 :            :     // Creates the stopwatch.  The stopwatch is stopped
      69                 :            :     // until start() is called.
      70                 :          0 :     StopWatch() : isRunning( false ), totalTime( 0.0 ), numStarts( 0 ) {}
      71                 :            : 
      72                 :            :     // Starts the stopwatch.  If it was already running,
      73                 :            :     // this function does nothing.
      74                 :            :     void start();
      75                 :            : 
      76                 :            :     // Stops the stopwatch.  If it was not already running,
      77                 :            :     // this function does nothing
      78                 :            :     void stop();
      79                 :            : 
      80                 :            :     // Stops the stopwatch and resets the total_time() to zero.
      81                 :            :     void reset();
      82                 :            : 
      83                 :            :     // Returns the total accumulated time.  If the stopwatch
      84                 :            :     // is currently running, the time between the last start()
      85                 :            :     // and the current time IS included in total_time().
      86                 :            :     double total_time() const;
      87                 :            : 
      88                 :            :     /*! \brief Returns the number of times this StopWatch has
      89                 :            :       been started.*/
      90                 :          0 :     int number_of_starts() const
      91                 :            :     {
      92                 :          0 :         return numStarts;
      93                 :            :     }
      94                 :            : 
      95                 :            :   private:
      96                 :            :     bool isRunning;
      97                 :            :     double timeAtLastStart;
      98                 :            :     double totalTime;
      99                 :            :     int numStarts;
     100                 :            : };
     101                 :            : 
     102                 :         60 : class StopWatchCollection
     103                 :            : {
     104                 :            :   public:
     105                 :            :     typedef size_t Key;
     106                 :            : 
     107                 :            :     // Create a new collection
     108                 :         60 :     MESQUITE_EXPORT StopWatchCollection() {}
     109                 :            : 
     110                 :            :     // Add a stopwatch to the collection.  Returns a non-zero
     111                 :            :     // StopWatchCollection::Key if it succeeds, zero if it fails.
     112                 :            :     // If a StopWatch with the given name already exists in the
     113                 :            :     // collection, the Key of the existing StopWatch is returned
     114                 :            :     // if 'fail_if_exists' is false, or zero is returned if
     115                 :            :     // 'fail_if_exists' is true.
     116                 :            :     MESQUITE_EXPORT Key add( const std::string& name, bool fail_if_exists = true );
     117                 :            : 
     118                 :            :     // Gets the Key for an existing stopwatch.  If a stopwatch
     119                 :            :     // with the given name does not exist, function returns zero.
     120                 :            :     MESQUITE_EXPORT Key get_key( const std::string& name ) const;
     121                 :            : 
     122                 :            :     //! Gets the string associated with a key
     123                 :          0 :     MESQUITE_EXPORT std::string get_string( const Key key )
     124                 :            :     {
     125                 :          0 :         return mEntries[key - 1].first;
     126                 :            :     }
     127                 :            :     //! Gets the string associated with a key
     128                 :            :     MESQUITE_EXPORT void get_string( const Key key, std::string& new_string )
     129                 :            :     {
     130                 :            :         new_string = mEntries[key - 1].first;
     131                 :            :     }
     132                 :            : 
     133                 :            :     // Remove a specific stopwatch.
     134                 :            :     MESQUITE_EXPORT void remove( const Key key );
     135                 :            :     MESQUITE_EXPORT void remove( const std::string& name )
     136                 :            :     {
     137                 :            :         remove( get_key( name ) );
     138                 :            :     }
     139                 :            : 
     140                 :            :     // start a specific stopwatch
     141                 :            :     MESQUITE_EXPORT void start( const Key key );
     142                 :            :     MESQUITE_EXPORT void start( const std::string& name )
     143                 :            :     {
     144                 :            :         start( get_key( name ) );
     145                 :            :     }
     146                 :            : 
     147                 :            :     // stop a specific stopwatch
     148                 :            :     MESQUITE_EXPORT void stop( const Key key );
     149                 :            :     MESQUITE_EXPORT void stop( const std::string& name )
     150                 :            :     {
     151                 :            :         stop( get_key( name ) );
     152                 :            :     }
     153                 :            : 
     154                 :            :     // reset a specific stopwatch
     155                 :            :     MESQUITE_EXPORT void reset( const Key key );
     156                 :            :     MESQUITE_EXPORT void reset( const std::string& name )
     157                 :            :     {
     158                 :            :         reset( get_key( name ) );
     159                 :            :     }
     160                 :            : 
     161                 :            :     // Get the total time for a specific stopwatch, zero if
     162                 :            :     // the stopwatch doesn't exist.
     163                 :            :     MESQUITE_EXPORT double total_time( const Key key ) const;
     164                 :            :     MESQUITE_EXPORT double total_time( const std::string& name ) const
     165                 :            :     {
     166                 :            :         return total_time( get_key( name ) );
     167                 :            :     }
     168                 :            :     // Get the number of times a StopWatch was started.
     169                 :            :     MESQUITE_EXPORT int number_of_starts( const Key key ) const;
     170                 :            :     MESQUITE_EXPORT int number_of_starts( const std::string& name ) const
     171                 :            :     {
     172                 :            :         return number_of_starts( get_key( name ) );
     173                 :            :     }
     174                 :            : 
     175                 :            :     // Gets the number of stop watches in the collection
     176                 :            :     MESQUITE_EXPORT int number_of_stop_watches()
     177                 :            :     {
     178                 :            :         return (int)mEntries.size();
     179                 :            :     }
     180                 :            : 
     181                 :            :     MESQUITE_EXPORT void get_keys_sorted_by_time( std::vector< Key >& sorted_keys );
     182                 :            : 
     183                 :            :   private:
     184                 :            :     std::vector< std::pair< std::string, StopWatch > > mEntries;
     185                 :            : };
     186                 :            : 
     187                 :            : MESQUITE_EXPORT std::ostream& operator<<( std::ostream&, StopWatchCollection& coll );
     188                 :            : 
     189                 :            : // A stopWatchCollection available anywhere
     190                 :            : extern MBMesquite::StopWatchCollection GlobalStopWatches;
     191                 :            : 
     192                 :            : MESQUITE_EXPORT inline void print_timing_diagnostics( int debugflag )
     193                 :            : {
     194                 :            :     MSQ_DBGOUT( debugflag ) << GlobalStopWatches;
     195                 :            : }
     196                 :            : 
     197                 :          4 : MESQUITE_EXPORT inline void print_timing_diagnostics( std::ostream& stream )
     198                 :            : {
     199                 :          4 :     stream << GlobalStopWatches;
     200                 :          4 : }
     201                 :            : 
     202                 :            : class FunctionTimer
     203                 :            : {
     204                 :            :   public:
     205                 :            :     inline FunctionTimer( StopWatchCollection::Key key ) : mKey( key ) {}
     206                 :            :     inline void start()
     207                 :            :     {
     208                 :            :         GlobalStopWatches.start( mKey );
     209                 :            :     }
     210                 :            :     inline ~FunctionTimer()
     211                 :            :     {
     212                 :            :         GlobalStopWatches.stop( mKey );
     213                 :            :     }
     214                 :            : 
     215                 :            :   private:
     216                 :            :     StopWatchCollection::Key mKey;
     217                 :            :     // Don't allow any of this stuff (make them private)
     218                 :            :     void* operator new( size_t size );
     219                 :            :     FunctionTimer( const FunctionTimer& );
     220                 :            :     FunctionTimer& operator=( const FunctionTimer& );
     221                 :            : };
     222                 :            : 
     223                 :            : #ifdef MSQ_USE_FUNCTION_TIMERS
     224                 :            : #define MSQ_FUNCTION_TIMER( NAME )                                    \
     225                 :            :     static MBMesquite::StopWatchCollection::Key _mesquite_timer_key = \
     226                 :            :         MBMesquite::GlobalStopWatches.add( NAME, false );             \
     227                 :            :     FunctionTimer _mesquite_timer( _mesquite_timer_key );             \
     228                 :            :     _mesquite_timer.start()
     229                 :            : #else
     230                 :            : #define MSQ_FUNCTION_TIMER( NAME )
     231                 :            : #endif
     232                 :            : 
     233                 :            : }  // namespace MBMesquite
     234                 :            : 
     235                 :            : #endif

Generated by: LCOV version 1.11