Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:03

0001 // hrtimer.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file hrtimer.h

0004 /// \brief Classes for timers

0005 
0006 #ifndef CRYPTOPP_HRTIMER_H
0007 #define CRYPTOPP_HRTIMER_H
0008 
0009 #include "config.h"
0010 
0011 #if !defined(HIGHRES_TIMER_AVAILABLE) || (defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE))
0012 #include <time.h>
0013 #endif
0014 
0015 NAMESPACE_BEGIN(CryptoPP)
0016 
0017 #ifdef HIGHRES_TIMER_AVAILABLE
0018     /// \brief TimerWord is a 64-bit word

0019     typedef word64 TimerWord;
0020 #else
0021     /// \brief TimerWord is a clock_t

0022     typedef clock_t TimerWord;
0023 #endif
0024 
0025 /// \brief Base class for timers

0026 /// \sa ThreadUserTimer, Timer

0027 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE TimerBase
0028 {
0029 public:
0030     /// \brief Unit of measure

0031     /// \details Unit selects the unit of measure as returned by functions

0032     ///  ElapsedTimeAsDouble() and ElapsedTime().

0033     /// \sa ElapsedTimeAsDouble, ElapsedTime

0034     enum Unit {
0035         /// \brief Timer unit is seconds

0036         /// \details All timers support seconds

0037         SECONDS = 0,
0038         /// \brief Timer unit is milliseconds

0039         /// \details All timers support milliseconds

0040         MILLISECONDS,
0041         /// \brief Timer unit is microseconds

0042         /// \details The timer requires hardware support microseconds

0043         MICROSECONDS,
0044         /// \brief Timer unit is nanoseconds

0045         /// \details The timer requires hardware support nanoseconds

0046         NANOSECONDS
0047     };
0048 
0049     /// \brief Construct a TimerBase

0050     /// \param unit the unit of measure

0051     /// \param stuckAtZero flag

0052     TimerBase(Unit unit, bool stuckAtZero)
0053         : m_timerUnit(unit), m_stuckAtZero(stuckAtZero), m_started(false)
0054         , m_start(0), m_last(0) {}
0055 
0056     /// \brief Retrieve the current timer value

0057     /// \return the current timer value

0058     virtual TimerWord GetCurrentTimerValue() =0;
0059 
0060     /// \brief Retrieve ticks per second

0061     /// \return ticks per second

0062     /// \details TicksPerSecond() is not the timer resolution. It is a

0063     ///  conversion factor into seconds.

0064     virtual TimerWord TicksPerSecond() =0;
0065 
0066     /// \brief Start the timer

0067     void StartTimer();
0068 
0069     /// \brief Retrieve the elapsed time

0070     /// \return the elapsed time as a double

0071     /// \details The return value of ElapsedTimeAsDouble() depends upon

0072     ///  the Unit selected during construction of the timer. For example,

0073     ///  if <tt>Unit = SECONDS</tt> and ElapsedTimeAsDouble() returns 3,

0074     ///  then the timer has run for 3 seconds. If

0075     ///  <tt>Unit = MILLISECONDS</tt> and ElapsedTimeAsDouble() returns

0076     ///  3000, then the timer has run for 3 seconds.

0077     /// \sa Unit, ElapsedTime

0078     double ElapsedTimeAsDouble();
0079 
0080     /// \brief Retrieve the elapsed time

0081     /// \return the elapsed time as an unsigned long

0082     /// \details The return value of ElapsedTime() depends upon the

0083     ///  Unit selected during construction of the timer. For example, if

0084     ///  <tt>Unit = SECONDS</tt> and ElapsedTime() returns 3, then

0085     ///  the timer has run for 3 seconds. If <tt>Unit = MILLISECONDS</tt>

0086     ///  and ElapsedTime() returns 3000, then the timer has run for 3

0087     ///  seconds.

0088     /// \sa Unit, ElapsedTimeAsDouble

0089     unsigned long ElapsedTime();
0090 
0091 private:
0092     double ConvertTo(TimerWord t, Unit unit);
0093 
0094     Unit m_timerUnit;   // HPUX workaround: m_unit is a system macro on HPUX

0095     bool m_stuckAtZero, m_started;
0096     TimerWord m_start, m_last;
0097 };
0098 
0099 /// \brief Measure CPU time spent executing instructions of this thread

0100 /// \details ThreadUserTimer requires support of the OS. On Unix-based it

0101 ///  reports process time. On Windows NT or later desktops and servers it

0102 ///  reports thread times with performance counter precision.. On Windows

0103 ///  Phone and Windows Store it reports wall clock time with performance

0104 ///  counter precision. On all others it reports wall clock time.

0105 /// \note ThreadUserTimer only works correctly on Windows NT or later

0106 ///  desktops and servers.

0107 /// \sa Timer

0108 class ThreadUserTimer : public TimerBase
0109 {
0110 public:
0111     /// \brief Construct a ThreadUserTimer

0112     /// \param unit the unit of measure

0113     /// \param stuckAtZero flag

0114     ThreadUserTimer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
0115     TimerWord GetCurrentTimerValue();
0116     TimerWord TicksPerSecond();
0117 };
0118 
0119 /// \brief High resolution timer

0120 /// \sa ThreadUserTimer

0121 class CRYPTOPP_DLL Timer : public TimerBase
0122 {
0123 public:
0124     /// \brief Construct a Timer

0125     /// \param unit the unit of measure

0126     /// \param stuckAtZero flag

0127     Timer(Unit unit = TimerBase::SECONDS, bool stuckAtZero = false) : TimerBase(unit, stuckAtZero) {}
0128     TimerWord GetCurrentTimerValue();
0129     TimerWord TicksPerSecond();
0130 };
0131 
0132 NAMESPACE_END
0133 
0134 #endif