[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[35aab3] | 5 | |
---|
| 6 | /* |
---|
| 7 | * ABSTRACT - get the computing time |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include "mod2.h" |
---|
[cc477d] | 11 | // undef GETRUSAGE to get the original timer code (w.r.t. CLOCK_TICKS) |
---|
| 12 | #define GETRUSAGE |
---|
[51a2f3] | 13 | #ifdef GETRUSAGE |
---|
| 14 | #include <sys/time.h> |
---|
| 15 | #include <sys/resource.h> |
---|
| 16 | #include <unistd.h> |
---|
| 17 | #endif |
---|
[35aab3] | 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 |
---|
[9bbbc8] | 53 | |
---|
[35aab3] | 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 |
---|
[9bbbc8] | 63 | |
---|
[35aab3] | 64 | #if !defined(HZ) && defined(CLOCKS_PER_SEC) |
---|
| 65 | #define HZ CLOCKS_PER_SEC |
---|
| 66 | #endif |
---|
[9bbbc8] | 67 | |
---|
[35aab3] | 68 | #if !defined(HZ) && defined(CLK_TCK) |
---|
| 69 | #define HZ CLK_TCK |
---|
| 70 | #endif |
---|
[9bbbc8] | 71 | |
---|
| 72 | #if !defined(HZ) && defined(HAVE_SYSCONF) |
---|
| 73 | #define HZ sysconf(_SC_CLK_TCK) |
---|
[35aab3] | 74 | #endif |
---|
[9bbbc8] | 75 | |
---|
| 76 | #ifndef HZ |
---|
| 77 | // last resort |
---|
| 78 | #ifdef sun |
---|
| 79 | #define HZ 60.0 |
---|
| 80 | #else |
---|
| 81 | #define HZ 100.0 |
---|
| 82 | #endif |
---|
[35aab3] | 83 | #endif |
---|
| 84 | |
---|
| 85 | #include "timer.h" |
---|
| 86 | #include "febase.h" |
---|
| 87 | /*3 |
---|
| 88 | * the start time of the timer |
---|
| 89 | */ |
---|
[971108] | 90 | #ifdef GETRUSAGE |
---|
| 91 | static int64 siStartTime; |
---|
| 92 | static int64 startl; |
---|
| 93 | #else |
---|
[35aab3] | 94 | static clock_t siStartTime; |
---|
[971108] | 95 | static clock_t startl; |
---|
| 96 | #endif |
---|
[35aab3] | 97 | |
---|
| 98 | /*3 |
---|
| 99 | * temp structure to get the time |
---|
| 100 | */ |
---|
[51a2f3] | 101 | #ifdef GETRUSAGE |
---|
| 102 | static struct rusage t_rec; |
---|
| 103 | #else |
---|
[35aab3] | 104 | static struct tms t_rec; |
---|
[51a2f3] | 105 | #endif |
---|
[35aab3] | 106 | /*0 implementation*/ |
---|
| 107 | |
---|
| 108 | int initTimer() |
---|
| 109 | { |
---|
[51a2f3] | 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 |
---|
[199de1] | 115 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
| 116 | siStartTime += (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
| 117 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 118 | +5000)/10000; // unit is 1/100 sec |
---|
[51a2f3] | 119 | #else |
---|
[35aab3] | 120 | times(&t_rec); |
---|
| 121 | siStartTime = t_rec.tms_utime+t_rec.tms_stime; |
---|
[51a2f3] | 122 | #endif |
---|
[35aab3] | 123 | return (int)time(NULL); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void startTimer() |
---|
| 127 | { |
---|
[51a2f3] | 128 | #ifdef GETRUSAGE |
---|
| 129 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 130 | startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 131 | +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 132 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[199de1] | 133 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
| 134 | startl += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 135 | +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 136 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[51a2f3] | 137 | #else |
---|
[35aab3] | 138 | times(&t_rec); |
---|
| 139 | startl = t_rec.tms_utime+t_rec.tms_stime; |
---|
[51a2f3] | 140 | #endif |
---|
[35aab3] | 141 | } |
---|
| 142 | |
---|
| 143 | /*2 |
---|
| 144 | * returns the time since a fixed point in seconds |
---|
| 145 | */ |
---|
| 146 | int getTimer() |
---|
| 147 | { |
---|
[51a2f3] | 148 | #ifdef GETRUSAGE |
---|
[971108] | 149 | int64 curr; |
---|
[51a2f3] | 150 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 151 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 152 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 153 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[199de1] | 154 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
| 155 | curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 156 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 157 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[51a2f3] | 158 | curr -= siStartTime; |
---|
| 159 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
| 160 | #else |
---|
[971108] | 161 | clock_t curr; |
---|
[35aab3] | 162 | |
---|
| 163 | times(&t_rec); |
---|
| 164 | curr = t_rec.tms_utime+t_rec.tms_stime - siStartTime; |
---|
| 165 | |
---|
| 166 | double f = ((double)curr) * timer_resolution / (double)HZ; |
---|
[51a2f3] | 167 | #endif |
---|
[35aab3] | 168 | return (int)(f+0.5); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /*2 |
---|
| 172 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 173 | * if this time is > mintime sec |
---|
| 174 | */ |
---|
[05504b7] | 175 | #ifdef EXTEND_TIMER_D |
---|
| 176 | extern int iiOp; |
---|
| 177 | #endif |
---|
[85e68dd] | 178 | void writeTime(const char* v) |
---|
[35aab3] | 179 | { |
---|
| 180 | |
---|
[51a2f3] | 181 | #ifdef GETRUSAGE |
---|
[971108] | 182 | int64 curr; |
---|
[51a2f3] | 183 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 184 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 185 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 186 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[199de1] | 187 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
| 188 | curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 189 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 190 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[51a2f3] | 191 | curr -= startl; |
---|
| 192 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
| 193 | #else |
---|
[971108] | 194 | clock_t curr; |
---|
[35aab3] | 195 | times(&t_rec); |
---|
| 196 | curr = t_rec.tms_utime+t_rec.tms_stime - startl; |
---|
| 197 | |
---|
| 198 | double f = ((double)curr) / (double)HZ; |
---|
[51a2f3] | 199 | #endif |
---|
[c6971d] | 200 | if (f/timer_resolution > mintime) |
---|
[05504b7] | 201 | { |
---|
| 202 | #ifdef EXTEND_TIMER_D |
---|
[c6971d] | 203 | Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf); |
---|
[05504b7] | 204 | #else |
---|
[c6971d] | 205 | if (timer_resolution==(double)1.0) |
---|
| 206 | Print("//%s %.2f sec\n" ,v ,f); |
---|
| 207 | else |
---|
| 208 | Print("//%s %.2f/%d sec\n" ,v ,f,(int)timer_resolution); |
---|
[05504b7] | 209 | #endif |
---|
| 210 | } |
---|
[35aab3] | 211 | } |
---|
| 212 | |
---|
| 213 | #ifdef HAVE_RTIMER |
---|
| 214 | /*0 Real timer implementation*/ |
---|
| 215 | int rtimerv = 0; |
---|
| 216 | static struct timeval startRl; |
---|
| 217 | static struct timeval siStartRTime; |
---|
| 218 | static struct timezone tzp; |
---|
| 219 | void initRTimer() |
---|
| 220 | { |
---|
| 221 | gettimeofday(&startRl, &tzp); |
---|
| 222 | gettimeofday(&siStartRTime, &tzp); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void startRTimer() |
---|
| 226 | { |
---|
| 227 | gettimeofday(&siStartRTime, &tzp); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /*2 |
---|
| 231 | * returns the time since a fixed point in resolutions |
---|
| 232 | */ |
---|
| 233 | int getRTimer() |
---|
| 234 | { |
---|
| 235 | struct timeval now; |
---|
| 236 | |
---|
| 237 | gettimeofday(&now, &tzp); |
---|
| 238 | |
---|
| 239 | if (startRl.tv_usec > now.tv_usec) |
---|
| 240 | { |
---|
| 241 | now.tv_usec += 1000000; |
---|
| 242 | now.tv_sec --; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
| 246 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
| 247 | (double) 1000000; |
---|
| 248 | |
---|
| 249 | return (int)(f+0.5); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /*2 |
---|
| 253 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 254 | * if this time is > mintime |
---|
| 255 | */ |
---|
[85e68dd] | 256 | void writeRTime(const char* v) |
---|
[35aab3] | 257 | { |
---|
| 258 | struct timeval now; |
---|
| 259 | |
---|
| 260 | gettimeofday(&now, &tzp); |
---|
| 261 | |
---|
| 262 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
| 263 | { |
---|
| 264 | now.tv_usec += 1000000; |
---|
| 265 | now.tv_sec --; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
| 269 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
| 270 | (double) 1000000; |
---|
| 271 | |
---|
| 272 | if (f > mintime) |
---|
| 273 | Print("//%s %.2f sec \n" ,v ,f); |
---|
| 274 | } |
---|
| 275 | #endif |
---|