diff options
| author | Konstantin <const@const.me> | 2023-01-16 14:52:43 +0100 |
|---|---|---|
| committer | Konstantin <const@const.me> | 2023-01-16 14:52:43 +0100 |
| commit | 8c4603c73675958efc960fbd4bb599a2909d106a (patch) | |
| tree | 714dc6fc9a1672d5fd7f89676b97e10959662abc /Whisper/Utils/CpuProfiler.cpp | |
| parent | 990a8d0dbaefc996244097397259e92758b15cce (diff) | |
Source codes
Diffstat (limited to 'Whisper/Utils/CpuProfiler.cpp')
| -rw-r--r-- | Whisper/Utils/CpuProfiler.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Whisper/Utils/CpuProfiler.cpp b/Whisper/Utils/CpuProfiler.cpp new file mode 100644 index 0000000..6161e95 --- /dev/null +++ b/Whisper/Utils/CpuProfiler.cpp @@ -0,0 +1,65 @@ +#include "stdafx.h" +#include "CpuProfiler.h" + +namespace +{ + using namespace Whisper; + + inline int64_t qpcNow() + { + int64_t res; + QueryPerformanceCounter( (LARGE_INTEGER*)&res ); + return res; + } + + class CpuTimescale + { + uint64_t frequency = 0; + const int64_t tscStart; + const int64_t qpcStart; + + uint64_t computeTscFrequency(); + + public: + + CpuTimescale() : + tscStart( tscNow() ), + qpcStart( qpcNow() ) + { } + + inline uint64_t computeTicks( uint64_t tsc ) + { + uint64_t freq = frequency; + if( freq == 0 ) + freq = computeTscFrequency(); + + return makeTime( tsc, freq ); + } + }; + + uint64_t __declspec( noinline ) CpuTimescale::computeTscFrequency() + { + int64_t tsc = tscNow(); + int64_t qpc = qpcNow(); + tsc -= tscStart; + qpc -= qpcStart; + + uint64_t qpcFreq; + QueryPerformanceFrequency( (LARGE_INTEGER*)&qpcFreq ); + + // Seconds = qpc / qpcFreq + // ticks per second = tsc / seconds = tsc * qpcFreq / qpc + uint64_t res = ( (uint64_t)tsc * qpcFreq + ( (uint64_t)qpc / 2 ) - 1 ) / (uint64_t)qpc; + frequency = res; + const double GHz = (double)(int64_t)res * 1.0E-9; + logDebug( u8"Computed CPU base frequency: %g GHz", GHz ); + return res; + } + + static CpuTimescale timescale; +} + +uint64_t Whisper::ticksFromTsc( uint64_t tscDiff ) +{ + return timescale.computeTicks( tscDiff ); +}
\ No newline at end of file |
