OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / time_1.h
1 /* Implementation of the CPU_TIME intrinsic.
2    Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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.
10
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.
15
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.
19
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/>.  */
24
25 #ifndef LIBGFORTRAN_TIME_H
26 #define LIBGFORTRAN_TIME_H
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 /* The time related intrinsics (DTIME, ETIME, CPU_TIME) to "compare
33    different algorithms on the same computer or discover which parts
34    are the most expensive", need a way to get the CPU time with the
35    finest resolution possible. We can only be accurate up to
36    microseconds.
37
38    As usual with UNIX systems, unfortunately no single way is
39    available for all systems.  */
40
41 #ifdef TIME_WITH_SYS_TIME
42 #  include <sys/time.h>
43 #  include <time.h>
44 #else
45 #  if HAVE_SYS_TIME_H
46 #    include <sys/time.h>
47 #  else
48 #    ifdef HAVE_TIME_H
49 #      include <time.h>
50 #    endif
51 #  endif
52 #endif
53
54 #ifdef HAVE_SYS_TYPES_H
55      #include <sys/types.h>
56 #endif
57
58 /* The most accurate way to get the CPU time is getrusage (). */
59 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
60 #  include <sys/resource.h>
61 #endif  /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H  */
62
63 #if defined (__GNUC__) && (__GNUC__ >= 3)
64 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
65 #else
66 #  define ATTRIBUTE_ALWAYS_INLINE
67 #endif
68
69 static inline int __time_1 (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
70
71 /* Helper function for the actual implementation of the DTIME, ETIME and
72    CPU_TIME intrinsics.  Returns a CPU time in microseconds or -1 if no
73    CPU time could be computed.  */
74
75 #ifdef __MINGW32__
76
77 #define WIN32_LEAN_AND_MEAN
78 #include <windows.h>
79
80 static int
81 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
82 {
83   union {
84     FILETIME ft;
85     unsigned long long ulltime;
86   } kernel_time,  user_time;
87
88   FILETIME unused1, unused2;
89
90   /* No support for Win9x.  The high order bit of the DWORD
91      returned by GetVersion is 0 for NT and higher. */
92   if (GetVersion () >= 0x80000000)
93     {
94       *user_sec = *system_sec = 0;
95       *user_usec = *system_usec = 0;
96       return -1;
97     }
98
99   /* The FILETIME structs filled in by GetProcessTimes represent
100      time in 100 nanosecond units. */
101   GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
102                    &kernel_time.ft, &user_time.ft);
103
104   *user_sec = user_time.ulltime / 10000000;
105   *user_usec = (user_time.ulltime % 10000000) / 10;
106
107   *system_sec = kernel_time.ulltime / 10000000;
108   *system_usec = (kernel_time.ulltime % 10000000) / 10;
109   return 0;
110 }
111
112 #else
113
114 static inline int
115 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
116 {
117 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
118   struct rusage usage;
119   getrusage (RUSAGE_SELF, &usage);
120
121   *user_sec = usage.ru_utime.tv_sec;
122   *user_usec = usage.ru_utime.tv_usec;
123   *system_sec = usage.ru_stime.tv_sec;
124   *system_usec = usage.ru_stime.tv_usec;
125   return 0;
126
127 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H  */
128
129   /* We have nothing to go on.  Return -1.  */
130   *user_sec = *system_sec = 0;
131   *user_usec = *system_usec = 0;
132   return -1;
133
134 #endif
135 }
136
137 #endif
138
139
140 #endif /* LIBGFORTRAN_TIME_H */