Initial import from FreeBSD RELENG_4:
[games.git] / contrib / libf2c / libU77 / etime_.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_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"
43
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;
50
51 #ifdef KR_headers
52 double G77_etime_0 (tarray)
53      real tarray[2];
54 #else
55 double G77_etime_0 (real tarray[2])
56 #endif
57 {
58 #if defined (_WIN32)
59   static int win32_platform = -1;
60   double usertime, systime;
61
62   if (win32_platform == -1)
63     {
64       OSVERSIONINFO osv;
65       osv.dwOSVersionInfoSize = sizeof (osv);
66       GetVersionEx (&osv);
67       win32_platform = osv.dwPlatformId;
68     }
69   
70   /* non-NT platforms don't have a clue as to how long a process has
71      been running, so simply return the uptime. Bad judgement call? */
72   if (win32_platform != VER_PLATFORM_WIN32_NT)
73     {
74       static unsigned long long clock_freq;
75       static unsigned long long old_count;
76       unsigned long long count;
77       LARGE_INTEGER counter_val;
78
79       if (clock_freq == 0)
80         {
81           LARGE_INTEGER freq;
82           if (! QueryPerformanceFrequency (&freq))
83             {
84               errno = ENOSYS;
85               return 0.0;
86             }
87           else
88             {
89               clock_freq = ((unsigned long long) freq.HighPart << 32)
90                            + ((unsigned) freq.LowPart);
91               if (! QueryPerformanceCounter (&counter_val))
92                 return -1.0;
93               old_count = ((unsigned long long) counter_val.HighPart << 32)
94                           + (unsigned) counter_val.LowPart;
95             }
96         }
97
98       if (! QueryPerformanceCounter (&counter_val))
99         return -1.0;
100
101       count = ((unsigned long long) counter_val.HighPart << 32)
102               + (unsigned) counter_val.LowPart;
103       tarray[0] = usertime = (double) (count - old_count) / clock_freq;
104       tarray[1] = systime = 0.0;
105     }
106   else
107     {
108       FILETIME creation_time, exit_time, kernel_time, user_time;
109       unsigned long long utime, stime;
110
111       GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
112                        &kernel_time, &user_time);
113       utime = ((unsigned long long) user_time.dwHighDateTime << 32)
114               + (unsigned) user_time.dwLowDateTime;
115       stime = ((unsigned long long) kernel_time.dwHighDateTime << 32)
116               + (unsigned) kernel_time.dwLowDateTime;
117
118       tarray[0] = usertime = utime / 1.0e7;
119       tarray[1] = systime = stime / 1.0e7;
120   }
121   return usertime + systime;
122
123 #elif defined (HAVE_GETRUSAGE) || defined (HAVE_TIMES)
124   /* The getrusage version is only the default for convenience. */
125 #ifdef HAVE_GETRUSAGE
126   struct rusage rbuff;
127
128    if (getrusage (RUSAGE_SELF, &rbuff) != 0)
129      abort ();
130    tarray[0] = ((float) (rbuff.ru_utime).tv_sec +
131                (float) (rbuff.ru_utime).tv_usec/1000000.0);
132    tarray[1] = ((float) (rbuff.ru_stime).tv_sec +
133                (float) (rbuff.ru_stime).tv_usec/1000000.0);
134 #else  /* HAVE_GETRUSAGE */
135   struct tms buffer;
136
137 /* NeXTStep seems to define _SC_CLK_TCK but not to have sysconf;
138    fixme: does using _POSIX_VERSION help? */
139 #  if defined _SC_CLK_TCK && defined _POSIX_VERSION
140   if (! clk_tck) clk_tck = sysconf(_SC_CLK_TCK);
141 #  elif defined CLOCKS_PER_SECOND
142   if (! clk_tck) clk_tck = CLOCKS_PER_SECOND;
143 #  elif defined CLK_TCK
144   if (! clk_tck) clk_tck = CLK_TCK;
145 #  elif defined HZ
146   if (! clk_tck) clk_tck = HZ;
147 #  elif defined HAVE_GETRUSAGE
148 #  else
149   #error Dont know clock tick length
150 #  endif
151   if (times(&buffer) == (clock_t)-1) return -1.0;
152   tarray[0] = (float) buffer.tms_utime / (float)clk_tck;
153   tarray[1] = (float) buffer.tms_stime / (float)clk_tck;
154 #endif /* HAVE_GETRUSAGE */
155   return (tarray[0]+tarray[1]);
156 #else /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
157   errno = ENOSYS;
158   return 0.0;
159 #endif /* ! HAVE_GETRUSAGE && ! HAVE_TIMES */
160 }