Blender V5.3
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
31double time_dt()
32{
33 return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch())
34 .count();
35}
36
37#ifdef _WIN32
38void time_sleep(const double t)
39{
40 Sleep((int)(t * 1000));
41}
42#else
43/* sleep t seconds */
44void time_sleep(double t)
45{
46 /* get whole seconds */
47 const int s = (int)t;
48
49 if (s >= 1) {
50 sleep(s);
51
52 /* adjust parameter to remove whole seconds */
53 t -= s;
54 }
55
56 /* get microseconds */
57 const int us = (int)(t * 1e6);
58 if (us > 0) {
59 usleep(us);
60 }
61}
62#endif
63
64#if defined(__aarch64__) || defined(_M_ARM64)
65/* Use cntvct_el0/cntfrq_el0 registers on ARM64. */
66
67uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
68{
69 /* MSVC does not define __aarch64__, or support inline ASM */
70# if !defined(__aarch64__)
71 return _ReadStatusReg(ARM64_CNTVCT_EL0);
72# else
73 uint64_t counter;
74 asm("mrs %x0, cntvct_el0" : "=r"(counter));
75 return counter;
76# endif
77}
79{
80 /* MSVC does not define __aarch64__, or support inline ASM */
81# if !defined(__aarch64__)
82 return _ReadStatusReg(ARM64_CNTFRQ_EL0);
83# else
84 uint64_t freq;
85 asm("mrs %x0, cntfrq_el0" : "=r"(freq));
86 return freq;
87# endif
88}
89#elif defined(__x86_64__) || defined(_M_X64)
90/* Use RDTSCP on x86-64. */
91
92uint64_t time_fast_tick(uint32_t *last_cpu)
93{
94 return __rdtscp(last_cpu);
95}
97{
98 /* Unfortunately TSC does not provide a easily accessible frequency value, so roughly calibrate
99 * by sleeping a millisecond. Not ideal, but good enough for our purposes. */
100 static const uint64_t frequency = []() {
101 uint32_t cpu;
102 uint64_t start_tick = time_fast_tick(&cpu);
103 double start_precise = time_dt();
104 time_sleep(0.001);
105 uint64_t end_tick = time_fast_tick(&cpu);
106 double end_precise = time_dt();
107 return uint64_t(double(end_tick - start_tick) / (end_precise - start_precise));
108 }();
109
110 return frequency;
111}
112#else
113/* Fall back to std::chrono::steady_clock. */
114
115uint64_t time_fast_tick(uint32_t * /*last_cpu*/)
116{
117 auto now = std::chrono::steady_clock::now();
118 auto nanoseconds = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
119 return nanoseconds.time_since_epoch().count();
120}
122{
123 return 1000000000;
124}
125#endif
126
127/* Time in format "hours:minutes:seconds.hundreds" */
128
129string time_human_readable_from_seconds(const double seconds)
130{
131 const int h = (((int)seconds) / (60 * 60));
132 const int m = (((int)seconds) / 60) % 60;
133 const int s = (((int)seconds) % 60);
134 const int r = (((int)(seconds * 100)) % 100);
135
136 if (h > 0) {
137 return string_printf("%.2d:%.2d:%.2d.%.2d", h, m, s, r);
138 }
139 return string_printf("%.2d:%.2d.%.2d", m, s, r);
140}
141
142double time_human_readable_to_seconds(const string &time_string)
143{
144 /* Those are multiplies of a corresponding token surrounded by : in the
145 * time string, which denotes how to convert value to seconds.
146 * Effectively: seconds, minutes, hours, days in seconds. */
147 const int multipliers[] = {1, 60, 60 * 60, 24 * 60 * 60};
148 const int num_multiplies = sizeof(multipliers) / sizeof(*multipliers);
149 if (time_string.empty()) {
150 return 0.0;
151 }
152 double result = 0.0;
153 /* Split fractions of a second from the encoded time. */
154 vector<string> fraction_tokens;
155 string_split(fraction_tokens, time_string, ".", false);
156 const int num_fraction_tokens = fraction_tokens.size();
157 if (num_fraction_tokens == 0) {
158 /* Time string is malformed. */
159 return 0.0;
160 }
161 if (fraction_tokens.size() == 1) {
162 /* There is no fraction of a second specified, the rest of the code
163 * handles this normally. */
164 }
165 else if (fraction_tokens.size() == 2) {
166 result = atof(fraction_tokens[1].c_str());
167 result *= ::pow(0.1, fraction_tokens[1].length());
168 }
169 else {
170 /* This is not a valid string, the result can not be reliable. */
171 return 0.0;
172 }
173 /* Split hours, minutes and seconds.
174 * Hours part is optional. */
175 vector<string> tokens;
176 string_split(tokens, fraction_tokens[0], ":", false);
177 const int num_tokens = tokens.size();
178 if (num_tokens > num_multiplies) {
179 /* Can not reliably represent the value. */
180 return 0.0;
181 }
182 for (int i = 0; i < num_tokens; ++i) {
183 result += atoi(tokens[num_tokens - i - 1].c_str()) * multipliers[i];
184 }
185 return result;
186}
187
unsigned long long int uint64_t
#define CCL_NAMESPACE_END
#define frequency(freq)
#define pow
float length(VecOp< float, D >) RET
unsigned int uint32_t
Definition BKE_text.h:81
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition string.cpp:72
uint64_t time_fast_frequency()
Definition time.cpp:121
uint64_t time_fast_tick(uint32_t *)
Definition time.cpp:115
void time_sleep(double t)
Definition time.cpp:44
double time_human_readable_to_seconds(const string &time_string)
Definition time.cpp:142
string time_human_readable_from_seconds(const double seconds)
Definition time.cpp:129
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:31