OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / cpu_time.c
1 /* Implementation of the CPU_TIME intrinsic.
2    Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
5
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.
10
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.
15
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.  */
20
21 #include "config.h"
22 #include <sys/types.h>
23 #include "libgfortran.h"
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
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.
33
34    As usual with UNIX systems, unfortunately no single way is
35    available for all systems.  */
36
37 #ifdef TIME_WITH_SYS_TIME
38 #  include <sys/time.h>
39 #  include <time.h>
40 #else
41 #  if HAVE_SYS_TIME_H
42 #    include <sys/time.h>
43 #  else
44 #    ifdef HAVE_TIME_H
45 #      include <time.h>
46 #    endif
47 #  endif
48 #endif
49
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>
54 #else
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>
59 #    endif
60 #    include <sys/times.h>
61 #    ifndef HZ
62 #      if defined _SC_CLK_TCK
63 #        define HZ  sysconf(_SC_CLK_TCK)
64 #      else
65 #        define HZ  CLK_TCK
66 #      endif
67 #    endif
68 #  endif  /* HAVE_TIMES etc.  */
69 #endif  /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H  */
70
71 #if defined (__GNUC__) && (__GNUC__ >= 3)
72 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
73 #else
74 #  define ATTRIBUTE_ALWAYS_INLINE
75 #endif
76
77 static inline void __cpu_time_1 (long *, long *) ATTRIBUTE_ALWAYS_INLINE;
78
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
81    could be computed.  */
82 static inline void
83 __cpu_time_1 (long *sec, long *usec)
84 {
85 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
86   struct rusage usage;
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  */
91 #ifdef HAVE_TIMES
92   struct tms buf;
93   times (&buf);
94   *sec = 0;
95   *usec = (buf.tms_utime + buf.tms_stime) * (1000000 / HZ);
96 #else /* ! HAVE_TIMES */
97   /* We have nothing to go on.  Return -1.  */
98   *sec = -1;
99   *usec = 0;
100 #endif  /* HAVE_TIMES */
101 #endif  /* HAVE_GETRUSAGE */
102 }
103
104 #undef CPU_TIME
105 #define CPU_TIME(KIND)                                          \
106 void prefix(cpu_time_##KIND) (GFC_REAL_##KIND *__time)          \
107 {                                                               \
108   long sec, usec;                                               \
109   __cpu_time_1 (&sec, &usec);                                   \
110   *__time = (GFC_REAL_##KIND) sec +                             \
111                 ((GFC_REAL_##KIND) usec) * 1.e-6;               \
112 }
113
114 CPU_TIME(4)
115 CPU_TIME(8)
116