1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
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 | #ifdef GETRUSAGE |
---|
91 | static int64 siStartTime; |
---|
92 | static int64 startl; |
---|
93 | #else |
---|
94 | static clock_t siStartTime; |
---|
95 | static clock_t startl; |
---|
96 | #endif |
---|
97 | |
---|
98 | /*3 |
---|
99 | * temp structure to get the time |
---|
100 | */ |
---|
101 | #ifdef GETRUSAGE |
---|
102 | static struct rusage t_rec; |
---|
103 | #else |
---|
104 | static struct tms t_rec; |
---|
105 | #endif |
---|
106 | /*0 implementation*/ |
---|
107 | |
---|
108 | int initTimer() |
---|
109 | { |
---|
110 | #ifdef GETRUSAGE |
---|
111 | getrusage(RUSAGE_SELF,&t_rec); |
---|
112 | siStartTime = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
113 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
114 | +5000)/10000; // unit is 1/100 sec |
---|
115 | #else |
---|
116 | times(&t_rec); |
---|
117 | siStartTime = t_rec.tms_utime+t_rec.tms_stime; |
---|
118 | #endif |
---|
119 | return (int)time(NULL); |
---|
120 | } |
---|
121 | |
---|
122 | void startTimer() |
---|
123 | { |
---|
124 | #ifdef GETRUSAGE |
---|
125 | getrusage(RUSAGE_SELF,&t_rec); |
---|
126 | startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
127 | +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
128 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
129 | #else |
---|
130 | times(&t_rec); |
---|
131 | startl = t_rec.tms_utime+t_rec.tms_stime; |
---|
132 | #endif |
---|
133 | } |
---|
134 | |
---|
135 | /*2 |
---|
136 | * returns the time since a fixed point in seconds |
---|
137 | */ |
---|
138 | int getTimer() |
---|
139 | { |
---|
140 | #ifdef GETRUSAGE |
---|
141 | int64 curr; |
---|
142 | getrusage(RUSAGE_SELF,&t_rec); |
---|
143 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
144 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
145 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
146 | curr -= siStartTime; |
---|
147 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
148 | #else |
---|
149 | clock_t curr; |
---|
150 | |
---|
151 | times(&t_rec); |
---|
152 | curr = t_rec.tms_utime+t_rec.tms_stime - siStartTime; |
---|
153 | |
---|
154 | double f = ((double)curr) * timer_resolution / (double)HZ; |
---|
155 | #endif |
---|
156 | return (int)(f+0.5); |
---|
157 | } |
---|
158 | |
---|
159 | /*2 |
---|
160 | * stops timer, writes string s and the time since last call of startTimer |
---|
161 | * if this time is > mintime sec |
---|
162 | */ |
---|
163 | #ifdef EXTEND_TIMER_D |
---|
164 | extern int iiOp; |
---|
165 | #endif |
---|
166 | void writeTime(const char* v) |
---|
167 | { |
---|
168 | |
---|
169 | #ifdef GETRUSAGE |
---|
170 | int64 curr; |
---|
171 | getrusage(RUSAGE_SELF,&t_rec); |
---|
172 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
173 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
174 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
175 | curr -= startl; |
---|
176 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
177 | #else |
---|
178 | clock_t curr; |
---|
179 | times(&t_rec); |
---|
180 | curr = t_rec.tms_utime+t_rec.tms_stime - startl; |
---|
181 | |
---|
182 | double f = ((double)curr) / (double)HZ; |
---|
183 | #endif |
---|
184 | if (f/timer_resolution > mintime) |
---|
185 | { |
---|
186 | #ifdef EXTEND_TIMER_D |
---|
187 | Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf); |
---|
188 | #else |
---|
189 | if (timer_resolution==(double)1.0) |
---|
190 | Print("//%s %.2f sec\n" ,v ,f); |
---|
191 | else |
---|
192 | Print("//%s %.2f/%d sec\n" ,v ,f,(int)timer_resolution); |
---|
193 | #endif |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | #ifdef HAVE_RTIMER |
---|
198 | /*0 Real timer implementation*/ |
---|
199 | int rtimerv = 0; |
---|
200 | static struct timeval startRl; |
---|
201 | static struct timeval siStartRTime; |
---|
202 | static struct timezone tzp; |
---|
203 | void initRTimer() |
---|
204 | { |
---|
205 | gettimeofday(&startRl, &tzp); |
---|
206 | gettimeofday(&siStartRTime, &tzp); |
---|
207 | } |
---|
208 | |
---|
209 | void startRTimer() |
---|
210 | { |
---|
211 | gettimeofday(&siStartRTime, &tzp); |
---|
212 | } |
---|
213 | |
---|
214 | /*2 |
---|
215 | * returns the time since a fixed point in resolutions |
---|
216 | */ |
---|
217 | int getRTimer() |
---|
218 | { |
---|
219 | struct timeval now; |
---|
220 | |
---|
221 | gettimeofday(&now, &tzp); |
---|
222 | |
---|
223 | if (startRl.tv_usec > now.tv_usec) |
---|
224 | { |
---|
225 | now.tv_usec += 1000000; |
---|
226 | now.tv_sec --; |
---|
227 | } |
---|
228 | |
---|
229 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
230 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
231 | (double) 1000000; |
---|
232 | |
---|
233 | return (int)(f+0.5); |
---|
234 | } |
---|
235 | |
---|
236 | /*2 |
---|
237 | * stops timer, writes string s and the time since last call of startTimer |
---|
238 | * if this time is > mintime |
---|
239 | */ |
---|
240 | void writeRTime(const char* v) |
---|
241 | { |
---|
242 | struct timeval now; |
---|
243 | |
---|
244 | gettimeofday(&now, &tzp); |
---|
245 | |
---|
246 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
247 | { |
---|
248 | now.tv_usec += 1000000; |
---|
249 | now.tv_sec --; |
---|
250 | } |
---|
251 | |
---|
252 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
253 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
254 | (double) 1000000; |
---|
255 | |
---|
256 | if (f > mintime) |
---|
257 | Print("//%s %.2f sec \n" ,v ,f); |
---|
258 | } |
---|
259 | #endif |
---|