~drizzle-trunk/drizzle/development

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
25
#include "mysys_priv.h"
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
/*
55
  Return current time
56
57
  SYNOPSIS
58
    my_time()
59
    flags	If MY_WME is set, write error if time call fails
60
61
*/
62
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
63
time_t my_time(myf flags)
1 by brian
clean slate
64
{
65
  time_t t;
66
#ifdef HAVE_GETHRTIME
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
67
  (void)flags;
1 by brian
clean slate
68
  (void) my_micro_time_and_time(&t);
69
  return t;
70
#else
71
  /* The following loop is here beacuse time() may fail on some systems */
72
  while ((t= time(0)) == (time_t) -1)
73
  {
74
    if (flags & MY_WME)
75
      fprintf(stderr, "%s: Warning: time() call failed\n", my_progname);
76
  }
77
  return t;
78
#endif
79
}
80
81
82
/*
83
  Return time in micro seconds
84
85
  SYNOPSIS
86
    my_micro_time()
87
88
  NOTES
89
    This function is to be used to measure performance in micro seconds.
90
    As it's not defined whats the start time for the clock, this function
91
    us only useful to measure time between two moments.
92
93
    For windows platforms we need the frequency value of the CUP. This is
94
    initalized in my_init.c through QueryPerformanceFrequency().
95
96
    If Windows platform doesn't support QueryPerformanceFrequency() we will
97
    obtain the time via GetClockCount, which only supports milliseconds.
98
99
  RETURN
100
    Value in microseconds from some undefined point in time
101
*/
102
151 by Brian Aker
Ulonglong to uint64_t
103
uint64_t my_micro_time()
1 by brian
clean slate
104
{
105
#if defined(HAVE_GETHRTIME)
106
  return gethrtime()/1000;
107
#else
151 by Brian Aker
Ulonglong to uint64_t
108
  uint64_t newtime;
1 by brian
clean slate
109
  struct timeval t;
110
  /*
111
    The following loop is here because gettimeofday may fail on some systems
112
  */
113
  while (gettimeofday(&t, NULL) != 0)
114
  {}
151 by Brian Aker
Ulonglong to uint64_t
115
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
1 by brian
clean slate
116
  return newtime;
117
#endif  /* defined(HAVE_GETHRTIME) */
118
}
119
120
121
/*
122
  Return time in seconds and timer in microseconds (not different start!)
123
124
  SYNOPSIS
125
    my_micro_time_and_time()
126
    time_arg		Will be set to seconds since epoch (00:00:00 UTC,
127
                        January 1, 1970)
128
129
  NOTES
130
    This function is to be useful when we need both the time and microtime.
131
    For example in MySQL this is used to get the query time start of a query
132
    and to measure the time of a query (for the slow query log)
133
134
  IMPLEMENTATION
135
    Value of time is as in time() call.
136
    Value of microtime is same as my_micro_time(), which may be totally
137
    unrealated to time()
138
139
  RETURN
140
    Value in microseconds from some undefined point in time
141
*/
142
143
151 by Brian Aker
Ulonglong to uint64_t
144
uint64_t my_micro_time_and_time(time_t *time_arg)
1 by brian
clean slate
145
{
146
#if defined(HAVE_GETHRTIME)
147
  /*
148
    Solaris has a very slow time() call. We optimize this by using the very
149
    fast gethrtime() call and only calling time() every 1/2 second
150
  */
511.2.1 by Monty Taylor
Added -Wunused-macros.
151
152
#define DELTA_FOR_SECONDS 500000000LL  /* Half a second */
153
1 by brian
clean slate
154
  static hrtime_t prev_gethrtime= 0;
155
  static time_t cur_time= 0;
156
  hrtime_t cur_gethrtime;
157
158
  pthread_mutex_lock(&THR_LOCK_time);
159
  cur_gethrtime= gethrtime();
160
  if ((cur_gethrtime - prev_gethrtime) > DELTA_FOR_SECONDS)
161
  {
162
    cur_time= time(0);
163
    prev_gethrtime= cur_gethrtime;
164
  }
165
  *time_arg= cur_time;
166
  pthread_mutex_unlock(&THR_LOCK_time);
167
  return cur_gethrtime/1000;
168
#else
151 by Brian Aker
Ulonglong to uint64_t
169
  uint64_t newtime;
1 by brian
clean slate
170
  struct timeval t;
171
  /*
172
    The following loop is here because gettimeofday may fail on some systems
173
  */
174
  while (gettimeofday(&t, NULL) != 0)
175
  {}
176
  *time_arg= t.tv_sec;
151 by Brian Aker
Ulonglong to uint64_t
177
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
1 by brian
clean slate
178
  return newtime;
179
#endif  /* defined(HAVE_GETHRTIME) */
180
}
181
182
183
/*
184
  Returns current time
185
186
  SYNOPSIS
187
    my_time_possible_from_micro()
188
    microtime		Value from very recent my_micro_time()
189
190
  NOTES
191
    This function returns the current time. The microtime argument is only used
192
    if my_micro_time() uses a function that can safely be converted to the
193
    current time.
194
195
  RETURN
196
    current time
197
*/
198
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
199
time_t my_time_possible_from_micro(uint64_t microtime)
1 by brian
clean slate
200
{
201
#if defined(HAVE_GETHRTIME)
632.1.11 by Monty Taylor
Fixed Sun Studio warnings in mysys.
202
  (void) microtime;
1 by brian
clean slate
203
  return my_time(0);                            /* Cached time */
204
#else
205
  return (time_t) (microtime / 1000000);
206
#endif  /* defined(HAVE_GETHRTIME) */
207
}
208