source: git/kernel/timer.cc @ 576f5b

spielwiese
Last change on this file since 576f5b was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/*
6*  ABSTRACT - get the computing time
7*/
8#ifdef HAVE_CONFIG_H
9#include "singularconfig.h"
10#endif /* HAVE_CONFIG_H */
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
19int        timerv = 0;
20static double timer_resolution = TIMER_RESOLUTION;
21
22static double mintime = 0.5;
23
24void SetTimerResolution(int res)
25{
26  timer_resolution = (double) res;
27}
28
29void 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 <kernel/timer.h>
55#include <kernel/febase.h>
56
57/*3
58* the start time of the timer
59*/
60static int64 siStartTime;
61static int64 startl;
62
63/*3
64* temp structure to get the time
65*/
66static struct rusage t_rec;
67/*0 implementation*/
68
69int 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
82void 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*/
97int 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
118extern int iiOp;
119#endif
120
121void 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*/
148int rtimerv = 0;
149static struct timeval  startRl;
150static struct timeval  siStartRTime;
151static struct timezone tzp;
152
153void startRTimer()
154{
155  gettimeofday(&siStartRTime, &tzp);
156}
157
158void initRTimer()
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*/
172int getRTimer()
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*/
195void 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}
Note: See TracBrowser for help on using the repository browser.