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
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
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>
46 # include <sys/time.h>
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
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>
71 # if defined (HAVE_SYS_TIMES_H)
72 # include <sys/times.h>
75 # if defined _SC_CLK_TCK
76 # define HZ sysconf(_SC_CLK_TCK)
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__))
88 # define ATTRIBUTE_ALWAYS_INLINE
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. */
99 #define WIN32_LEAN_AND_MEAN
103 gf_cputime (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
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;
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;
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)
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;
150 #elif defined HAVE_TIMES
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)
164 /* We have nothing to go on. Return -1. */
165 *user_sec = *system_sec = 0;
166 *user_usec = *system_usec = 0;
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
181 #define GF_CLOCK_REALTIME 0
184 #ifdef CLOCK_MONOTONIC
185 #define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
187 #define GF_CLOCK_REALTIME GF_CLOCK_REALTIME
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
205 gf_gettime (int clock_id __attribute__((unused)), time_t * secs,
208 #ifdef HAVE_CLOCK_GETTIME
211 err = clock_gettime (clock_id, &ts);
214 *nanosecs = ts.tv_nsec;
216 #elif HAVE_GETTIMEOFDAY
219 err = gettimeofday (&tv, NULL);
222 *nanosecs = tv.tv_usec * 1000;
230 if (t == ((time_t)-1))
243 #endif /* LIBGFORTRAN_TIME_H */