[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[971108] | 4 | /* $Id: timer.cc,v 1.9 2009-01-29 16:57:02 Singular Exp $ */ |
---|
[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 |
---|
| 115 | #else |
---|
[35aab3] | 116 | times(&t_rec); |
---|
| 117 | siStartTime = t_rec.tms_utime+t_rec.tms_stime; |
---|
[51a2f3] | 118 | #endif |
---|
[35aab3] | 119 | return (int)time(NULL); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void startTimer() |
---|
| 123 | { |
---|
[51a2f3] | 124 | #ifdef GETRUSAGE |
---|
| 125 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 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 |
---|
[51a2f3] | 129 | #else |
---|
[35aab3] | 130 | times(&t_rec); |
---|
| 131 | startl = t_rec.tms_utime+t_rec.tms_stime; |
---|
[51a2f3] | 132 | #endif |
---|
[35aab3] | 133 | } |
---|
| 134 | |
---|
| 135 | /*2 |
---|
| 136 | * returns the time since a fixed point in seconds |
---|
| 137 | */ |
---|
| 138 | int getTimer() |
---|
| 139 | { |
---|
[51a2f3] | 140 | #ifdef GETRUSAGE |
---|
[971108] | 141 | int64 curr; |
---|
[51a2f3] | 142 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 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 |
---|
[51a2f3] | 146 | curr -= siStartTime; |
---|
| 147 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
| 148 | #else |
---|
[971108] | 149 | clock_t curr; |
---|
[35aab3] | 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; |
---|
[51a2f3] | 155 | #endif |
---|
[35aab3] | 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 | */ |
---|
[05504b7] | 163 | #ifdef EXTEND_TIMER_D |
---|
| 164 | extern int iiOp; |
---|
| 165 | #endif |
---|
[85e68dd] | 166 | void writeTime(const char* v) |
---|
[35aab3] | 167 | { |
---|
| 168 | |
---|
[51a2f3] | 169 | #ifdef GETRUSAGE |
---|
[971108] | 170 | int64 curr; |
---|
[51a2f3] | 171 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 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 |
---|
[51a2f3] | 175 | curr -= startl; |
---|
| 176 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
| 177 | #else |
---|
[971108] | 178 | clock_t curr; |
---|
[35aab3] | 179 | times(&t_rec); |
---|
| 180 | curr = t_rec.tms_utime+t_rec.tms_stime - startl; |
---|
| 181 | |
---|
| 182 | double f = ((double)curr) / (double)HZ; |
---|
[51a2f3] | 183 | #endif |
---|
[35aab3] | 184 | if (f > mintime) |
---|
[05504b7] | 185 | { |
---|
| 186 | #ifdef EXTEND_TIMER_D |
---|
| 187 | Print("//%s %.2f sec (%d) >>%s<<\n" ,v ,f,iiOp,my_yylinebuf); |
---|
| 188 | #else |
---|
[35aab3] | 189 | Print("//%s %.2f sec\n" ,v ,f); |
---|
[05504b7] | 190 | #endif |
---|
| 191 | } |
---|
[35aab3] | 192 | } |
---|
| 193 | |
---|
| 194 | #ifdef HAVE_RTIMER |
---|
| 195 | /*0 Real timer implementation*/ |
---|
| 196 | int rtimerv = 0; |
---|
| 197 | static struct timeval startRl; |
---|
| 198 | static struct timeval siStartRTime; |
---|
| 199 | static struct timezone tzp; |
---|
| 200 | void initRTimer() |
---|
| 201 | { |
---|
| 202 | gettimeofday(&startRl, &tzp); |
---|
| 203 | gettimeofday(&siStartRTime, &tzp); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | void startRTimer() |
---|
| 207 | { |
---|
| 208 | gettimeofday(&siStartRTime, &tzp); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /*2 |
---|
| 212 | * returns the time since a fixed point in resolutions |
---|
| 213 | */ |
---|
| 214 | int getRTimer() |
---|
| 215 | { |
---|
| 216 | struct timeval now; |
---|
| 217 | |
---|
| 218 | gettimeofday(&now, &tzp); |
---|
| 219 | |
---|
| 220 | if (startRl.tv_usec > now.tv_usec) |
---|
| 221 | { |
---|
| 222 | now.tv_usec += 1000000; |
---|
| 223 | now.tv_sec --; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
| 227 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
| 228 | (double) 1000000; |
---|
| 229 | |
---|
| 230 | return (int)(f+0.5); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /*2 |
---|
| 234 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 235 | * if this time is > mintime |
---|
| 236 | */ |
---|
[85e68dd] | 237 | void writeRTime(const char* v) |
---|
[35aab3] | 238 | { |
---|
| 239 | struct timeval now; |
---|
| 240 | |
---|
| 241 | gettimeofday(&now, &tzp); |
---|
| 242 | |
---|
| 243 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
| 244 | { |
---|
| 245 | now.tv_usec += 1000000; |
---|
| 246 | now.tv_sec --; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
| 250 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
| 251 | (double) 1000000; |
---|
| 252 | |
---|
| 253 | if (f > mintime) |
---|
| 254 | Print("//%s %.2f sec \n" ,v ,f); |
---|
| 255 | } |
---|
| 256 | #endif |
---|