Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / intrinsics / cpu_time.c
blob7b21233954a52d70313ffea661b43af1b2a62ace
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 (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
18 executable.)
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., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 #include "config.h"
31 #include <sys/types.h>
32 #include "libgfortran.h"
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 /* The CPU_TIME intrinsic to "compare different algorithms on the same
39 computer or discover which parts are the most expensive", so we
40 need a way to get the CPU time with the finest resolution possible.
41 We can only be accurate up to microseconds.
43 As usual with UNIX systems, unfortunately no single way is
44 available for all systems. */
46 #ifdef TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # if HAVE_SYS_TIME_H
51 # include <sys/time.h>
52 # else
53 # ifdef HAVE_TIME_H
54 # include <time.h>
55 # endif
56 # endif
57 #endif
59 /* The most accurate way to get the CPU time is getrusage ().
60 If we have times(), that's good enough, too. */
61 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
62 # include <sys/resource.h>
63 #else
64 /* For times(), we _must_ know the number of clock ticks per second. */
65 # if defined (HAVE_TIMES) && (defined (HZ) || defined (_SC_CLK_TCK) || defined (CLK_TCK))
66 # ifdef HAVE_SYS_PARAM_H
67 # include <sys/param.h>
68 # endif
69 # include <sys/times.h>
70 # ifndef HZ
71 # if defined _SC_CLK_TCK
72 # define HZ sysconf(_SC_CLK_TCK)
73 # else
74 # define HZ CLK_TCK
75 # endif
76 # endif
77 # endif /* HAVE_TIMES etc. */
78 #endif /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H */
80 #if defined (__GNUC__) && (__GNUC__ >= 3)
81 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
82 #else
83 # define ATTRIBUTE_ALWAYS_INLINE
84 #endif
86 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
88 /* Helper function for the actual implementation of the CPU_TIME
89 intrnsic. Returns a CPU time in microseconds or -1 if no CPU time
90 could be computed. */
91 static inline void
92 __cpu_time_1 (long *sec, long *usec)
94 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
95 struct rusage usage;
96 getrusage (0, &usage);
97 *sec = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
98 *usec = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
99 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
100 #ifdef HAVE_TIMES
101 struct tms buf;
102 times (&buf);
103 *sec = 0;
104 *usec = (buf.tms_utime + buf.tms_stime) * (1000000 / HZ);
105 #else /* ! HAVE_TIMES */
106 /* We have nothing to go on. Return -1. */
107 *sec = -1;
108 *usec = 0;
109 #endif /* HAVE_TIMES */
110 #endif /* HAVE_GETRUSAGE */
113 extern void cpu_time_4 (GFC_REAL_4 *);
114 iexport_proto(cpu_time_4);
116 void cpu_time_4 (GFC_REAL_4 *time)
118 long sec, usec;
119 __cpu_time_1 (&sec, &usec);
120 *time = sec + usec * (GFC_REAL_4)1.e-6;
122 iexport(cpu_time_4);
124 extern void cpu_time_8 (GFC_REAL_8 *);
125 export_proto(cpu_time_8);
127 void cpu_time_8 (GFC_REAL_8 *time)
129 long sec, usec;
130 __cpu_time_1 (&sec, &usec);
131 *time = sec + usec * (GFC_REAL_8)1.e-6;
134 extern void second_sub (GFC_REAL_4 *);
135 export_proto(second_sub);
137 void
138 second_sub (GFC_REAL_4 *s)
140 cpu_time_4 (s);
143 extern GFC_REAL_4 second (void);
144 export_proto(second);
146 GFC_REAL_4
147 second (void)
149 GFC_REAL_4 s;
150 cpu_time_4 (&s);
151 return s;