lang-specs.h: Ensure -mrtp switch is passed when using either rtp-smp or ravenscar...
[official-gcc.git] / libgfortran / intrinsics / time_1.h
blob12d79ebc12fecf52baa0895c7ab8accc41dab500
1 /* Wrappers for platform timing functions.
2 Copyright (C) 2003, 2007, 2009, 2011 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
25 #ifndef LIBGFORTRAN_TIME_H
26 #define LIBGFORTRAN_TIME_H
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <errno.h>
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.
40 As usual with UNIX systems, unfortunately no single way is
41 available for all systems. */
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
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
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 */
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 */
87 /* If the re-entrant version of localtime is not available, provide a
88 fallback implementation. On some targets where the _r version is
89 not available, localtime uses thread-local storage so it's
90 threadsafe. */
92 #ifndef HAVE_LOCALTIME_R
93 /* If _POSIX is defined localtime_r gets defined by mingw-w64 headers. */
94 #ifdef localtime_r
95 #undef localtime_r
96 #endif
98 static inline struct tm *
99 localtime_r (const time_t * timep, struct tm * result)
101 *result = *localtime (timep);
102 return result;
104 #endif
107 #if defined (__GNUC__) && (__GNUC__ >= 3)
108 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
109 #else
110 # define ATTRIBUTE_ALWAYS_INLINE
111 #endif
113 static inline int gf_cputime (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
115 /* Helper function for the actual implementation of the DTIME, ETIME and
116 CPU_TIME intrinsics. Returns 0 for success or -1 if no
117 CPU time could be computed. */
119 #ifdef __MINGW32__
121 #define WIN32_LEAN_AND_MEAN
122 #include <windows.h>
124 static int
125 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
127 union {
128 FILETIME ft;
129 unsigned long long ulltime;
130 } kernel_time, user_time;
132 FILETIME unused1, unused2;
134 /* No support for Win9x. The high order bit of the DWORD
135 returned by GetVersion is 0 for NT and higher. */
136 if (GetVersion () >= 0x80000000)
138 *user_sec = *system_sec = 0;
139 *user_usec = *system_usec = 0;
140 return -1;
143 /* The FILETIME structs filled in by GetProcessTimes represent
144 time in 100 nanosecond units. */
145 GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
146 &kernel_time.ft, &user_time.ft);
148 *user_sec = user_time.ulltime / 10000000;
149 *user_usec = (user_time.ulltime % 10000000) / 10;
151 *system_sec = kernel_time.ulltime / 10000000;
152 *system_usec = (kernel_time.ulltime % 10000000) / 10;
153 return 0;
156 #else
158 static inline int
159 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
161 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
162 struct rusage usage;
163 int err;
164 err = getrusage (RUSAGE_SELF, &usage);
166 *user_sec = usage.ru_utime.tv_sec;
167 *user_usec = usage.ru_utime.tv_usec;
168 *system_sec = usage.ru_stime.tv_sec;
169 *system_usec = usage.ru_stime.tv_usec;
170 return err;
172 #elif defined HAVE_TIMES
173 struct tms buf;
174 clock_t err;
175 err = times (&buf);
176 *user_sec = buf.tms_utime / HZ;
177 *user_usec = buf.tms_utime % HZ * (1000000 / HZ);
178 *system_sec = buf.tms_stime / HZ;
179 *system_usec = buf.tms_stime % HZ * (1000000 / HZ);
180 if ((err == (clock_t) -1) && errno != 0)
181 return -1;
182 return 0;
184 #else
186 /* We have nothing to go on. Return -1. */
187 *user_sec = *system_sec = 0;
188 *user_usec = *system_usec = 0;
189 errno = ENOSYS;
190 return -1;
192 #endif
195 #endif
198 /* Realtime clock with microsecond resolution, falling back to less
199 precise functions if the target does not support gettimeofday().
201 Arguments:
202 secs - OUTPUT, seconds
203 usecs - OUTPUT, microseconds
205 The OUTPUT arguments shall represent the number of seconds and
206 nanoseconds since the Epoch.
208 Return value: 0 for success, -1 for error. In case of error, errno
209 is set.
211 static inline int
212 gf_gettime (time_t * secs, long * usecs)
214 #ifdef HAVE_GETTIMEOFDAY
215 struct timeval tv;
216 int err;
217 err = gettimeofday (&tv, NULL);
218 *secs = tv.tv_sec;
219 *usecs = tv.tv_usec;
220 return err;
221 #elif HAVE_TIME
222 time_t t, t2;
223 t = time (&t2);
224 *secs = t2;
225 *usecs = 0;
226 if (t == ((time_t)-1))
227 return -1;
228 return 0;
229 #else
230 *secs = 0;
231 *usecs = 0;
232 errno = ENOSYS;
233 return -1;
234 #endif
238 #endif /* LIBGFORTRAN_TIME_H */