Use clock_gettime in libgfortran timing intrinsics, cleanup
[official-gcc.git] / libgfortran / intrinsics / time_1.h
blob5ee5d5acb4a55bf11dacdf0ced96cbf1be436ee0
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 /* The time related intrinsics (DTIME, ETIME, CPU_TIME) to "compare
33 different algorithms on the same computer or discover which parts
34 are the most expensive", need a way to get the CPU time with the
35 finest resolution possible. We can only be accurate up to
36 microseconds.
38 As usual with UNIX systems, unfortunately no single way is
39 available for all systems. */
41 #ifdef TIME_WITH_SYS_TIME
42 # include <sys/time.h>
43 # include <time.h>
44 #else
45 # if HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 # else
48 # ifdef HAVE_TIME_H
49 # include <time.h>
50 # endif
51 # endif
52 #endif
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
56 #endif
58 /* The most accurate way to get the CPU time is getrusage (). */
59 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
60 # include <sys/resource.h>
61 #endif /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H */
63 /* The most accurate way to get the CPU time is getrusage ().
64 If we have times(), that's good enough, too. */
65 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
66 /* For times(), we _must_ know the number of clock ticks per second. */
67 # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
68 # ifdef HAVE_SYS_PARAM_H
69 # include <sys/param.h>
70 # endif
71 # if defined (HAVE_SYS_TIMES_H)
72 # include <sys/times.h>
73 # endif
74 # ifndef HZ
75 # if defined _SC_CLK_TCK
76 # define HZ sysconf(_SC_CLK_TCK)
77 # else
78 # define HZ CLK_TCK
79 # endif
80 # endif
81 # endif /* HAVE_TIMES etc. */
82 #endif /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H */
85 #if defined (__GNUC__) && (__GNUC__ >= 3)
86 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
87 #else
88 # define ATTRIBUTE_ALWAYS_INLINE
89 #endif
91 static inline int gf_cputime (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
93 /* Helper function for the actual implementation of the DTIME, ETIME and
94 CPU_TIME intrinsics. Returns 0 for success or -1 if no
95 CPU time could be computed. */
97 #ifdef __MINGW32__
99 #define WIN32_LEAN_AND_MEAN
100 #include <windows.h>
102 static int
103 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
105 union {
106 FILETIME ft;
107 unsigned long long ulltime;
108 } kernel_time, user_time;
110 FILETIME unused1, unused2;
112 /* No support for Win9x. The high order bit of the DWORD
113 returned by GetVersion is 0 for NT and higher. */
114 if (GetVersion () >= 0x80000000)
116 *user_sec = *system_sec = 0;
117 *user_usec = *system_usec = 0;
118 return -1;
121 /* The FILETIME structs filled in by GetProcessTimes represent
122 time in 100 nanosecond units. */
123 GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
124 &kernel_time.ft, &user_time.ft);
126 *user_sec = user_time.ulltime / 10000000;
127 *user_usec = (user_time.ulltime % 10000000) / 10;
129 *system_sec = kernel_time.ulltime / 10000000;
130 *system_usec = (kernel_time.ulltime % 10000000) / 10;
131 return 0;
134 #else
136 static inline int
137 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
139 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
140 struct rusage usage;
141 int err;
142 err = getrusage (RUSAGE_SELF, &usage);
144 *user_sec = usage.ru_utime.tv_sec;
145 *user_usec = usage.ru_utime.tv_usec;
146 *system_sec = usage.ru_stime.tv_sec;
147 *system_usec = usage.ru_stime.tv_usec;
148 return err;
150 #elif defined HAVE_TIMES
151 struct tms buf;
152 clock_t err;
153 err = times (&buf);
154 *user_sec = buf.tms_utime / HZ;
155 *user_usec = buf.tms_utime % HZ * (1000000 / HZ);
156 *system_sec = buf.tms_stime / HZ;
157 *system_usec = buf.tms_stime % HZ * (1000000 / HZ);
158 if ((err == (clock_t) -1) && errno != 0)
159 return -1;
160 return 0;
162 #else
164 /* We have nothing to go on. Return -1. */
165 *user_sec = *system_sec = 0;
166 *user_usec = *system_usec = 0;
167 errno = ENOSYS;
168 return -1;
170 #endif
173 #endif
176 /* POSIX states that CLOCK_REALTIME must be present if clock_gettime
177 is available, others are optional. */
178 #ifdef CLOCK_REALTIME
179 #define GF_CLOCK_REALTIME CLOCK_REALTIME
180 #else
181 #define GF_CLOCK_REALTIME 0
182 #endif
184 #ifdef CLOCK_MONOTONIC
185 #define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
186 #else
187 #define GF_CLOCK_REALTIME GF_CLOCK_REALTIME
188 #endif
190 /* Arguments:
191 clock_id - INPUT, must be either GF_CLOCK_REALTIME or GF_CLOCK_MONOTONIC
192 secs - OUTPUT, seconds
193 nanosecs - OUTPUT, OPTIONAL, nanoseconds
195 If clock_id equals GF_CLOCK_REALTIME, the OUTPUT arguments shall be
196 the number of seconds and nanoseconds since the Epoch. If clock_id
197 equals GF_CLOCK_MONOTONIC, and if the target supports it, the
198 OUTPUT arguments represent a monotonically incrementing clock
199 starting from some unspecified time in the past.
201 Return value: 0 for success, -1 for error. In case of error, errno
202 is set.
204 static inline int
205 gf_gettime (int clock_id __attribute__((unused)), time_t * secs,
206 long * nanosecs)
208 #ifdef HAVE_CLOCK_GETTIME
209 struct timespec ts;
210 int err;
211 err = clock_gettime (clock_id, &ts);
212 *secs = ts.tv_sec;
213 if (nanosecs)
214 *nanosecs = ts.tv_nsec;
215 return err;
216 #elif HAVE_GETTIMEOFDAY
217 struct timeval tv;
218 int err;
219 err = gettimeofday (&tv, NULL);
220 *secs = tv.tv_sec;
221 if (nanosecs)
222 *nanosecs = tv.tv_usec * 1000;
223 return err;
224 #elif HAVE_TIME
225 time_t t, t2;
226 t = time (&t2);
227 *secs = t2;
228 if (nanosecs)
229 *nanosecs = 0;
230 if (t == ((time_t)-1))
231 return -1;
232 return 0;
233 #else
234 *secs = 0;
235 if (nanosecs)
236 *nanosecs = 0;
237 errno = ENOSYS;
238 return -1;
239 #endif
243 #endif /* LIBGFORTRAN_TIME_H */