Library of Assembled Shared Sources
clock_impl.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#include "lass_common.h"
46#include "clock_impl.h"
47
48
49#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
50
51// --- win32 ---------------------------------------------------------------------------------------
52
53#define NOMINMAX
54#define WIN32_LEAN_AND_MEAN
55#include <windows.h>
56
57namespace lass
58{
59namespace util
60{
61namespace impl
62{
63
64ClockImpl::TTick ClockImpl::frequency()
65{
66 TTick result;
67 LASS_ENFORCE_WINAPI(QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&result)))
68 ("Hardware does not support a high-resolution performance counter.");
69 return result;
70}
71
72
73
74ClockImpl::TTick ClockImpl::tick()
75{
76 TTick result;
77 LASS_ENFORCE_WINAPI(QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&result)));
78 return result;
79}
80
81}
82
83}
84
85}
86
87#else
88
89// --- POSIX ---------------------------------------------------------------------------------------
90
91#include <sys/time.h>
92
93namespace
94{
95
96const lass::util::impl::ClockImpl::TTick nanoSecondsPerSecond = 1000000000;
97
98// No longer using CLOCK_MONOTONIC_RAW for following reasons:
99// - Not all kernels support it, even if it's defined, resulting in a runtime error.
100// We discovered this behavior on the Windows Subsystem for Linux (WSL)
101// - It turns out to be more expensive, see section "Overhead of Clock Queries"
102// of http://btorpey.github.io/blog/2014/02/18/clock-sources-in-linux/
103// - It's actually not what you want, because this clock isn't slewed by
104// time adjusting code. So it probably has a systematic error of running
105// either too fast or too slow.
106
107// There's also question on FreeBSD whether you actually want CLOCK_MONOTONIC_PRECISE.
108// It is more precise for sure, but it has a greater overhead ...
109
110#if defined(CLOCK_MONOTONIC_PRECISE)
111const clockid_t clkId = CLOCK_MONOTONIC_PRECISE;
112#else
113const clockid_t clkId = CLOCK_MONOTONIC;
114#endif
115
116}
117
118namespace lass
119{
120namespace util
121{
122namespace impl
123{
124
125ClockImpl::TTick ClockImpl::frequency()
126{
127 return nanoSecondsPerSecond;
128}
129
130
131
132ClockImpl::TTick ClockImpl::tick()
133{
134 timespec tp;
135 LASS_ENFORCE_CLIB(clock_gettime(clkId, &tp));
136 return tp.tv_sec * nanoSecondsPerSecond + tp.tv_nsec;
137}
138
139}
140
141}
142
143}
144
145#endif
146
147// EOF
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53