1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | |
---|
5 | /* |
---|
6 | * ABSTRACT - get the computing time |
---|
7 | */ |
---|
8 | #include <sys/resource.h> |
---|
9 | #include <time.h> |
---|
10 | #include <sys/time.h> |
---|
11 | #include <unistd.h> |
---|
12 | |
---|
13 | #include "config.h" |
---|
14 | #include <kernel/mod2.h> |
---|
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 |
---|
45 | |
---|
46 | #ifdef HAVE_SYS_TIMES_H |
---|
47 | #include <sys/times.h> |
---|
48 | #endif |
---|
49 | |
---|
50 | |
---|
51 | #include <kernel/timer.h> |
---|
52 | #include <kernel/febase.h> |
---|
53 | |
---|
54 | /*3 |
---|
55 | * the start time of the timer |
---|
56 | */ |
---|
57 | static int64 siStartTime; |
---|
58 | static int64 startl; |
---|
59 | |
---|
60 | /*3 |
---|
61 | * temp structure to get the time |
---|
62 | */ |
---|
63 | static struct rusage t_rec; |
---|
64 | /*0 implementation*/ |
---|
65 | |
---|
66 | int initTimer() |
---|
67 | { |
---|
68 | getrusage(RUSAGE_SELF,&t_rec); |
---|
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 |
---|
72 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
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 |
---|
76 | return (int)time(NULL); |
---|
77 | } |
---|
78 | |
---|
79 | void startTimer() |
---|
80 | { |
---|
81 | getrusage(RUSAGE_SELF,&t_rec); |
---|
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 |
---|
85 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
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 |
---|
89 | } |
---|
90 | |
---|
91 | /*2 |
---|
92 | * returns the time since a fixed point in seconds |
---|
93 | */ |
---|
94 | int getTimer() |
---|
95 | { |
---|
96 | int64 curr; |
---|
97 | getrusage(RUSAGE_SELF,&t_rec); |
---|
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 |
---|
101 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
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 |
---|
105 | curr -= siStartTime; |
---|
106 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
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 | */ |
---|
114 | #ifdef EXTEND_TIMER_D |
---|
115 | extern int iiOp; |
---|
116 | #endif |
---|
117 | |
---|
118 | void writeTime(const char* v) |
---|
119 | { |
---|
120 | int64 curr; |
---|
121 | getrusage(RUSAGE_SELF,&t_rec); |
---|
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 |
---|
125 | getrusage(RUSAGE_CHILDREN,&t_rec); |
---|
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 |
---|
129 | curr -= startl; |
---|
130 | double f = ((double)curr) * timer_resolution / (double)100; |
---|
131 | if (f/timer_resolution > mintime) |
---|
132 | { |
---|
133 | #ifdef EXTEND_TIMER_D |
---|
134 | Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf); |
---|
135 | #else |
---|
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); |
---|
140 | #endif |
---|
141 | } |
---|
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; |
---|
149 | |
---|
150 | void startRTimer() |
---|
151 | { |
---|
152 | gettimeofday(&siStartRTime, &tzp); |
---|
153 | } |
---|
154 | |
---|
155 | void initRTimer() |
---|
156 | { |
---|
157 | #ifdef HAVE_GETTIMEOFDAY |
---|
158 | gettimeofday(&startRl, &tzp); |
---|
159 | gettimeofday(&siStartRTime, &tzp); |
---|
160 | #else |
---|
161 | memset(&startRl,0,sizeof(startRl)); |
---|
162 | memset(&siStartRTime,0,sizeof(siStartRTime)); |
---|
163 | #endif |
---|
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 | */ |
---|
192 | void writeRTime(const char* v) |
---|
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 | } |
---|