Merge from vendor branch BZIP:
[dragonfly.git] / contrib / gcc-3.4 / libf2c / libU77 / dtime_.c
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of GNU Fortran libU77 library.
3
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.
8
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.
13
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.  */
18
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"
46
47 double
48 G77_dtime_0 (real tarray[2])
49 {
50 #if defined (_WIN32)
51   static int win32_platform = -1;
52
53   if (win32_platform == -1)
54     {
55       OSVERSIONINFO osv;
56       osv.dwOSVersionInfoSize = sizeof (osv);
57       GetVersionEx (&osv);
58       win32_platform = osv.dwPlatformId;
59     }
60
61   /* We need to use this hack on non-NT platforms, where the first call
62      returns 0.0 and subsequent ones return the correct value. */
63   if (win32_platform != VER_PLATFORM_WIN32_NT)
64     {
65       static unsigned long long clock_freq;
66       static unsigned long long old_count;
67       unsigned long long count;
68       double delta;
69       LARGE_INTEGER counter_val;
70
71       if (clock_freq == 0)
72         {
73           LARGE_INTEGER freq;
74           if (!QueryPerformanceFrequency (&freq))
75             {
76               errno = ENOSYS;
77               return 0.0;
78             }
79           else
80             {
81               clock_freq = ((unsigned long long) freq.HighPart << 32)
82                 + ((unsigned) freq.LowPart);
83             }
84         }
85
86       if (!QueryPerformanceCounter (&counter_val))
87         return -1.0;
88
89       count = ((unsigned long long) counter_val.HighPart << 32)
90         + (unsigned) counter_val.LowPart;
91       delta = ((double) (count - old_count)) / clock_freq;
92       tarray[0] = (float) delta;
93       tarray[1] = 0.0;
94       old_count = count;
95     }
96   else
97     {
98       static unsigned long long old_utime, old_stime;
99       unsigned long long utime, stime;
100       FILETIME creation_time, exit_time, kernel_time, user_time;
101
102       GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
103                        &kernel_time, &user_time);
104       utime = ((unsigned long long) user_time.dwHighDateTime << 32)
105         + (unsigned) user_time.dwLowDateTime;
106       stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
107         + (unsigned) kernel_time.dwLowDateTime;
108
109       tarray[0] = (utime - old_utime) / 1.0e7;
110       tarray[1] = (stime - old_stime) / 1.0e7;
111       old_utime = utime;
112       old_stime = stime;
113     }
114   return tarray[0] + tarray[1];
115
116 #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
117   /* The getrusage version is only the default for convenience. */
118 #ifdef HAVE_GETRUSAGE
119   float utime, stime;
120   static float old_utime = 0.0, old_stime = 0.0;
121   struct rusage rbuff;
122
123   if (getrusage (RUSAGE_SELF, &rbuff) != 0)
124     abort ();
125   utime = (float) (rbuff.ru_utime).tv_sec +
126     (float) (rbuff.ru_utime).tv_usec / 1000000.0;
127   tarray[0] = utime - (float) old_utime;
128   stime = (float) (rbuff.ru_stime).tv_sec +
129     (float) (rbuff.ru_stime).tv_usec / 1000000.0;
130   tarray[1] = stime - old_stime;
131 #else /* HAVE_GETRUSAGE */
132   /* For dtime, etime we store the clock tick parameter (clk_tck) the
133      first time either of them is invoked rather than each time.  This
134      approach probably speeds up each invocation by avoiding a system
135      call each time, but means that the overhead of the first call is
136      different to all others. */
137   static long clk_tck = 0;
138   time_t utime, stime;
139   static time_t old_utime = 0, old_stime = 0;
140   struct tms buffer;
141
142 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
143    fixme: does using _POSIX_VERSION help? */
144 #  if defined _SC_CLK_TCK && defined _POSIX_VERSION
145   if (!clk_tck)
146     clk_tck = sysconf (_SC_CLK_TCK);
147 #  elif defined CLOCKS_PER_SECOND
148   if (!clk_tck)
149     clk_tck = CLOCKS_PER_SECOND;
150 #  elif defined CLK_TCK
151   if (!clk_tck)
152     clk_tck = CLK_TCK;
153 #  elif defined HZ
154   if (!clk_tck)
155     clk_tck = HZ;
156 #  elif defined HAVE_GETRUSAGE
157 #  else
158 #error Dont know clock tick length
159 #  endif
160   if (times (&buffer) == (clock_t) - 1)
161     return -1.0;
162   utime = buffer.tms_utime;
163   stime = buffer.tms_stime;
164   tarray[0] = ((float) (utime - old_utime)) / (float) clk_tck;
165   tarray[1] = ((float) (stime - old_stime)) / (float) clk_tck;
166 #endif /* HAVE_GETRUSAGE */
167   old_utime = utime;
168   old_stime = stime;
169   return (tarray[0] + tarray[1]);
170 #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
171   errno = ENOSYS;
172   return 0.0;
173 #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
174 }