2023-08-02 21:52:46 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2017-02-24 19:26:38 +00:00
|
|
|
#include <cstdio>
|
2020-09-29 11:46:43 +00:00
|
|
|
#include <cstdint>
|
2020-10-05 18:58:33 +00:00
|
|
|
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2024-06-05 08:29:04 +00:00
|
|
|
#include "Common/Log.h"
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2020-10-05 18:58:33 +00:00
|
|
|
#ifdef HAVE_LIBNX
|
|
|
|
#include <switch.h>
|
|
|
|
#endif // HAVE_LIBNX
|
|
|
|
|
2023-08-12 19:38:35 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif // __EMSCRIPTEN__
|
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
2020-10-05 18:58:33 +00:00
|
|
|
#include "CommonWindows.h"
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#include <sys/timeb.h>
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2023-08-07 19:38:03 +00:00
|
|
|
|
|
|
|
// for _mm_pause
|
|
|
|
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
|
|
|
|
#include <immintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2021-02-14 17:49:14 +00:00
|
|
|
#include <ctime>
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2022-05-29 19:06:20 +00:00
|
|
|
// TODO: https://github.com/floooh/sokol/blob/9a6237fcdf213e6da48e4f9201f144bcb2dcb46f/sokol_time.h#L229-L248
|
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
constexpr double micros = 1000000.0;
|
|
|
|
constexpr double nanos = 1000000000.0;
|
2023-08-03 09:11:16 +00:00
|
|
|
|
2023-08-07 19:38:03 +00:00
|
|
|
#if PPSSPP_PLATFORM(WINDOWS)
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2020-09-25 07:33:42 +00:00
|
|
|
static LARGE_INTEGER frequency;
|
|
|
|
static double frequencyMult;
|
|
|
|
static LARGE_INTEGER startTime;
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
static inline void InitTime() {
|
2014-12-29 18:06:19 +00:00
|
|
|
if (frequency.QuadPart == 0) {
|
|
|
|
QueryPerformanceFrequency(&frequency);
|
|
|
|
QueryPerformanceCounter(&startTime);
|
|
|
|
frequencyMult = 1.0 / static_cast<double>(frequency.QuadPart);
|
2012-10-30 12:20:55 +00:00
|
|
|
}
|
2024-06-05 08:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double time_now_d() {
|
|
|
|
InitTime();
|
2014-12-29 18:06:19 +00:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
QueryPerformanceCounter(&time);
|
|
|
|
double elapsed = static_cast<double>(time.QuadPart - startTime.QuadPart);
|
|
|
|
return elapsed * frequencyMult;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:52:46 +00:00
|
|
|
// Fake, but usable in a pinch. Don't, though.
|
|
|
|
uint64_t time_now_raw() {
|
2023-08-03 09:11:16 +00:00
|
|
|
return (uint64_t)(time_now_d() * nanos);
|
|
|
|
}
|
|
|
|
|
|
|
|
double from_time_raw(uint64_t raw_time) {
|
|
|
|
if (raw_time == 0) {
|
|
|
|
return 0.0; // invalid time
|
|
|
|
}
|
|
|
|
return (double)raw_time * (1.0 / nanos);
|
2023-08-02 21:52:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 10:59:25 +00:00
|
|
|
double from_time_raw_relative(uint64_t raw_time) {
|
|
|
|
return from_time_raw(raw_time);
|
|
|
|
}
|
2023-08-03 09:11:16 +00:00
|
|
|
|
2023-11-20 10:46:36 +00:00
|
|
|
double time_now_unix_utc() {
|
|
|
|
const int64_t UNIX_TIME_START = 0x019DB1DED53E8000; //January 1, 1970 (start of Unix epoch) in "ticks"
|
|
|
|
const double TICKS_PER_SECOND = 10000000; //a tick is 100ns
|
|
|
|
|
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft); //returns ticks in UTC
|
|
|
|
// Copy the low and high parts of FILETIME into a LARGE_INTEGER
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
li.LowPart = ft.dwLowDateTime;
|
|
|
|
li.HighPart = ft.dwHighDateTime;
|
|
|
|
//Convert ticks since 1/1/1970 into seconds
|
|
|
|
return (double)(li.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND;
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:38:03 +00:00
|
|
|
void yield() {
|
|
|
|
YieldProcessor();
|
|
|
|
}
|
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
TimeSpan::TimeSpan() {
|
|
|
|
_dbg_assert_(frequencyMult != 0.0);
|
|
|
|
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER *>(&nativeStart_));
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeSpan::ElapsedSeconds() const {
|
|
|
|
LARGE_INTEGER time;
|
|
|
|
QueryPerformanceCounter(&time);
|
|
|
|
double elapsed = static_cast<double>(time.QuadPart - nativeStart_);
|
|
|
|
return elapsed * frequencyMult;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t TimeSpan::ElapsedNanos() const {
|
|
|
|
return (int64_t)(ElapsedSeconds() * 1000000000.0);
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:52:46 +00:00
|
|
|
#elif PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(LINUX) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS)
|
|
|
|
|
|
|
|
// The only intended use is to match the timings in VK_GOOGLE_display_timing
|
|
|
|
uint64_t time_now_raw() {
|
|
|
|
struct timespec tp;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tp);
|
|
|
|
return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:11:16 +00:00
|
|
|
static uint64_t g_startTime;
|
|
|
|
|
|
|
|
double from_time_raw(uint64_t raw_time) {
|
|
|
|
return (double)(raw_time - g_startTime) * (1.0 / nanos);
|
|
|
|
}
|
|
|
|
|
2023-08-02 21:52:46 +00:00
|
|
|
double time_now_d() {
|
|
|
|
uint64_t raw_time = time_now_raw();
|
2023-08-03 09:11:16 +00:00
|
|
|
if (g_startTime == 0) {
|
|
|
|
g_startTime = raw_time;
|
2023-08-02 21:52:46 +00:00
|
|
|
}
|
2023-08-03 09:11:16 +00:00
|
|
|
return from_time_raw(raw_time);
|
2023-08-02 21:52:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 10:59:25 +00:00
|
|
|
double from_time_raw_relative(uint64_t raw_time) {
|
|
|
|
return (double)raw_time * (1.0 / nanos);
|
|
|
|
}
|
|
|
|
|
2023-11-20 10:46:36 +00:00
|
|
|
double time_now_unix_utc() {
|
|
|
|
struct timespec tp;
|
|
|
|
clock_gettime(CLOCK_REALTIME, &tp);
|
|
|
|
return tp.tv_sec * 1000000000ULL + tp.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:38:03 +00:00
|
|
|
void yield() {
|
|
|
|
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
|
|
|
|
_mm_pause();
|
|
|
|
#elif PPSSPP_ARCH(ARM64)
|
2023-08-09 10:20:27 +00:00
|
|
|
// Took this out for now. See issue #17877
|
|
|
|
// __builtin_arm_isb(15);
|
2023-08-07 19:38:03 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
TimeSpan::TimeSpan() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
nativeStart_ = ts.tv_sec;
|
|
|
|
nsecs_ = ts.tv_nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t TimeSpan::ElapsedNanos() const {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
int64_t secs = ts.tv_sec - nativeStart_;
|
|
|
|
int64_t nsecs = ts.tv_nsec - nsecs_;
|
|
|
|
if (nsecs < 0) {
|
|
|
|
secs--;
|
|
|
|
nsecs += 1000000000;
|
|
|
|
}
|
|
|
|
return secs * 1000000000ULL + nsecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeSpan::ElapsedSeconds() const {
|
|
|
|
return (double)ElapsedNanos() * (1.0 / nanos);
|
|
|
|
}
|
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
static time_t start;
|
|
|
|
|
2020-09-24 21:52:03 +00:00
|
|
|
double time_now_d() {
|
2015-04-04 09:27:22 +00:00
|
|
|
struct timeval tv;
|
2020-10-10 17:01:40 +00:00
|
|
|
gettimeofday(&tv, nullptr);
|
2015-04-04 09:27:22 +00:00
|
|
|
if (start == 0) {
|
|
|
|
start = tv.tv_sec;
|
|
|
|
}
|
2023-08-03 09:11:16 +00:00
|
|
|
return (double)(tv.tv_sec - start) + (double)tv.tv_usec * (1.0 / micros);
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 21:52:46 +00:00
|
|
|
uint64_t time_now_raw() {
|
2023-11-20 10:46:36 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
if (start == 0) {
|
|
|
|
start = tv.tv_sec;
|
|
|
|
}
|
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec * (1.0 / micros);
|
2023-08-03 09:11:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double from_time_raw(uint64_t raw_time) {
|
|
|
|
return (double)raw_time * (1.0 / nanos);
|
2023-08-02 21:52:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 10:59:25 +00:00
|
|
|
double from_time_raw_relative(uint64_t raw_time) {
|
|
|
|
return from_time_raw(raw_time);
|
|
|
|
}
|
|
|
|
|
2023-08-07 19:38:03 +00:00
|
|
|
void yield() {}
|
|
|
|
|
2023-11-20 10:46:36 +00:00
|
|
|
double time_now_unix_utc() {
|
|
|
|
return time_now_raw();
|
|
|
|
}
|
|
|
|
|
2024-06-05 08:29:04 +00:00
|
|
|
TimeSpan::TimeSpan() {
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
nativeStart_ = tv.tv_sec;
|
|
|
|
nsecs_ = tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t TimeSpan::ElapsedNanos() const {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
|
|
|
int64_t secs = ts.tv_sec - nativeStart_;
|
|
|
|
int64_t usecs = ts.tv_nsec - nsecs_;
|
|
|
|
if (usecs < 0) {
|
|
|
|
secs--;
|
|
|
|
usecs += 1000000;
|
|
|
|
}
|
|
|
|
return secs * 1000000000 + usecs * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeSpan::ElapsedSeconds() const {
|
|
|
|
return (double)ElapsedNanos() * (1.0 / 1000000000);
|
|
|
|
}
|
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void sleep_ms(int ms) {
|
|
|
|
#ifdef _WIN32
|
2012-10-30 12:20:55 +00:00
|
|
|
Sleep(ms);
|
2020-03-15 14:56:38 +00:00
|
|
|
#elif defined(HAVE_LIBNX)
|
|
|
|
svcSleepThread(ms * 1000000);
|
2023-08-12 19:38:35 +00:00
|
|
|
#elif defined(__EMSCRIPTEN__)
|
|
|
|
emscripten_sleep(ms);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-10-30 12:20:55 +00:00
|
|
|
usleep(ms * 1000);
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-10-05 18:58:33 +00:00
|
|
|
|
|
|
|
// Return the current time formatted as Minutes:Seconds:Milliseconds
|
|
|
|
// in the form 00:00:000.
|
2024-06-05 08:28:49 +00:00
|
|
|
void GetCurrentTimeFormatted(char formattedTime[13]) {
|
2020-10-05 18:58:33 +00:00
|
|
|
time_t sysTime;
|
|
|
|
time(&sysTime);
|
|
|
|
|
2022-08-07 14:51:27 +00:00
|
|
|
uint32_t milliseconds;
|
2020-10-05 18:58:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct timeb tp;
|
|
|
|
(void)::ftime(&tp);
|
2022-08-07 14:51:27 +00:00
|
|
|
milliseconds = tp.millitm;
|
2020-10-05 18:58:33 +00:00
|
|
|
#else
|
|
|
|
struct timeval t;
|
|
|
|
(void)gettimeofday(&t, NULL);
|
2022-08-07 14:51:27 +00:00
|
|
|
milliseconds = (int)(t.tv_usec / 1000);
|
2020-10-05 18:58:33 +00:00
|
|
|
#endif
|
2022-08-07 14:51:27 +00:00
|
|
|
|
|
|
|
struct tm *gmTime = localtime(&sysTime);
|
|
|
|
char tmp[6];
|
|
|
|
strftime(tmp, sizeof(tmp), "%M:%S", gmTime);
|
|
|
|
|
|
|
|
// Now tack on the milliseconds
|
|
|
|
snprintf(formattedTime, 11, "%s:%03u", tmp, milliseconds % 1000);
|
2020-10-05 18:58:33 +00:00
|
|
|
}
|