2015-05-24 07:55:12 +03:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 04:22:19 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2019-06-14 17:53:46 +03:00
|
|
|
#include "Common/Timer.h"
|
|
|
|
|
2020-07-12 20:36:58 +03:00
|
|
|
#include <chrono>
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2010-01-21 21:55:01 +02:00
|
|
|
#ifdef _WIN32
|
2022-07-18 06:43:47 +03:00
|
|
|
#include <Windows.h>
|
2022-07-18 06:59:31 +03:00
|
|
|
#include <ctime>
|
|
|
|
#include <timeapi.h>
|
2010-11-20 05:24:51 +02:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2010-01-21 21:55:01 +02:00
|
|
|
|
2014-02-20 05:11:52 +02:00
|
|
|
#include "Common/CommonTypes.h"
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2010-01-21 21:55:01 +02:00
|
|
|
namespace Common
|
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
template <typename Clock, typename Duration>
|
|
|
|
static typename Clock::rep time_now()
|
2010-07-22 07:15:11 +03:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
return std::chrono::time_point_cast<Duration>(Clock::now()).time_since_epoch().count();
|
2010-07-22 07:15:11 +03:00
|
|
|
}
|
2009-02-16 08:18:18 +02:00
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
template <typename Duration>
|
|
|
|
static auto steady_time_now()
|
2014-11-19 20:57:12 +02:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
return time_now<std::chrono::steady_clock, Duration>();
|
2014-11-19 20:57:12 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
u64 Timer::NowUs()
|
2014-11-19 20:57:12 +02:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
return steady_time_now<std::chrono::microseconds>();
|
2014-11-19 20:57:12 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
u64 Timer::NowMs()
|
2008-07-12 20:40:22 +03:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
return steady_time_now<std::chrono::milliseconds>();
|
2008-07-12 20:40:22 +03:00
|
|
|
}
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2009-02-16 08:18:18 +02:00
|
|
|
void Timer::Start()
|
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
m_start_ms = NowMs();
|
|
|
|
m_end_ms = 0;
|
|
|
|
m_running = true;
|
2009-02-16 08:18:18 +02:00
|
|
|
}
|
2008-12-08 07:30:24 +02:00
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
void Timer::StartWithOffset(u64 offset)
|
2009-02-16 08:18:18 +02:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
Start();
|
|
|
|
if (m_start_ms > offset)
|
|
|
|
m_start_ms -= offset;
|
2009-02-16 08:18:18 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
void Timer::Stop()
|
2009-02-16 08:18:18 +02:00
|
|
|
{
|
2022-07-18 06:43:47 +03:00
|
|
|
m_end_ms = NowMs();
|
|
|
|
m_running = false;
|
2009-02-16 08:18:18 +02:00
|
|
|
}
|
2010-07-22 07:15:11 +03:00
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
u64 Timer::ElapsedMs() const
|
2009-02-16 08:18:18 +02:00
|
|
|
{
|
2020-07-01 22:29:30 +03:00
|
|
|
// If we have not started yet, return zero
|
2022-07-18 06:43:47 +03:00
|
|
|
if (m_start_ms == 0)
|
2020-07-01 22:29:30 +03:00
|
|
|
return 0;
|
2009-02-16 08:18:18 +02:00
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
if (m_running)
|
|
|
|
{
|
|
|
|
u64 now = NowMs();
|
|
|
|
if (m_start_ms >= now)
|
|
|
|
return 0;
|
|
|
|
return now - m_start_ms;
|
|
|
|
}
|
2009-02-16 08:18:18 +02:00
|
|
|
else
|
2022-07-18 06:43:47 +03:00
|
|
|
{
|
|
|
|
if (m_start_ms >= m_end_ms)
|
|
|
|
return 0;
|
|
|
|
return m_end_ms - m_start_ms;
|
|
|
|
}
|
2008-07-12 20:40:22 +03:00
|
|
|
}
|
2008-12-13 13:57:01 +02:00
|
|
|
|
2009-02-22 14:43:25 +02:00
|
|
|
u64 Timer::GetLocalTimeSinceJan1970()
|
2008-12-13 13:57:01 +02:00
|
|
|
{
|
2010-08-26 22:24:47 +03:00
|
|
|
time_t sysTime, tzDiff, tzDST;
|
2008-12-13 13:57:01 +02:00
|
|
|
time(&sysTime);
|
2016-07-13 23:46:14 +03:00
|
|
|
tm* gmTime = localtime(&sysTime);
|
2010-08-26 22:24:47 +03:00
|
|
|
|
|
|
|
// Account for DST where needed
|
2014-03-10 13:30:55 +02:00
|
|
|
if (gmTime->tm_isdst == 1)
|
2010-08-26 22:24:47 +03:00
|
|
|
tzDST = 3600;
|
|
|
|
else
|
|
|
|
tzDST = 0;
|
|
|
|
|
2008-12-13 13:57:01 +02:00
|
|
|
// Lazy way to get local time in sec
|
|
|
|
gmTime = gmtime(&sysTime);
|
|
|
|
tzDiff = sysTime - mktime(gmTime);
|
|
|
|
|
2016-07-13 23:46:14 +03:00
|
|
|
return static_cast<u64>(sysTime + tzDiff + tzDST);
|
2008-12-13 13:57:01 +02:00
|
|
|
}
|
2009-01-15 08:48:15 +02:00
|
|
|
|
2022-07-18 06:43:47 +03:00
|
|
|
void Timer::IncreaseResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeBeginPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::RestoreResolution()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
timeEndPeriod(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-28 10:57:34 +02:00
|
|
|
} // Namespace Common
|