2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / intrinsics / time_1.h
blob3dcb9d5f5aed58efe616a96bc01dacb3034f381a
1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003, 2007 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., 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
30 #ifndef LIBGFORTRAN_TIME_H
31 #define LIBGFORTRAN_TIME_H
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
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.
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 defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
61 # include <sys/resource.h>
62 #endif /* HAVE_GETRUSAGE && HAVE_SYS_RESOURCE_H */
64 #if defined (__GNUC__) && (__GNUC__ >= 3)
65 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
66 #else
67 # define ATTRIBUTE_ALWAYS_INLINE
68 #endif
70 static inline int __time_1 (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE;
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. */
76 #ifdef __MINGW32__
78 #define WIN32_LEAN_AND_MEAN
79 #include <windows.h>
81 static int
82 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
84 union {
85 FILETIME ft;
86 unsigned long long ulltime;
87 } kernel_time, user_time;
89 FILETIME unused1, unused2;
91 /* No support for Win9x. The high order bit of the DWORD
92 returned by GetVersion is 0 for NT and higher. */
93 if (GetVersion () >= 0x80000000)
95 *user_sec = *system_sec = 0;
96 *user_usec = *system_usec = 0;
97 return -1;
100 /* The FILETIME structs filled in by GetProcessTimes represent
101 time in 100 nanosecond units. */
102 GetProcessTimes (GetCurrentProcess (), &unused1, &unused2,
103 &kernel_time.ft, &user_time.ft);
105 *user_sec = user_time.ulltime / 10000000;
106 *user_usec = (user_time.ulltime % 10000000) / 10;
108 *system_sec = kernel_time.ulltime / 10000000;
109 *system_usec = (kernel_time.ulltime % 10000000) / 10;
110 return 0;
113 #else
115 static inline int
116 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
118 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
119 struct rusage usage;
120 getrusage (0, &usage);
122 *user_sec = usage.ru_utime.tv_sec;
123 *user_usec = usage.ru_utime.tv_usec;
124 *system_sec = usage.ru_stime.tv_sec;
125 *system_usec = usage.ru_stime.tv_usec;
126 return 0;
128 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
130 /* We have nothing to go on. Return -1. */
131 *user_sec = *system_sec = 0;
132 *user_usec = *system_usec = 0;
133 return -1;
135 #endif
138 #endif
141 #endif /* LIBGFORTRAN_TIME_H */