Blender V5.0
time.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/time.h"
6
7#include <chrono>
8#include <cstdlib>
9
10#if !defined(_WIN32)
11# include <sys/time.h>
12# include <unistd.h>
13#endif
14
15#include "util/string.h"
16
17#ifdef _WIN32
18# include "util/windows.h"
19#endif
20
21#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
22# ifdef _MSC_VER
23# include <intrin.h>
24# else
25# include <x86intrin.h>
26# endif
27#endif
28
30
31#ifdef _WIN32
32double time_dt()
33{
34 __int64 frequency, counter;
35
36 QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
37 QueryPerformanceCounter((LARGE_INTEGER *)&counter);
38
39 return (double)counter / (double)frequency;
40}
41
42void time_sleep(const double t)
43{
44 Sleep((int)(t * 1000));
45}
46#else
47double time_dt()
48{
49 struct timeval now;
50 gettimeofday(&now, nullptr);
51
52 return now.tv_sec + now.tv_usec * 1e-6;
53}
54
55/* sleep t seconds */
56void time_sleep(double t)
57{
58 /* get whole seconds */
59 const int s = (int)t;
60
61 if (s >= 1) {
62 sleep(s);
63
64 /* adjust parameter to remove whole seconds */
65 t -= s;
66 }
67
68 /* get microseconds */
69 const int us = (int)(t * 1e6);
70 if (us > 0) {
71 usleep(us);
72 }
73}
74#endif
75
76#if defined(__aarch64__) || defined(_M_ARM64)
77/* Use cntvct_el0/cntfrq_el0 registers on ARM64. */
78
79uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
80{
81# if defined(ARCH_COMPILER_MSVC)
82 return _ReadStatusReg(ARM64_CNTVCT_EL0);
83# else
84 uint64_t counter;
85 asm("mrs %x0, cntvct_el0" : "=r"(counter));
86 return counter;
87# endif
88}
90{
91# if defined(ARCH_COMPILER_MSVC)
92 return _ReadStatusReg(ARM64_CNTFRQ_EL0);
93# else
94 uint64_t freq;
95 asm("mrs %x0, cntfrq_el0" : "=r"(freq));
96 return freq;
97# endif
98}
99#elif defined(__x86_64__) || defined(_M_X64)
100/* Use RDTSCP on x86-64. */
101
102uint64_t time_fast_tick(uint32_t *last_cpu)
103{
104 return __rdtscp(last_cpu);
105}
107{
108 static bool initialized = false;
109 static uint64_t frequency;
110
111 /* Unfortunately TSC does not provide a easily accessible frequency value, so roughly calibrate
112 * by sleeping a millisecond. Not ideal, but good enough for our purposes. */
113 if (!initialized) {
114 uint32_t cpu;
115 uint64_t start_tick = time_fast_tick(&cpu);
116 double start_precise = time_dt();
117 time_sleep(0.001);
118 uint64_t end_tick = time_fast_tick(&cpu);
119 double end_precise = time_dt();
120 frequency = uint64_t(double(end_tick - start_tick) / (end_precise - start_precise));
121 initialized = true;
122 }
123
124 return frequency;
125}
126#else
127/* Fall back to std::chrono::steady_clock. */
128
129uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
130{
131 auto now = std::chrono::steady_clock::now();
132 auto nanoseconds = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
133 return nanoseconds.time_since_epoch().count();
134}
136{
137 return 1000000000;
138}
139#endif
140
141/* Time in format "hours:minutes:seconds.hundreds" */
142
143string time_human_readable_from_seconds(const double seconds)
144{
145 const int h = (((int)seconds) / (60 * 60));
146 const int m = (((int)seconds) / 60) % 60;
147 const int s = (((int)seconds) % 60);
148 const int r = (((int)(seconds * 100)) % 100);
149
150 if (h > 0) {
151 return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
152 }
153 return string_printf("%.2d:%.2d.%.2d", m, s, r);
154}
155
156double time_human_readable_to_seconds(const string &time_string)
157{
158 /* Those are multiplies of a corresponding token surrounded by : in the
159 * time string, which denotes how to convert value to seconds.
160 * Effectively: seconds, minutes, hours, days in seconds. */
161 const int multipliers[] = {1, 60, 60 * 60, 24 * 60 * 60};
162 const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
163 if (time_string.empty()) {
164 return 0.0;
165 }
166 double result = 0.0;
167 /* Split fractions of a second from the encoded time. */
168 vector<string> fraction_tokens;
169 string_split(fraction_tokens, time_string, ".", false);
170 const int num_fraction_tokens = fraction_tokens.size();
171 if (num_fraction_tokens == 0) {
172 /* Time string is malformed. */
173 return 0.0;
174 }
175 if (fraction_tokens.size() == 1) {
176 /* There is no fraction of a second specified, the rest of the code
177 * handles this normally. */
178 }
179 else if (fraction_tokens.size() == 2) {
180 result = atof(fraction_tokens[1].c_str());
181 result *= ::pow(0.1, fraction_tokens[1].length());
182 }
183 else {
184 /* This is not a valid string, the result can not be reliable. */
185 return 0.0;
186 }
187 /* Split hours, minutes and seconds.
188 * Hours part is optional. */
189 vector<string> tokens;
190 string_split(tokens, fraction_tokens[0], ":", false);
191 const int num_tokens = tokens.size();
192 if (num_tokens > num_multiplies) {
193 /* Can not reliably represent the value. */
194 return 0.0;
195 }
196 for (int i = 0; i < num_tokens; ++i) {
197 result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
198 }
199 return result;
200}
201
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
unsigned long long int uint64_t
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
#define CCL_NAMESPACE_END
static bool initialized
#define pow
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:70
i
Definition text_draw.cc:230
uint64_t time_fast_frequency()
Definition time.cpp:135
uint64_t time_fast_tick(uint32_t *)
Definition time.cpp:129
void time_sleep(double t)
Definition time.cpp:56
double time_human_readable_to_seconds(const string &time_string)
Definition time.cpp:156
string time_human_readable_from_seconds(const double seconds)
Definition time.cpp:143
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:47