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