MySQL  8.0.20
Source Code Documentation
my_systime.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
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 <chrono> // std::chrono::microseconds
34 #include <cstdint> // std::int64_t
35 #include <limits> // std::numeric_limits
36 #include <thread> // std::this_thread::wait_for
37 
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 
47 using UTC_clock = std::chrono::system_clock;
48 
49 /* Bits for get_date timeflag */
50 constexpr const int GETDATE_DATE_TIME = 1;
51 constexpr const int GETDATE_SHORT_DATE = 2;
52 constexpr const int GETDATE_HHMMSSTIME = 4;
53 constexpr const int GETDATE_GMT = 8;
54 constexpr const int GETDATE_FIXEDLENGTH = 16;
55 constexpr const int GETDATE_T_DELIMITER = 32;
56 constexpr 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 */
63 inline 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 
75 inline struct tm *localtime_r(const time_t *timep, struct tm *tmp) {
76  localtime_s(tmp, timep);
77  return tmp;
78 }
79 
80 inline 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 */
90 inline 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  @deprecated New code should use std::chrono directly.
100 
101  @return current high-resolution time in multiples of 100 nanoseconds.
102 */
103 inline unsigned long long int my_getsystime() {
104 #ifdef HAVE_CLOCK_GETTIME
105  // Performance regression testing showed this to be preferable
106  struct timespec tp;
107  clock_gettime(CLOCK_REALTIME, &tp);
108  return (static_cast<unsigned long long int>(tp.tv_sec) * 10000000 +
109  static_cast<unsigned long long int>(tp.tv_nsec) / 100);
110 #else
111  return std::chrono::duration_cast<
112  std::chrono::duration<std::int64_t, std::ratio<1, 10000000>>>(
113  UTC_clock::now().time_since_epoch())
114  .count();
115 #endif /* HAVE_CLOCK_GETTIME */
116 }
117 
118 /**
119  The maximum timespec value used to represent "inifinity" (as when
120  requesting an "inifinite" timeout.
121  */
122 constexpr const timespec TIMESPEC_POSINF = {
123  std::numeric_limits<decltype(timespec::tv_sec)>::max(), 999999999};
124 
125 /** Type alias to reduce chance of coversion errors on timeout values. */
126 using Timeout_type = std::uint64_t;
127 
128 /** Value representing "infinite" timeout. */
129 constexpr const Timeout_type TIMEOUT_INF =
130  std::numeric_limits<Timeout_type>::max() - 1;
131 
132 void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec);
133 void set_timespec(struct timespec *abstime, Timeout_type sec);
134 timespec timespec_now();
135 
136 /**
137  Compare two timespec structs.
138 
139  @retval 1 If ts1 ends after ts2.
140  @retval -1 If ts1 ends before ts2.
141  @retval 0 If ts1 is equal to ts2.
142 */
143 inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2) {
144  if (ts1->tv_sec > ts2->tv_sec ||
145  (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
146  return 1;
147  if (ts1->tv_sec < ts2->tv_sec ||
148  (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
149  return -1;
150  return 0;
151 }
152 
153 /**
154  Calculate the diff between two timespec values.
155 
156  @return difference in nanoseconds between ts1 and ts2
157 */
158 inline unsigned long long int diff_timespec(struct timespec *ts1,
159  struct timespec *ts2) {
160  return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL + ts1->tv_nsec -
161  ts2->tv_nsec;
162 }
163 
164 /**
165  Return current time. Takes an int argument
166  for backward compatibility. This argument is ignored.
167 
168  @deprecated New code should use std::time() directly.
169 
170  @retval current time.
171 */
172 inline time_t my_time(int) { return time(nullptr); }
173 
174 /**
175  Return time in microseconds. Uses std::chrono::high_resolution_clock
176 
177  @remark This function is to be used to measure performance in
178  micro seconds.
179 
180  @deprecated New code should use std::chrono directly.
181 
182  @retval Number of microseconds since the Epoch, 1970-01-01 00:00:00 +0000
183  (UTC)
184 */
185 inline unsigned long long int my_micro_time() {
186 #ifdef _WIN32
187  return std::chrono::duration_cast<std::chrono::microseconds>(
188  UTC_clock::now().time_since_epoch())
189  .count();
190 #else
191  struct timeval t;
192  /*
193  The following loop is here because gettimeofday may fail on some systems
194  */
195  while (gettimeofday(&t, nullptr) != 0) {
196  }
197  return (static_cast<unsigned long long int>(t.tv_sec) * 1000000 + t.tv_usec);
198 #endif /* _WIN32 */
199 }
200 
201 /**
202  Return time in milliseconds. Uses std::chrono::high_resolution_clock
203 
204  @remark This function is to be used to measure time in
205  millisecond.
206 
207  @retval Number of milliseconds since the Epoch, 1970-01-01 00:00:00 +0000
208  (UTC)
209 */
210 inline unsigned long long int my_milli_time() {
211 #ifdef _WIN32
212  return std::chrono::duration_cast<std::chrono::milliseconds>(
213  UTC_clock::now().time_since_epoch())
214  .count();
215 #else
216  struct timeval t;
217  /*
218  The following loop is here because gettimeofday may fail on some systems
219  */
220  while (gettimeofday(&t, nullptr) != 0) {
221  }
222  return (static_cast<unsigned long long int>(t.tv_sec) * 1000 + t.tv_usec);
223 #endif /* _WIN32 */
224 }
225 
226 /**
227  Convert microseconds since epoch to timeval.
228  @param micro_time Microseconds.
229  @param[out] tm A timeval variable to write to.
230 */
231 inline void my_micro_time_to_timeval(std::uint64_t micro_time,
232  struct timeval *tm) {
233  tm->tv_sec = static_cast<long>(micro_time / 1000000);
234  tm->tv_usec = static_cast<long>(micro_time % 1000000);
235 }
236 
237 void get_date(char *to, int flag, time_t date);
238 
239 #endif // MY_SYSTIME_INCLUDED
ssize_t count
Definition: memcached.c:386
Include file for Sun RPC to compile out of the box.
timespec timespec_now()
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:82
constexpr const int GETDATE_HHMMSSTIME
Definition: my_systime.h:52
int sleep(int seconds)
Definition: win32.c:256
unsigned long long int diff_timespec(struct timespec *ts1, struct timespec *ts2)
Calculate the diff between two timespec values.
Definition: my_systime.h:158
std::uint64_t Timeout_type
Type alias to reduce chance of coversion errors on timeout values.
Definition: my_systime.h:126
constexpr const Timeout_type TIMEOUT_INF
Value representing "infinite" timeout.
Definition: my_systime.h:129
constexpr const int GETDATE_DATE_TIME
Definition: my_systime.h:50
unsigned long long int my_getsystime()
Get high-resolution time.
Definition: my_systime.h:103
time_t my_time(int)
Return current time.
Definition: my_systime.h:172
void my_sleep(time_t m_seconds)
Wait a given number of microseconds.
Definition: my_systime.h:63
constexpr const timespec TIMESPEC_POSINF
The maximum timespec value used to represent "inifinity" (as when requesting an "inifinite" timeout...
Definition: my_systime.h:122
unsigned long long int my_milli_time()
Return time in milliseconds.
Definition: my_systime.h:210
unsigned long long int my_micro_time()
Return time in microseconds.
Definition: my_systime.h:185
double seconds()
Definition: task.c:298
static int flag
Definition: hp_test1.cc:39
void get_date(char *to, int flag, time_t date)
Store textual representation of date in a character array.
Definition: my_systime.cc:107
std::chrono::system_clock UTC_clock
Definition: my_systime.h:47
constexpr const int GETDATE_FIXEDLENGTH
Definition: my_systime.h:54
int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
Compare two timespec structs.
Definition: my_systime.h:143
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:56
constexpr const int GETDATE_SHORT_DATE_FULL_YEAR
Definition: my_systime.h:56
constexpr const int GETDATE_SHORT_DATE
Definition: my_systime.h:51
static time_t abstime(const rel_time_t exptime)
Convert the relative time to an absolute time (relative to EPOC ;) )
Definition: memcached.c:271
constexpr const int GETDATE_GMT
Definition: my_systime.h:53
constexpr const int GETDATE_T_DELIMITER
Definition: my_systime.h:55
void my_micro_time_to_timeval(std::uint64_t micro_time, struct timeval *tm)
Convert microseconds since epoch to timeval.
Definition: my_systime.h:231
static struct my_cs_file_section_st sec[]
Definition: ctype.cc:159