MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/util/stopwatch.h
Go to the documentation of this file.
00001 #ifndef MECHSYS_STOPWATCH_H
00002 #define MECHSYS_STOPWATCH_H
00003 
00004 // Std Lib
00005 #include <stdio.h>        // for printf
00006 #include <sys/time.h>     // for gettimeofday, getrusage
00007 #include <sys/resource.h> // for getrusage
00008 #include <time.h>         // for localtime
00009 
00010 // proc
00011 #ifdef HAS_PROC
00012   #include <proc/readproc.h>
00013   #ifdef FUNCTION
00014     #undef FUNCTION
00015   #endif
00016 #endif
00017 
00018 // mechsys
00019 #include <mechsys/util/string.h>
00020 
00021 #define CONVERT_TO_HMS(seconds,H,M,S)                         \
00022     H  = (unsigned int) ((unsigned int)seconds / 3600);       \
00023     M  = (unsigned int)(((unsigned int)seconds % 3600) / 60); \
00024     S  = seconds-(unsigned int)(H*3600)-(unsigned int)(M*60);
00025 
00026 namespace Util
00027 {
00028 
00029 class Stopwatch
00030 {
00031 public:
00032     // Constructor
00033      Stopwatch (bool Activated=true, bool MemUsage=true);
00034 
00035     // Destructor
00036     ~Stopwatch (); 
00037 
00038     // Methods
00039     double CPUTime () const; 
00040 
00041 private:
00042     bool    _activated; 
00043     bool    _mem_usage; 
00044     timeval _start;     
00045 };
00046 
00047 
00049 
00050 
00051 inline Stopwatch::Stopwatch (bool Activated, bool MemUsage)
00052     : _activated(Activated), _mem_usage(MemUsage)
00053 {
00054     // initial time
00055     if (_activated) gettimeofday (&_start, NULL);
00056 }
00057 
00058 inline Stopwatch::~Stopwatch ()
00059 {
00060     // skip if not activated
00061     if (!_activated) return;
00062 
00063     // final time
00064     timeval end;
00065     gettimeofday (&end, NULL);
00066 
00067     // interval
00068     double dif = end.tv_sec + (end.tv_usec/1000000.0) - _start.tv_sec - (_start.tv_usec/1000000.0);
00069     double cpu = CPUTime();
00070 
00071     // time H:M:S
00072     tm   t1 = (*(localtime (&_start.tv_sec)));
00073     tm * t2 =    localtime (&   end.tv_sec);
00074 
00075     // output time
00076     if (dif>60)
00077     {
00078         int h,m,H,M;
00079         double s,S;
00080         CONVERT_TO_HMS (dif,h,m,s)
00081         CONVERT_TO_HMS (cpu,H,M,S)
00082         printf("%s  Elapsed time       = %dh%dm%gs  CPU %dh%dm%gs  (%d:%02d:%02d => %d:%02d:%02d)%s\n", TERM_CLR3, h,m,s, H,M,S,
00083                 t1. tm_hour, t1. tm_min, t1. tm_sec,
00084                 t2->tm_hour, t2->tm_min, t2->tm_sec, TERM_RST);
00085     }
00086     else
00087     {
00088         printf("%s  Elapsed time       = %.6lfs  CPU %.6lfs  (%d:%02d:%02d => %d:%02d:%02d)%s\n", TERM_CLR3, dif, cpu,
00089                 t1. tm_hour, t1. tm_min, t1. tm_sec,
00090                 t2->tm_hour, t2->tm_min, t2->tm_sec, TERM_RST);
00091     }
00092 
00093     // memory usage
00094     if (_mem_usage)
00095     {
00096 #ifdef HAS_PROC
00097         proc_t p;
00098         look_up_our_self (&p);
00099         printf("%s  Process memory     = %lu [kb]  %lu [Mb]%s\n", TERM_CLR5, p.vsize/1024, p.vsize/1048576, TERM_RST);
00100 #endif
00101     }
00102 }
00103 
00104 inline double Stopwatch::CPUTime() const
00105 {
00106     /* The timeval struct used to measure time has only two fields, and
00107        both are unsigned ints. They are named tv_sec and tv_usec, and
00108        jointly represent one single value. tv_sec*1000000+tv_usec gives the
00109        number of microseconds. http://rabbit.eng.miami.edu/info/functions/time.html#gtod */
00110 
00111     /* This function returns the total CPU time consumed by the current
00112        process, measured in seconds, as a double precision floating point number.
00113        It adds together the user time and the system time.
00114        Note: Although the format used is capable of measuring time to an accuracy
00115        of a microsecond, do not expect that much precision from any real system. */
00116 
00117     timeval tim;
00118     rusage  ru;
00119     getrusage (RUSAGE_SELF, &ru);
00120     tim = ru.ru_utime;
00121     double t = (double)tim.tv_sec + (double)tim.tv_usec/1000000.0;
00122     tim = ru.ru_stime;
00123     t += (double)tim.tv_sec + (double)tim.tv_usec/1000000.0;
00124     return t;
00125 }
00126 
00127 }; // namespace Util
00128 
00129 #endif // MECHSYS_STOPWATCH_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines