PR opt/3995
[official-gcc.git] / libf2c / libU77 / dtime_.c
blob19100e698de6f0554e3195c6788e310621b44ab1
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_STDLIB_H
23 # include <stdlib.h>
24 #endif
25 #if HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <sys/types.h>
29 #if HAVE_SYS_TIMES_H
30 # include <sys/times.h>
31 #endif
32 #if HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
35 #if HAVE_GETRUSAGE
36 # include <sys/time.h>
37 # include <sys/resource.h>
38 #endif
39 #if defined (_WIN32)
40 # include <windows.h>
41 # undef min
42 # undef max
43 #endif
44 #include <errno.h> /* for ENOSYS */
45 #include "f2c.h"
47 /* For dtime, etime we store the clock tick parameter (clk_tck) the
48 first time either of them is invoked rather than each time. This
49 approach probably speeds up each invocation by avoiding a system
50 call each time, but means that the overhead of the first call is
51 different to all others. */
52 static long clk_tck = 0;
54 #ifdef KR_headers
55 double G77_dtime_0 (tarray)
56 real tarray[2];
57 #else
58 double G77_dtime_0 (real tarray[2])
59 #endif
61 #if defined (_WIN32)
62 static int win32_platform = -1;
64 if (win32_platform == -1)
66 OSVERSIONINFO osv;
67 osv.dwOSVersionInfoSize = sizeof (osv);
68 GetVersionEx (&osv);
69 win32_platform = osv.dwPlatformId;
72 /* We need to use this hack on non-NT platforms, where the first call
73 returns 0.0 and subsequent ones return the correct value. */
74 if (win32_platform != VER_PLATFORM_WIN32_NT)
76 static unsigned long long clock_freq;
77 static unsigned long long old_count;
78 unsigned long long count;
79 double delta;
80 LARGE_INTEGER counter_val;
82 if (clock_freq == 0)
84 LARGE_INTEGER freq;
85 if (! QueryPerformanceFrequency (&freq))
87 errno = ENOSYS;
88 return 0.0;
90 else
92 clock_freq = ((unsigned long long) freq.HighPart << 32)
93 + ((unsigned) freq.LowPart);
97 if (! QueryPerformanceCounter (&counter_val))
98 return -1.0;
100 count = ((unsigned long long) counter_val.HighPart << 32)
101 + (unsigned) counter_val.LowPart;
102 delta = ((double) (count - old_count)) / clock_freq;
103 tarray[0] = (float) delta;
104 tarray[1] = 0.0;
105 old_count = count;
107 else
109 static unsigned long long old_utime, old_stime;
110 unsigned long long utime, stime;
111 FILETIME creation_time, exit_time, kernel_time, user_time;
113 GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
114 &kernel_time, &user_time);
115 utime = ((unsigned long long) user_time.dwHighDateTime << 32)
116 + (unsigned) user_time.dwLowDateTime;
117 stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
118 + (unsigned) kernel_time.dwLowDateTime;
120 tarray[0] = (utime - old_utime) / 1.0e7;
121 tarray[1] = (stime - old_stime) / 1.0e7;
122 old_utime = utime;
123 old_stime = stime;
125 return tarray[0] + tarray[1];
127 #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
128 /* The getrusage version is only the default for convenience. */
129 #ifdef HAVE_GETRUSAGE
130 float utime, stime;
131 static float old_utime = 0.0, old_stime = 0.0;
132 struct rusage rbuff;
134 if (getrusage (RUSAGE_SELF, &rbuff) != 0)
135 abort ();
136 utime = (float) (rbuff.ru_utime).tv_sec +
137 (float) (rbuff.ru_utime).tv_usec/1000000.0;
138 tarray[0] = utime - (float) old_utime;
139 stime = (float) (rbuff.ru_stime).tv_sec +
140 (float) (rbuff.ru_stime).tv_usec/1000000.0;
141 tarray[1] = stime - old_stime;
142 #else /* HAVE_GETRUSAGE */
143 time_t utime, stime;
144 static time_t old_utime = 0, old_stime = 0;
145 struct tms buffer;
147 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
148 fixme: does using _POSIX_VERSION help? */
149 # if defined _SC_CLK_TCK && defined _POSIX_VERSION
150 if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
151 # elif defined CLOCKS_PER_SECOND
152 if (! clk_tck) clk_tck = CLOCKS_PER_SECOND;
153 # elif defined CLK_TCK
154 if (! clk_tck) clk_tck = CLK_TCK;
155 # elif defined HZ
156 if (! clk_tck) clk_tck = HZ;
157 # elif defined HAVE_GETRUSAGE
158 # else
159 #error Dont know clock tick length
160 # endif
161 if (times(&buffer) == (clock_t)-1) return -1.0;
162 utime = buffer.tms_utime; stime = buffer.tms_stime;
163 tarray[0] = ((float)(utime - old_utime)) / (float)clk_tck;
164 tarray[1] = ((float)(stime - old_stime)) / (float)clk_tck;
165 #endif /* HAVE_GETRUSAGE */
166 old_utime = utime; old_stime = stime;
167 return (tarray[0]+tarray[1]);
168 #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
169 errno = ENOSYS;
170 return 0.0;
171 #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */