source: git/kernel/timer.cc @ 199de1

fieker-DuValspielwiese
Last change on this file since 199de1 was 199de1, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: count self time + children git-svn-id: file:///usr/local/Singular/svn/trunk@12307 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7*  ABSTRACT - get the computing time
8*/
9
10#include "mod2.h"
11// undef GETRUSAGE to get the original timer code (w.r.t. CLOCK_TICKS)
12#define GETRUSAGE
13#ifdef GETRUSAGE
14       #include <sys/time.h>
15       #include <sys/resource.h>
16       #include <unistd.h>
17#endif
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#include <math.h>
36#include <unistd.h>
37#include <float.h>
38#include <mylimits.h>
39#include <sys/types.h>
40
41#ifdef TIME_WITH_SYS_TIME
42# include <time.h>
43# ifdef HAVE_SYS_TIME_H
44#   include <sys/time.h>
45# endif
46#else
47# ifdef HAVE_SYS_TIME_H
48#   include <sys/time.h>
49# else
50#   include <time.h>
51# endif
52#endif
53
54#ifdef HAVE_SYS_TIMES_H
55#include <sys/times.h>
56#endif
57
58
59// need to be adjusted on your machine: the number of ticks per second: HZ
60#ifndef HZ
61#include <sys/param.h>
62#endif
63
64#if !defined(HZ) && defined(CLOCKS_PER_SEC)
65#define HZ CLOCKS_PER_SEC
66#endif
67
68#if !defined(HZ) && defined(CLK_TCK)
69#define HZ CLK_TCK
70#endif
71
72#if !defined(HZ) && defined(HAVE_SYSCONF)
73#define HZ sysconf(_SC_CLK_TCK)
74#endif
75
76#ifndef HZ
77  // last resort
78  #ifdef sun
79  #define HZ 60.0
80  #else
81  #define HZ 100.0
82  #endif
83#endif
84
85#include "timer.h"
86#include "febase.h"
87/*3
88* the start time of the timer
89*/
90#ifdef GETRUSAGE
91static int64 siStartTime;
92static int64 startl;
93#else
94static clock_t siStartTime;
95static clock_t startl;
96#endif
97
98/*3
99* temp structure to get the time
100*/
101#ifdef GETRUSAGE
102static struct rusage t_rec;
103#else
104static struct tms t_rec;
105#endif
106/*0 implementation*/
107
108int initTimer()
109{
110#ifdef GETRUSAGE
111  getrusage(RUSAGE_SELF,&t_rec); 
112  siStartTime = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec
113               +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
114               +5000)/10000; // unit is 1/100 sec
115  getrusage(RUSAGE_CHILDREN,&t_rec); 
116  siStartTime += (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec
117               +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
118               +5000)/10000; // unit is 1/100 sec
119#else
120  times(&t_rec);
121  siStartTime = t_rec.tms_utime+t_rec.tms_stime;
122#endif
123  return (int)time(NULL);
124}
125
126void startTimer()
127{
128#ifdef GETRUSAGE
129  getrusage(RUSAGE_SELF,&t_rec); 
130  startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
131               +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
132               +(int64)5000)/(int64)10000; // unit is 1/100 sec
133  getrusage(RUSAGE_CHILDREN,&t_rec); 
134  startl += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
135               +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
136               +(int64)5000)/(int64)10000; // unit is 1/100 sec
137#else
138  times(&t_rec);
139  startl = t_rec.tms_utime+t_rec.tms_stime;
140#endif
141}
142
143/*2
144* returns the time since a fixed point in seconds
145*/
146int getTimer()
147{
148#ifdef GETRUSAGE
149  int64 curr;
150  getrusage(RUSAGE_SELF,&t_rec); 
151  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
152         +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
153         +(int64)5000)/(int64)10000; // unit is 1/100 sec
154  getrusage(RUSAGE_CHILDREN,&t_rec); 
155  curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
156         +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
157         +(int64)5000)/(int64)10000; // unit is 1/100 sec
158  curr -= siStartTime;
159  double f =  ((double)curr) * timer_resolution / (double)100;
160#else
161  clock_t curr;
162
163  times(&t_rec);
164  curr = t_rec.tms_utime+t_rec.tms_stime - siStartTime;
165
166  double f =  ((double)curr) * timer_resolution / (double)HZ;
167#endif
168  return (int)(f+0.5);
169}
170
171/*2
172* stops timer, writes string s and the time since last call of startTimer
173* if this time is > mintime sec
174*/
175#ifdef EXTEND_TIMER_D
176extern int iiOp;
177#endif
178void writeTime(const char* v)
179{
180
181#ifdef GETRUSAGE
182  int64 curr;
183  getrusage(RUSAGE_SELF,&t_rec); 
184  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
185               +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
186               +(int64)5000)/(int64)10000; // unit is 1/100 sec
187  getrusage(RUSAGE_CHILDREN,&t_rec); 
188  curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
189               +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
190               +(int64)5000)/(int64)10000; // unit is 1/100 sec
191  curr -= startl;
192  double f =  ((double)curr) * timer_resolution / (double)100;
193#else
194  clock_t curr;
195  times(&t_rec);
196  curr = t_rec.tms_utime+t_rec.tms_stime - startl;
197
198  double f =  ((double)curr) / (double)HZ;
199#endif
200  if (f/timer_resolution > mintime)
201  {
202#ifdef EXTEND_TIMER_D
203    Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf);
204#else
205    if (timer_resolution==(double)1.0)
206      Print("//%s %.2f sec\n" ,v ,f);
207    else
208      Print("//%s %.2f/%d sec\n" ,v ,f,(int)timer_resolution);
209#endif
210  }
211}
212
213#ifdef HAVE_RTIMER
214/*0 Real timer implementation*/
215int rtimerv = 0;
216static struct timeval  startRl;
217static struct timeval  siStartRTime;
218static struct timezone tzp;
219void initRTimer()
220{
221  gettimeofday(&startRl, &tzp);
222  gettimeofday(&siStartRTime, &tzp);
223}
224
225void startRTimer()
226{
227  gettimeofday(&siStartRTime, &tzp);
228}
229
230/*2
231* returns the time since a fixed point in resolutions
232*/
233int getRTimer()
234{
235  struct timeval now;
236
237  gettimeofday(&now, &tzp);
238
239  if (startRl.tv_usec > now.tv_usec)
240  {
241    now.tv_usec += 1000000;
242    now.tv_sec --;
243  }
244
245  double f =((double)  (now.tv_sec - startRl.tv_sec))*timer_resolution +
246    ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution /
247    (double) 1000000;
248
249  return (int)(f+0.5);
250}
251
252/*2
253* stops timer, writes string s and the time since last call of startTimer
254* if this time is > mintime
255*/
256void writeRTime(const char* v)
257{
258  struct timeval now;
259
260  gettimeofday(&now, &tzp);
261
262  if (siStartRTime.tv_usec > now.tv_usec)
263  {
264    now.tv_usec += 1000000;
265    now.tv_sec --;
266  }
267
268  double f =((double)  (now.tv_sec - siStartRTime.tv_sec)) +
269    ((double) (now.tv_usec - siStartRTime.tv_usec)) /
270    (double) 1000000;
271
272  if (f > mintime)
273   Print("//%s %.2f sec \n" ,v ,f);
274}
275#endif
Note: See TracBrowser for help on using the repository browser.