2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:09:55 +02:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2014-02-12 21:08:23 +01:00
|
|
|
#include <cinttypes>
|
2014-02-19 01:54:11 +01:00
|
|
|
#include <ctime>
|
2014-02-20 04:11:52 +01:00
|
|
|
#include <string>
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-01-21 20:55:01 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <mmsystem.h>
|
2010-07-22 06:15:11 +02:00
|
|
|
#include <sys/timeb.h>
|
2014-02-19 01:54:11 +01:00
|
|
|
#include <windows.h>
|
2010-11-20 04:24:51 +01:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2010-01-21 20:55:01 +01:00
|
|
|
|
2014-02-20 04:11:52 +01:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/Timer.h"
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-01-21 20:55:01 +01:00
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
u32 Timer::GetTimeMs()
|
|
|
|
{
|
2010-01-21 21:49:45 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
return timeGetTime();
|
2015-01-28 20:28:01 +01:00
|
|
|
#elif defined __APPLE__
|
2010-11-20 04:24:51 +01:00
|
|
|
struct timeval t;
|
2014-03-09 21:14:26 +01:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2010-11-20 04:24:51 +01:00
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
|
2015-01-28 20:28:01 +01:00
|
|
|
#else
|
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000));
|
2008-12-08 06:30:24 +01:00
|
|
|
#endif
|
2010-07-22 06:15:11 +02:00
|
|
|
}
|
2009-02-16 07:18:18 +01:00
|
|
|
|
2014-11-19 19:57:12 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
double GetFreq()
|
|
|
|
{
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
return 1000000.0 / double(freq.QuadPart);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 Timer::GetTimeUs()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER time;
|
|
|
|
static double freq = GetFreq();
|
|
|
|
QueryPerformanceCounter(&time);
|
|
|
|
return u64(double(time.QuadPart) * freq);
|
2015-01-28 20:28:01 +01:00
|
|
|
#elif defined __APPLE__
|
2014-11-19 19:57:12 +01:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_usec));
|
2015-01-28 20:28:01 +01:00
|
|
|
#else
|
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000));
|
2014-11-19 19:57:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-28 09:57:34 +01:00
|
|
|
// --------------------------------------------
|
2009-02-16 07:18:18 +01:00
|
|
|
// Initiate, Start, Stop, and Update the time
|
2009-03-28 09:57:34 +01:00
|
|
|
// --------------------------------------------
|
2009-02-16 07:18:18 +01:00
|
|
|
|
|
|
|
// Set initial values for the class
|
2009-02-22 13:43:25 +01:00
|
|
|
Timer::Timer()
|
2009-02-16 07:18:18 +01:00
|
|
|
: m_LastTime(0), m_StartTime(0), m_Running(false)
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
|
2009-02-16 07:18:18 +01:00
|
|
|
// Write the starting time
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
2010-01-21 21:49:45 +01:00
|
|
|
m_StartTime = GetTimeMs();
|
2009-02-16 07:18:18 +01:00
|
|
|
m_Running = true;
|
|
|
|
}
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2009-02-16 07:18:18 +01:00
|
|
|
// Stop the timer
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
|
|
|
// Write the final time
|
2010-01-21 21:49:45 +01:00
|
|
|
m_LastTime = GetTimeMs();
|
2009-02-16 07:18:18 +01:00
|
|
|
m_Running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
2009-02-22 13:43:25 +01:00
|
|
|
void Timer::Update()
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2010-01-21 21:49:45 +01:00
|
|
|
m_LastTime = GetTimeMs();
|
2008-12-08 06:30:24 +01:00
|
|
|
//TODO(ector) - QPF
|
|
|
|
}
|
2009-02-16 07:18:18 +01:00
|
|
|
|
2009-03-28 09:57:34 +01:00
|
|
|
// -------------------------------------
|
2009-02-16 07:18:18 +01:00
|
|
|
// Get time difference and elapsed time
|
2009-03-28 09:57:34 +01:00
|
|
|
// -------------------------------------
|
2009-02-16 07:18:18 +01:00
|
|
|
|
|
|
|
// Get the number of milliseconds since the last Update()
|
2010-01-07 21:01:41 +01:00
|
|
|
u64 Timer::GetTimeDifference()
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
2010-01-21 21:49:45 +01:00
|
|
|
return GetTimeMs() - m_LastTime;
|
2008-12-08 06:30:24 +01:00
|
|
|
}
|
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
// Add the time difference since the last Update() to the starting time.
|
|
|
|
// This is used to compensate for a paused game.
|
2009-02-16 07:18:18 +01:00
|
|
|
void Timer::AddTimeDifference()
|
|
|
|
{
|
|
|
|
m_StartTime += GetTimeDifference();
|
|
|
|
}
|
2010-07-22 06:15:11 +02:00
|
|
|
|
2009-02-16 07:18:18 +01:00
|
|
|
// Get the time elapsed since the Start()
|
2009-02-22 13:43:25 +01:00
|
|
|
u64 Timer::GetTimeElapsed()
|
2009-02-16 07:18:18 +01:00
|
|
|
{
|
2010-07-22 06:15:11 +02:00
|
|
|
// If we have not started yet, return 1 (because then I don't
|
|
|
|
// have to change the FPS calculation in CoreRerecording.cpp .
|
2009-02-16 07:18:18 +01:00
|
|
|
if (m_StartTime == 0) return 1;
|
|
|
|
|
2010-07-22 06:21:10 +02:00
|
|
|
// Return the final timer time if the timer is stopped
|
2009-02-16 07:18:18 +01:00
|
|
|
if (!m_Running) return (m_LastTime - m_StartTime);
|
2008-12-08 06:30:24 +01:00
|
|
|
|
2010-01-21 21:49:45 +01:00
|
|
|
return (GetTimeMs() - m_StartTime);
|
2009-02-16 07:18:18 +01:00
|
|
|
}
|
|
|
|
|
2010-07-22 06:21:10 +02:00
|
|
|
// Get the formatted time elapsed since the Start()
|
2009-02-22 13:43:25 +01:00
|
|
|
std::string Timer::GetTimeElapsedFormatted() const
|
2009-02-16 07:18:18 +01:00
|
|
|
{
|
|
|
|
// If we have not started yet, return zero
|
2009-02-20 23:04:52 +01:00
|
|
|
if (m_StartTime == 0)
|
|
|
|
return "00:00:00:000";
|
2009-02-16 07:18:18 +01:00
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
// The number of milliseconds since the start.
|
|
|
|
// Use a different value if the timer is stopped.
|
2009-02-22 13:43:25 +01:00
|
|
|
u64 Milliseconds;
|
2009-02-20 23:04:52 +01:00
|
|
|
if (m_Running)
|
2010-01-21 22:27:52 +01:00
|
|
|
Milliseconds = GetTimeMs() - m_StartTime;
|
2009-02-16 07:18:18 +01:00
|
|
|
else
|
|
|
|
Milliseconds = m_LastTime - m_StartTime;
|
|
|
|
// Seconds
|
2009-02-22 13:43:25 +01:00
|
|
|
u32 Seconds = (u32)(Milliseconds / 1000);
|
2009-02-16 07:18:18 +01:00
|
|
|
// Minutes
|
|
|
|
u32 Minutes = Seconds / 60;
|
|
|
|
// Hours
|
|
|
|
u32 Hours = Minutes / 60;
|
|
|
|
|
2014-02-12 21:08:23 +01:00
|
|
|
std::string TmpStr = StringFromFormat("%02i:%02i:%02i:%03" PRIu64,
|
2010-07-22 06:15:11 +02:00
|
|
|
Hours, Minutes % 60, Seconds % 60, Milliseconds % 1000);
|
2009-02-16 07:18:18 +01:00
|
|
|
return TmpStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current time
|
2008-12-08 06:30:24 +01:00
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeEndPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-16 07:18:18 +01:00
|
|
|
// Get the number of seconds since January 1 1970
|
2009-02-22 13:43:25 +01:00
|
|
|
u64 Timer::GetTimeSinceJan1970()
|
2008-12-08 06:30:24 +01:00
|
|
|
{
|
|
|
|
time_t ltime;
|
|
|
|
time(<ime);
|
|
|
|
return((u64)ltime);
|
|
|
|
}
|
2008-12-13 12:57:01 +01:00
|
|
|
|
2009-02-22 13:43:25 +01:00
|
|
|
u64 Timer::GetLocalTimeSinceJan1970()
|
2008-12-13 12:57:01 +01:00
|
|
|
{
|
2010-08-26 21:24:47 +02:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
2008-12-13 12:57:01 +01:00
|
|
|
struct tm * gmTime;
|
|
|
|
|
|
|
|
time(&sysTime);
|
2010-08-26 21:24:47 +02:00
|
|
|
|
|
|
|
// Account for DST where needed
|
|
|
|
gmTime = localtime(&sysTime);
|
2014-03-10 12:30:55 +01:00
|
|
|
if (gmTime->tm_isdst == 1)
|
2010-08-26 21:24:47 +02:00
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
|
|
|
|
2008-12-13 12:57:01 +01:00
|
|
|
// Lazy way to get local time in sec
|
2014-02-17 05:51:41 +01:00
|
|
|
gmTime = gmtime(&sysTime);
|
2008-12-13 12:57:01 +01:00
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
|
|
|
|
2010-08-26 21:24:47 +02:00
|
|
|
return (u64)(sysTime + tzDiff + tzDST);
|
2008-12-13 12:57:01 +01:00
|
|
|
}
|
2009-01-15 07:48:15 +01:00
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
2009-02-22 13:43:25 +01:00
|
|
|
std::string Timer::GetTimeFormatted()
|
2009-01-15 07:48:15 +01:00
|
|
|
{
|
2009-03-15 06:34:35 +01:00
|
|
|
time_t sysTime;
|
|
|
|
time(&sysTime);
|
2014-03-29 11:05:44 +01:00
|
|
|
|
2014-02-08 02:23:34 +01:00
|
|
|
struct tm * gmTime = localtime(&sysTime);
|
2009-03-22 00:00:25 +01:00
|
|
|
|
2014-02-08 02:23:34 +01:00
|
|
|
char tmp[13];
|
2009-03-22 00:00:25 +01:00
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
|
|
|
|
2009-03-15 06:34:35 +01:00
|
|
|
// Now tack on the milliseconds
|
2010-07-22 06:15:11 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2014-02-08 02:23:34 +01:00
|
|
|
return StringFromFormat("%s:%03i", tmp, tp.millitm);
|
2015-01-28 20:28:01 +01:00
|
|
|
#elif defined __APPLE__
|
2010-11-20 04:24:51 +01:00
|
|
|
struct timeval t;
|
2014-03-09 21:14:26 +01:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2014-02-08 02:23:34 +01:00
|
|
|
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_usec / 1000));
|
2015-01-28 20:28:01 +01:00
|
|
|
#else
|
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return StringFromFormat("%s:%03d", tmp, (int)(t.tv_nsec / 1000000));
|
2010-07-22 06:15:11 +02:00
|
|
|
#endif
|
2009-01-15 07:48:15 +01:00
|
|
|
}
|
2009-02-16 07:18:18 +01:00
|
|
|
|
2009-08-25 08:34:58 +02:00
|
|
|
// Returns a timestamp with decimals for precise time comparisons
|
2009-09-08 18:07:13 +02:00
|
|
|
// ----------------
|
2009-08-25 08:34:58 +02:00
|
|
|
double Timer::GetDoubleTime()
|
|
|
|
{
|
2010-07-22 06:15:11 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2015-01-28 20:28:01 +01:00
|
|
|
#elif defined __APPLE__
|
2010-11-20 04:24:51 +01:00
|
|
|
struct timeval t;
|
2014-03-09 21:14:26 +01:00
|
|
|
(void)gettimeofday(&t, nullptr);
|
2015-01-28 20:28:01 +01:00
|
|
|
#else
|
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
2010-07-22 06:15:11 +02:00
|
|
|
#endif
|
2010-07-22 06:21:10 +02:00
|
|
|
// Get continuous timestamp
|
2010-07-22 06:15:11 +02:00
|
|
|
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
|
2009-08-25 08:34:58 +02:00
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
// Remove a few years. We only really want enough seconds to make
|
|
|
|
// sure that we are detecting actual actions, perhaps 60 seconds is
|
|
|
|
// enough really, but I leave a year of seconds anyway, in case the
|
|
|
|
// user's clock is incorrect or something like that.
|
2009-08-25 08:34:58 +02:00
|
|
|
TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60);
|
|
|
|
|
2010-07-22 06:15:11 +02:00
|
|
|
// Make a smaller integer that fits in the double
|
|
|
|
u32 Seconds = (u32)TmpSeconds;
|
|
|
|
#ifdef _WIN32
|
2010-07-22 06:21:10 +02:00
|
|
|
double ms = tp.millitm / 1000.0 / 1000.0;
|
2015-01-28 20:28:01 +01:00
|
|
|
#elif defined __APPLE__
|
2010-11-22 03:42:53 +01:00
|
|
|
double ms = t.tv_usec / 1000000.0;
|
2015-01-28 20:28:01 +01:00
|
|
|
#else
|
|
|
|
double ms = t.tv_nsec / 1000000000.0;
|
2010-07-22 06:15:11 +02:00
|
|
|
#endif
|
2009-08-25 08:34:58 +02:00
|
|
|
double TmpTime = Seconds + ms;
|
2010-07-22 06:15:11 +02:00
|
|
|
|
2009-08-25 08:34:58 +02:00
|
|
|
return TmpTime;
|
|
|
|
}
|
|
|
|
|
2009-03-28 09:57:34 +01:00
|
|
|
} // Namespace Common
|