1 /* Implementation of the CPU_TIME intrinsic.
2 Copyright (C) 2003, 2007, 2009 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 3 of the License, or (at your option) any later version.
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.
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.
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/>. */
25 #ifndef LIBGFORTRAN_TIME_H
26 #define LIBGFORTRAN_TIME_H
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
38 As usual with UNIX systems, unfortunately no single way is
39 available for all systems. */
41 #ifdef TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
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 */
59 #if defined (__GNUC__) && (__GNUC__ >= 3)
60 # define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((__always_inline__))
62 # define ATTRIBUTE_ALWAYS_INLINE
65 static inline int __time_1 (long *, long *, long *, long *) ATTRIBUTE_ALWAYS_INLINE
;
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. */
73 #define WIN32_LEAN_AND_MEAN
77 __time_1 (long *user_sec
, long *user_usec
, long *system_sec
, long *system_usec
)
81 unsigned long long ulltime
;
82 } kernel_time
, user_time
;
84 FILETIME unused1
, unused2
;
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)
90 *user_sec
= *system_sec
= 0;
91 *user_usec
= *system_usec
= 0;
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
);
100 *user_sec
= user_time
.ulltime
/ 10000000;
101 *user_usec
= (user_time
.ulltime
% 10000000) / 10;
103 *system_sec
= kernel_time
.ulltime
/ 10000000;
104 *system_usec
= (kernel_time
.ulltime
% 10000000) / 10;
111 __time_1 (long *user_sec
, long *user_usec
, long *system_sec
, long *system_usec
)
113 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
115 getrusage (0, &usage
);
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
;
123 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
125 /* We have nothing to go on. Return -1. */
126 *user_sec
= *system_sec
= 0;
127 *user_usec
= *system_usec
= 0;
136 #endif /* LIBGFORTRAN_TIME_H */