* test_summary: In generated script, use cat <<'EOF' not cat <<\EOF.
[official-gcc.git] / libf2c / libU77 / dtime_.c
blobaa34bc027e0881a736fa2a742d43a9e2dee37006
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of GNU Fortran libU77 library.
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 GNU Fortran is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with GNU Fortran; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #if HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <sys/types.h>
26 #if HAVE_SYS_TIMES_H
27 # include <sys/times.h>
28 #endif
29 #if HAVE_SYS_PARAM_H
30 # include <sys/param.h>
31 #endif
32 #if HAVE_GETRUSAGE
33 # include <sys/time.h>
34 # include <sys/resource.h>
35 #endif
36 #if defined (_WIN32)
37 # include <windows.h>
38 # undef min
39 # undef max
40 #endif
41 #include <errno.h> /* for ENOSYS */
42 #include "f2c.h"
44 /* For dtime, etime we store the clock tick parameter (clk_tck) the
45 first time either of them is invoked rather than each time. This
46 approach probably speeds up each invocation by avoiding a system
47 call each time, but means that the overhead of the first call is
48 different to all others. */
49 static long clk_tck = 0;
51 #ifdef KR_headers
52 double G77_dtime_0 (tarray)
53 real tarray[2];
54 #else
55 double G77_dtime_0 (real tarray[2])
56 #endif
58 #if defined (_WIN32)
59 static int win32_platform = -1;
61 if (win32_platform == -1)
63 OSVERSIONINFO osv;
64 osv.dwOSVersionInfoSize = sizeof (osv);
65 GetVersionEx (&osv);
66 win32_platform = osv.dwPlatformId;
69 /* We need to use this hack on non-NT platforms, where the first call
70 returns 0.0 and subsequent ones return the correct value. */
71 if (win32_platform != VER_PLATFORM_WIN32_NT)
73 static unsigned long long clock_freq;
74 static unsigned long long old_count;
75 unsigned long long count;
76 double delta;
77 LARGE_INTEGER counter_val;
79 if (clock_freq == 0)
81 LARGE_INTEGER freq;
82 if (! QueryPerformanceFrequency (&freq))
84 errno = ENOSYS;
85 return 0.0;
87 else
89 clock_freq = ((unsigned long long) freq.HighPart << 32)
90 + ((unsigned) freq.LowPart);
94 if (! QueryPerformanceCounter (&counter_val))
95 return -1.0;
97 count = ((unsigned long long) counter_val.HighPart << 32)
98 + (unsigned) counter_val.LowPart;
99 delta = ((double) (count - old_count)) / clock_freq;
100 tarray[0] = (float) delta;
101 tarray[1] = 0.0;
102 old_count = count;
104 else
106 static unsigned long long old_utime, old_stime;
107 unsigned long long utime, stime;
108 FILETIME creation_time, exit_time, kernel_time, user_time;
110 GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
111 &kernel_time, &user_time);
112 utime = ((unsigned long long) user_time.dwHighDateTime << 32)
113 + (unsigned) user_time.dwLowDateTime;
114 stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
115 + (unsigned) kernel_time.dwLowDateTime;
117 tarray[0] = (utime - old_utime) / 1.0e7;
118 tarray[1] = (stime - old_stime) / 1.0e7;
119 old_utime = utime;
120 old_stime = stime;
122 return tarray[0] + tarray[1];
124 #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
125 /* The getrusage version is only the default for convenience. */
126 #ifdef HAVE_GETRUSAGE
127 float utime, stime;
128 static float old_utime = 0.0, old_stime = 0.0;
129 struct rusage rbuff;
131 if (getrusage (RUSAGE_SELF, &rbuff) != 0)
132 abort ();
133 utime = (float) (rbuff.ru_utime).tv_sec +
134 (float) (rbuff.ru_utime).tv_usec/1000000.0;
135 tarray[0] = utime - (float) old_utime;
136 stime = (float) (rbuff.ru_stime).tv_sec +
137 (float) (rbuff.ru_stime).tv_usec/1000000.0;
138 tarray[1] = stime - old_stime;
139 #else /* HAVE_GETRUSAGE */
140 time_t utime, stime;
141 static time_t old_utime = 0, old_stime = 0;
142 struct tms buffer;
144 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
145 fixme: does using _POSIX_VERSION help? */
146 # if defined _SC_CLK_TCK && defined _POSIX_VERSION
147 if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
148 # elif defined CLOCKS_PER_SECOND
149 if (! clk_tck) clk_tck = CLOCKS_PER_SECOND;
150 # elif defined CLK_TCK
151 if (! clk_tck) clk_tck = CLK_TCK;
152 # elif defined HZ
153 if (! clk_tck) clk_tck = HZ;
154 # elif defined HAVE_GETRUSAGE
155 # else
156 #error Dont know clock tick length
157 # endif
158 if (times(&buffer) == (clock_t)-1) return -1.0;
159 utime = buffer.tms_utime; stime = buffer.tms_stime;
160 tarray[0] = ((float)(utime - old_utime)) / (float)clk_tck;
161 tarray[1] = ((float)(stime - old_stime)) / (float)clk_tck;
162 #endif /* HAVE_GETRUSAGE */
163 old_utime = utime; old_stime = stime;
164 return (tarray[0]+tarray[1]);
165 #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
166 errno = ENOSYS;
167 return 0.0;
168 #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */