1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
6 Libgfor is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 Libgfor 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with libgfor; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
23 #include "libgfortran.h"
29 /* The CPU_TIME intrinsic to "compare different algorithms on the same
30 computer or discover which parts are the most expensive", so we
31 need a way to get the CPU time with the finest resolution possible.
32 We can only be accurate up to microseconds.
34 As usual with UNIX systems, unfortunately no single way is
35 available for all systems. */
37 #ifdef TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
50 /* The most accurate way to get the CPU time is getrusage ().
51 If we have times(), that's good enough, too. */
52 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
53 # include <sys/resource.h>
55 /* For times(), we _must_ know the number of clock ticks per second. */
56 # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
57 # ifdef HAVE_SYS_PARAM_H
58 # include <sys/param.h>
60 # include <sys/times.h>
62 # if defined _SC_CLK_TCK
63 # define HZ sysconf(_SC_CLK_TCK)
68 # endif /* HAVE_TIMES etc. */
69 #endif /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H */
71 #if defined (__GNUC__) && (__GNUC__ >= 3)
72 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
74 # define ATTRIBUTE_ALWAYS_INLINE
77 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE
;
79 /* Helper function for the actual implementation of the CPU_TIME
80 intrnsic. Returns a CPU time in microseconds or -1 if no CPU time
83 __cpu_time_1 (long *sec
, long *usec
)
85 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
87 getrusage (0, &usage
);
88 *sec
= usage
.ru_utime
.tv_sec
+ usage
.ru_stime
.tv_sec
;
89 *usec
= usage
.ru_utime
.tv_usec
+ usage
.ru_stime
.tv_usec
;
90 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
95 *usec
= (buf
.tms_utime
+ buf
.tms_stime
) * (1000000 / HZ
);
96 #else /* ! HAVE_TIMES */
97 /* We have nothing to go on. Return -1. */
100 #endif /* HAVE_TIMES */
101 #endif /* HAVE_GETRUSAGE */
105 #define CPU_TIME(KIND) \
106 void prefix(cpu_time_##KIND) (GFC_REAL_##KIND *__time) \
109 __cpu_time_1 (&sec, &usec); \
110 *__time = (GFC_REAL_##KIND) sec + \
111 ((GFC_REAL_##KIND) usec) * 1.e-6; \
118 prefix(second_sub
) (GFC_REAL_4
*s
)
120 prefix(cpu_time_4
)(s
);
124 prefix(second
) (void)
127 prefix(cpu_time_4
)(&s
);