summaryrefslogtreecommitdiff
path: root/zen/tick_count.h
blob: 98e59ae50031acc7a8139621246561b68b1a46a5 (plain)
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
119
120
121
122
123
124
// **************************************************************************
// * This file is part of the zenXML project. It is distributed under the   *
// * Boost Software License: http://www.boost.org/LICENSE_1_0.txt           *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef ZEN_TICK_COUNT_HEADER_3807326
#define ZEN_TICK_COUNT_HEADER_3807326

#include <cstdint>
#include <algorithm>
#include "type_traits.h"
#include "assert_static.h"
#include <cmath>

#ifdef FFS_WIN
#include "win.h" //includes "windows.h"
#elif defined FFS_LINUX
#include <time.h> //Posix ::clock_gettime()
#endif

namespace zen
{
//a portable "GetTickCount()" using "wall time equivalent" - e.g. no jumps due to ntp time corrections
class TickVal;
std::int64_t dist(const TickVal& lhs, const TickVal& rhs); //use absolute difference for paranoid security: even QueryPerformanceCounter "wraps-around" at *some* time

std::int64_t ticksPerSec(); //return 0 on error
TickVal getTicks();         //return invalid value on error: !TickVal::isValid()















//############################ implementation ##############################
class TickVal
{
public:
#ifdef FFS_WIN
    typedef LARGE_INTEGER NativeVal;
#elif defined FFS_LINUX
    typedef timespec NativeVal;
#endif

    TickVal() : val_() {}
    explicit TickVal(const NativeVal& val) : val_(val) {}

    inline friend
    std::int64_t dist(const TickVal& lhs, const TickVal& rhs)
    {
#ifdef FFS_WIN
        assert_static(IsSignedInt<decltype(lhs.val_.QuadPart)>::value);
        return std::abs(lhs.val_.QuadPart - rhs.val_.QuadPart);
#elif defined FFS_LINUX
        assert_static(IsSignedInt<decltype(lhs.val_.tv_sec)>::value);
        assert_static(IsSignedInt<decltype(lhs.val_.tv_nsec)>::value);
        return std::abs(static_cast<std::int64_t>(lhs.val_.tv_sec - rhs.val_.tv_sec) * 1000000000.0 + (lhs.val_.tv_nsec - rhs.val_.tv_nsec));
#endif
    }

    inline friend
    bool operator<(const TickVal& lhs, const TickVal& rhs) //evaluate directly rather than reuse operator-
    {
#ifdef FFS_WIN
        return lhs.val_.QuadPart < rhs.val_.QuadPart;
#elif defined FFS_LINUX
        if (lhs.val_.tv_sec != rhs.val_.tv_sec)
            return lhs.val_.tv_sec < rhs.val_.tv_sec;
        return lhs.val_.tv_nsec < rhs.val_.tv_nsec;
#endif
    }

    bool isValid() const { return dist(*this, TickVal()) != 0; }

private:
    NativeVal val_;
};


inline
std::int64_t ticksPerSec() //return 0 on error
{
#ifdef FFS_WIN
    LARGE_INTEGER frequency = {};
    if (!::QueryPerformanceFrequency(&frequency)) //MSDN promises: "The frequency cannot change while the system is running."
        return 0;
    assert_static(sizeof(std::int64_t) >= sizeof(frequency.QuadPart));
    return frequency.QuadPart;

#elif defined FFS_LINUX
    return 1000000000; //precision: nanoseconds
#endif
}


inline
TickVal getTicks() //return 0 on error
{
#ifdef FFS_WIN
    LARGE_INTEGER now = {};
    if (!::QueryPerformanceCounter(&now)) //msdn: SetThreadAffinityMask() may be required if there are bugs in BIOS or HAL"
        return TickVal();

#elif defined FFS_LINUX
    //gettimeofday() seems fine but is deprecated
    timespec now = {};
    if (::clock_gettime(CLOCK_MONOTONIC_RAW, &now) != 0) //CLOCK_MONOTONIC measures time reliably across processors!
        return TickVal();
#endif
    return TickVal(now);
}
}

#endif //ZEN_TICK_COUNT_HEADER_3807326
bgstack15