OSDN Git Service

a93b7e0202a39bdfb77d1dc2b0db73ef6f2e2a12
[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 /* The most accurate way to get the CPU time is getrusage (). */
55 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
56 #  include <sys/resource.h>
57 #endif  /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H  */
58
59 #if defined (__GNUC__) && (__GNUC__ >= 3)
60 #  define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
61 #else
62 #  define ATTRIBUTE_ALWAYS_INLINE
63 #endif
64
65 static inline int __time_1 (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
66
67 /* Helper function for the actual implementation of the DTIME, ETIME and
68    CPU_TIME intrinsics.  Returns a CPU time in microseconds or -1 if no
69    CPU time could be computed.  */
70
71 #ifdef __MINGW32__
72
73 #define WIN32_LEAN_AND_MEAN
74 #include <windows.h>
75
76 static int
77 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
78 {
79   union {
80     FILETIME ft;
81     unsigned long long ulltime;
82   } kernel_time,  user_time;
83
84   FILETIME unused1, unused2;
85
86   /* No support for Win9x.  The high order bit of the DWORD
87      returned by GetVersion is 0 for NT and higher. */
88   if (GetVersion () >= 0x80000000)
89     {
90       *user_sec = *system_sec = 0;
91       *user_usec = *system_usec = 0;
92       return -1;
93     }
94
95   /* The FILETIME structs filled in by GetProcessTimes represent
96      time in 100 nanosecond units. */
97   GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
98                    &kernel_time.ft, &user_time.ft);
99
100   *user_sec = user_time.ulltime / 10000000;
101   *user_usec = (user_time.ulltime % 10000000) / 10;
102
103   *system_sec = kernel_time.ulltime / 10000000;
104   *system_usec = (kernel_time.ulltime % 10000000) / 10;
105   return 0;
106 }
107
108 #else
109
110 static inline int
111 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
112 {
113 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
114   struct rusage usage;
115   getrusage (0, &usage);
116
117   *user_sec = usage.ru_utime.tv_sec;
118   *user_usec = usage.ru_utime.tv_usec;
119   *system_sec = usage.ru_stime.tv_sec;
120   *system_usec = usage.ru_stime.tv_usec;
121   return 0;
122
123 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H  */
124
125   /* We have nothing to go on.  Return -1.  */
126   *user_sec = *system_sec = 0;
127   *user_usec = *system_usec = 0;
128   return -1;
129
130 #endif
131 }
132
133 #endif
134
135
136 #endif /* LIBGFORTRAN_TIME_H */