1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran 95 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 #include "libgfortran.h"
28 /* The most accurate way to get the CPU time is getrusage ().
29 If we have times(), that's good enough, too. */
30 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
31 /* For times(), we _must_ know the number of clock ticks per second. */
32 # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
33 # ifdef HAVE_SYS_PARAM_H
34 # include <sys/param.h>
36 # if defined (HAVE_SYS_TIMES_H)
37 # include <sys/times.h>
40 # if defined _SC_CLK_TCK
41 # define HZ sysconf(_SC_CLK_TCK)
46 # endif /* HAVE_TIMES etc. */
47 #endif /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H */
49 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE
;
52 __cpu_time_1 (long *sec
, long *usec
)
54 #if defined(__MINGW32__) || defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
55 long user_sec
, user_usec
, system_sec
, system_usec
;
56 __time_1 (&user_sec
, &user_usec
, &system_sec
, &system_usec
);
57 *sec
= user_sec
+ system_sec
;
58 *usec
= user_usec
+ system_usec
;
59 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
64 *usec
= (buf
.tms_utime
+ buf
.tms_stime
) * (1000000 / HZ
);
65 #else /* ! HAVE_TIMES */
66 /* We have nothing to go on. Return -1. */
69 #endif /* HAVE_TIMES */
70 #endif /* __MINGW32__ || HAVE_GETRUSAGE */
74 extern void cpu_time_4 (GFC_REAL_4
*);
75 iexport_proto(cpu_time_4
);
77 void cpu_time_4 (GFC_REAL_4
*time
)
80 __cpu_time_1 (&sec
, &usec
);
81 *time
= sec
+ usec
* GFC_REAL_4_LITERAL(1.e
-6);
85 extern void cpu_time_8 (GFC_REAL_8
*);
86 export_proto(cpu_time_8
);
88 void cpu_time_8 (GFC_REAL_8
*time
)
91 __cpu_time_1 (&sec
, &usec
);
92 *time
= sec
+ usec
* GFC_REAL_8_LITERAL(1.e
-6);
95 #ifdef HAVE_GFC_REAL_10
96 extern void cpu_time_10 (GFC_REAL_10
*);
97 export_proto(cpu_time_10
);
99 void cpu_time_10 (GFC_REAL_10
*time
)
102 __cpu_time_1 (&sec
, &usec
);
103 *time
= sec
+ usec
* GFC_REAL_10_LITERAL(1.e
-6);
107 #ifdef HAVE_GFC_REAL_16
108 extern void cpu_time_16 (GFC_REAL_16
*);
109 export_proto(cpu_time_16
);
111 void cpu_time_16 (GFC_REAL_16
*time
)
114 __cpu_time_1 (&sec
, &usec
);
115 *time
= sec
+ usec
* GFC_REAL_16_LITERAL(1.e
-6);
119 extern void second_sub (GFC_REAL_4
*);
120 export_proto(second_sub
);
123 second_sub (GFC_REAL_4
*s
)
128 extern GFC_REAL_4
second (void);
129 export_proto(second
);