ext-cryptopp/hrtimer.cpp

141 lines
3.3 KiB
C++
Raw Normal View History

2002-10-04 17:31:41 +00:00
// hrtimer.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "hrtimer.h"
2003-06-19 19:09:57 +00:00
#include "misc.h"
#include "trap.h"
2002-10-04 17:31:41 +00:00
#include <stddef.h> // for NULL
2007-05-05 18:29:44 +00:00
#include <time.h>
2002-10-04 17:31:41 +00:00
#if defined(CRYPTOPP_WIN32_AVAILABLE)
#include <windows.h>
2002-10-04 21:45:04 +00:00
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
2002-10-04 17:31:41 +00:00
#include <sys/time.h>
2004-04-21 08:39:59 +00:00
#include <sys/times.h>
2004-04-08 01:23:05 +00:00
#include <unistd.h>
2002-10-04 17:31:41 +00:00
#endif
NAMESPACE_BEGIN(CryptoPP)
#ifndef CRYPTOPP_IMPORTS
double TimerBase::ConvertTo(TimerWord t, Unit unit)
2002-10-04 17:31:41 +00:00
{
2004-06-20 17:56:15 +00:00
static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
CRYPTOPP_ASSERT(unit < COUNTOF(unitsPerSecondTable));
return (double)CRYPTOPP_VC6_INT64 t * unitsPerSecondTable[unit] / CRYPTOPP_VC6_INT64 TicksPerSecond();
2002-10-04 17:31:41 +00:00
}
2004-06-20 17:56:15 +00:00
void TimerBase::StartTimer()
2002-10-04 17:31:41 +00:00
{
m_last = m_start = GetCurrentTimerValue();
2004-06-20 17:56:15 +00:00
m_started = true;
}
double TimerBase::ElapsedTimeAsDouble()
{
if (m_stuckAtZero)
return 0;
if (m_started)
2002-10-04 17:31:41 +00:00
{
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);
2002-10-04 17:31:41 +00:00
}
StartTimer();
return 0;
2004-06-20 17:56:15 +00:00
}
unsigned long TimerBase::ElapsedTime()
{
double elapsed = ElapsedTimeAsDouble();
CRYPTOPP_ASSERT(elapsed <= ULONG_MAX);
2004-06-20 17:56:15 +00:00
return (unsigned long)elapsed;
2003-06-19 17:09:07 +00:00
}
TimerWord Timer::GetCurrentTimerValue()
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
LARGE_INTEGER now;
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;
gettimeofday(&now, NULL);
return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
#else
clock_t now;
return clock();
#endif
}
TimerWord Timer::TicksPerSecond()
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
// Hack to silence GCC under MinGW
static LARGE_INTEGER freq = { {0,0} };
if (freq.QuadPart == 0)
{
if (!QueryPerformanceFrequency(&freq))
throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
}
return freq.QuadPart;
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
return 1000000;
#else
2007-05-05 02:15:11 +00:00
return CLOCKS_PER_SEC;
#endif
}
#endif // #ifndef CRYPTOPP_IMPORTS
TimerWord ThreadUserTimer::GetCurrentTimerValue()
2004-04-08 01:23:05 +00:00
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
static bool getCurrentThreadImplemented = true;
if (getCurrentThreadImplemented)
{
FILETIME now, ignored;
if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now))
{
DWORD lastError = GetLastError();
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);
2004-04-08 01:23:05 +00:00
}
GetCurrentThreadNotImplemented:
return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
2004-04-08 01:23:05 +00:00
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
tms now;
times(&now);
return now.tms_utime;
2004-06-20 17:56:15 +00:00
#else
return clock();
2004-04-08 01:23:05 +00:00
#endif
}
TimerWord ThreadUserTimer::TicksPerSecond()
2004-04-08 01:23:05 +00:00
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
return 10*1000*1000;
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
return ticksPerSecond;
2004-06-20 17:56:15 +00:00
#else
return CLOCKS_PER_SEC;
2004-04-08 01:23:05 +00:00
#endif
}
2002-10-04 17:31:41 +00:00
NAMESPACE_END