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 <unistd.h>
16 
17 int timerv = 0;
19 
20 static double mintime = 0.5;
21 
23 {
24  timer_resolution = (double) res;
25 }
26 
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 */
59 static int64 startl;
60 
61 /*3
62 * temp structure to get the time
63 */
64 static struct rusage t_rec;
65 /*0 implementation*/
66 
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 
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 */
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 int iiOp;
117 #endif
118 
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 int rtimerv = 0;
147 static struct timeval startRl;
148 static struct timeval siStartRTime;
149 static struct timezone tzp;
150 
152 {
153  gettimeofday(&siStartRTime, &tzp);
154 }
155 
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 */
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 */
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 }
static double mintime
Definition: timer.cc:20
#define Print
Definition: emacs.cc:80
static int64 startl
Definition: timer.cc:59
int getRTimer()
Definition: timer.cc:170
static double timer_resolution
Definition: timer.cc:18
void writeTime(const char *v)
Definition: timer.cc:119
long int64
Definition: auxiliary.h:66
void initRTimer()
Definition: timer.cc:156
int timerv
Definition: timer.cc:17
static struct timeval siStartRTime
Definition: timer.cc:148
char my_yylinebuf[80]
Definition: febase.cc:43
#define TIMER_RESOLUTION
Definition: mod2.h:34
CanonicalForm res
Definition: facAbsFact.cc:64
void SetTimerResolution(int res)
Definition: timer.cc:22
static struct rusage t_rec
Definition: timer.cc:64
FILE * f
Definition: checklibs.c:9
void startRTimer()
Definition: timer.cc:151
void SetMinDisplayTime(double mtime)
Definition: timer.cc:27
static struct timezone tzp
Definition: timer.cc:149
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
#define NULL
Definition: omList.c:12
static struct timeval startRl
Definition: timer.cc:147
static int64 siStartTime
Definition: timer.cc:58
int initTimer()
Definition: timer.cc:67
int rtimerv
Definition: timer.cc:146
int getTimer()
Definition: timer.cc:95
void startTimer()
Definition: timer.cc:80
int iiOp
Definition: iparith.cc:216
void writeRTime(const char *v)
Definition: timer.cc:193