FSF GCC merge 02/23/03
[official-gcc.git] / libf2c / libU77 / etime_.c
blobd0edb2fbcd3eb41a00c5823e688bb0a5f73fbb04
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 double
48 G77_etime_0 (real tarray[2])
50 #if defined (_WIN32)
51 static int win32_platform = -1;
52 double usertime, systime;
54 if (win32_platform == -1)
56 OSVERSIONINFO osv;
57 osv.dwOSVersionInfoSize = sizeof (osv);
58 GetVersionEx (&osv);
59 win32_platform = osv.dwPlatformId;
62 /* non-NT platforms don't have a clue as to how long a process has
63 been running, so simply return the uptime. Bad judgement call? */
64 if (win32_platform != VER_PLATFORM_WIN32_NT)
66 static unsigned long long clock_freq;
67 static unsigned long long old_count;
68 unsigned long long count;
69 LARGE_INTEGER counter_val;
71 if (clock_freq == 0)
73 LARGE_INTEGER freq;
74 if (!QueryPerformanceFrequency (&freq))
76 errno = ENOSYS;
77 return 0.0;
79 else
81 clock_freq = ((unsigned long long) freq.HighPart << 32)
82 + ((unsigned) freq.LowPart);
83 if (!QueryPerformanceCounter (&counter_val))
84 return -1.0;
85 old_count = ((unsigned long long) counter_val.HighPart << 32)
86 + (unsigned) counter_val.LowPart;
90 if (!QueryPerformanceCounter (&counter_val))
91 return -1.0;
93 count = ((unsigned long long) counter_val.HighPart << 32)
94 + (unsigned) counter_val.LowPart;
95 tarray[0] = usertime = (double) (count - old_count) / clock_freq;
96 tarray[1] = systime = 0.0;
98 else
100 FILETIME creation_time, exit_time, kernel_time, user_time;
101 unsigned long long utime, stime;
103 GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
104 &kernel_time, &user_time);
105 utime = ((unsigned long long) user_time.dwHighDateTime << 32)
106 + (unsigned) user_time.dwLowDateTime;
107 stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
108 + (unsigned) kernel_time.dwLowDateTime;
110 tarray[0] = usertime = utime / 1.0e7;
111 tarray[1] = systime = stime / 1.0e7;
113 return usertime + systime;
115 #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
116 /* The getrusage version is only the default for convenience. */
117 #ifdef HAVE_GETRUSAGE
118 struct rusage rbuff;
120 if (getrusage (RUSAGE_SELF, &rbuff) != 0)
121 abort ();
122 tarray[0] = ((float) (rbuff.ru_utime).tv_sec +
123 (float) (rbuff.ru_utime).tv_usec / 1000000.0);
124 tarray[1] = ((float) (rbuff.ru_stime).tv_sec +
125 (float) (rbuff.ru_stime).tv_usec / 1000000.0);
126 #else /* HAVE_GETRUSAGE */
127 /* For dtime, etime we store the clock tick parameter (clk_tck) the
128 first time either of them is invoked rather than each time. This
129 approach probably speeds up each invocation by avoiding a system
130 call each time, but means that the overhead of the first call is
131 different to all others. */
132 static long clk_tck = 0;
133 struct tms buffer;
135 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
136 fixme: does using _POSIX_VERSION help? */
137 # if defined _SC_CLK_TCK && defined _POSIX_VERSION
138 if (!clk_tck)
139 clk_tck = sysconf (_SC_CLK_TCK);
140 # elif defined CLOCKS_PER_SECOND
141 if (!clk_tck)
142 clk_tck = CLOCKS_PER_SECOND;
143 # elif defined CLK_TCK
144 if (!clk_tck)
145 clk_tck = CLK_TCK;
146 # elif defined HZ
147 if (!clk_tck)
148 clk_tck = HZ;
149 # elif defined HAVE_GETRUSAGE
150 # else
151 #error Dont know clock tick length
152 # endif
153 if (times (&buffer) == (clock_t) - 1)
154 return -1.0;
155 tarray[0] = (float) buffer.tms_utime / (float) clk_tck;
156 tarray[1] = (float) buffer.tms_stime / (float) clk_tck;
157 #endif /* HAVE_GETRUSAGE */
158 return (tarray[0] + tarray[1]);
159 #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
160 errno = ENOSYS;
161 return 0.0;
162 #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */