2017-01-27 12:05:45 +00:00
|
|
|
// hrtimer.cpp - originally written and placed in the public domain by Wei Dai
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "hrtimer.h"
|
|
|
|
#include "misc.h"
|
2016-05-03 04:23:05 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#include <stddef.h> // for NULL
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE)
|
2016-04-28 05:05:47 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-11-05 06:59:46 +00:00
|
|
|
#include <windows.h>
|
2016-05-03 04:23:05 +00:00
|
|
|
# if ((WINVER >= 0x0602 /*_WIN32_WINNT_WIN8*/) || (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/))
|
|
|
|
# include <processthreadsapi.h>
|
|
|
|
# if defined(WINAPI_FAMILY)
|
|
|
|
# if (WINAPI_FAMILY_PARTITION(WINAPI_FAMILY_PHONE_APP))
|
|
|
|
# include <profileapi.h>
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CRYPTOPP_UNIX_AVAILABLE)
|
2015-11-05 06:59:46 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2016-09-16 15:27:15 +00:00
|
|
|
#include "trap.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE)
|
2016-05-17 07:05:18 +00:00
|
|
|
static TimerWord InitializePerformanceCounterFrequency()
|
2016-05-03 04:23:05 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER freq = {0,0};
|
|
|
|
if (!QueryPerformanceFrequency(&freq))
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
|
|
|
|
return freq.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline TimerWord PerformanceCounterFrequency()
|
|
|
|
{
|
|
|
|
static const word64 freq = InitializePerformanceCounterFrequency();
|
|
|
|
return freq;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-26 07:13:18 +00:00
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
double TimerBase::ConvertTo(TimerWord t, Unit unit)
|
|
|
|
{
|
|
|
|
static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
|
|
|
|
|
|
|
|
// When 'unit' is an enum 'Unit', a Clang warning is generated.
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(static_cast<unsigned int>(unit) < COUNTOF(unitsPerSecondTable));
|
2016-12-03 10:05:56 +00:00
|
|
|
return static_cast<double>(t) * unitsPerSecondTable[unit] / TicksPerSecond();
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimerBase::StartTimer()
|
|
|
|
{
|
|
|
|
m_last = m_start = GetCurrentTimerValue();
|
|
|
|
m_started = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimerBase::ElapsedTimeAsDouble()
|
|
|
|
{
|
|
|
|
if (m_stuckAtZero)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (m_started)
|
|
|
|
{
|
|
|
|
TimerWord now = GetCurrentTimerValue();
|
|
|
|
if (m_last < now) // protect against OS bugs where time goes backwards
|
|
|
|
m_last = now;
|
|
|
|
return ConvertTo(m_last - m_start, m_timerUnit);
|
|
|
|
}
|
|
|
|
|
|
|
|
StartTimer();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long TimerBase::ElapsedTime()
|
|
|
|
{
|
|
|
|
double elapsed = ElapsedTimeAsDouble();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(elapsed <= (double)ULONG_MAX);
|
2015-11-05 06:59:46 +00:00
|
|
|
return (unsigned long)elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimerWord Timer::GetCurrentTimerValue()
|
|
|
|
{
|
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE)
|
|
|
|
// Use the first union member to avoid an uninitialized warning
|
|
|
|
LARGE_INTEGER now = {0,0};
|
|
|
|
if (!QueryPerformanceCounter(&now))
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
|
|
|
|
return now.QuadPart;
|
|
|
|
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
|
|
|
|
timeval now;
|
2017-03-01 11:10:06 +00:00
|
|
|
gettimeofday(&now, NULLPTR);
|
2015-11-05 06:59:46 +00:00
|
|
|
return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
|
|
|
|
#else
|
|
|
|
// clock_t now;
|
|
|
|
return clock();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TimerWord Timer::TicksPerSecond()
|
|
|
|
{
|
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE)
|
2016-05-03 04:23:05 +00:00
|
|
|
return PerformanceCounterFrequency();
|
2015-11-05 06:59:46 +00:00
|
|
|
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
|
|
|
|
return 1000000;
|
|
|
|
#else
|
|
|
|
return CLOCKS_PER_SEC;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // #ifndef CRYPTOPP_IMPORTS
|
|
|
|
|
|
|
|
TimerWord ThreadUserTimer::GetCurrentTimerValue()
|
|
|
|
{
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE) && defined(THREAD_TIMER_AVAILABLE)
|
2015-11-05 06:59:46 +00:00
|
|
|
static bool getCurrentThreadImplemented = true;
|
|
|
|
if (getCurrentThreadImplemented)
|
|
|
|
{
|
|
|
|
FILETIME now, ignored;
|
|
|
|
if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now))
|
|
|
|
{
|
2016-05-03 04:23:05 +00:00
|
|
|
const DWORD lastError = GetLastError();
|
2015-11-05 06:59:46 +00:00
|
|
|
if (lastError == ERROR_CALL_NOT_IMPLEMENTED)
|
|
|
|
{
|
|
|
|
getCurrentThreadImplemented = false;
|
|
|
|
goto GetCurrentThreadNotImplemented;
|
|
|
|
}
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "ThreadUserTimer: GetThreadTimes failed with error " + IntToString(lastError));
|
|
|
|
}
|
|
|
|
return now.dwLowDateTime + ((TimerWord)now.dwHighDateTime << 32);
|
|
|
|
}
|
|
|
|
GetCurrentThreadNotImplemented:
|
|
|
|
return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
|
2016-05-03 04:23:05 +00:00
|
|
|
#elif defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE)
|
|
|
|
LARGE_INTEGER now;
|
|
|
|
if (!QueryPerformanceCounter(&now))
|
|
|
|
{
|
|
|
|
const DWORD lastError = GetLastError();
|
2016-09-10 08:57:48 +00:00
|
|
|
throw Exception(Exception::OTHER_ERROR, "ThreadUserTimer: QueryPerformanceCounter failed with error " + IntToString(lastError));
|
2016-05-03 04:23:05 +00:00
|
|
|
}
|
|
|
|
return now.QuadPart;
|
2015-11-05 06:59:46 +00:00
|
|
|
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
|
|
|
|
tms now;
|
|
|
|
times(&now);
|
|
|
|
return now.tms_utime;
|
|
|
|
#else
|
|
|
|
return clock();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TimerWord ThreadUserTimer::TicksPerSecond()
|
|
|
|
{
|
2016-05-03 04:23:05 +00:00
|
|
|
#if defined(CRYPTOPP_WIN32_AVAILABLE) && defined(THREAD_TIMER_AVAILABLE)
|
2015-11-05 06:59:46 +00:00
|
|
|
return 10*1000*1000;
|
2016-05-03 04:23:05 +00:00
|
|
|
#elif defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE)
|
|
|
|
static const TimerWord ticksPerSecond = PerformanceCounterFrequency();
|
|
|
|
return ticksPerSecond;
|
2015-11-05 06:59:46 +00:00
|
|
|
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
|
|
|
|
static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
|
|
|
|
return ticksPerSecond;
|
|
|
|
#else
|
|
|
|
return CLOCKS_PER_SEC;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|