aboutsummaryrefslogtreecommitdiff
path: root/zencore/timer.cpp
blob: 1655e912db76367dc800c15d0c0165560e8256b1 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/thread.h>
#include <zencore/timer.h>

#include <zencore/testing.h>

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>
#elif ZEN_PLATFORM_LINUX
#	include <time.h>
#	include <unistd.h>
#endif

namespace zen {

uint64_t
GetHifreqTimerValue()
{
	uint64_t Timestamp;

#if ZEN_PLATFORM_WINDOWS
	LARGE_INTEGER li;
	QueryPerformanceCounter(&li);

	Timestamp = li.QuadPart;
#else
	struct timespec ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	Timestamp = (uint64_t(ts.tv_sec) * 1000000ull) + (uint64_t(ts.tv_nsec) / 1000ull);
#endif

	return Timestamp;
}

uint64_t
InternalGetHifreqTimerFrequency()
{
#if ZEN_PLATFORM_WINDOWS
	LARGE_INTEGER li;
	QueryPerformanceFrequency(&li);

	return li.QuadPart;
#else
	return 1000000ull;
#endif
}

uint64_t			QpcFreq	  = InternalGetHifreqTimerFrequency();
static const double QpcFactor = 1.0 / InternalGetHifreqTimerFrequency();

uint64_t
GetHifreqTimerFrequency()
{
	return QpcFreq;
}

double
GetHifreqTimerToSeconds()
{
	return QpcFactor;
}

uint64_t
GetHifreqTimerFrequencySafe()
{
	if (!QpcFreq)
	{
		QpcFreq = InternalGetHifreqTimerFrequency();
	}

	return QpcFreq;
}

//////////////////////////////////////////////////////////////////////////

uint64_t detail::g_LofreqTimerValue = GetHifreqTimerValue();

void
UpdateLofreqTimerValue()
{
	detail::g_LofreqTimerValue = GetHifreqTimerValue();
}

uint64_t
GetLofreqTimerFrequency()
{
	return GetHifreqTimerFrequencySafe();
}

//////////////////////////////////////////////////////////////////////////
//
// Testing related code follows...
//

#if ZEN_WITH_TESTS

void
timer_forcelink()
{
}

#endif

}  // namespace zen