1 /* Wrappers for platform timing functions.
2 Copyright (C) 2003-2015 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
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
40 As usual with UNIX systems, unfortunately no single way is
41 available for all systems. */
43 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_SYS_TYPES_H
50 #include <sys/types.h>
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 */
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>
66 # if defined (HAVE_SYS_TIMES_H)
67 # include <sys/times.h>
70 # if defined _SC_CLK_TCK
71 # define HZ sysconf(_SC_CLK_TCK)
76 # endif /* HAVE_TIMES etc. */
77 #endif /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H */
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
85 #ifndef HAVE_LOCALTIME_R
86 /* If _POSIX is defined localtime_r gets defined by mingw-w64 headers. */
91 static inline struct tm
*
92 localtime_r (const time_t * timep
, struct tm
* result
)
94 *result
= *localtime (timep
);
100 /* Helper function for the actual implementation of the DTIME, ETIME and
101 CPU_TIME intrinsics. Returns 0 for success or -1 if no
102 CPU time could be computed. */
104 #if defined(__MINGW32__) || defined(__CYGWIN__)
106 #define WIN32_LEAN_AND_MEAN
110 gf_cputime (long *user_sec
, long *user_usec
, long *system_sec
, long *system_usec
)
114 unsigned long long ulltime
;
115 } kernel_time
, user_time
;
117 FILETIME unused1
, unused2
;
119 /* No support for Win9x. The high order bit of the DWORD
120 returned by GetVersion is 0 for NT and higher. */
121 if (GetVersion () >= 0x80000000)
123 *user_sec
= *system_sec
= 0;
124 *user_usec
= *system_usec
= 0;
128 /* The FILETIME structs filled in by GetProcessTimes represent
129 time in 100 nanosecond units. */
130 GetProcessTimes (GetCurrentProcess (), &unused1
, &unused2
,
131 &kernel_time
.ft
, &user_time
.ft
);
133 *user_sec
= user_time
.ulltime
/ 10000000;
134 *user_usec
= (user_time
.ulltime
% 10000000) / 10;
136 *system_sec
= kernel_time
.ulltime
/ 10000000;
137 *system_usec
= (kernel_time
.ulltime
% 10000000) / 10;
144 gf_cputime (long *user_sec
, long *user_usec
, long *system_sec
, long *system_usec
)
146 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
149 err
= getrusage (RUSAGE_SELF
, &usage
);
151 *user_sec
= usage
.ru_utime
.tv_sec
;
152 *user_usec
= usage
.ru_utime
.tv_usec
;
153 *system_sec
= usage
.ru_stime
.tv_sec
;
154 *system_usec
= usage
.ru_stime
.tv_usec
;
157 #elif defined HAVE_TIMES
162 *user_sec
= buf
.tms_utime
/ hz
;
163 *user_usec
= (buf
.tms_utime
% hz
) * (1000000. / hz
);
164 *system_sec
= buf
.tms_stime
/ hz
;
165 *system_usec
= (buf
.tms_stime
% hz
) * (1000000. / hz
);
166 if ((err
== (clock_t) -1) && errno
!= 0)
170 #elif defined(HAVE_CLOCK_GETTIME) && (defined(CLOCK_PROCESS_CPUTIME_ID) \
171 || defined(CLOCK_THREAD_CPUTIME_ID))
172 /* Newer versions of VxWorks have CLOCK_THREAD_CPUTIME_ID giving
173 per-thread CPU time. CLOCK_PROCESS_CPUTIME_ID would be better
174 but is not available. */
175 #ifndef CLOCK_PROCESS_CPUTIME_ID
176 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_THREAD_CPUTIME_ID
179 int err
= clock_gettime (CLOCK_PROCESS_CPUTIME_ID
, &ts
);
180 *user_sec
= ts
.tv_sec
;
181 *user_usec
= ts
.tv_nsec
/ 1000;
182 *system_sec
= *system_usec
= 0;
186 clock_t c
= clock ();
187 *user_sec
= c
/ CLOCKS_PER_SEC
;
188 *user_usec
= (c
% CLOCKS_PER_SEC
) * (1000000. / CLOCKS_PER_SEC
);
189 *system_sec
= *system_usec
= 0;
190 if (c
== (clock_t) -1)
200 /* Realtime clock with microsecond resolution, falling back to other
201 functions if the target does not support gettimeofday().
204 secs - OUTPUT, seconds
205 usecs - OUTPUT, microseconds
207 The OUTPUT arguments shall represent the number of seconds and
208 microseconds since the Epoch.
210 Return value: 0 for success, -1 for error. In case of error, errno
214 gf_gettime (time_t * secs
, long * usecs
)
216 #ifdef HAVE_GETTIMEOFDAY
219 err
= gettimeofday (&tv
, NULL
);
223 #elif defined(HAVE_CLOCK_GETTIME)
225 int err
= clock_gettime (CLOCK_REALTIME
, &ts
);
227 *usecs
= ts
.tv_nsec
/ 1000;
230 time_t t
= time (NULL
);
233 if (t
== ((time_t)-1))
240 #endif /* LIBGFORTRAN_TIME_H */