From c345b9e0248266d09a08a575ba325b773f46e4f1 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Fri, 4 Aug 2017 12:53:14 +0500 Subject: [PATCH] Timer: QPC usage replaced by std::chrono xrGame profiler need to be updated too... From commit https://github.com/Im-dex/xray-162/commit/4b7c63bdcecd1e05a66e93aeeb5561f9365b7ff8#diff-35623a6ce0c7456fe4f02444978619bbR40 Get rid of some Borland stuff --- src/utils/xrLC_Light/LightThread.cpp | 4 +- .../xrLC_Light/detail_slot_calculate.cpp | 10 +- src/utils/xrLC_Light/detail_slot_calculate.h | 8 +- src/xrCore/FTimer.cpp | 47 ++-- src/xrCore/FTimer.h | 213 ++++++++---------- src/xrCore/_math.cpp | 132 ++++------- src/xrCore/_math.h | 10 +- src/xrCore/xrCore.cpp | 2 +- src/xrCore/xrCore.h | 9 +- src/xrCore/xrCore.vcxproj | 6 +- src/xrEngine/profiler.cpp | 1 + src/xrScriptEngine/ScriptEngineScript.cpp | 86 ++++--- 12 files changed, 223 insertions(+), 305 deletions(-) diff --git a/src/utils/xrLC_Light/LightThread.cpp b/src/utils/xrLC_Light/LightThread.cpp index bd13c60fbb7..555a9bb1a85 100644 --- a/src/utils/xrLC_Light/LightThread.cpp +++ b/src/utils/xrLC_Light/LightThread.cpp @@ -24,7 +24,9 @@ void LightThread::Execute() gl_data.slots_data.set_slot_calculated(_x, _z); thProgress = float(_z - Nstart) / float(Nend - Nstart); - thPerformance = float(double(t_count) / double(t_time * CPU::clk_to_seconds)) / 1000.f; + + const auto secs = std::chrono::duration_cast(t_time).count(); + thPerformance = float(double(t_count) / double(secs)) / 1000.f; } } } diff --git a/src/utils/xrLC_Light/detail_slot_calculate.cpp b/src/utils/xrLC_Light/detail_slot_calculate.cpp index 66e029c7a2e..95a2a56d616 100644 --- a/src/utils/xrLC_Light/detail_slot_calculate.cpp +++ b/src/utils/xrLC_Light/detail_slot_calculate.cpp @@ -83,9 +83,9 @@ class base_color const int LIGHT_Count = 7; //----------------------------------------------------------------- -__declspec(thread) u64 t_start = 0; -__declspec(thread) u64 t_time = 0; -__declspec(thread) u64 t_count = 0; +thread_local Time t_start; +thread_local Duration t_time; +thread_local u64 t_count = 0; IC bool RayPick(CDB::COLLIDER& DB, Fvector& P, Fvector& D, float r, R_Light& L) { @@ -99,9 +99,9 @@ IC bool RayPick(CDB::COLLIDER& DB, Fvector& P, Fvector& D, float r, R_Light& L) } // 2. Polygon doesn't pick - real database query - t_start = CPU::GetCLK(); + t_start = Clock::now(); DB.ray_query(&gl_data.RCAST_Model, P, D, r); - t_time += CPU::GetCLK() - t_start - CPU::clk_overhead; + t_time += Clock::now() - t_start; t_count += 1; // 3. Analyze diff --git a/src/utils/xrLC_Light/detail_slot_calculate.h b/src/utils/xrLC_Light/detail_slot_calculate.h index 5a140651600..d1109bf47a2 100644 --- a/src/utils/xrLC_Light/detail_slot_calculate.h +++ b/src/utils/xrLC_Light/detail_slot_calculate.h @@ -7,6 +7,10 @@ #ifndef DETAIL_SLOT_CALCULATE_H_INCLUDED #define DETAIL_SLOT_CALCULATE_H_INCLUDED +using Clock = std::chrono::high_resolution_clock; +using Time = Clock::time_point; +using Duration = Clock::duration; + DEFINE_VECTOR(u32, DWORDVec, DWORDIt); namespace CDB { @@ -15,8 +19,8 @@ class COLLIDER; class base_lighting; struct DetailSlot; -extern __declspec(thread) u64 t_time; -extern __declspec(thread) u64 t_count; +extern thread_local Duration t_time; +extern thread_local u64 t_count; bool detail_slot_calculate( u32 _x, u32 _z, DetailSlot& DS, DWORDVec& box_result, CDB::COLLIDER& DB, base_lighting& Selected); diff --git a/src/xrCore/FTimer.cpp b/src/xrCore/FTimer.cpp index 223261f519c..3c94232d90d 100644 --- a/src/xrCore/FTimer.cpp +++ b/src/xrCore/FTimer.cpp @@ -1,27 +1,20 @@ #include "stdafx.h" -#pragma hdrstop -XRCORE_API BOOL g_bEnableStatGather = FALSE; - -CStatTimer::CStatTimer() -{ - accum = 0; - result = 0.f; - count = 0; -} +XRCORE_API bool g_bEnableStatGather = false; void CStatTimer::FrameStart() { - accum = 0; + accum = Duration(); count = 0; } -void CStatTimer::FrameEnd() -{ - float _time = 1000.f * float(double(accum) / double(CPU::qpc_freq)); - if (_time > result) - result = _time; + +void CStatTimer::FrameEnd() { + + const float time = GetElapsed_sec(); + if (time > result) + result = time; else - result = 0.99f * result + 0.01f * _time; + result = 0.99f * result + 0.01f * time; } XRCORE_API pauseMngr* g_pauseMngr() @@ -36,23 +29,25 @@ XRCORE_API pauseMngr* g_pauseMngr() return manager; } -pauseMngr::pauseMngr() : m_paused(FALSE) { m_timers.reserve(3); } -void pauseMngr::Pause(BOOL b) +pauseMngr::pauseMngr() : paused(FALSE) { m_timers.reserve(3); } +void pauseMngr::Pause(const bool b) { - if (m_paused == b) + if (paused == b) return; - xr_vector::iterator it = m_timers.begin(); - for (; it != m_timers.end(); ++it) - (*it)->Pause(b); + for (auto& timer : m_timers) + { + timer->Pause(b); + } - m_paused = b; + paused = b; } -void pauseMngr::Register(CTimer_paused* t) { m_timers.push_back(t); } -void pauseMngr::UnRegister(CTimer_paused* t) +void pauseMngr::Register(CTimer_paused& t) { m_timers.push_back(&t); } + +void pauseMngr::UnRegister(CTimer_paused& t) { - xr_vector::iterator it = std::find(m_timers.begin(), m_timers.end(), t); + const auto it = std::find(m_timers.cbegin(), m_timers.cend(), &t); if (it != m_timers.end()) m_timers.erase(it); } diff --git a/src/xrCore/FTimer.h b/src/xrCore/FTimer.h index caa2542fcb0..5528b1e13ac 100644 --- a/src/xrCore/FTimer.h +++ b/src/xrCore/FTimer.h @@ -1,210 +1,189 @@ +#pragma once #ifndef FTimerH #define FTimerH -#pragma once class CTimer_paused; class XRCORE_API pauseMngr { xr_vector m_timers; - BOOL m_paused; + bool paused; public: pauseMngr(); - BOOL Paused() { return m_paused; }; - void Pause(BOOL b); - void Register(CTimer_paused* t); - void UnRegister(CTimer_paused* t); + bool Paused() const { return paused; }; + void Pause(const bool b); + void Register(CTimer_paused& t); + void UnRegister(CTimer_paused& t); }; extern XRCORE_API pauseMngr* g_pauseMngr(); class XRCORE_API CTimerBase { +public: + using Clock = std::chrono::high_resolution_clock; + using Time = std::chrono::time_point; + using Duration = Time::duration; + protected: - u64 qwStartTime; - u64 qwPausedTime; - u64 qwPauseAccum; - BOOL bPause; + Time startTime; + Duration pauseDuration; + Duration pauseAccum; + bool paused; public: - CTimerBase() : qwStartTime(0), qwPausedTime(0), qwPauseAccum(0), bPause(FALSE) {} - ICF void Start() + CTimerBase() : startTime(), pauseDuration(), pauseAccum(), paused(false) {} + + void Start() { - if (bPause) - return; - qwStartTime = CPU::QPC() - qwPauseAccum; + if (paused) return; + startTime = Clock::now() - pauseAccum; } - ICF u64 GetElapsed_ticks() const + + Duration getElapsedTime() const { - if (bPause) - return qwPausedTime; - else - return CPU::QPC() - qwStartTime - CPU::qpc_overhead - qwPauseAccum; + if (paused) return pauseDuration; + return Clock::now() - startTime - pauseAccum; } - IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); } - IC float GetElapsed_sec() const + + u64 GetElapsed_ms() const { -#ifndef _EDITOR - FPU::m64r(); -#endif - float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); -#ifndef _EDITOR - FPU::m24r(); -#endif - return _result; + using namespace std::chrono; + return duration_cast(getElapsedTime()).count(); } - IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } + + float GetElapsed_sec() const + { + using namespace std::chrono; + const auto nanos = duration_cast(getElapsedTime()).count(); + return float(nanos) / 1000000000.0; + } + + void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } }; class XRCORE_API CTimer : public CTimerBase { -private: - typedef CTimerBase inherited; + using super = CTimerBase; -private: float m_time_factor; - u64 m_real_ticks; - u64 m_ticks; + Duration realTime; + Duration time; -private: - IC u64 GetElapsed_ticks(const u64& current_ticks) const + Duration getElapsedTime(const Duration current) const { - u64 delta = current_ticks - m_real_ticks; - double delta_d = (double)delta; - double time_factor_d = time_factor(); - double time = delta_d * time_factor_d + .5; - u64 result = (u64)time; - return (m_ticks + result); + const auto delta = current - realTime; + const auto deltaD = double(delta.count()); + const auto time_factor_d = double(time_factor()); + const double time = deltaD * time_factor_d + .5; + const auto result = u64(time); + return Duration(result); } public: - IC CTimer() : m_time_factor(1.f), m_real_ticks(0), m_ticks(0) {} - ICF void Start() - { - if (bPause) - return; + CTimer() : m_time_factor(1.f), realTime(), time() {} - inherited::Start(); + void Start() + { + if (paused) return; - m_real_ticks = 0; - m_ticks = 0; + super::Start(); } - IC const float& time_factor() const { return (m_time_factor); } - IC void time_factor(const float& time_factor) - { - u64 current = inherited::GetElapsed_ticks(); - m_ticks = GetElapsed_ticks(current); - m_real_ticks = current; - m_time_factor = time_factor; - } + float time_factor() const { return m_time_factor; } - IC u64 GetElapsed_ticks() const + void time_factor(const float time_factor) { -#ifndef _EDITOR - FPU::m64r(); -#endif // _EDITOR - - u64 result = GetElapsed_ticks(inherited::GetElapsed_ticks()); - -#ifndef _EDITOR - FPU::m24r(); -#endif // _EDITOR - - return (result); + const auto current = super::getElapsedTime(); + time = getElapsedTime(current); + realTime = current; + m_time_factor = time_factor; } - IC u32 GetElapsed_ms() const { return (u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq)); } - IC float GetElapsed_sec() const + Duration getElapsedTime() const { -#ifndef _EDITOR - FPU::m64r(); -#endif - float result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); -#ifndef _EDITOR - FPU::m24r(); -#endif - return (result); + return super::getElapsedTime(); } - - IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } }; class XRCORE_API CTimer_paused_ex : public CTimer { - u64 save_clock; + Time save_clock; public: - CTimer_paused_ex() {} + CTimer_paused_ex() : save_clock() {} virtual ~CTimer_paused_ex() {} - IC BOOL Paused() const { return bPause; } - IC void Pause(BOOL b) + bool Paused() const { return paused; } + + void Pause(const bool b) { - if (bPause == b) - return; + if (paused == b) return; - u64 _current = CPU::QPC() - CPU::qpc_overhead; + const auto current = Clock::now(); if (b) { - save_clock = _current; - qwPausedTime = CTimerBase::GetElapsed_ticks(); + save_clock = current; + pauseDuration = CTimerBase::getElapsedTime(); } else - { - qwPauseAccum += _current - save_clock; - } - bPause = b; + pauseAccum += current - save_clock; + + paused = b; } }; class XRCORE_API CTimer_paused : public CTimer_paused_ex { public: - CTimer_paused() { g_pauseMngr()->Register(this); } - virtual ~CTimer_paused() { g_pauseMngr()->UnRegister(this); } + CTimer_paused() { g_pauseMngr()->Register(*this); } + virtual ~CTimer_paused() { g_pauseMngr()->UnRegister(*this); } }; -extern XRCORE_API BOOL g_bEnableStatGather; +extern XRCORE_API bool g_bEnableStatGather; + class XRCORE_API CStatTimer { + using Duration = CTimerBase::Duration; + public: CTimer T; - u64 accum; + Duration accum; float result; u32 count; -public: - CStatTimer(); + CStatTimer() : T(), accum(), result(.0f), count(0) {} void FrameStart(); void FrameEnd(); - ICF void Begin() + void Begin() { if (!g_bEnableStatGather) return; count++; T.Start(); } - ICF void End() + + void End() { if (!g_bEnableStatGather) return; - accum += T.GetElapsed_ticks(); + accum += T.getElapsedTime(); + } + + Duration getElapsedTime() const { return accum; } + + u64 GetElapsed_ms() const + { + using namespace std::chrono; + return duration_cast(getElapsedTime()).count(); } - ICF u64 GetElapsed_ticks() const { return accum; } - IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); } - IC float GetElapsed_sec() const + float GetElapsed_sec() const { -#ifndef _EDITOR - FPU::m64r(); -#endif - float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); -#ifndef _EDITOR - FPU::m24r(); -#endif - return _result; + using namespace std::chrono; + const auto nanos = duration_cast(getElapsedTime()).count(); + return float(nanos) / 1000000000.0; } }; diff --git a/src/xrCore/_math.cpp b/src/xrCore/_math.cpp index fd3b125bce8..cbecc618e28 100644 --- a/src/xrCore/_math.cpp +++ b/src/xrCore/_math.cpp @@ -2,6 +2,7 @@ #pragma hdrstop #include +#include // mmsystem.h #define MMNOSOUND @@ -11,6 +12,16 @@ #define MMNOJOY #include +typedef struct _PROCESSOR_POWER_INFORMATION +{ + ULONG Number; + ULONG MaxMhz; + ULONG CurrentMhz; + ULONG MhzLimit; + ULONG MaxIdleState; + ULONG CurrentIdleState; +} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION; + // Initialized on startup XRCORE_API Fmatrix Fidentity; XRCORE_API Dmatrix Didentity; @@ -132,15 +143,13 @@ void initialize() namespace CPU { -XRCORE_API u64 clk_per_second; -XRCORE_API u64 clk_per_milisec; -XRCORE_API u64 clk_per_microsec; -XRCORE_API u64 clk_overhead; -XRCORE_API float clk_to_seconds; -XRCORE_API float clk_to_milisec; -XRCORE_API float clk_to_microsec; -XRCORE_API u64 qpc_freq = 0; -XRCORE_API u64 qpc_overhead = 0; +XRCORE_API u64 qpc_freq = [] +{ + u64 result; + QueryPerformanceCounter((PLARGE_INTEGER)&result); + return result; +}(); + XRCORE_API u32 qpc_counter = 0; XRCORE_API processor_info ID; @@ -169,88 +178,19 @@ void Detect() // Core.Fatal ("Fatal error: can't detect CPU/FPU."); abort(); } - - // Timers & frequency - u64 start, end; - u32 dwStart, dwTest; - - SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); - - // Detect Freq - dwTest = timeGetTime(); - do - { - dwStart = timeGetTime(); - } while (dwTest == dwStart); - start = GetCLK(); - while (timeGetTime() - dwStart < 1000) - ; - end = GetCLK(); - clk_per_second = end - start; - - // Detect RDTSC Overhead - clk_overhead = 0; - u64 dummy = 0; - for (int i = 0; i < 256; i++) - { - start = GetCLK(); - clk_overhead += GetCLK() - start - dummy; - } - clk_overhead /= 256; - - // Detect QPC Overhead - QueryPerformanceFrequency((PLARGE_INTEGER)&qpc_freq); - qpc_overhead = 0; - for (int i = 0; i < 256; i++) - { - start = QPC(); - qpc_overhead += QPC() - start - dummy; - } - qpc_overhead /= 256; - - SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS); - - clk_per_second -= clk_overhead; - clk_per_milisec = clk_per_second / 1000; - clk_per_microsec = clk_per_milisec / 1000; - - _control87(_PC_64, MCW_PC); - // _control87 ( _RC_CHOP, MCW_RC ); - double a, b; - a = 1; - b = double(clk_per_second); - clk_to_seconds = float(double(a / b)); - a = 1000; - b = double(clk_per_second); - clk_to_milisec = float(double(a / b)); - a = 1000000; - b = double(clk_per_second); - clk_to_microsec = float(double(a / b)); } -}; + +} // namespace CPU bool g_initialize_cpu_called = false; //------------------------------------------------------------------------------------ -void _initialize_cpu(void) +void _initialize_cpu() { - Msg("* Detected CPU: %s [%s], F%d/M%d/S%d, %.2f mhz, %d-clk 'rdtsc'", CPU::ID.modelName, CPU::ID.vendor, - CPU::ID.family, CPU::ID.model, CPU::ID.stepping, float(CPU::clk_per_second / u64(1000000)), - u32(CPU::clk_overhead)); - - // DUMP_PHASE; + Msg("* Detected CPU: %s [%s], F%d/M%d/S%d, 'rdtsc'", CPU::ID.modelName, + +CPU::ID.vendor, CPU::ID.family, CPU::ID.model, CPU::ID.stepping); - if (strstr(Core.Params, "-x86")) - { - CPU::ID.hasFeature(CpuFeature::Mmx); - CPU::ID.hasFeature(CpuFeature::_3dNow); - CPU::ID.hasFeature(CpuFeature::Sse); - CPU::ID.hasFeature(CpuFeature::Sse2); - CPU::ID.hasFeature(CpuFeature::Sse3); - CPU::ID.hasFeature(CpuFeature::Ssse3); - CPU::ID.hasFeature(CpuFeature::Sse41); - CPU::ID.hasFeature(CpuFeature::Sse42); - }; + //DUMP_PHASE; string256 features; xr_strcpy(features, sizeof(features), "RDTSC"); @@ -266,7 +206,24 @@ void _initialize_cpu(void) if (CPU::ID.hasFeature(CpuFeature::HT)) xr_strcat(features, ", HTT"); Msg("* CPU features: %s", features); - Msg("* CPU cores/threads: %d/%d\n", CPU::ID.n_cores, CPU::ID.n_threads); + Msg("* CPU cores/threads: %d/%d", CPU::ID.n_cores, CPU::ID.n_threads); + + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + const size_t cpusCount = sysInfo.dwNumberOfProcessors; + + xr_vector cpusInfo(cpusCount); + CallNtPowerInformation(ProcessorInformation, nullptr, 0, cpusInfo.data(), + sizeof(PROCESSOR_POWER_INFORMATION) * cpusCount); + + for (size_t i = 0; i < cpusInfo.size(); i++) + { + const PROCESSOR_POWER_INFORMATION& cpuInfo = cpusInfo[i]; + Msg("* CPU%zu current freq: %lu MHz, max freq: %lu MHz", + i, cpuInfo.CurrentMhz, cpuInfo.MaxMhz); + } + + Log(""); Fidentity.identity(); // Identity matrix Didentity.identity(); // Identity matrix @@ -277,9 +234,6 @@ void _initialize_cpu(void) g_initialize_cpu_called = true; } -#ifdef M_BORLAND -void _initialize_cpu_thread() {} -#else // per-thread initialization #include #define _MM_DENORMALS_ZERO_MASK 0x0040 @@ -313,7 +267,7 @@ void _initialize_cpu_thread() } } } -#endif + // threading API #pragma pack(push, 8) struct THREAD_NAME diff --git a/src/xrCore/_math.h b/src/xrCore/_math.h index 22495c130fc..3682ad2c0af 100644 --- a/src/xrCore/_math.h +++ b/src/xrCore/_math.h @@ -1,3 +1,4 @@ +#pragma once #ifndef __XR_MATH_H__ #define __XR_MATH_H__ @@ -14,16 +15,7 @@ XRCORE_API void m64r(void); }; namespace CPU { -XRCORE_API extern u64 clk_per_second; -XRCORE_API extern u64 clk_per_milisec; -XRCORE_API extern u64 clk_per_microsec; -XRCORE_API extern u64 clk_overhead; -XRCORE_API extern float clk_to_seconds; -XRCORE_API extern float clk_to_milisec; -XRCORE_API extern float clk_to_microsec; - XRCORE_API extern u64 qpc_freq; -XRCORE_API extern u64 qpc_overhead; XRCORE_API extern u32 qpc_counter; XRCORE_API extern processor_info ID; diff --git a/src/xrCore/xrCore.cpp b/src/xrCore/xrCore.cpp index 42f70596e42..1a02d760019 100644 --- a/src/xrCore/xrCore.cpp +++ b/src/xrCore/xrCore.cpp @@ -20,7 +20,7 @@ XRCORE_API xrCore Core; namespace CPU { extern void Detect(); -}; +} static u32 init_counter = 0; diff --git a/src/xrCore/xrCore.h b/src/xrCore/xrCore.h index f6a9943e156..7351cc76549 100644 --- a/src/xrCore/xrCore.h +++ b/src/xrCore/xrCore.h @@ -59,10 +59,8 @@ #ifndef DEBUG #pragma inline_depth(254) #pragma inline_recursion(on) -#ifndef __BORLANDC__ #pragma intrinsic(abs, fabs, fmod, sin, cos, tan, asin, acos, atan, sqrt, exp, log, log10, strcat) #endif -#endif #include @@ -93,10 +91,11 @@ #include #include -#ifndef _EDITOR #include #include -#endif + +#include +#include #include #pragma warning(pop) @@ -209,7 +208,6 @@ class destructor // ********************************************** The Core definition class XRCORE_API xrCore { -private: const char* buildDate; u32 buildId; @@ -223,7 +221,6 @@ class XRCORE_API xrCore DWORD dwFrame; bool PluginMode; -public: void _initialize( LPCSTR ApplicationName, LogCallback cb = 0, BOOL init_fs = TRUE, LPCSTR fs_fname = 0, bool plugin = false); void _destroy(); diff --git a/src/xrCore/xrCore.vcxproj b/src/xrCore/xrCore.vcxproj index c64dbae3710..8c76ee4a046 100644 --- a/src/xrCore/xrCore.vcxproj +++ b/src/xrCore/xrCore.vcxproj @@ -91,7 +91,7 @@ Windows false MachineX86 - cryptlib.lib;lzo2.lib;%(AdditionalDependencies) + cryptlib.lib;lzo2.lib;PowrProf.lib;%(AdditionalDependencies) true @@ -125,7 +125,7 @@ UseLinkTimeCodeGeneration false MachineX86 - cryptlib.lib;lzo2.lib;%(AdditionalDependencies) + cryptlib.lib;lzo2.lib;PowrProf.lib;%(AdditionalDependencies) true @@ -157,7 +157,7 @@ false $(xrLibDir)$(TargetName).lib MachineX86 - cryptlib.lib;lzo2.lib;%(AdditionalDependencies) + cryptlib.lib;lzo2.lib;PowrProf.lib;%(AdditionalDependencies) diff --git a/src/xrEngine/profiler.cpp b/src/xrEngine/profiler.cpp index f2f726eac43..58212060e0e 100644 --- a/src/xrEngine/profiler.cpp +++ b/src/xrEngine/profiler.cpp @@ -87,6 +87,7 @@ IC void CProfiler::convert_string (LPCSTR str, shared_str &out, u32 max_string_s out = m_temp; } +// TODO [imdex]: replace qpc_freq by std::chrono void CProfiler::setup_timer (LPCSTR timer_id, const u64 &timer_time, const u32 &call_count) { string256 m_temp; diff --git a/src/xrScriptEngine/ScriptEngineScript.cpp b/src/xrScriptEngine/ScriptEngineScript.cpp index fc092e7d25a..2601e8ec3be 100644 --- a/src/xrScriptEngine/ScriptEngineScript.cpp +++ b/src/xrScriptEngine/ScriptEngineScript.cpp @@ -66,80 +66,74 @@ bool is_editor() #endif } -int bit_and(int i, int j) { return i & j; } -int bit_or(int i, int j) { return i | j; } -int bit_xor(int i, int j) { return i ^ j; } -int bit_not(int i) { return ~i; } -const char* user_name() { return Core.UserName; } +inline int bit_and(const int i, const int j) { return i & j; } +inline int bit_or(const int i, const int j) { return i | j; } +inline int bit_xor(const int i, const int j) { return i ^ j; } +inline int bit_not(const int i) { return ~i; } +inline const char* user_name() { return Core.UserName; } + void prefetch_module(LPCSTR file_name) { GlobalEnv.ScriptEngine->process_file(file_name); } + struct profile_timer_script { - u64 m_start_cpu_tick_count; - u64 m_accumulator; - u64 m_count; - int m_recurse_mark; + using Clock = std::chrono::high_resolution_clock; + using Time = Clock::time_point; + using Duration = Clock::duration; - profile_timer_script() - { - m_start_cpu_tick_count = 0; - m_accumulator = 0; - m_count = 0; - m_recurse_mark = 0; - } + Time start_time; + Duration accumulator; + u64 count = 0; + int recurse_mark = 0; - profile_timer_script(const profile_timer_script& profile_timer) { *this = profile_timer; } - profile_timer_script& operator=(const profile_timer_script& profile_timer) - { - m_start_cpu_tick_count = profile_timer.m_start_cpu_tick_count; - m_accumulator = profile_timer.m_accumulator; - m_count = profile_timer.m_count; - m_recurse_mark = profile_timer.m_recurse_mark; - return *this; - } + profile_timer_script() + : start_time(), + accumulator(), + count(0), + recurse_mark(0) {} bool operator<(const profile_timer_script& profile_timer) const { - return m_accumulator < profile_timer.m_accumulator; + return accumulator < profile_timer.accumulator; } void start() { - if (m_recurse_mark) + if (recurse_mark) { - m_recurse_mark++; + ++recurse_mark; return; } - m_recurse_mark++; - m_count++; - m_start_cpu_tick_count = CPU::GetCLK(); + + ++recurse_mark; + ++count; + start_time = Clock::now(); } void stop() { - if (!m_recurse_mark) - return; - m_recurse_mark--; - if (m_recurse_mark) - return; - u64 finish = CPU::GetCLK(); - if (finish > m_start_cpu_tick_count) - m_accumulator += finish - m_start_cpu_tick_count; + if (!recurse_mark) return; + + --recurse_mark; + + if (recurse_mark) return; + + const auto finish = Clock::now(); + if (finish > start_time) + accumulator += finish - start_time; } float time() const { - FPU::m64r(); - float result = float(double(m_accumulator) / double(CPU::clk_per_second)) * 1000000.f; - FPU::m24r(); - return result; + using namespace std::chrono; + return float(duration_cast(accumulator).count()) * 1000000.f; } }; -IC profile_timer_script operator+(const profile_timer_script& portion0, const profile_timer_script& portion1) +inline profile_timer_script operator+(const profile_timer_script& portion0, const profile_timer_script& portion1) { profile_timer_script result; - result.m_accumulator = portion0.m_accumulator + portion1.m_accumulator; - result.m_count = portion0.m_count + portion1.m_count; + result.accumulator = portion0.accumulator + portion1.accumulator; + result.count = portion0.count + portion1.count; return result; }