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