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