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
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
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
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>
51 # include <sys/time.h>
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__))
67 # define ATTRIBUTE_ALWAYS_INLINE
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. */
78 #define WIN32_LEAN_AND_MEAN
82 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
86 unsigned long long ulltime;
87 } kernel_time, user_time;
89 FILETIME unused1, unused2;
90 unsigned long long total_time;
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)
96 *user_sec = *system_sec = 0;
97 *user_usec = *system_usec = 0;
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);
106 *user_sec = user_time.ulltime / 10000000;
107 *user_usec = (user_time.ulltime % 10000000) / 10;
109 *system_sec = kernel_time.ulltime / 10000000;
110 *system_usec = (kernel_time.ulltime % 10000000) / 10;
117 __time_1 (long *user_sec, long *user_usec, long *system_sec, long *system_usec)
119 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
121 getrusage (0, &usage);
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;
129 #else /* ! HAVE_GETRUSAGE || ! HAVE_SYS_RESOURCE_H */
131 /* We have nothing to go on. Return -1. */
132 *user_sec = *system_sec = 0;
133 *user_usec = *system_usec = 0;
142 #endif /* LIBGFORTRAN_TIME_H */