2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include "Common/Timer.h"
|
|
|
|
|
2014-02-19 00:54:11 +00:00
|
|
|
#include <ctime>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <string>
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2010-01-21 19:55:01 +00:00
|
|
|
#ifdef _WIN32
|
2018-10-26 12:19:16 +00:00
|
|
|
#include <cwchar>
|
|
|
|
|
2017-01-23 07:08:45 +00:00
|
|
|
#include <windows.h>
|
2010-01-21 19:55:01 +00:00
|
|
|
#include <mmsystem.h>
|
2010-07-22 04:15:11 +00:00
|
|
|
#include <sys/timeb.h>
|
2010-11-20 03:24:51 +00:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2010-01-21 19:55:01 +00:00
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2010-01-21 19:55:01 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2010-07-22 04:15:11 +00:00
|
|
|
u32 Timer::GetTimeMs()
|
|
|
|
{
|
2010-01-21 20:49:45 +00:00
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
return timeGetTime();
|
2015-01-28 19:28:01 +00:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_usec / 1000));
|
2015-01-28 19:28:01 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u32)(t.tv_sec * 1000 + t.tv_nsec / 1000000));
|
2008-12-08 05:30:24 +00:00
|
|
|
#endif
|
2010-07-22 04:15:11 +00:00
|
|
|
}
|
2009-02-16 06:18:18 +00:00
|
|
|
|
2014-11-19 18:57:12 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
double GetFreq()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
LARGE_INTEGER freq;
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
return 1000000.0 / double(freq.QuadPart);
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 Timer::GetTimeUs()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
static double freq = GetFreq();
|
|
|
|
QueryPerformanceCounter(&time);
|
|
|
|
return u64(double(time.QuadPart) * freq);
|
2015-01-28 19:28:01 +00:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_usec));
|
2015-01-28 19:28:01 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
|
|
|
return ((u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000));
|
2014-11-19 18:57:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
// --------------------------------------------
|
2009-02-16 06:18:18 +00:00
|
|
|
// Initiate, Start, Stop, and Update the time
|
2009-03-28 08:57:34 +00:00
|
|
|
// --------------------------------------------
|
2009-02-16 06:18:18 +00:00
|
|
|
|
|
|
|
// Set initial values for the class
|
2016-06-24 08:43:46 +00:00
|
|
|
Timer::Timer() : m_LastTime(0), m_StartTime(0), m_Running(false)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Update();
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2009-02-16 06:18:18 +00:00
|
|
|
// Write the starting time
|
|
|
|
void Timer::Start()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_StartTime = GetTimeMs();
|
|
|
|
m_Running = true;
|
2009-02-16 06:18:18 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-02-16 06:18:18 +00:00
|
|
|
// Stop the timer
|
|
|
|
void Timer::Stop()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Write the final time
|
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
m_Running = false;
|
2009-02-16 06:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the last time variable
|
2009-02-22 12:43:25 +00:00
|
|
|
void Timer::Update()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_LastTime = GetTimeMs();
|
|
|
|
// TODO(ector) - QPF
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-02-16 06:18:18 +00:00
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
// -------------------------------------
|
2009-02-16 06:18:18 +00:00
|
|
|
// Get time difference and elapsed time
|
2009-03-28 08:57:34 +00:00
|
|
|
// -------------------------------------
|
2009-02-16 06:18:18 +00:00
|
|
|
|
|
|
|
// Get the number of milliseconds since the last Update()
|
2010-01-07 20:01:41 +00:00
|
|
|
u64 Timer::GetTimeDifference()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return GetTimeMs() - m_LastTime;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 04:15:11 +00: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 06:18:18 +00:00
|
|
|
void Timer::AddTimeDifference()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_StartTime += GetTimeDifference();
|
2009-02-16 06:18:18 +00:00
|
|
|
}
|
2010-07-22 04:15:11 +00:00
|
|
|
|
2009-02-16 06:18:18 +00:00
|
|
|
// Get the time elapsed since the Start()
|
2009-02-22 12:43:25 +00:00
|
|
|
u64 Timer::GetTimeElapsed()
|
2009-02-16 06:18:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// If we have not started yet, return 1 (because then I don't
|
|
|
|
// have to change the FPS calculation in CoreRerecording.cpp .
|
|
|
|
if (m_StartTime == 0)
|
|
|
|
return 1;
|
2009-02-16 06:18:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Return the final timer time if the timer is stopped
|
|
|
|
if (!m_Running)
|
|
|
|
return (m_LastTime - m_StartTime);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return (GetTimeMs() - m_StartTime);
|
2009-02-16 06:18:18 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 04:21:10 +00:00
|
|
|
// Get the formatted time elapsed since the Start()
|
2009-02-22 12:43:25 +00:00
|
|
|
std::string Timer::GetTimeElapsedFormatted() const
|
2009-02-16 06:18:18 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// If we have not started yet, return zero
|
|
|
|
if (m_StartTime == 0)
|
|
|
|
return "00:00:00:000";
|
|
|
|
|
|
|
|
// The number of milliseconds since the start.
|
|
|
|
// Use a different value if the timer is stopped.
|
|
|
|
u64 Milliseconds;
|
|
|
|
if (m_Running)
|
|
|
|
Milliseconds = GetTimeMs() - m_StartTime;
|
|
|
|
else
|
|
|
|
Milliseconds = m_LastTime - m_StartTime;
|
|
|
|
// Seconds
|
|
|
|
u32 Seconds = (u32)(Milliseconds / 1000);
|
|
|
|
// Minutes
|
|
|
|
u32 Minutes = Seconds / 60;
|
|
|
|
// Hours
|
|
|
|
u32 Hours = Minutes / 60;
|
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{:02}:{:02}:{:02}:{:03}", Hours, Minutes % 60, Seconds % 60,
|
|
|
|
Milliseconds % 1000);
|
2009-02-16 06:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get current time
|
2008-12-08 05:30:24 +00:00
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
timeBeginPeriod(1);
|
2008-12-08 05:30:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
timeEndPeriod(1);
|
2008-12-08 05:30:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-16 06:18:18 +00:00
|
|
|
// Get the number of seconds since January 1 1970
|
2009-02-22 12:43:25 +00:00
|
|
|
u64 Timer::GetTimeSinceJan1970()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
time_t ltime;
|
|
|
|
time(<ime);
|
|
|
|
return ((u64)ltime);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2008-12-13 11:57:01 +00:00
|
|
|
|
2009-02-22 12:43:25 +00:00
|
|
|
u64 Timer::GetLocalTimeSinceJan1970()
|
2008-12-13 11:57:01 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
|
|
|
time(&sysTime);
|
2016-07-13 20:46:14 +00:00
|
|
|
tm* gmTime = localtime(&sysTime);
|
2010-08-26 19:24:47 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Account for DST where needed
|
|
|
|
if (gmTime->tm_isdst == 1)
|
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
2010-08-26 19:24:47 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Lazy way to get local time in sec
|
|
|
|
gmTime = gmtime(&sysTime);
|
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
2008-12-13 11:57:01 +00:00
|
|
|
|
2016-07-13 20:46:14 +00:00
|
|
|
return static_cast<u64>(sysTime + tzDiff + tzDST);
|
2008-12-13 11:57:01 +00:00
|
|
|
}
|
2009-01-15 06:48:15 +00:00
|
|
|
|
2010-07-22 04:15:11 +00:00
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
2009-02-22 12:43:25 +00:00
|
|
|
std::string Timer::GetTimeFormatted()
|
2009-01-15 06:48:15 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
time_t sysTime;
|
|
|
|
time(&sysTime);
|
2014-03-29 10:05:44 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
struct tm* gmTime = localtime(&sysTime);
|
2009-03-21 23:00:25 +00:00
|
|
|
|
2018-10-26 12:19:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
wchar_t tmp[13];
|
|
|
|
wcsftime(tmp, 6, L"%M:%S", gmTime);
|
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
char tmp[13];
|
|
|
|
strftime(tmp, 6, "%M:%S", gmTime);
|
2018-10-26 12:19:16 +00:00
|
|
|
#endif
|
2009-03-21 23:00:25 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Now tack on the milliseconds
|
2010-07-22 04:15:11 +00:00
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2019-06-14 14:53:46 +00:00
|
|
|
return UTF16ToUTF8(tmp) + fmt::format(":{:03}", tp.millitm);
|
2015-01-28 19:28:01 +00:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}:{:03}", tmp, t.tv_usec / 1000);
|
2015-01-28 19:28:01 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
2019-06-14 14:53:46 +00:00
|
|
|
return fmt::format("{}:{:03}", tmp, t.tv_nsec / 1000000);
|
2010-07-22 04:15:11 +00:00
|
|
|
#endif
|
2009-01-15 06:48:15 +00:00
|
|
|
}
|
2009-02-16 06:18:18 +00:00
|
|
|
|
2009-08-25 06:34:58 +00:00
|
|
|
// Returns a timestamp with decimals for precise time comparisons
|
|
|
|
double Timer::GetDoubleTime()
|
|
|
|
{
|
2010-07-22 04:15:11 +00:00
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2015-01-28 19:28:01 +00:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, nullptr);
|
2015-01-28 19:28:01 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
struct timespec t;
|
|
|
|
(void)clock_gettime(CLOCK_MONOTONIC, &t);
|
2010-07-22 04:15:11 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
// Get continuous timestamp
|
|
|
|
u64 TmpSeconds = Common::Timer::GetTimeSinceJan1970();
|
2009-08-25 06:34:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00: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.
|
|
|
|
TmpSeconds = TmpSeconds - DOUBLE_TIME_OFFSET;
|
2009-08-25 06:34:58 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Make a smaller integer that fits in the double
|
|
|
|
u32 Seconds = (u32)TmpSeconds;
|
2010-07-22 04:15:11 +00:00
|
|
|
#ifdef _WIN32
|
2016-06-24 08:43:46 +00:00
|
|
|
double ms = tp.millitm / 1000.0 / 1000.0;
|
2015-01-28 19:28:01 +00:00
|
|
|
#elif defined __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
double ms = t.tv_usec / 1000000.0;
|
2015-01-28 19:28:01 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
double ms = t.tv_nsec / 1000000000.0;
|
2010-07-22 04:15:11 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
double TmpTime = Seconds + ms;
|
2010-07-22 04:15:11 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return TmpTime;
|
2009-08-25 06:34:58 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 23:49:12 +00:00
|
|
|
// Formats a timestamp from GetDoubleTime() into a date and time string
|
|
|
|
std::string Timer::GetDateTimeFormatted(double time)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// revert adjustments from GetDoubleTime() to get a normal Unix timestamp again
|
|
|
|
time_t seconds = (time_t)time + DOUBLE_TIME_OFFSET;
|
|
|
|
tm* localTime = localtime(&seconds);
|
2015-07-11 23:49:12 +00:00
|
|
|
|
2018-10-26 12:19:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
wchar_t tmp[32] = {};
|
|
|
|
wcsftime(tmp, sizeof(tmp), L"%x %X", localTime);
|
|
|
|
return UTF16ToUTF8(tmp);
|
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
char tmp[32] = {};
|
|
|
|
strftime(tmp, sizeof(tmp), "%x %X", localTime);
|
|
|
|
return tmp;
|
2018-10-26 12:19:16 +00:00
|
|
|
#endif
|
2015-07-11 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
} // Namespace Common
|