MySQL 8.0.40
Source Code Documentation
my_systime.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef MY_SYSTIME_INCLUDED
26#define MY_SYSTIME_INCLUDED
27
28/**
29 @file include/my_systime.h
30 Defines for getting and processing the current system type programmatically.
31*/
32
33#include <time.h> // time_t, struct timespec (C11/C++17)
34#include <cassert> // assert
35#include <chrono> // std::chrono::microseconds
36#include <cstdint> // std::int64_t
37#include <limits> // std::numeric_limits
38#include <thread> // std::this_thread::wait_for
39#include "my_config.h"
40
41#ifdef HAVE_SYS_TIME_H
42#include <sys/time.h> // clock_gettime()
43#endif /* HAVE_SYS_TIME_H */
44#ifdef _WIN32
45#include <winsock2.h> // struct timeval
46#endif /* _WIN32 */
47#include "my_time_t.h"
48using UTC_clock = std::chrono::system_clock;
49
50/* Bits for get_date timeflag */
51constexpr const int GETDATE_DATE_TIME = 1;
52constexpr const int GETDATE_SHORT_DATE = 2;
53constexpr const int GETDATE_HHMMSSTIME = 4;
54constexpr const int GETDATE_GMT = 8;
55constexpr const int GETDATE_FIXEDLENGTH = 16;
56constexpr const int GETDATE_T_DELIMITER = 32;
57constexpr const int GETDATE_SHORT_DATE_FULL_YEAR = 64;
58
59/**
60 Wait a given number of microseconds.
61
62 @param m_seconds number of microseconds to wait.
63*/
64inline void my_sleep(time_t m_seconds) {
65 std::this_thread::sleep_for(std::chrono::microseconds{m_seconds});
66}
67
68#ifdef _WIN32
69
70#include <windows.h>
71
72/****************************************************************************
73** Replacements for localtime_r and gmtime_r
74****************************************************************************/
75
76inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
77 localtime_s(tmp, timep);
78 return tmp;
79}
80
81inline struct tm *gmtime_r(const time_t *clock, struct tm *res) {
82 gmtime_s(res, clock);
83 return res;
84}
85
86/**
87 Sleep the given number of seconds. POSIX compatibility.
88
89 @param seconds number of seconds to wait
90*/
91inline void sleep(unsigned long seconds) {
92 std::this_thread::sleep_for(std::chrono::seconds{seconds});
93}
94
95#endif /* _WIN32 */
96
97/**
98 Get high-resolution time. Forwards to std::chrono.
99
100 @return current high-resolution time in multiples of 100 nanoseconds.
101*/
102inline unsigned long long int my_getsystime() {
103#ifdef HAVE_CLOCK_GETTIME
104 // Performance regression testing showed this to be preferable
105 struct timespec tp;
106 clock_gettime(CLOCK_REALTIME, &tp);
107 return (static_cast<unsigned long long int>(tp.tv_sec) * 10000000 +
108 static_cast<unsigned long long int>(tp.tv_nsec) / 100);
109#else
110 return std::chrono::duration_cast<
111 std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>>(
112 UTC_clock::now().time_since_epoch())
113 .count();
114#endif /* HAVE_CLOCK_GETTIME */
115}
116
117/**
118 The maximum timespec value used to represent "inifinity" (as when
119 requesting an "infinite" timeout.
120 */
121constexpr const timespec TIMESPEC_POSINF = {
122 std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999};
123
124/** Type alias to reduce chance of conversion errors on timeout values. */
125using Timeout_type = std::uint64_t;
126
127/** Value representing "infinite" timeout. */
128constexpr const Timeout_type TIMEOUT_INF =
129 std::numeric_limits<Timeout_type>::max() - 1;
130
131void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec);
132void set_timespec(struct timespec *abstime, Timeout_type sec);
133timespec timespec_now();
134
135/**
136 Compare two timespec structs.
137
138 @retval 1 If ts1 ends after ts2.
139 @retval -1 If ts1 ends before ts2.
140 @retval 0 If ts1 is equal to ts2.
141*/
142inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
143 if (ts1->tv_sec > ts2->tv_sec ||
144 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
145 return 1;
146 if (ts1->tv_sec < ts2->tv_sec ||
147 (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
148 return -1;
149 return 0;
150}
151
152/**
153 Calculate the diff between two timespec values.
154
155 @return difference in nanoseconds between ts1 and ts2
156*/
157inline unsigned long long int diff_timespec(struct timespec *ts1,
158 struct timespec *ts2) {
159 return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL + ts1->tv_nsec -
160 ts2->tv_nsec;
161}
162
163/**
164 Return current time. Takes an int argument
165 for backward compatibility. This argument is ignored.
166
167 @deprecated New code should use std::time() directly.
168
169 @retval current time.
170*/
171[[deprecated]] inline time_t my_time(int) { return time(nullptr); }
172
173/**
174 Return time in microseconds. Uses std::chrono::high_resolution_clock
175
176 @remark This function is to be used to measure performance in
177 micro seconds.
178
179 @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000
180 (UTC)
181*/
182inline unsigned long long int my_micro_time() {
183#ifdef _WIN32
184 return std::chrono::duration_cast<std::chrono::microseconds>(
185 UTC_clock::now().time_since_epoch())
186 .count();
187#else
188 struct timeval t;
189 /*
190 The following loop is here because gettimeofday may fail on some systems
191 */
192 while (gettimeofday(&t, nullptr) != 0) {
193 }
194 return (static_cast<unsigned long long int>(t.tv_sec) * 1000000 + t.tv_usec);
195#endif /* _WIN32 */
196}
197
198/**
199 Return time in milliseconds. Uses std::chrono::high_resolution_clock
200
201 @remark This function is to be used to measure time in
202 millisecond.
203
204 @retval Number of milliseconds since the Epoch, 1970-01-01 00:00:00 +0000
205 (UTC)
206*/
207inline unsigned long long int my_milli_time() {
208#ifdef _WIN32
209 return std::chrono::duration_cast<std::chrono::milliseconds>(
210 UTC_clock::now().time_since_epoch())
211 .count();
212#else
213 struct timeval t;
214 /*
215 The following loop is here because gettimeofday may fail on some systems
216 */
217 while (gettimeofday(&t, nullptr) != 0) {
218 }
219 return (static_cast<unsigned long long int>(t.tv_sec) * 1000 +
220 (t.tv_usec / 1000));
221#endif /* _WIN32 */
222}
223
224/**
225 Convert microseconds since epoch to my_timeval.
226 @param micro_time Microseconds.
227 @param[out] tm A timeval variable to write to.
228*/
229inline void my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm) {
230 tm->m_tv_sec = static_cast<int64_t>(micro_time / 1000000);
231 tm->m_tv_usec = static_cast<int64_t>(micro_time % 1000000);
232}
233
234/**
235
236 Convert microseconds since epoch to timeval. Prefer
237
238 my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm)
239
240 which is 64 bits safe on all platforms: Window's timeval's long
241 members are only 32 bits. Unless you need to use the host system's
242 struct timeval, of course.
243
244 @param micro_time Microseconds.
245 @param[out] tm A timeval variable to write to.
246 */
247inline void my_micro_time_to_timeval(std::uint64_t micro_time, timeval *tm) {
248 assert(static_cast<std::int64_t>(micro_time / 1000000) <=
249 std::numeric_limits<long>::max());
250 tm->tv_sec = static_cast<long>(micro_time / 1000000);
251 tm->tv_usec = static_cast<long>(micro_time % 1000000);
252}
253
254void get_date(char *to, int flag, time_t date);
255
256#endif // MY_SYSTIME_INCLUDED
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:162
static int flag
Definition: hp_test1.cc:40
constexpr const int GETDATE_T_DELIMITER
Definition: my_systime.h:56
void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec)
Set the value of a timespec object to the current time plus a number of nanosconds.
Definition: my_systime.cc:58
timespec timespec_now()
time_t my_time(int)
Return current time.
Definition: my_systime.h:171
void get_date(char *to, int flag, time_t date)
Store textual representation of date in a character array.
Definition: my_systime.cc:109
int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_systime.h:142
constexpr const timespec TIMESPEC_POSINF
The maximum timespec value used to represent "inifinity" (as when requesting an "infinite" timeout.
Definition: my_systime.h:121
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:102
std::chrono::system_clock UTC_clock
Definition: my_systime.h:48
constexpr const int GETDATE_GMT
Definition: my_systime.h:54
void my_micro_time_to_timeval(std::uint64_t micro_time, my_timeval *tm)
Convert microseconds since epoch to my_timeval.
Definition: my_systime.h:229
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR
Definition: my_systime.h:57
constexpr const int GETDATE_FIXEDLENGTH
Definition: my_systime.h:55
unsigned long long int my_milli_time()
Return time in milliseconds.
Definition: my_systime.h:207
unsigned long long int diff_timespec(struct timespec *ts1, struct timespec *ts2)
Calculate the diff between two timespec values.
Definition: my_systime.h:157
void my_sleep(time_t m_seconds)
Wait a given number of microseconds.
Definition: my_systime.h:64
constexpr const int GETDATE_DATE_TIME
Definition: my_systime.h:51
constexpr const int GETDATE_HHMMSSTIME
Definition: my_systime.h:53
unsigned long long int my_micro_time()
Return time in microseconds.
Definition: my_systime.h:182
void set_timespec(struct timespec *abstime, Timeout_type sec)
Set the value of a timespec object to the current time plus a number of seconds using seconds.
Definition: my_systime.cc:84
constexpr const int GETDATE_SHORT_DATE
Definition: my_systime.h:52
constexpr const Timeout_type TIMEOUT_INF
Value representing "infinite" timeout.
Definition: my_systime.h:128
std::uint64_t Timeout_type
Type alias to reduce chance of conversion errors on timeout values.
Definition: my_systime.h:125
static int count
Definition: myisam_ftdump.cc:43
Replacement of system's struct timeval to ensure we can carry 64 bit values even on a platform which ...
Definition: my_time_t.h:45
int64_t m_tv_sec
Definition: my_time_t.h:46
int64_t m_tv_usec
Definition: my_time_t.h:47
double seconds()
Definition: task.cc:310
Include file for Sun RPC to compile out of the box.