"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/be13_api/aftimer.h" (19 Feb 2021, 4453 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "aftimer.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.4_vs_1.4.5.

    1 #ifndef __AFTIMER_H__
    2 #define __AFTIMER_H__
    3 
    4 #ifdef __cplusplus
    5 #ifndef WIN32
    6 #ifndef __STDC_FORMAT_MACROS
    7 #define __STDC_FORMAT_MACROS
    8 #endif
    9 #include <inttypes.h>
   10 #include <sys/time.h>
   11 #endif
   12 #include <sys/types.h>
   13 #include <stdio.h>
   14 #include <string>
   15 
   16 class aftimer {
   17     struct timeval t0;
   18     bool running;
   19     long total_sec;
   20     long total_usec;
   21     double lap_time_;           // time from when we last did a "stop"
   22 public:
   23     aftimer():t0(),running(false),total_sec(0),total_usec(0),lap_time_(0){}
   24 
   25     void start();           // start the timer
   26     void stop();            // stop the timer
   27 
   28     time_t tstart() const { return t0.tv_sec;} // time we started
   29     double elapsed_seconds() const;  // how long timer has been running, total
   30     double lap_time() const;         // how long the timer is running this time
   31     double eta(double fraction_done) const; // calculate ETA in seconds, given fraction
   32     std::string hms(long t) const;  // turn a number of seconds into h:m:s
   33     std::string elapsed_text() const;       /* how long we have been running */
   34     std::string eta_text(double fraction_done) const; // h:m:s
   35     std::string eta_time(double fraction_done) const; // the actual time
   36 };
   37 
   38 /* This code in part from 
   39  * http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/430449b3-f6dd-4e18-84de-eebd26a8d668
   40  */
   41 
   42 #if defined(WIN32) || defined(__MINGW32__)
   43 #  include <winsock2.h>
   44 #  include <windows.h>          
   45 #  ifndef DELTA_EPOCH_IN_MICROSECS
   46 #    if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
   47 #      define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
   48 #    else
   49 #      define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
   50 #    endif
   51 #  endif
   52 #endif
   53 
   54 inline void timestamp(struct timeval *t)
   55 {
   56 #ifdef WIN32
   57     FILETIME ft;
   58     GetSystemTimeAsFileTime(&ft);
   59     unsigned __int64 tmpres = 0;
   60     tmpres |= ft.dwHighDateTime;
   61     tmpres <<= 32;
   62     tmpres |= ft.dwLowDateTime;
   63  
   64     /*converting file time to unix epoch*/
   65     tmpres -= DELTA_EPOCH_IN_MICROSECS; 
   66     tmpres /= 10;  /*convert into microseconds*/
   67     t->tv_sec = (long)(tmpres / 1000000UL);
   68     t->tv_usec = (long)(tmpres % 1000000UL);
   69 #else
   70     gettimeofday(t,NULL);
   71 #endif    
   72 }
   73 
   74 inline void aftimer::start()
   75 {
   76     timestamp(&t0);
   77     running = 1;
   78 }
   79 
   80 inline void aftimer::stop(){
   81     if(running){
   82     struct timeval t;
   83     timestamp(&t);
   84     total_sec  += t.tv_sec - t0.tv_sec;
   85     total_usec += t.tv_usec - t0.tv_usec;
   86     lap_time_   = (double)(t.tv_sec - t0.tv_sec)  + (double)(t.tv_usec - t0.tv_usec)/1000000.0;
   87     running = false;
   88     }
   89 }
   90 
   91 inline double aftimer::lap_time() const
   92 {
   93     return lap_time_;
   94 }
   95 
   96 inline double aftimer::elapsed_seconds() const
   97 {
   98     double ret = (double)total_sec + (double)total_usec/1000000.0;
   99     if(running){
  100     struct timeval t;
  101     timestamp(&t);
  102     ret += t.tv_sec - t0.tv_sec;
  103     ret += (t.tv_usec - t0.tv_usec) / 1000000.0;
  104     }
  105     return ret;
  106 }
  107 
  108 inline std::string aftimer::hms(long t) const
  109 {
  110     char   buf[64];
  111     int    days = t / (60*60*24);
  112     
  113     t = t % (60*60*24);         /* what's left */
  114 
  115     int    h = t / 3600;
  116     int    m = (t / 60) % 60;
  117     int    s = t % 60;
  118     buf[0] = 0;
  119     switch(days){
  120     case 0:
  121     snprintf(buf,sizeof(buf),"%2d:%02d:%02d",h,m,s);
  122     break;
  123     case 1:
  124     snprintf(buf,sizeof(buf),"%d day, %2d:%02d:%02d",days,h,m,s);
  125     break;
  126     default:
  127     snprintf(buf,sizeof(buf),"%d days %2d:%02d:%02d",days,h,m,s);
  128     }
  129     return std::string(buf);
  130 }
  131 
  132 inline std::string aftimer::elapsed_text() const
  133 {
  134     return hms((int)elapsed_seconds());
  135 }
  136 
  137 /**
  138  * returns the number of seconds until the job is complete.
  139  */
  140 inline double aftimer::eta(double fraction_done) const
  141 {
  142     double t = elapsed_seconds();
  143     if(t<=0) return -1;         // can't figure it out
  144     if(fraction_done<=0) return -1; // can't figure it out
  145     return (t * 1.0/fraction_done - t);
  146 }
  147 
  148 /**
  149  * Retuns the number of hours:minutes:seconds until the job is done.
  150  */
  151 inline std::string aftimer::eta_text(double fraction_done) const
  152 {
  153     double e = eta(fraction_done);
  154     if(e<0) return std::string("n/a");      // can't figure it out
  155     return hms((long)e);
  156 }
  157 
  158 /**
  159  * Returns the time when data is due.
  160  */
  161 inline std::string aftimer::eta_time(double fraction_done) const 
  162 {
  163     time_t t = time_t(eta(fraction_done)) + time(0);
  164     struct tm tm;
  165 #ifdef HAVE_LOCALTIME_R
  166     localtime_r(&t,&tm);
  167 #else
  168     tm = *localtime(&t);
  169 #endif
  170     
  171     char buf[64];
  172     snprintf(buf,sizeof(buf),"%02d:%02d:%02d",tm.tm_hour,tm.tm_min,tm.tm_sec);
  173     return std::string(buf);
  174 }
  175 
  176 #endif
  177 
  178 #endif