aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/hp_timer.cpp
blob: 27f97a9b2b2826fd0b12b4d164b3378f99b7df36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "hp_timer.h"
 
#include <util/generic/algorithm.h>
#include <util/generic/singleton.h> 
#include <util/datetime/cputimer.h> 
 
using namespace NHPTimer;

namespace { 
    struct TFreq { 
        inline TFreq() 
            : Freq(InitHPTimer()) 
            , Rate(1.0 / Freq)
            , CyclesPerSecond(static_cast<ui64>(Rate))
        { 
        }
 
        static inline const TFreq& Instance() { 
            return *SingletonWithPriority<TFreq, 1>();
        }

        static double EstimateCPUClock() { 
            for (;;) { 
                ui64 startCycle = 0;
                ui64 startMS = 0;
 
                for (;;) {
                    startMS = MicroSeconds();
                    startCycle = GetCycleCount();
 
                    ui64 n = MicroSeconds();

                    if (n - startMS < 100) {
                        break;
                    }
                } 
 
                Sleep(TDuration::MicroSeconds(5000));
 
                ui64 finishCycle = 0;
                ui64 finishMS = 0;
 
                for (;;) {
                    finishMS = MicroSeconds();
 
                    if (finishMS - startMS < 100) {
                        continue;
                    }
 
                    finishCycle = GetCycleCount();
 
                    ui64 n = MicroSeconds();
 
                    if (n - finishMS < 100) {
                        break;
                    }
                } 
                if (startMS < finishMS && startCycle < finishCycle) {
                    return (finishCycle - startCycle) * 1000000.0 / (finishMS - startMS);
                }
            } 
        } 
 
        static double InitHPTimer() { 
            const size_t N_VEC = 9; 
 
            double vec[N_VEC]; 
 
            for (auto& i : vec) {
                i = EstimateCPUClock();
            } 
 
            Sort(vec, vec + N_VEC); 
 
            return 1.0 / vec[N_VEC / 2]; 
        } 
 
        inline double GetSeconds(const STime& a) const { 
            return static_cast<double>(a) * Freq; 
        } 
 
        inline double GetClockRate() const { 
            return Rate;
        } 
 
        inline ui64 GetCyclesPerSecond() const {
            return CyclesPerSecond;
        }

        const double Freq; 
        const double Rate;
        const ui64 CyclesPerSecond;
    }; 
}

double NHPTimer::GetSeconds(const STime& a) noexcept {
    return TFreq::Instance().GetSeconds(a); 
}

double NHPTimer::GetClockRate() noexcept {
    return TFreq::Instance().GetClockRate(); 
}

ui64 NHPTimer::GetCyclesPerSecond() noexcept {
    return TFreq::Instance().GetCyclesPerSecond();
}

void NHPTimer::GetTime(STime* pTime) noexcept {
    *pTime = GetCycleCount();
}

double NHPTimer::GetTimePassed(STime* pTime) noexcept {
    STime old(*pTime);
 
    *pTime = GetCycleCount();
 
    return GetSeconds(*pTime - old);
}