1*58194Selan /*
2*58194Selan Copyright (C) 1990, 1992 Free Software Foundation
3*58194Selan     written by Doug Lea (dl@rocky.oswego.edu)
4*58194Selan 
5*58194Selan This file is part of the GNU C++ Library.  This library is free
6*58194Selan software; you can redistribute it and/or modify it under the terms of
7*58194Selan the GNU Library General Public License as published by the Free
8*58194Selan Software Foundation; either version 2 of the License, or (at your
9*58194Selan option) any later version.  This library is distributed in the hope
10*58194Selan that it will be useful, but WITHOUT ANY WARRANTY; without even the
11*58194Selan implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12*58194Selan PURPOSE.  See the GNU Library General Public License for more details.
13*58194Selan You should have received a copy of the GNU Library General Public
14*58194Selan License along with this library; if not, write to the Free Software
15*58194Selan Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16*58194Selan */
17*58194Selan 
18*58194Selan #ifdef __GNUG__
19*58194Selan #pragma implementation
20*58194Selan #endif
21*58194Selan #include <builtin.h>
22*58194Selan 
23*58194Selan // Timing functions from Doug Schmidt...
24*58194Selan 
25*58194Selan /* no such thing as "negative time"! */
26*58194Selan #define  TIMER_ERROR_VALUE -1.0
27*58194Selan 
28*58194Selan // If this does not work on your system, change this to #if 0, and
29*58194Selan // report the problem
30*58194Selan 
31*58194Selan #if 1
32*58194Selan 
33*58194Selan #include <_G_config.h>
34*58194Selan #include <osfcn.h>
35*58194Selan #if !_G_HAVE_SYS_RESOURCE || !defined(RUSAGE_SELF)
36*58194Selan #define USE_TIMES
37*58194Selan #include <sys/param.h>
38*58194Selan #include <sys/times.h>
39*58194Selan #if !defined (HZ) && defined(CLK_TCK)
40*58194Selan #define HZ CLK_TCK
41*58194Selan #endif
42*58194Selan static struct tms Old_Time;
43*58194Selan static struct tms New_Time;
44*58194Selan #else
45*58194Selan static struct rusage Old_Time;
46*58194Selan static struct rusage New_Time;
47*58194Selan #endif
48*58194Selan static int    Timer_Set = 0;
49*58194Selan 
start_timer()50*58194Selan double start_timer()
51*58194Selan {
52*58194Selan    Timer_Set = 1;
53*58194Selan #ifdef USE_TIMES
54*58194Selan    times(&Old_Time);
55*58194Selan    return((double) Old_Time.tms_utime / HZ);
56*58194Selan #else
57*58194Selan    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
58*58194Selan    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
59*58194Selan #endif
60*58194Selan }
61*58194Selan 
62*58194Selan /* Returns process time since Last_Time.
63*58194Selan    If parameter is 0.0, returns time since the Old_Time was set.
64*58194Selan    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
65*58194Selan 
return_elapsed_time(double Last_Time)66*58194Selan double return_elapsed_time(double Last_Time)
67*58194Selan {
68*58194Selan    if (!Timer_Set) {
69*58194Selan       return(TIMER_ERROR_VALUE);
70*58194Selan    }
71*58194Selan    else {
72*58194Selan     /* get process time */
73*58194Selan #ifdef USE_TIMES
74*58194Selan       times(&New_Time);
75*58194Selan #else
76*58194Selan       getrusage(RUSAGE_SELF,&New_Time);
77*58194Selan #endif
78*58194Selan       if (Last_Time == 0.0) {
79*58194Selan #ifdef USE_TIMES
80*58194Selan 	 return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
81*58194Selan #else
82*58194Selan          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) +
83*58194Selan                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec)
84*58194Selan                 / 1000000.0));
85*58194Selan #endif
86*58194Selan       }
87*58194Selan       else {
88*58194Selan #ifdef USE_TIMES
89*58194Selan 	 return((double) New_Time.tms_utime / HZ - Last_Time);
90*58194Selan #else
91*58194Selan          return((New_Time.ru_utime.tv_sec +
92*58194Selan                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
93*58194Selan #endif
94*58194Selan       }
95*58194Selan    }
96*58194Selan }
97*58194Selan 
98*58194Selan #ifdef VMS
99*58194Selan void sys$gettim(unsigned int*) asm("sys$gettim");
100*58194Selan 
getrusage(int dummy,struct rusage * time)101*58194Selan getrusage(int dummy,struct rusage* time){
102*58194Selan 	double rtime;
103*58194Selan 	unsigned int systime[2];
104*58194Selan 	int i;
105*58194Selan 	sys$gettim(&systime[0]);
106*58194Selan 	rtime=systime[1];
107*58194Selan 	for(i=0;i<4;i++) rtime *= 256;
108*58194Selan 	rtime+= systime[0];
109*58194Selan /* we subtract an offset to make sure that the number fits in a long int*/
110*58194Selan 	rtime=rtime/1.0e+7-4.144e+9;
111*58194Selan 	time->ru_utime.tv_sec= rtime;
112*58194Selan 	rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;
113*58194Selan 	time->ru_utime.tv_usec= rtime;
114*58194Selan }
115*58194Selan #endif
116*58194Selan #else /* dummy them out */
117*58194Selan 
start_timer()118*58194Selan double start_timer()
119*58194Selan {
120*58194Selan   return TIMER_ERROR_VALUE;
121*58194Selan }
122*58194Selan 
return_elapsed_time(double)123*58194Selan double return_elapsed_time(double)
124*58194Selan {
125*58194Selan   return TIMER_ERROR_VALUE;
126*58194Selan }
127*58194Selan 
128*58194Selan #endif /* timing stuff */
129*58194Selan 
130*58194Selan 
131