OSDN Git Service

Configure cleanup.
[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 HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46
47 #include <time.h>
48
49 #ifdef HAVE_SYS_TYPES_H
50      #include <sys/types.h>
51 #endif
52
53 /* The most accurate way to get the CPU time is getrusage (). */
54 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
55 #  include <sys/resource.h>
56 #endif  /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H  */
57
58 /* The most accurate way to get the CPU time is getrusage ().
59    If we have times(), that's good enough, too.  */
60 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
61 /* For times(), we _must_ know the number of clock ticks per second.  */
62 #  if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
63 #    ifdef HAVE_SYS_PARAM_H
64 #      include <sys/param.h>
65 #    endif
66 #    if defined (HAVE_SYS_TIMES_H)
67 #      include <sys/times.h>
68 #    endif
69 #    ifndef HZ
70 #      if defined _SC_CLK_TCK
71 #        define HZ  sysconf(_SC_CLK_TCK)
72 #      else
73 #        define HZ  CLK_TCK
74 #      endif
75 #    endif
76 #  endif  /* HAVE_TIMES etc.  */
77 #endif  /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H  */
78
79
80 /* If the re-entrant version of localtime is not available, provide a
81    fallback implementation.  On some targets where the _r version is
82    not available, localtime uses thread-local storage so it's
83    threadsafe.  */
84
85 #ifndef HAVE_LOCALTIME_R
86 /* If _POSIX is defined localtime_r gets defined by mingw-w64 headers.  */
87 #ifdef localtime_r
88 #undef localtime_r
89 #endif
90
91 static inline struct tm *
92 localtime_r (const time_t * timep, struct tm * result)
93 {
94   *result = *localtime (timep);
95   return result;
96 }
97 #endif
98
99
100 #if defined (__GNUC__) && (__GNUC__ >= 3)
101 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
102 #else
103 #  define ATTRIBUTE_ALWAYS_INLINE
104 #endif
105
106 static inline int gf_cputime (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
107
108 /* Helper function for the actual implementation of the DTIME, ETIME and
109    CPU_TIME intrinsics.  Returns 0 for success or -1 if no
110    CPU time could be computed.  */
111
112 #ifdef __MINGW32__
113
114 #define WIN32_LEAN_AND_MEAN
115 #include <windows.h>
116
117 static int
118 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
119 {
120   union {
121     FILETIME ft;
122     unsigned long long ulltime;
123   } kernel_time,  user_time;
124
125   FILETIME unused1, unused2;
126
127   /* No support for Win9x.  The high order bit of the DWORD
128      returned by GetVersion is 0 for NT and higher. */
129   if (GetVersion () >= 0x80000000)
130     {
131       *user_sec = *system_sec = 0;
132       *user_usec = *system_usec = 0;
133       return -1;
134     }
135
136   /* The FILETIME structs filled in by GetProcessTimes represent
137      time in 100 nanosecond units. */
138   GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
139                    &kernel_time.ft, &user_time.ft);
140
141   *user_sec = user_time.ulltime / 10000000;
142   *user_usec = (user_time.ulltime % 10000000) / 10;
143
144   *system_sec = kernel_time.ulltime / 10000000;
145   *system_usec = (kernel_time.ulltime % 10000000) / 10;
146   return 0;
147 }
148
149 #else
150
151 static inline int
152 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
153 {
154 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
155   struct rusage usage;
156   int err;
157   err = getrusage (RUSAGE_SELF, &usage);
158
159   *user_sec = usage.ru_utime.tv_sec;
160   *user_usec = usage.ru_utime.tv_usec;
161   *system_sec = usage.ru_stime.tv_sec;
162   *system_usec = usage.ru_stime.tv_usec;
163   return err;
164
165 #elif defined HAVE_TIMES
166   struct tms buf;
167   clock_t err;
168   err = times (&buf);
169   *user_sec = buf.tms_utime / HZ;
170   *user_usec = buf.tms_utime % HZ * (1000000. / HZ);
171   *system_sec = buf.tms_stime / HZ;
172   *system_usec = buf.tms_stime % HZ * (1000000. / HZ);
173   if ((err == (clock_t) -1) && errno != 0)
174     return -1;
175   return 0;
176
177 #else 
178   clock_t c = clock ();
179   *user_sec = c / CLOCKS_PER_SEC;
180   *user_usec = c % CLOCKS_PER_SEC * (1000000. / CLOCKS_PER_SEC);
181   *system_sec = *system_usec = 0;
182   if (c == (clock_t) -1)
183     return -1;
184   return 0;
185
186 #endif
187 }
188
189 #endif
190
191
192 /* Realtime clock with microsecond resolution, falling back to less
193    precise functions if the target does not support gettimeofday().
194
195    Arguments:
196    secs     - OUTPUT, seconds
197    usecs    - OUTPUT, microseconds
198
199    The OUTPUT arguments shall represent the number of seconds and
200    nanoseconds since the Epoch.
201
202    Return value: 0 for success, -1 for error. In case of error, errno
203    is set.
204 */
205 static inline int
206 gf_gettime (time_t * secs, long * usecs)
207 {
208 #ifdef HAVE_GETTIMEOFDAY
209   struct timeval tv;
210   int err;
211   err = gettimeofday (&tv, NULL);
212   *secs = tv.tv_sec;
213   *usecs = tv.tv_usec;
214   return err;
215 #else
216   time_t t, t2;
217   t = time (&t2);
218   *secs = t2;
219   *usecs = 0;
220   if (t == ((time_t)-1))
221     return -1;
222   return 0;
223 #endif
224 }
225
226
227 #endif /* LIBGFORTRAN_TIME_H */