Loading src/Examples/CMakeLists.txt +7 −1 Original line number Diff line number Diff line Loading @@ -47,6 +47,10 @@ ADD_EXECUTABLE( StringExampleStrip StringExampleStrip.cpp ) ADD_CUSTOM_COMMAND( COMMAND StringExampleStrip > StringExampleStrip.out OUTPUT StringExampleStrip.out ) ADD_EXECUTABLE( TimerExample TimerExample.cpp ) ADD_CUSTOM_COMMAND( COMMAND TimerExample > TimerExample.out OUTPUT TimerExample.out ) ADD_EXECUTABLE( TimerExampleLogger TimerExampleLogger.cpp ) ADD_CUSTOM_COMMAND( COMMAND TimerExampleLogger > TimerExampleLogger.out OUTPUT TimerExampleLogger.out ) ADD_EXECUTABLE( VectorExample VectorExample.cpp ) Loading @@ -55,4 +59,6 @@ ADD_CUSTOM_TARGET( run ALL DEPENDS StringExampleGetAllocatedSize.out StringExampleReplace.out StringExampleSplit.out StringExampleStrip.out ) StringExampleStrip.out TimerExample.out TimerExampleLogger.out ) src/Examples/TimerExample.cpp +7 −7 Original line number Diff line number Diff line #include <iostream> #include <TNL/Timer.h> #include <TNL/Logger.h> #include <unistd.h> using namespace TNL; Loading @@ -13,11 +12,12 @@ int main() time.start(); usleep(microseconds); time.stop(); cout << "before reset:" << time.getRealTime() << endl; cout << "Elapsed real time: " << time.getRealTime() << endl; cout << "Elapsed CPU time: " << time.getCPUTime() << endl; cout << "Elapsed CPU cycles: " << time.getCPUCycles() << endl; time.reset(); cout << "after reset:" << time.getRealTime() << endl; // writeLog example Logger log1(50,cout); time.writeLog( log1, 0 ); cout << "Real time after reset:" << time.getRealTime() << endl; cout << "CPU time after reset: " << time.getCPUTime() << endl; cout << "CPU cycles after reset: " << time.getCPUCycles() << endl; } src/Examples/TimerExampleLogger.cpp 0 → 100644 +19 −0 Original line number Diff line number Diff line #include <iostream> #include <TNL/Timer.h> #include <TNL/Logger.h> #include <unistd.h> using namespace TNL; using namespace std; int main() { unsigned int microseconds = 0.5; Timer time; time.start(); usleep(microseconds); time.stop(); Logger logger( 50,cout ); time.writeLog( logger, 0 ); } src/TNL/Timer.h +93 −78 Original line number Diff line number Diff line Loading @@ -16,78 +16,91 @@ namespace TNL { class Logger; /// \brief Class for time measuring. /// /// Counts the elapsed time in seconds between the \ref start and \ref stop methods. /// \par Example /// \include TimerExample.cpp // \par Output // \include TimerExample.out /** * \brief Class for time measuring. * * Counts the elapsed real time and CPU time in seconds together with CPU cycles * between the \ref start and \ref stop methods. * * \par Example * \include TimerExample.cpp * \par Output * \include TimerExample.out */ class Timer { public: ///// /// \brief Basic constructor. /// /// This function creates a new timer and resets it. /** * \brief Basic constructor. * * This function creates a new timer and resets it. */ Timer(); ///// /// \brief Resets timer. /// /// Resets all time and cycle measurements such as real time, CPU time and CPU cycles. /// Sets all of them to zero. /** * \brief Resets timer. * * Resets all time and cycle measurements such as real time, CPU time and * CPU cycles. Sets all of them to zero. */ void reset(); //// /// \brief Stops (pauses) the timer. /// /// Pauses all time and cycle measurements such as real time, CPU time and /// CPU cycles, but does not set them to zero. /** * \brief Stops (pauses) the timer. * * Pauses all time and cycle measurements such as real time, CPU time and * CPU cycles, but does not set them to zero. */ void stop(); ///// /// \brief Starts timer. /// /// Starts all time and cycle measurements such as real time, CPU time and /// CPU cycles. This method can be used also after using the \ref stop method. /// The timer then continues measuring the time without reseting. /** * \brief Starts timer. * * Starts all time and cycle measurements such as real time, CPU time and * CPU cycles. This method can be used also after using the \ref stop * method. The timer then continues measuring the time without reseting. */ void start(); ///// /// \brief Returns the elapsed time on given timer. /// /// It returns the elapsed time (in seconds) between calling the \ref start and \ref stop methods. /// Starts counting the real time after the method \ref start is called and /// pauses when the method \ref stop is called. /// If the timer has been started more then once without resetting, /// the real time is counted by adding all intervals (between \ref start and \ref stop /// methods) together. /// This function can be called while the timer is running, there is no /// need to use \ref stop method first. /** * \brief Returns the elapsed real time on this timer. * * This method returns the real time elapsed so far (in seconds). * This method can be called while the timer is running, there is no * need to use \ref stop method first. */ double getRealTime() const; ///// /// \brief Returns the elapsed CPU time on given timer. /// /// The CPU time is measured in seconds. /// CPU time is the amount of time for which a central processing unit (CPU) /// was used for processing instructions of a computer program or operating system. /// The CPU time is measured by adding the amount of CPU time between \ref start and \ref stop /// methods together. /** * \brief Returns the elapsed CPU time on this timer. * * This method returns the CPU time (i.e. time the CPU spent by processing * this process) elapsed so far (in seconds). This method can be called * while the timer is running, there is no need to use \ref stop method * first. */ double getCPUTime() const; /// \brief Returns the number of CPU cycles (machine cycles). /// /// CPU cycles are counted by adding the number of CPU cycles between \ref start and \ref stop /// methods together. /** * \brief Returns the number of CPU cycles (machine cycles) elapsed on this timer. * * CPU cycles are counted by adding the number of CPU cycles between * \ref start and \ref stop methods together. */ unsigned long long int getCPUCycles() const; /// \brief Writes a record into the \e logger. /// /// \param logger Name of Logger object. /// \param logLevel A non-negative integer recording the log record indent. /** * \brief Writes a record into the \e logger. * * \param logger Name of Logger object. * \param logLevel A non-negative integer recording the log record indent. * * \par Example * \include TimerExampleLogger.cpp * \par Output * \include TimerExampleLogger.out */ bool writeLog( Logger& logger, int logLevel = 0 ) const; protected: Loading @@ -95,43 +108,45 @@ class Timer using TimePoint = typename std::chrono::high_resolution_clock::time_point; using Duration = typename std::chrono::high_resolution_clock::duration; /// \brief Function for measuring the real time. /** * \brief Function for measuring the real time. */ TimePoint readRealTime() const; /// \brief Function for measuring the CPU time. /// /// CPU time is the amount of time for which a central processing unit (CPU) /// was used for processing instructions of a computer program or operating system. /** * \brief Function for measuring the CPU time. */ double readCPUTime() const; /// \brief Function for counting the number of CPU cycles (machine cycles). /** * \brief Function for counting the number of CPU cycles (machine cycles). */ unsigned long long int readCPUCycles() const; /** * \brief Converts the real time into seconds as a floating point number. */ double durationToDouble( const Duration& duration ) const; /** * \brief Time Stamp Counter returning number of CPU cycles since reset. * * Only for x86 compatible CPUs. */ inline unsigned long long rdtsc() const; TimePoint initialRealTime; Duration totalRealTime; double initialCPUTime, totalCPUTime; unsigned long long int initialCPUCycles, totalCPUCycles; /// \brief Saves information about the state of given timer. /// /// Knows whether the timer is currently stopped or it is running. bool stopState; /// \brief Time Stamp Counter returning number of CPU cycles since reset. /// /// Only for x86 compatibile CPUs. inline unsigned long long rdtsc() const { unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( ( unsigned long long ) lo ) | ( ( ( unsigned long long ) hi ) << 32 ); } }; } // namespace TNL #include <TNL/Timer_impl.h> #include <TNL/Timer.hpp> src/TNL/Timer_impl.h→src/TNL/Timer.hpp +12 −6 Original line number Diff line number Diff line Loading @@ -78,6 +78,14 @@ inline unsigned long long int Timer::getCPUCycles() const return this->totalCPUCycles; } inline bool Timer::writeLog( Logger& logger, int logLevel ) const { logger.writeParameter< double >( "Real time:", this->getRealTime(), logLevel ); logger.writeParameter< double >( "CPU time:", this->getCPUTime(), logLevel ); logger.writeParameter< unsigned long long int >( "CPU Cycles:", this->getCPUCycles(), logLevel ); return true; } inline typename Timer::TimePoint Timer::readRealTime() const { return std::chrono::high_resolution_clock::now(); Loading Loading @@ -105,13 +113,11 @@ inline double Timer::durationToDouble( const Duration& duration ) const return dur.count(); } inline bool Timer::writeLog( Logger& logger, int logLevel ) const inline unsigned long long Timer::rdtsc() const { logger.writeParameter< double >( "Real time:", this->getRealTime(), logLevel ); logger.writeParameter< double >( "CPU time:", this->getCPUTime(), logLevel ); logger.writeParameter< unsigned long long int >( "CPU Cycles:", this->getCPUCycles(), logLevel ); return true; unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( ( unsigned long long ) lo ) | ( ( ( unsigned long long ) hi ) << 32 ); } } // namespace TNL Loading
src/Examples/CMakeLists.txt +7 −1 Original line number Diff line number Diff line Loading @@ -47,6 +47,10 @@ ADD_EXECUTABLE( StringExampleStrip StringExampleStrip.cpp ) ADD_CUSTOM_COMMAND( COMMAND StringExampleStrip > StringExampleStrip.out OUTPUT StringExampleStrip.out ) ADD_EXECUTABLE( TimerExample TimerExample.cpp ) ADD_CUSTOM_COMMAND( COMMAND TimerExample > TimerExample.out OUTPUT TimerExample.out ) ADD_EXECUTABLE( TimerExampleLogger TimerExampleLogger.cpp ) ADD_CUSTOM_COMMAND( COMMAND TimerExampleLogger > TimerExampleLogger.out OUTPUT TimerExampleLogger.out ) ADD_EXECUTABLE( VectorExample VectorExample.cpp ) Loading @@ -55,4 +59,6 @@ ADD_CUSTOM_TARGET( run ALL DEPENDS StringExampleGetAllocatedSize.out StringExampleReplace.out StringExampleSplit.out StringExampleStrip.out ) StringExampleStrip.out TimerExample.out TimerExampleLogger.out )
src/Examples/TimerExample.cpp +7 −7 Original line number Diff line number Diff line #include <iostream> #include <TNL/Timer.h> #include <TNL/Logger.h> #include <unistd.h> using namespace TNL; Loading @@ -13,11 +12,12 @@ int main() time.start(); usleep(microseconds); time.stop(); cout << "before reset:" << time.getRealTime() << endl; cout << "Elapsed real time: " << time.getRealTime() << endl; cout << "Elapsed CPU time: " << time.getCPUTime() << endl; cout << "Elapsed CPU cycles: " << time.getCPUCycles() << endl; time.reset(); cout << "after reset:" << time.getRealTime() << endl; // writeLog example Logger log1(50,cout); time.writeLog( log1, 0 ); cout << "Real time after reset:" << time.getRealTime() << endl; cout << "CPU time after reset: " << time.getCPUTime() << endl; cout << "CPU cycles after reset: " << time.getCPUCycles() << endl; }
src/Examples/TimerExampleLogger.cpp 0 → 100644 +19 −0 Original line number Diff line number Diff line #include <iostream> #include <TNL/Timer.h> #include <TNL/Logger.h> #include <unistd.h> using namespace TNL; using namespace std; int main() { unsigned int microseconds = 0.5; Timer time; time.start(); usleep(microseconds); time.stop(); Logger logger( 50,cout ); time.writeLog( logger, 0 ); }
src/TNL/Timer.h +93 −78 Original line number Diff line number Diff line Loading @@ -16,78 +16,91 @@ namespace TNL { class Logger; /// \brief Class for time measuring. /// /// Counts the elapsed time in seconds between the \ref start and \ref stop methods. /// \par Example /// \include TimerExample.cpp // \par Output // \include TimerExample.out /** * \brief Class for time measuring. * * Counts the elapsed real time and CPU time in seconds together with CPU cycles * between the \ref start and \ref stop methods. * * \par Example * \include TimerExample.cpp * \par Output * \include TimerExample.out */ class Timer { public: ///// /// \brief Basic constructor. /// /// This function creates a new timer and resets it. /** * \brief Basic constructor. * * This function creates a new timer and resets it. */ Timer(); ///// /// \brief Resets timer. /// /// Resets all time and cycle measurements such as real time, CPU time and CPU cycles. /// Sets all of them to zero. /** * \brief Resets timer. * * Resets all time and cycle measurements such as real time, CPU time and * CPU cycles. Sets all of them to zero. */ void reset(); //// /// \brief Stops (pauses) the timer. /// /// Pauses all time and cycle measurements such as real time, CPU time and /// CPU cycles, but does not set them to zero. /** * \brief Stops (pauses) the timer. * * Pauses all time and cycle measurements such as real time, CPU time and * CPU cycles, but does not set them to zero. */ void stop(); ///// /// \brief Starts timer. /// /// Starts all time and cycle measurements such as real time, CPU time and /// CPU cycles. This method can be used also after using the \ref stop method. /// The timer then continues measuring the time without reseting. /** * \brief Starts timer. * * Starts all time and cycle measurements such as real time, CPU time and * CPU cycles. This method can be used also after using the \ref stop * method. The timer then continues measuring the time without reseting. */ void start(); ///// /// \brief Returns the elapsed time on given timer. /// /// It returns the elapsed time (in seconds) between calling the \ref start and \ref stop methods. /// Starts counting the real time after the method \ref start is called and /// pauses when the method \ref stop is called. /// If the timer has been started more then once without resetting, /// the real time is counted by adding all intervals (between \ref start and \ref stop /// methods) together. /// This function can be called while the timer is running, there is no /// need to use \ref stop method first. /** * \brief Returns the elapsed real time on this timer. * * This method returns the real time elapsed so far (in seconds). * This method can be called while the timer is running, there is no * need to use \ref stop method first. */ double getRealTime() const; ///// /// \brief Returns the elapsed CPU time on given timer. /// /// The CPU time is measured in seconds. /// CPU time is the amount of time for which a central processing unit (CPU) /// was used for processing instructions of a computer program or operating system. /// The CPU time is measured by adding the amount of CPU time between \ref start and \ref stop /// methods together. /** * \brief Returns the elapsed CPU time on this timer. * * This method returns the CPU time (i.e. time the CPU spent by processing * this process) elapsed so far (in seconds). This method can be called * while the timer is running, there is no need to use \ref stop method * first. */ double getCPUTime() const; /// \brief Returns the number of CPU cycles (machine cycles). /// /// CPU cycles are counted by adding the number of CPU cycles between \ref start and \ref stop /// methods together. /** * \brief Returns the number of CPU cycles (machine cycles) elapsed on this timer. * * CPU cycles are counted by adding the number of CPU cycles between * \ref start and \ref stop methods together. */ unsigned long long int getCPUCycles() const; /// \brief Writes a record into the \e logger. /// /// \param logger Name of Logger object. /// \param logLevel A non-negative integer recording the log record indent. /** * \brief Writes a record into the \e logger. * * \param logger Name of Logger object. * \param logLevel A non-negative integer recording the log record indent. * * \par Example * \include TimerExampleLogger.cpp * \par Output * \include TimerExampleLogger.out */ bool writeLog( Logger& logger, int logLevel = 0 ) const; protected: Loading @@ -95,43 +108,45 @@ class Timer using TimePoint = typename std::chrono::high_resolution_clock::time_point; using Duration = typename std::chrono::high_resolution_clock::duration; /// \brief Function for measuring the real time. /** * \brief Function for measuring the real time. */ TimePoint readRealTime() const; /// \brief Function for measuring the CPU time. /// /// CPU time is the amount of time for which a central processing unit (CPU) /// was used for processing instructions of a computer program or operating system. /** * \brief Function for measuring the CPU time. */ double readCPUTime() const; /// \brief Function for counting the number of CPU cycles (machine cycles). /** * \brief Function for counting the number of CPU cycles (machine cycles). */ unsigned long long int readCPUCycles() const; /** * \brief Converts the real time into seconds as a floating point number. */ double durationToDouble( const Duration& duration ) const; /** * \brief Time Stamp Counter returning number of CPU cycles since reset. * * Only for x86 compatible CPUs. */ inline unsigned long long rdtsc() const; TimePoint initialRealTime; Duration totalRealTime; double initialCPUTime, totalCPUTime; unsigned long long int initialCPUCycles, totalCPUCycles; /// \brief Saves information about the state of given timer. /// /// Knows whether the timer is currently stopped or it is running. bool stopState; /// \brief Time Stamp Counter returning number of CPU cycles since reset. /// /// Only for x86 compatibile CPUs. inline unsigned long long rdtsc() const { unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( ( unsigned long long ) lo ) | ( ( ( unsigned long long ) hi ) << 32 ); } }; } // namespace TNL #include <TNL/Timer_impl.h> #include <TNL/Timer.hpp>
src/TNL/Timer_impl.h→src/TNL/Timer.hpp +12 −6 Original line number Diff line number Diff line Loading @@ -78,6 +78,14 @@ inline unsigned long long int Timer::getCPUCycles() const return this->totalCPUCycles; } inline bool Timer::writeLog( Logger& logger, int logLevel ) const { logger.writeParameter< double >( "Real time:", this->getRealTime(), logLevel ); logger.writeParameter< double >( "CPU time:", this->getCPUTime(), logLevel ); logger.writeParameter< unsigned long long int >( "CPU Cycles:", this->getCPUCycles(), logLevel ); return true; } inline typename Timer::TimePoint Timer::readRealTime() const { return std::chrono::high_resolution_clock::now(); Loading Loading @@ -105,13 +113,11 @@ inline double Timer::durationToDouble( const Duration& duration ) const return dur.count(); } inline bool Timer::writeLog( Logger& logger, int logLevel ) const inline unsigned long long Timer::rdtsc() const { logger.writeParameter< double >( "Real time:", this->getRealTime(), logLevel ); logger.writeParameter< double >( "CPU time:", this->getCPUTime(), logLevel ); logger.writeParameter< unsigned long long int >( "CPU Cycles:", this->getCPUCycles(), logLevel ); return true; unsigned hi, lo; __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); return ( ( unsigned long long ) lo ) | ( ( ( unsigned long long ) hi ) << 32 ); } } // namespace TNL