OSDN Git Service

PR 47571 Weakref trickery for clock_gettime()
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / time_1.h
1 /* Wrappers for platform timing functions.
2    Copyright (C) 2003, 2007, 2009, 2011 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
10
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 <http://www.gnu.org/licenses/>.  */
24
25 #ifndef LIBGFORTRAN_TIME_H
26 #define LIBGFORTRAN_TIME_H
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include <errno.h>
33
34 /* The time related intrinsics (DTIME, ETIME, CPU_TIME) to "compare
35    different algorithms on the same computer or discover which parts
36    are the most expensive", need a way to get the CPU time with the
37    finest resolution possible. We can only be accurate up to
38    microseconds.
39
40    As usual with UNIX systems, unfortunately no single way is
41    available for all systems.  */
42
43 #ifdef TIME_WITH_SYS_TIME
44 #  include <sys/time.h>
45 #  include <time.h>
46 #else
47 #  if HAVE_SYS_TIME_H
48 #    include <sys/time.h>
49 #  else
50 #    ifdef HAVE_TIME_H
51 #      include <time.h>
52 #    endif
53 #  endif
54 #endif
55
56 #ifdef HAVE_SYS_TYPES_H
57      #include <sys/types.h>
58 #endif
59
60 /* The most accurate way to get the CPU time is getrusage (). */
61 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
62 #  include <sys/resource.h>
63 #endif  /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H  */
64
65 /* The most accurate way to get the CPU time is getrusage ().
66    If we have times(), that's good enough, too.  */
67 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
68 /* For times(), we _must_ know the number of clock ticks per second.  */
69 #  if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
70 #    ifdef HAVE_SYS_PARAM_H
71 #      include <sys/param.h>
72 #    endif
73 #    if defined (HAVE_SYS_TIMES_H)
74 #      include <sys/times.h>
75 #    endif
76 #    ifndef HZ
77 #      if defined _SC_CLK_TCK
78 #        define HZ  sysconf(_SC_CLK_TCK)
79 #      else
80 #        define HZ  CLK_TCK
81 #      endif
82 #    endif
83 #  endif  /* HAVE_TIMES etc.  */
84 #endif  /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H  */
85
86
87 #if defined (__GNUC__) && (__GNUC__ >= 3)
88 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
89 #else
90 #  define ATTRIBUTE_ALWAYS_INLINE
91 #endif
92
93 static inline int gf_cputime (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
94
95 /* Helper function for the actual implementation of the DTIME, ETIME and
96    CPU_TIME intrinsics.  Returns 0 for success or -1 if no
97    CPU time could be computed.  */
98
99 #ifdef __MINGW32__
100
101 #define WIN32_LEAN_AND_MEAN
102 #include <windows.h>
103
104 static int
105 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
106 {
107   union {
108     FILETIME ft;
109     unsigned long long ulltime;
110   } kernel_time,  user_time;
111
112   FILETIME unused1, unused2;
113
114   /* No support for Win9x.  The high order bit of the DWORD
115      returned by GetVersion is 0 for NT and higher. */
116   if (GetVersion () >= 0x80000000)
117     {
118       *user_sec = *system_sec = 0;
119       *user_usec = *system_usec = 0;
120       return -1;
121     }
122
123   /* The FILETIME structs filled in by GetProcessTimes represent
124      time in 100 nanosecond units. */
125   GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
126                    &kernel_time.ft, &user_time.ft);
127
128   *user_sec = user_time.ulltime / 10000000;
129   *user_usec = (user_time.ulltime % 10000000) / 10;
130
131   *system_sec = kernel_time.ulltime / 10000000;
132   *system_usec = (kernel_time.ulltime % 10000000) / 10;
133   return 0;
134 }
135
136 #else
137
138 static inline int
139 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
140 {
141 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
142   struct rusage usage;
143   int err;
144   err = getrusage (RUSAGE_SELF, &usage);
145
146   *user_sec = usage.ru_utime.tv_sec;
147   *user_usec = usage.ru_utime.tv_usec;
148   *system_sec = usage.ru_stime.tv_sec;
149   *system_usec = usage.ru_stime.tv_usec;
150   return err;
151
152 #elif defined HAVE_TIMES
153   struct tms buf;
154   clock_t err;
155   err = times (&buf);
156   *user_sec = buf.tms_utime / HZ;
157   *user_usec = buf.tms_utime % HZ * (1000000 / HZ);
158   *system_sec = buf.tms_stime / HZ;
159   *system_usec = buf.tms_stime % HZ * (1000000 / HZ);
160   if ((err == (clock_t) -1) && errno != 0)
161     return -1;
162   return 0;
163
164 #else 
165
166   /* We have nothing to go on.  Return -1.  */
167   *user_sec = *system_sec = 0;
168   *user_usec = *system_usec = 0;
169   errno = ENOSYS;
170   return -1;
171
172 #endif
173 }
174
175 #endif
176
177
178 /* POSIX states that CLOCK_REALTIME must be present if clock_gettime
179    is available, others are optional.  */
180 #ifdef CLOCK_REALTIME
181 #define GF_CLOCK_REALTIME CLOCK_REALTIME
182 #else
183 #define GF_CLOCK_REALTIME 0
184 #endif
185
186 #ifdef CLOCK_MONOTONIC
187 #define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
188 #else
189 #define GF_CLOCK_MONOTONIC GF_CLOCK_REALTIME
190 #endif
191
192 /* Weakref trickery for clock_gettime().  On Glibc, clock_gettime()
193    requires us to link in librt, which also pulls in libpthread.  In
194    order to avoid this by default, only call clock_gettime() through a
195    weak reference.  */
196 #ifdef HAVE_CLOCK_GETTIME
197 #ifdef SUPPORTS_WEAK
198 static int weak_gettime (clockid_t, struct timespec *) 
199   __attribute__((__weakref__("clock_gettime")));
200 #else
201 static inline int weak_gettime (clockid_t clk_id, struct timespec *res)
202 {
203   return clock_gettime (clk_id, res);
204 }
205 #endif
206 #endif
207
208 /* Arguments:
209    clock_id - INPUT, must be either GF_CLOCK_REALTIME or GF_CLOCK_MONOTONIC
210    secs     - OUTPUT, seconds
211    nanosecs - OUTPUT, OPTIONAL, nanoseconds
212
213    If clock_id equals GF_CLOCK_REALTIME, the OUTPUT arguments shall be
214    the number of seconds and nanoseconds since the Epoch. If clock_id
215    equals GF_CLOCK_MONOTONIC, and if the target supports it, the
216    OUTPUT arguments represent a monotonically incrementing clock
217    starting from some unspecified time in the past.
218
219    Return value: 0 for success, -1 for error. In case of error, errno
220    is set.
221 */
222 static inline int
223 gf_gettime (int clock_id __attribute__((unused)), time_t * secs, 
224             long * nanosecs)
225 {
226 #ifdef HAVE_CLOCK_GETTIME
227   if (weak_gettime)
228     {
229       struct timespec ts;
230       int err;
231       err = weak_gettime (clock_id, &ts);
232       *secs = ts.tv_sec;
233       if (nanosecs)
234         *nanosecs = ts.tv_nsec;
235       return err;
236     }
237 #endif
238 #ifdef HAVE_GETTIMEOFDAY
239   struct timeval tv;
240   int err;
241   err = gettimeofday (&tv, NULL);
242   *secs = tv.tv_sec;
243   if (nanosecs)
244     *nanosecs = tv.tv_usec * 1000;
245   return err;
246 #elif HAVE_TIME
247   time_t t, t2;
248   t = time (&t2);
249   *secs = t2;
250   if (nanosecs)
251     *nanosecs = 0;
252   if (t == ((time_t)-1))
253     return -1;
254   return 0;
255 #else
256   *secs = 0;
257   if (nanosecs)
258     *nanosecs = 0;
259   errno = ENOSYS;
260   return -1;
261 #endif
262 }
263
264
265 #endif /* LIBGFORTRAN_TIME_H */