Clock.cpp
Go to the documentation of this file.
00001 static const char rcsid[] = "$Id: Clock.cpp,v 1.9 2002/09/15 22:40:50 bastiaan Exp $";
00002 
00003 /* 
00004  * See the COPYING file for the terms of usage and distribution.
00005  */
00006 
00007 #include <cstdlib>
00008 #include <sys/time.h>                   // for struct timeval
00009 #ifdef __osf__
00010 #    include <machine/builtins.h>       // for __RPCC()
00011 #elif __linux__ && __i386__
00012 #    define rdtscl(low) \
00013      __asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")
00014 #endif
00015 #include <iostream>
00016 
00017 #include "Clock.hh"
00018 
00019 namespace 
00020 {
00021     const usec_t UsecPerSec = INT64_CONSTANT(1000000);
00022 }
00023 
00024 bool Clock::UsingCPU  = std::getenv("CLOCK_USE_CPU") ? true : false;
00025 
00026 // -----------------------------------------------------------------------------
00027 usec_t Clock::time(void)
00028 {
00029     if (UsingCPU) {
00030         static bool warn = true;
00031                 
00032         if (warn) {
00033             std::cout << "Using CPU clock." << std::endl;
00034             warn = false;
00035         }
00036 
00037 #ifdef __osf__
00038         return (usec_t) __RPCC();
00039 #elif __linux__ && __i386__
00040         {
00041             unsigned long tsc;
00042                         
00043             rdtscl(tsc);
00044             return (usec_t) tsc;
00045         }
00046 #else
00047         {
00048             std::cerr << "CPU clock not implemented for this architecture" << std::endl;
00049             UsingCPU = false;
00050             return Clock::time();
00051         }
00052 #endif  
00053     } else {
00054         struct timeval tv;
00055         
00056         gettimeofday(&tv, NULL);        
00057         return (usec_t) (tv.tv_sec * UsecPerSec + tv.tv_usec);
00058     }
00059 }
00060 
00061 // -----------------------------------------------------------------------------
00062 Clock::Clock(void)
00063     : _start(0),
00064       _elapsed(0),
00065       _active(false) 
00066 {
00067     start();
00068 }
00069 
00070 // -----------------------------------------------------------------------------
00071 Clock::~Clock(void)
00072 {
00073     ;
00074 }
00075 
00076 // -----------------------------------------------------------------------------
00077 usec_t Clock::elapsed(void) const
00078 {
00079     if (!active())
00080         return _elapsed;
00081 
00082     return time() - _start;
00083 }
00084 
00085 
00086 // -----------------------------------------------------------------------------
00087 usec_t Clock::start(void)
00088 {
00089     _active = true;
00090 
00091     return _start = time();
00092 }
00093 
00094 // -----------------------------------------------------------------------------
00095 usec_t Clock::stop(void)
00096 {
00097     _elapsed = elapsed();
00098     _active  = false;
00099 
00100     return _elapsed;
00101 }
00102 


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14