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