LCOV - code coverage report
Current view: top level - src/mesquite/Misc - MsqTimer.cpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 31 118 26.3 %
Date: 2020-07-18 00:09:26 Functions: 8 21 38.1 %
Branches: 12 156 7.7 %

           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                 :            : #include "moab/MOABConfig.h"
      28                 :            : #include "MsqTimer.hpp"
      29                 :            : 
      30                 :            : #include <iostream>
      31                 :            : #include <iomanip>
      32                 :            : 
      33                 :            : // Create the global collection of stop watches
      34                 :         30 : MBMesquite::StopWatchCollection MBMesquite::GlobalStopWatches;
      35                 :            : 
      36                 :            : #ifdef MOAB_HAVE_TIMES
      37                 :            : #include <sys/times.h>
      38                 :            : #include <unistd.h>
      39                 :            : #include <limits.h>
      40                 :            : #ifndef CLK_TCK
      41                 :            : #ifdef _SC_CLK_TCK
      42                 :            : #define CLK_TCK sysconf( _SC_CLK_TCK )
      43                 :            : #else
      44                 :            : #include <sys/param.h>
      45                 :            : #ifdef HZ
      46                 :            : #define CLK_TCK HZ
      47                 :            : #else
      48                 :            : #error times(3) w/out CLK_TCK.  Please report this.
      49                 :            : #undef MOAB_HAVE_TIMES
      50                 :            : #endif
      51                 :            : #endif
      52                 :            : #endif
      53                 :            : #endif
      54                 :            : 
      55                 :            : #ifdef MOAB_HAVE_TIMES
      56                 :       1033 : static inline double now()
      57                 :            : {
      58                 :            :     tms t;
      59                 :       1033 :     times( &t );
      60                 :       1033 :     return (double)( t.tms_utime + t.tms_stime ) / CLK_TCK;
      61                 :            : }
      62                 :            : #elif defined( MOAB_HAVE_CLOCK )
      63                 :            : #include <ctime>
      64                 :            : static inline double now()
      65                 :            : {
      66                 :            :     return (double)std::clock() / CLOCKS_PER_SEC;
      67                 :            : }
      68                 :            : #endif
      69                 :            : 
      70                 :       1840 : MBMesquite::Timer::Timer() : atBirth( now() )
      71                 :            : {
      72                 :        920 :     atLastCheck = atBirth;
      73                 :        920 : }
      74                 :            : 
      75                 :         23 : void MBMesquite::Timer::reset()
      76                 :            : {
      77                 :         23 :     atBirth     = now();
      78                 :         23 :     atLastCheck = atBirth;
      79                 :         23 : }
      80                 :            : 
      81                 :          0 : double MBMesquite::Timer::since_last_check()
      82                 :            : {
      83                 :          0 :     double right_now = now();
      84                 :          0 :     double rv        = right_now - atLastCheck;
      85                 :          0 :     atLastCheck      = right_now;
      86                 :          0 :     return rv;
      87                 :            : }
      88                 :            : 
      89                 :         90 : double MBMesquite::Timer::since_birth() const
      90                 :            : {
      91                 :         90 :     return now() - atBirth;
      92                 :            : }
      93                 :            : 
      94                 :          0 : void MBMesquite::StopWatch::start()
      95                 :            : {
      96         [ #  # ]:          0 :     if( !isRunning )
      97                 :            :     {
      98                 :          0 :         isRunning       = true;
      99                 :          0 :         timeAtLastStart = now();
     100                 :          0 :         ++numStarts;
     101                 :            :     }
     102                 :          0 : }
     103                 :            : 
     104                 :          0 : void MBMesquite::StopWatch::stop()
     105                 :            : {
     106         [ #  # ]:          0 :     if( isRunning )
     107                 :            :     {
     108                 :          0 :         isRunning = false;
     109                 :          0 :         totalTime += now() - timeAtLastStart;
     110                 :            :     }
     111                 :          0 : }
     112                 :            : 
     113                 :          0 : void MBMesquite::StopWatch::reset()
     114                 :            : {
     115                 :          0 :     isRunning = false;
     116                 :          0 :     totalTime = 0;
     117                 :          0 :     numStarts = 0;
     118                 :          0 : }
     119                 :            : 
     120                 :          0 : double MBMesquite::StopWatch::total_time() const
     121                 :            : {
     122                 :          0 :     double rv = totalTime;
     123         [ #  # ]:          0 :     if( isRunning ) rv += now() - timeAtLastStart;
     124                 :          0 :     return rv;
     125                 :            : }
     126                 :            : 
     127                 :          0 : MBMesquite::StopWatchCollection::Key MBMesquite::StopWatchCollection::add( const std::string& name,
     128                 :            :                                                                            bool fail_if_exists )
     129                 :            : {
     130                 :            :     // Don't allow empty name
     131         [ #  # ]:          0 :     if( name == "" ) return 0;
     132                 :            : 
     133                 :          0 :     Key key = get_key( name );
     134                 :            : 
     135                 :            :     // If the named stopwatch doesn't exist...
     136         [ #  # ]:          0 :     if( !key )
     137                 :            :     {
     138                 :            :         // See if there is an unused existing stopwatch
     139                 :            :         size_t i;
     140         [ #  # ]:          0 :         for( i = 0; i < mEntries.size(); i++ )
     141                 :            :         {
     142         [ #  # ]:          0 :             if( mEntries[i].first == "" )
     143                 :            :             {
     144                 :          0 :                 mEntries[i].first = name;
     145                 :          0 :                 mEntries[i].second.reset();
     146                 :          0 :                 break;
     147                 :            :             }
     148                 :            :         }
     149                 :            :         // If not, create a new one
     150 [ #  # ][ #  # ]:          0 :         if( i == mEntries.size() ) { mEntries.push_back( std::pair< std::string, StopWatch >( name, StopWatch() ) ); }
                 [ #  # ]
     151                 :          0 :         key = i + 1;
     152                 :            :     }
     153                 :            :     // If it already existed...
     154         [ #  # ]:          0 :     else if( fail_if_exists )
     155                 :          0 :         key = 0;
     156                 :            : 
     157                 :          0 :     return key;
     158                 :            : }
     159                 :            : 
     160                 :          0 : MBMesquite::StopWatchCollection::Key MBMesquite::StopWatchCollection::get_key( const std::string& name ) const
     161                 :            : {
     162                 :          0 :     Key key = 0;
     163                 :            : 
     164         [ #  # ]:          0 :     for( size_t i = 0; i < mEntries.size(); i++ )
     165                 :            :     {
     166         [ #  # ]:          0 :         if( mEntries[i].first == name )
     167                 :            :         {
     168                 :          0 :             key = i + 1;
     169                 :          0 :             break;
     170                 :            :         }
     171                 :            :     }
     172                 :            : 
     173                 :          0 :     return key;
     174                 :            : }
     175                 :            : 
     176                 :          0 : void MBMesquite::StopWatchCollection::remove( const MBMesquite::StopWatchCollection::Key key )
     177                 :            : {
     178                 :            :     // Get rid of anything at the end of the list
     179         [ #  # ]:          0 :     if( key == mEntries.size() )
     180                 :            :     {
     181                 :          0 :         mEntries.pop_back();
     182 [ #  # ][ #  # ]:          0 :         while( !mEntries.empty() && mEntries.back().first == "" )
                 [ #  # ]
     183                 :            :         {
     184                 :          0 :             mEntries.pop_back();
     185                 :            :         }
     186                 :            :     }
     187                 :            : 
     188 [ #  # ][ #  # ]:          0 :     else if( key > 0 && key < mEntries.size() )
                 [ #  # ]
     189                 :            :     {
     190                 :            :         // If in the middle of the list, set its name to ""
     191                 :          0 :         mEntries[key - 1].first = "";
     192                 :            :     }
     193                 :          0 : }
     194                 :            : 
     195                 :          0 : void MBMesquite::StopWatchCollection::start( const MBMesquite::StopWatchCollection::Key key )
     196                 :            : {
     197 [ #  # ][ #  # ]:          0 :     if( key > 0 && key <= mEntries.size() && mEntries[key - 1].first != "" ) mEntries[key - 1].second.start();
         [ #  # ][ #  # ]
     198                 :          0 : }
     199                 :            : 
     200                 :          0 : void MBMesquite::StopWatchCollection::stop( const MBMesquite::StopWatchCollection::Key key )
     201                 :            : {
     202 [ #  # ][ #  # ]:          0 :     if( key > 0 && key <= mEntries.size() && mEntries[key - 1].first != "" ) mEntries[key - 1].second.stop();
         [ #  # ][ #  # ]
     203                 :          0 : }
     204                 :            : 
     205                 :          0 : void MBMesquite::StopWatchCollection::reset( const MBMesquite::StopWatchCollection::Key key )
     206                 :            : {
     207 [ #  # ][ #  # ]:          0 :     if( key > 0 && key <= mEntries.size() ) mEntries[key - 1].second.reset();
                 [ #  # ]
     208                 :          0 : }
     209                 :            : 
     210                 :          0 : double MBMesquite::StopWatchCollection::total_time( const MBMesquite::StopWatchCollection::Key key ) const
     211                 :            : {
     212 [ #  # ][ #  # ]:          0 :     if( key > 0 && key <= mEntries.size() && mEntries[key - 1].first != "" )
         [ #  # ][ #  # ]
     213                 :          0 :         return mEntries[key - 1].second.total_time();
     214                 :            :     else
     215                 :          0 :         return 0.0;
     216                 :            : }
     217                 :            : 
     218                 :          0 : int MBMesquite::StopWatchCollection::number_of_starts( const MBMesquite::StopWatchCollection::Key key ) const
     219                 :            : {
     220 [ #  # ][ #  # ]:          0 :     if( key > 0 && key <= mEntries.size() && mEntries[key - 1].first != "" )
         [ #  # ][ #  # ]
     221                 :          0 :         return mEntries[key - 1].second.number_of_starts();
     222                 :            :     else
     223                 :          0 :         return 0;
     224                 :            : }
     225                 :            : /*! Fills a vector of StopWatchCollection::Key in which the Keys are ordered
     226                 :            :   by the associated StopWatch's total_time.  The key associated with the
     227                 :            :   largest total_time StopWatch is in the first position of the vector.  The
     228                 :            :   key associated with the smallest total_time StopWatch is in the last
     229                 :            :   position of the vector.*/
     230                 :          4 : void MBMesquite::StopWatchCollection::get_keys_sorted_by_time( std::vector< Key >& sorted_keys )
     231                 :            : {
     232                 :          4 :     int num_watches     = mEntries.size();
     233         [ +  - ]:          4 :     int* sorted_indices = new int[num_watches];
     234                 :          4 :     int i               = 0;
     235                 :          4 :     int counter         = 0;
     236         [ -  + ]:          4 :     for( i = 0; i < num_watches; ++i )
     237                 :            :     {
     238                 :          0 :         sorted_indices[i] = 0;
     239                 :            :     }
     240                 :            :     double current_max;
     241                 :            :     int index_to_max;
     242                 :            :     // While we haven't added all of the Keys to the vector
     243         [ -  + ]:          4 :     while( counter < num_watches )
     244                 :            :     {
     245                 :          0 :         current_max  = -1;
     246                 :          0 :         index_to_max = -1;
     247                 :            :         // loop over the times and find the largest remaining
     248         [ #  # ]:          0 :         for( i = 0; i < num_watches; ++i )
     249                 :            :         {
     250 [ #  # ][ #  # ]:          0 :             if( mEntries[i].second.total_time() > current_max && sorted_indices[i] == 0 )
                 [ #  # ]
     251                 :            :             {
     252                 :          0 :                 current_max  = mEntries[i].second.total_time();
     253                 :          0 :                 index_to_max = i;
     254                 :            :             }
     255                 :            :         }
     256                 :            :         // Add the key associated with index_to_max and any subsequent
     257                 :            :         // keys which are associated with a StopWatch that has a total
     258                 :            :         // time equal to current_max;
     259         [ #  # ]:          0 :         for( i = index_to_max; i < num_watches; ++i )
     260                 :            :         {
     261 [ #  # ][ #  # ]:          0 :             if( mEntries[i].second.total_time() >= current_max && sorted_indices[i] == 0 )
                 [ #  # ]
     262                 :            :             {
     263                 :          0 :                 counter++;
     264                 :          0 :                 sorted_indices[i] = counter;
     265         [ #  # ]:          0 :                 sorted_keys.push_back( i + 1 );
     266                 :            :             }
     267                 :            :         }
     268                 :            :     }
     269                 :            :     // clean up
     270         [ +  - ]:          4 :     delete[] sorted_indices;
     271                 :          4 : }
     272                 :            : 
     273                 :            : // Originally in MsqMessage.cpp
     274                 :            : // Moved here and converted to an ostream operator
     275                 :            : // by J.Kraftcheck, 2004-10-18
     276                 :          4 : std::ostream& MBMesquite::operator<<( std::ostream& str, MBMesquite::StopWatchCollection& )
     277                 :            : {
     278         [ +  - ]:          4 :     std::vector< MBMesquite::StopWatchCollection::Key > sorted_keys;
     279         [ +  - ]:          4 :     MBMesquite::GlobalStopWatches.get_keys_sorted_by_time( sorted_keys );
     280                 :          4 :     int number_of_keys = sorted_keys.size();
     281                 :          4 :     int i              = 0;
     282 [ +  - ][ +  - ]:          4 :     str << "\nTIME        | NUM. STARTS | TIMER NAME (" << number_of_keys << " timers)\n";
                 [ +  - ]
     283         [ -  + ]:          4 :     for( i = 0; i < number_of_keys; ++i )
     284                 :            :     {
     285 [ #  # ][ #  # ]:          0 :         str << std::setiosflags( std::ios::left ) << std::setw( 13 )
         [ #  # ][ #  # ]
     286 [ #  # ][ #  # ]:          0 :             << MBMesquite::GlobalStopWatches.total_time( sorted_keys[i] ) << " " << std::setw( 13 )
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     287 [ #  # ][ #  # ]:          0 :             << MBMesquite::GlobalStopWatches.number_of_starts( sorted_keys[i] ) << " "
         [ #  # ][ #  # ]
     288 [ #  # ][ #  # ]:          0 :             << MBMesquite::GlobalStopWatches.get_string( sorted_keys[i] ) << std::endl;
         [ #  # ][ #  # ]
     289                 :            :     }
     290                 :          4 :     return str;
     291 [ +  - ][ +  - ]:        120 : }

Generated by: LCOV version 1.11