1
by brian
clean slate |
1 |
/* Copyright (C) 2004 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* get time since epoc in 100 nanosec units */
|
|
17 |
/* thus to get the current time we should use the system function
|
|
18 |
with the highest possible resolution */
|
|
19 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
20 |
/*
|
1
by brian
clean slate |
21 |
TODO: in functions my_micro_time() and my_micro_time_and_time() there
|
22 |
exists some common code that should be merged into a function.
|
|
23 |
*/
|
|
24 |
||
994.2.4
by Monty Taylor
Blast. Fixed some make distcheck issues. |
25 |
#include "mysys/mysys_priv.h" |
1
by brian
clean slate |
26 |
#include "my_static.h" |
27 |
||
481.1.15
by Monty Taylor
Removed time.h and sys/time.h from global.h. |
28 |
#if TIME_WITH_SYS_TIME
|
29 |
# include <sys/time.h>
|
|
30 |
# include <time.h>
|
|
31 |
#else
|
|
32 |
# if HAVE_SYS_TIME_H
|
|
33 |
# include <sys/time.h>
|
|
34 |
# else
|
|
35 |
# include <time.h>
|
|
36 |
# endif
|
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
37 |
#endif
|
481.1.15
by Monty Taylor
Removed time.h and sys/time.h from global.h. |
38 |
|
151
by Brian Aker
Ulonglong to uint64_t |
39 |
uint64_t my_getsystime() |
1
by brian
clean slate |
40 |
{
|
41 |
#ifdef HAVE_CLOCK_GETTIME
|
|
42 |
struct timespec tp; |
|
43 |
clock_gettime(CLOCK_REALTIME, &tp); |
|
151
by Brian Aker
Ulonglong to uint64_t |
44 |
return (uint64_t)tp.tv_sec*10000000+(uint64_t)tp.tv_nsec/100; |
1
by brian
clean slate |
45 |
#else
|
46 |
/* TODO: check for other possibilities for hi-res timestamping */
|
|
47 |
struct timeval tv; |
|
48 |
gettimeofday(&tv,NULL); |
|
151
by Brian Aker
Ulonglong to uint64_t |
49 |
return (uint64_t)tv.tv_sec*10000000+(uint64_t)tv.tv_usec*10; |
1
by brian
clean slate |
50 |
#endif
|
51 |
}
|
|
52 |
||
53 |
/*
|
|
54 |
Return time in micro seconds
|
|
55 |
||
56 |
SYNOPSIS
|
|
57 |
my_micro_time()
|
|
58 |
||
59 |
NOTES
|
|
60 |
This function is to be used to measure performance in micro seconds.
|
|
61 |
As it's not defined whats the start time for the clock, this function
|
|
62 |
us only useful to measure time between two moments.
|
|
63 |
||
64 |
For windows platforms we need the frequency value of the CUP. This is
|
|
65 |
initalized in my_init.c through QueryPerformanceFrequency().
|
|
66 |
||
67 |
If Windows platform doesn't support QueryPerformanceFrequency() we will
|
|
68 |
obtain the time via GetClockCount, which only supports milliseconds.
|
|
69 |
||
70 |
RETURN
|
|
71 |
Value in microseconds from some undefined point in time
|
|
72 |
*/
|
|
73 |
||
151
by Brian Aker
Ulonglong to uint64_t |
74 |
uint64_t my_micro_time() |
1
by brian
clean slate |
75 |
{
|
76 |
#if defined(HAVE_GETHRTIME)
|
|
77 |
return gethrtime()/1000; |
|
78 |
#else
|
|
151
by Brian Aker
Ulonglong to uint64_t |
79 |
uint64_t newtime; |
1
by brian
clean slate |
80 |
struct timeval t; |
81 |
/*
|
|
82 |
The following loop is here because gettimeofday may fail on some systems
|
|
83 |
*/
|
|
84 |
while (gettimeofday(&t, NULL) != 0) |
|
85 |
{}
|
|
151
by Brian Aker
Ulonglong to uint64_t |
86 |
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec; |
1
by brian
clean slate |
87 |
return newtime; |
88 |
#endif /* defined(HAVE_GETHRTIME) */ |
|
89 |
}
|
|
90 |
||
91 |
||
92 |
/*
|
|
93 |
Return time in seconds and timer in microseconds (not different start!)
|
|
94 |
||
95 |
SYNOPSIS
|
|
96 |
my_micro_time_and_time()
|
|
97 |
time_arg Will be set to seconds since epoch (00:00:00 UTC,
|
|
98 |
January 1, 1970)
|
|
99 |
||
100 |
NOTES
|
|
101 |
This function is to be useful when we need both the time and microtime.
|
|
102 |
For example in MySQL this is used to get the query time start of a query
|
|
103 |
and to measure the time of a query (for the slow query log)
|
|
104 |
||
105 |
IMPLEMENTATION
|
|
106 |
Value of time is as in time() call.
|
|
107 |
Value of microtime is same as my_micro_time(), which may be totally
|
|
108 |
unrealated to time()
|
|
109 |
||
110 |
RETURN
|
|
111 |
Value in microseconds from some undefined point in time
|
|
112 |
*/
|
|
113 |
||
114 |
||
151
by Brian Aker
Ulonglong to uint64_t |
115 |
uint64_t my_micro_time_and_time(time_t *time_arg) |
1
by brian
clean slate |
116 |
{
|
151
by Brian Aker
Ulonglong to uint64_t |
117 |
uint64_t newtime; |
1
by brian
clean slate |
118 |
struct timeval t; |
119 |
/*
|
|
120 |
The following loop is here because gettimeofday may fail on some systems
|
|
121 |
*/
|
|
873
by Brian Aker
Solaris performanc fix. |
122 |
while (gettimeofday(&t, NULL) != 0) {} |
1
by brian
clean slate |
123 |
*time_arg= t.tv_sec; |
151
by Brian Aker
Ulonglong to uint64_t |
124 |
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec; |
873
by Brian Aker
Solaris performanc fix. |
125 |
|
1
by brian
clean slate |
126 |
return newtime; |
127 |
}
|
|
128 |
||
129 |
||
130 |
/*
|
|
131 |
Returns current time
|
|
132 |
||
133 |
SYNOPSIS
|
|
134 |
my_time_possible_from_micro()
|
|
135 |
microtime Value from very recent my_micro_time()
|
|
136 |
||
137 |
NOTES
|
|
138 |
This function returns the current time. The microtime argument is only used
|
|
139 |
if my_micro_time() uses a function that can safely be converted to the
|
|
140 |
current time.
|
|
141 |
||
142 |
RETURN
|
|
143 |
current time
|
|
144 |
*/
|
|
145 |
||
632.1.11
by Monty Taylor
Fixed Sun Studio warnings in mysys. |
146 |
time_t my_time_possible_from_micro(uint64_t microtime) |
1
by brian
clean slate |
147 |
{
|
148 |
return (time_t) (microtime / 1000000); |
|
149 |
}
|
|
150 |