xref: /386bsd/usr/src/lib/libg++/libg++/timer.cc (revision a2142627)
1 /*
2 Copyright (C) 1990 Free Software Foundation
3     written by Doug Lea (dl@rocky.oswego.edu)
4 
5 This file is part of GNU CC.
6 
7 GNU CC is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY.  No author or distributor
9 accepts responsibility to anyone for the consequences of using it
10 or for whether it serves any particular purpose or works at all,
11 unless he says so in writing.  Refer to the GNU CC General Public
12 License for full details.
13 
14 Everyone is granted permission to copy, modify and redistribute
15 GNU CC, but only under the conditions described in the
16 GNU CC General Public License.   A copy of this license is
17 supposed to have been given to you along with GNU CC so you
18 can know your rights and responsibilities.  It should be in a
19 file named COPYING.  Among other things, the copyright notice
20 and this notice must be preserved on all copies.
21 */
22 
23 #ifdef __GNUG__
24 #pragma implementation
25 #endif
26 #include <builtin.h>
27 #include <sys/resource.h>
28 
29 // Timing functions from Doug Schmidt...
30 
31 /* no such thing as "negative time"! */
32 #define  TIMER_ERROR_VALUE -1.0
33 
34 // If this does not work on your system, change this to #if 0, and
35 // report the problem
36 
37 #if 1
38 
39 #if defined(USG)
40 extern "C" {
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/times.h>
44 }
45 #else
46 #include <osfcn.h>
47 #endif
48 
49 #if defined(USG)
50 static struct tms Old_Time;
51 static struct tms New_Time;
52 #else
53 static struct rusage Old_Time;
54 static struct rusage New_Time;
55 #endif
56 static int    Timer_Set = 0;
57 
start_timer()58 double start_timer()
59 {
60    Timer_Set = 1;
61 #if defined(USG)
62    times(&Old_Time);
63    return((double) Old_Time.tms_utime / HZ);
64 #else
65    getrusage(RUSAGE_SELF,&Old_Time);        /* set starting process time */
66    return(Old_Time.ru_utime.tv_sec + (Old_Time.ru_utime.tv_usec / 1000000.0));
67 #endif
68 }
69 
70 /* Returns process time since Last_Time.
71    If parameter is 0.0, returns time since the Old_Time was set.
72    Returns TIMER_ERROR_VALUE if `start_timer' is not called first.  */
73 
return_elapsed_time(double Last_Time)74 double return_elapsed_time(double Last_Time)
75 {
76    if (!Timer_Set) {
77       return(TIMER_ERROR_VALUE);
78    }
79    else {
80     /* get process time */
81 #if defined(USG)
82       times(&New_Time);
83 #else
84       getrusage(RUSAGE_SELF,&New_Time);
85 #endif
86       if (Last_Time == 0.0) {
87 #if defined(USG)
88 	 return((double) (New_Time.tms_utime - Old_Time.tms_utime) / HZ);
89 #else
90          return((New_Time.ru_utime.tv_sec - Old_Time.ru_utime.tv_sec) +
91                ((New_Time.ru_utime.tv_usec - Old_Time.ru_utime.tv_usec)
92                 / 1000000.0));
93 #endif
94       }
95       else {
96 #if defined(USG)
97 	 return((double) New_Time.tms_utime / HZ - Last_Time);
98 #else
99          return((New_Time.ru_utime.tv_sec +
100                 (New_Time.ru_utime.tv_usec / 1000000.0)) - Last_Time);
101 #endif
102       }
103    }
104 }
105 
106 #ifdef VMS
107 void sys$gettim(unsigned int*) asm("sys$gettim");
108 
getrusage(int dummy,struct rusage * time)109 getrusage(int dummy,struct rusage* time){
110 	double rtime;
111 	unsigned int systime[2];
112 	int i;
113 	sys$gettim(&systime[0]);
114 	rtime=systime[1];
115 	for(i=0;i<4;i++) rtime *= 256;
116 	rtime+= systime[0];
117 /* we subtract an offset to make sure that the number fits in a long int*/
118 	rtime=rtime/1.0e+7-4.144e+9;
119 	time->ru_utime.tv_sec= rtime;
120 	rtime=(rtime-time->ru_utime.tv_sec)*1.0e6;
121 	time->ru_utime.tv_usec= rtime;
122 }
123 #endif
124 #else /* dummy them out */
125 
start_timer()126 double start_timer()
127 {
128   return TIMER_ERROR_VALUE;
129 }
130 
return_elapsed_time(double)131 double return_elapsed_time(double)
132 {
133   return TIMER_ERROR_VALUE;
134 }
135 
136 #endif /* timing stuff */
137 
138 
139