Cbeam
Loading...
Searching...
No Matches
clock_precision.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/logging/log_manager.hpp> // for CBEAM_LOG
29
30#ifdef _WIN32
31 #include "windows_config.hpp"
32#else
33 #include <time.h> // for clock_getres, timespec, CLOCK_MONOTONIC
34#endif
35
36#include <algorithm> // for std::min
37#include <chrono> // for std::chrono::operator-, std::chrono::duration, std::chrono::high_resolution_clock
38#include <mutex> // for std::mutex, std::lock_guard
39#include <string> // for std::string_literals::operator""s, std::string_literals
40
42{
55 static inline double get_clock_precision()
56 {
57 static double clock_precision{-1.0};
58 static std::mutex mtx;
59 std::lock_guard<std::mutex> lock(mtx);
60
61 if (clock_precision == -1.0)
62 {
63 using namespace std::string_literals;
64
65#ifdef _WIN32
66 LARGE_INTEGER frequency;
67 if (QueryPerformanceFrequency(&frequency))
68 {
69 clockPrecision = 1.0 / frequency.QuadPart;
70 }
71#elif defined(__linux__)
72 struct timespec ts;
73 if (clock_getres(CLOCK_MONOTONIC, &ts) == 0)
74 {
75 clock_precision = ts.tv_sec + ts.tv_nsec * 1e-9;
76 }
77#elif defined(__APPLE__)
78 mach_timebase_info_data_t info;
79 if (mach_timebase_info(&info) == 0)
80 {
81 clockPrecision = (double)info.numer / info.denom * 1e-9;
82 }
83#endif
84
85 if (clock_precision == -1.0)
86 {
87 CBEAM_LOG("Unable to determine resolution of high resolution clock, using fallback algorithm"s);
88 int iterations = 100;
89 while (iterations--)
90 {
91 auto startTime = std::chrono::high_resolution_clock::now();
92 double result;
93 do
94 {
95 result = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
96 } while (result == 0.0);
97
98 clock_precision = std::min(clock_precision, result);
99 }
100 }
101 }
102
103 return clock_precision;
104 }
105}
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Groups platform-specific helpers for Windows, Linux, and macOS. These utilities detect CPU architectu...
Definition clock_precision.hpp:42
Header file to manage inclusion of windows.h with specific settings.