1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003, 2007 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 2 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with libgfortran; see the file COPYING. If not,
27 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
30 #include "libgfortran.h"
33 /* The most accurate way to get the CPU time is getrusage ().
34 If we have times(), that's good enough, too. */
35 #if !defined (HAVE_GETRUSAGE) || !defined (HAVE_SYS_RESOURCE_H)
36 /* For times(), we _must_ know the number of clock ticks per second. */
37 # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
38 # ifdef HAVE_SYS_PARAM_H
39 # include <sys/param.h>
41 # if defined (HAVE_SYS_TIMES_H)
42 # include <sys/times.h>
45 # if defined _SC_CLK_TCK
46 # define HZ sysconf(_SC_CLK_TCK)
51 # endif /* HAVE_TIMES etc. */
52 #endif /* !HAVE_GETRUSAGE || !HAVE_SYS_RESOURCE_H */
54 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
57 __cpu_time_1 (long *sec, long *usec)
59 #if defined(__MINGW32__) || defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
60 long user_sec, user_usec, system_sec, system_usec;
61 __time_1 (&user_sec, &user_usec, &system_sec, &system_usec);
62 *sec = user_sec + system_sec;
63 *usec = user_usec + system_usec;
64 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
69 *usec = (buf.tms_utime + buf.tms_stime) * (1000000 / HZ);
70 #else /* ! HAVE_TIMES */
71 /* We have nothing to go on. Return -1. */
74 #endif /* HAVE_TIMES */
75 #endif /* __MINGW32__ || HAVE_GETRUSAGE */
79 extern void cpu_time_4 (GFC_REAL_4 *);
80 iexport_proto(cpu_time_4);
82 void cpu_time_4 (GFC_REAL_4 *time)
85 __cpu_time_1 (&sec, &usec);
86 *time = sec + usec * (GFC_REAL_4)1.e-6;
90 extern void cpu_time_8 (GFC_REAL_8 *);
91 export_proto(cpu_time_8);
93 void cpu_time_8 (GFC_REAL_8 *time)
96 __cpu_time_1 (&sec, &usec);
97 *time = sec + usec * (GFC_REAL_8)1.e-6;
100 #ifdef HAVE_GFC_REAL_10
101 extern void cpu_time_10 (GFC_REAL_10 *);
102 export_proto(cpu_time_10);
104 void cpu_time_10 (GFC_REAL_10 *time)
107 __cpu_time_1 (&sec, &usec);
108 *time = sec + usec * (GFC_REAL_10)1.e-6;
112 #ifdef HAVE_GFC_REAL_16
113 extern void cpu_time_16 (GFC_REAL_16 *);
114 export_proto(cpu_time_16);
116 void cpu_time_16 (GFC_REAL_16 *time)
119 __cpu_time_1 (&sec, &usec);
120 *time = sec + usec * (GFC_REAL_16)1.e-6;
124 extern void second_sub (GFC_REAL_4 *);
125 export_proto(second_sub);
128 second_sub (GFC_REAL_4 *s)
133 extern GFC_REAL_4 second (void);
134 export_proto(second);