~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getsystime.c

  • Committer: Brian Aker
  • Date: 2008-10-29 13:46:43 UTC
  • Revision ID: brian@tangent.org-20081029134643-z6jcwjvyruhk2vlu
Updates for ignore file.

Show diffs side-by-side

added added

removed removed

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