OSDN Git Service

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