1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: timer.cc,v 1.8 2008-08-22 12:11:05 Singular Exp $ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - get the computing time |
---|
8 | */ |
---|
9 | |
---|
10 | #include "mod2.h" |
---|
11 | // undef GETRUSAGE to get the original timer code (w.r.t. CLOCK_TICKS) |
---|
12 | #define GETRUSAGE |
---|
13 | #ifdef GETRUSAGE |
---|
14 | #include <sys/time.h> |
---|
15 | #include <sys/resource.h> |
---|
16 | #include <unistd.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | int timerv = 0; |
---|
20 | static double timer_resolution = TIMER_RESOLUTION; |
---|
21 | |
---|
22 | static double mintime = 0.5; |
---|
23 | |
---|
24 | void SetTimerResolution(int res) |
---|
25 | { |
---|
26 | timer_resolution = (double) res; |
---|
27 | } |
---|
28 | |
---|
29 | void SetMinDisplayTime(double mtime) |
---|
30 | { |
---|
31 | mintime = mtime; |
---|
32 | } |
---|
33 | |
---|
34 | #include <stdio.h> |
---|
35 | #include <math.h> |
---|
36 | #include <unistd.h> |
---|
37 | #include <float.h> |
---|
38 | #include <mylimits.h> |
---|
39 | #include <sys/types.h> |
---|
40 | |
---|
41 | #ifdef TIME_WITH_SYS_TIME |
---|
42 | # include <time.h> |
---|
43 | # ifdef HAVE_SYS_TIME_H |
---|
44 | # include <sys/time.h> |
---|
45 | # endif |
---|
46 | #else |
---|
47 | # ifdef HAVE_SYS_TIME_H |
---|
48 | # include <sys/time.h> |
---|
49 | # else |
---|
50 | # include <time.h> |
---|
51 | # endif |
---|
52 | #endif |
---|
53 | |
---|
54 | #ifdef HAVE_SYS_TIMES_H |
---|
55 | #include <sys/times.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | |
---|
59 | // need to be adjusted on your machine: the number of ticks per second: HZ |
---|
60 | #ifndef HZ |
---|
61 | #include <sys/param.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | #if !defined(HZ) && defined(CLOCKS_PER_SEC) |
---|
65 | #define HZ CLOCKS_PER_SEC |
---|
66 | #endif |
---|
67 | |
---|
68 | #if !defined(HZ) && defined(CLK_TCK) |
---|
69 | #define HZ CLK_TCK |
---|
70 | #endif |
---|
71 | |
---|
72 | #if !defined(HZ) && defined(HAVE_SYSCONF) |
---|
73 | #define HZ sysconf(_SC_CLK_TCK) |
---|
74 | #endif |
---|
75 | |
---|
76 | #ifndef HZ |
---|
77 | // last resort |
---|
78 | #ifdef sun |
---|
79 | #define HZ 60.0 |
---|
80 | #else |
---|
81 | #define HZ 100.0 |
---|
82 | #endif |
---|
83 | #endif |
---|
84 | |
---|
85 | #include "timer.h" |
---|
86 | #include "febase.h" |
---|
87 | /*3 |
---|
88 | * the start time of the timer |
---|
89 | */ |
---|
90 | static clock_t startl; |
---|
91 | static clock_t siStartTime; |
---|
92 | |
---|
93 | /*3 |
---|
94 | * temp structure to get the time |
---|
95 | */ |
---|
96 | #ifdef GETRUSAGE |
---|
97 | static struct rusage t_rec; |
---|
98 | #else |
---|
99 | static struct tms t_rec; |
---|
100 | #endif |
---|
101 | /*0 implementation*/ |
---|
102 | |
---|
103 | int initTimer() |
---|
104 | { |
---|
105 | #ifdef GETRUSAGE |
---|
106 | getrusage(RUSAGE_SELF,&t_rec); |
---|
107 | siStartTime = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
108 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
109 | +5000)/10000; // unit is 1/100 sec |
---|
110 | #else |
---|
111 | times(&t_rec); |
---|
112 | siStartTime = t_rec.tms_utime+t_rec.tms_stime; |
---|
113 | #endif |
---|
114 | return (int)time(NULL); |
---|
115 | } |
---|
116 | |
---|
117 | void startTimer() |
---|
118 | { |
---|
119 | #ifdef GETRUSAGE |
---|
120 | getrusage(RUSAGE_SELF,&t_rec); |
---|
121 | startl = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
122 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
123 | +5000)/10000; // unit is 1/100 sec |
---|
124 | #else |
---|
125 | times(&t_rec); |
---|
126 | startl = t_rec.tms_utime+t_rec.tms_stime; |
---|
127 | #endif |
---|
128 | } |
---|
129 | |
---|
130 | /*2 |
---|
131 | * returns the time since a fixed point in seconds |
---|
132 | */ |
---|
133 | int getTimer() |
---|
134 | { |
---|
135 | clock_t curr; |
---|
136 | #ifdef GETRUSAGE |
---|
137 | getrusage(RUSAGE_SELF,&t_rec); |
---|
138 | curr = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
139 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
140 | +5000)/10000; // unit is 1/100 sec |
---|
141 | curr -= siStartTime; |
---|
142 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
143 | #else |
---|
144 | |
---|
145 | times(&t_rec); |
---|
146 | curr = t_rec.tms_utime+t_rec.tms_stime - siStartTime; |
---|
147 | |
---|
148 | double f = ((double)curr) * timer_resolution / (double)HZ; |
---|
149 | #endif |
---|
150 | return (int)(f+0.5); |
---|
151 | } |
---|
152 | |
---|
153 | /*2 |
---|
154 | * stops timer, writes string s and the time since last call of startTimer |
---|
155 | * if this time is > mintime sec |
---|
156 | */ |
---|
157 | #ifdef EXTEND_TIMER_D |
---|
158 | extern int iiOp; |
---|
159 | #endif |
---|
160 | void writeTime(const char* v) |
---|
161 | { |
---|
162 | clock_t curr; |
---|
163 | |
---|
164 | #ifdef GETRUSAGE |
---|
165 | getrusage(RUSAGE_SELF,&t_rec); |
---|
166 | curr = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
167 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
168 | +5000)/10000; // unit is 1/100 sec |
---|
169 | curr -= startl; |
---|
170 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
171 | #else |
---|
172 | times(&t_rec); |
---|
173 | curr = t_rec.tms_utime+t_rec.tms_stime - startl; |
---|
174 | |
---|
175 | double f = ((double)curr) / (double)HZ; |
---|
176 | #endif |
---|
177 | if (f > mintime) |
---|
178 | { |
---|
179 | #ifdef EXTEND_TIMER_D |
---|
180 | Print("//%s %.2f sec (%d) >>%s<<\n" ,v ,f,iiOp,my_yylinebuf); |
---|
181 | #else |
---|
182 | Print("//%s %.2f sec\n" ,v ,f); |
---|
183 | #endif |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | #ifdef HAVE_RTIMER |
---|
188 | /*0 Real timer implementation*/ |
---|
189 | int rtimerv = 0; |
---|
190 | static struct timeval startRl; |
---|
191 | static struct timeval siStartRTime; |
---|
192 | static struct timezone tzp; |
---|
193 | void initRTimer() |
---|
194 | { |
---|
195 | gettimeofday(&startRl, &tzp); |
---|
196 | gettimeofday(&siStartRTime, &tzp); |
---|
197 | } |
---|
198 | |
---|
199 | void startRTimer() |
---|
200 | { |
---|
201 | gettimeofday(&siStartRTime, &tzp); |
---|
202 | } |
---|
203 | |
---|
204 | /*2 |
---|
205 | * returns the time since a fixed point in resolutions |
---|
206 | */ |
---|
207 | int getRTimer() |
---|
208 | { |
---|
209 | struct timeval now; |
---|
210 | |
---|
211 | gettimeofday(&now, &tzp); |
---|
212 | |
---|
213 | if (startRl.tv_usec > now.tv_usec) |
---|
214 | { |
---|
215 | now.tv_usec += 1000000; |
---|
216 | now.tv_sec --; |
---|
217 | } |
---|
218 | |
---|
219 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
220 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
221 | (double) 1000000; |
---|
222 | |
---|
223 | return (int)(f+0.5); |
---|
224 | } |
---|
225 | |
---|
226 | /*2 |
---|
227 | * stops timer, writes string s and the time since last call of startTimer |
---|
228 | * if this time is > mintime |
---|
229 | */ |
---|
230 | void writeRTime(const char* v) |
---|
231 | { |
---|
232 | struct timeval now; |
---|
233 | |
---|
234 | gettimeofday(&now, &tzp); |
---|
235 | |
---|
236 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
237 | { |
---|
238 | now.tv_usec += 1000000; |
---|
239 | now.tv_sec --; |
---|
240 | } |
---|
241 | |
---|
242 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
243 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
244 | (double) 1000000; |
---|
245 | |
---|
246 | if (f > mintime) |
---|
247 | Print("//%s %.2f sec \n" ,v ,f); |
---|
248 | } |
---|
249 | #endif |
---|