From 1cdfcb4935f2cdbc28cefb6ef1c716088643f030 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Tue, 31 Jan 2017 15:56:58 +0200 Subject: [PATCH] RDTSC-based cycle_t for macOS Windows and macOS now share most of related code Old implementation using mach_absolute_time() was more precise (at least in theory) but too costly --- src/posix/cocoa/i_system.mm | 21 +++++++++++ src/stats.cpp | 9 ----- src/stats.h | 75 +------------------------------------ 3 files changed, 22 insertions(+), 83 deletions(-) diff --git a/src/posix/cocoa/i_system.mm b/src/posix/cocoa/i_system.mm index 2e27a8002..35b71c64b 100644 --- a/src/posix/cocoa/i_system.mm +++ b/src/posix/cocoa/i_system.mm @@ -36,6 +36,7 @@ #include #include #include +#include #include "d_ticcmd.h" #include "doomdef.h" @@ -98,9 +99,29 @@ void SetLanguageIDs() void I_InitTimer(); void I_ShutdownTimer(); +double PerfToSec, PerfToMillisec; + +static void CalculateCPUSpeed() +{ + long long frequency; + size_t size = sizeof frequency; + + if (0 == sysctlbyname("machdep.tsc.frequency", &frequency, &size, nullptr, 0) && 0 != frequency) + { + PerfToSec = 1.0 / frequency; + PerfToMillisec = 1000.0 / frequency; + + if (!batchrun) + { + Printf("CPU speed: %.0f MHz\n", 0.001 / PerfToMillisec); + } + } +} + void I_Init(void) { CheckCPUID(&CPU); + CalculateCPUSpeed(); DumpCPUInfo(&CPU); atterm(I_ShutdownSound); diff --git a/src/stats.cpp b/src/stats.cpp index c57b32640..237436c41 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -42,15 +42,6 @@ #include "m_swap.h" #include "sbar.h" - -#if defined (__APPLE__) - -mach_timebase_info_data_t cycle_t::s_info; -bool cycle_t::s_initialized; - -#endif // __APPLE__ - - FStat *FStat::FirstStat; FStat::FStat (const char *name) diff --git a/src/stats.h b/src/stats.h index d90efa7e4..316633983 100644 --- a/src/stats.h +++ b/src/stats.h @@ -36,78 +36,7 @@ #include "zstring.h" -#ifndef _WIN32 - -#if defined (__APPLE__) - - -#include - - -class cycle_t -{ -public: - cycle_t() - { - if ( !s_initialized ) - { - mach_timebase_info( &s_info ); - s_initialized = true; - } - - Reset(); - } - - cycle_t &operator=( const cycle_t &other ) - { - if ( &other != this ) - { - m_seconds = other.m_seconds; - - } - - return *this; - } - - void Reset() - { - m_seconds = 0; - } - - void Clock() - { - m_seconds -= Nanoseconds() * 1e-9; - } - - void Unclock() - { - m_seconds += Nanoseconds() * 1e-9; - } - - double Time() - { - return m_seconds; - } - - double TimeMS() - { - return m_seconds * 1e3; - } - -private: - double m_seconds; - - static mach_timebase_info_data_t s_info; - static bool s_initialized; - - uint64_t Nanoseconds() const - { - return mach_absolute_time() * s_info.numer / s_info.denom; - } - -}; - -#else // !__APPLE__ +#if !defined _WIN32 && !defined __APPLE__ #ifdef NO_CLOCK_GETTIME class cycle_t @@ -171,8 +100,6 @@ private: #endif -#endif // __APPLE__ - #else // Windows