[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | |
---|
| 5 | /* |
---|
| 6 | * ABSTRACT - get the computing time |
---|
| 7 | */ |
---|
[fba1d9c] | 8 | #include <sys/resource.h> |
---|
[e32205] | 9 | #include <time.h> |
---|
| 10 | #include <sys/time.h> |
---|
[fba1d9c] | 11 | #include <unistd.h> |
---|
[35aab3] | 12 | |
---|
[762407] | 13 | #include "config.h" |
---|
[599326] | 14 | #include <kernel/mod2.h> |
---|
[35aab3] | 15 | |
---|
| 16 | int timerv = 0; |
---|
| 17 | static double timer_resolution = TIMER_RESOLUTION; |
---|
| 18 | |
---|
| 19 | static double mintime = 0.5; |
---|
| 20 | |
---|
| 21 | void SetTimerResolution(int res) |
---|
| 22 | { |
---|
| 23 | timer_resolution = (double) res; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | void SetMinDisplayTime(double mtime) |
---|
| 27 | { |
---|
| 28 | mintime = mtime; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | #include <stdio.h> |
---|
| 32 | |
---|
| 33 | #ifdef TIME_WITH_SYS_TIME |
---|
| 34 | # include <time.h> |
---|
| 35 | # ifdef HAVE_SYS_TIME_H |
---|
| 36 | # include <sys/time.h> |
---|
| 37 | # endif |
---|
| 38 | #else |
---|
| 39 | # ifdef HAVE_SYS_TIME_H |
---|
| 40 | # include <sys/time.h> |
---|
| 41 | # else |
---|
| 42 | # include <time.h> |
---|
| 43 | # endif |
---|
| 44 | #endif |
---|
[9bbbc8] | 45 | |
---|
[35aab3] | 46 | #ifdef HAVE_SYS_TIMES_H |
---|
| 47 | #include <sys/times.h> |
---|
| 48 | #endif |
---|
| 49 | |
---|
| 50 | |
---|
[599326] | 51 | #include <kernel/timer.h> |
---|
| 52 | #include <kernel/febase.h> |
---|
[fba1d9c] | 53 | |
---|
[35aab3] | 54 | /*3 |
---|
| 55 | * the start time of the timer |
---|
| 56 | */ |
---|
[971108] | 57 | static int64 siStartTime; |
---|
| 58 | static int64 startl; |
---|
[35aab3] | 59 | |
---|
| 60 | /*3 |
---|
| 61 | * temp structure to get the time |
---|
| 62 | */ |
---|
[51a2f3] | 63 | static struct rusage t_rec; |
---|
[35aab3] | 64 | /*0 implementation*/ |
---|
| 65 | |
---|
| 66 | int initTimer() |
---|
| 67 | { |
---|
[fba1d9c] | 68 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[51a2f3] | 69 | siStartTime = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
| 70 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 71 | +5000)/10000; // unit is 1/100 sec |
---|
[fba1d9c] | 72 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
[199de1] | 73 | siStartTime += (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec |
---|
| 74 | +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 75 | +5000)/10000; // unit is 1/100 sec |
---|
[35aab3] | 76 | return (int)time(NULL); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void startTimer() |
---|
| 80 | { |
---|
[fba1d9c] | 81 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 82 | startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 83 | +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 84 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[fba1d9c] | 85 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
[199de1] | 86 | startl += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 87 | +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec |
---|
| 88 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[35aab3] | 89 | } |
---|
| 90 | |
---|
| 91 | /*2 |
---|
| 92 | * returns the time since a fixed point in seconds |
---|
| 93 | */ |
---|
| 94 | int getTimer() |
---|
| 95 | { |
---|
[971108] | 96 | int64 curr; |
---|
[fba1d9c] | 97 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 98 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 99 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 100 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[fba1d9c] | 101 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
[199de1] | 102 | curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 103 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 104 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[51a2f3] | 105 | curr -= siStartTime; |
---|
| 106 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
[35aab3] | 107 | return (int)(f+0.5); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /*2 |
---|
| 111 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 112 | * if this time is > mintime sec |
---|
| 113 | */ |
---|
[05504b7] | 114 | #ifdef EXTEND_TIMER_D |
---|
| 115 | extern int iiOp; |
---|
| 116 | #endif |
---|
[f9bb68] | 117 | |
---|
[85e68dd] | 118 | void writeTime(const char* v) |
---|
[35aab3] | 119 | { |
---|
[971108] | 120 | int64 curr; |
---|
[fba1d9c] | 121 | getrusage(RUSAGE_SELF,&t_rec); |
---|
[971108] | 122 | curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 123 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 124 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[fba1d9c] | 125 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
[199de1] | 126 | curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec |
---|
| 127 | +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec |
---|
| 128 | +(int64)5000)/(int64)10000; // unit is 1/100 sec |
---|
[51a2f3] | 129 | curr -= startl; |
---|
| 130 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
[c6971d] | 131 | if (f/timer_resolution > mintime) |
---|
[05504b7] | 132 | { |
---|
| 133 | #ifdef EXTEND_TIMER_D |
---|
[c6971d] | 134 | Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf); |
---|
[05504b7] | 135 | #else |
---|
[c6971d] | 136 | if (timer_resolution==(double)1.0) |
---|
| 137 | Print("//%s %.2f sec\n" ,v ,f); |
---|
| 138 | else |
---|
| 139 | Print("//%s %.2f/%d sec\n" ,v ,f,(int)timer_resolution); |
---|
[05504b7] | 140 | #endif |
---|
| 141 | } |
---|
[35aab3] | 142 | } |
---|
| 143 | |
---|
| 144 | /*0 Real timer implementation*/ |
---|
| 145 | int rtimerv = 0; |
---|
| 146 | static struct timeval startRl; |
---|
| 147 | static struct timeval siStartRTime; |
---|
| 148 | static struct timezone tzp; |
---|
[e32205] | 149 | |
---|
| 150 | void startRTimer() |
---|
| 151 | { |
---|
| 152 | gettimeofday(&siStartRTime, &tzp); |
---|
| 153 | } |
---|
| 154 | |
---|
[35aab3] | 155 | void initRTimer() |
---|
| 156 | { |
---|
[4b889d] | 157 | #ifdef HAVE_GETTIMEOFDAY |
---|
[35aab3] | 158 | gettimeofday(&startRl, &tzp); |
---|
| 159 | gettimeofday(&siStartRTime, &tzp); |
---|
[4b889d] | 160 | #else |
---|
| 161 | memset(&startRl,0,sizeof(startRl)); |
---|
[e32205] | 162 | memset(&siStartRTime,0,sizeof(siStartRTime)); |
---|
[4b889d] | 163 | #endif |
---|
[35aab3] | 164 | } |
---|
| 165 | |
---|
| 166 | /*2 |
---|
| 167 | * returns the time since a fixed point in resolutions |
---|
| 168 | */ |
---|
| 169 | int getRTimer() |
---|
| 170 | { |
---|
| 171 | struct timeval now; |
---|
| 172 | |
---|
| 173 | gettimeofday(&now, &tzp); |
---|
| 174 | |
---|
| 175 | if (startRl.tv_usec > now.tv_usec) |
---|
| 176 | { |
---|
| 177 | now.tv_usec += 1000000; |
---|
| 178 | now.tv_sec --; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | double f =((double) (now.tv_sec - startRl.tv_sec))*timer_resolution + |
---|
| 182 | ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution / |
---|
| 183 | (double) 1000000; |
---|
| 184 | |
---|
| 185 | return (int)(f+0.5); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /*2 |
---|
| 189 | * stops timer, writes string s and the time since last call of startTimer |
---|
| 190 | * if this time is > mintime |
---|
| 191 | */ |
---|
[85e68dd] | 192 | void writeRTime(const char* v) |
---|
[35aab3] | 193 | { |
---|
| 194 | struct timeval now; |
---|
| 195 | |
---|
| 196 | gettimeofday(&now, &tzp); |
---|
| 197 | |
---|
| 198 | if (siStartRTime.tv_usec > now.tv_usec) |
---|
| 199 | { |
---|
| 200 | now.tv_usec += 1000000; |
---|
| 201 | now.tv_sec --; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | double f =((double) (now.tv_sec - siStartRTime.tv_sec)) + |
---|
| 205 | ((double) (now.tv_usec - siStartRTime.tv_usec)) / |
---|
| 206 | (double) 1000000; |
---|
| 207 | |
---|
| 208 | if (f > mintime) |
---|
| 209 | Print("//%s %.2f sec \n" ,v ,f); |
---|
| 210 | } |
---|