source: git/kernel/timer.cc @ b1c0a9

spielwiese
Last change on this file since b1c0a9 was 971108, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: int64 git-svn-id: file:///usr/local/Singular/svn/trunk@11341 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: timer.cc,v 1.9 2009-01-29 16:57:02 Singular Exp $ */
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#else
116  times(&t_rec);
117  siStartTime = t_rec.tms_utime+t_rec.tms_stime;
118#endif
119  return (int)time(NULL);
120}
121
122void startTimer()
123{
124#ifdef GETRUSAGE
125  getrusage(RUSAGE_SELF,&t_rec); 
126  startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
127               +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
128               +(int64)5000)/(int64)10000; // unit is 1/100 sec
129#else
130  times(&t_rec);
131  startl = t_rec.tms_utime+t_rec.tms_stime;
132#endif
133}
134
135/*2
136* returns the time since a fixed point in seconds
137*/
138int getTimer()
139{
140#ifdef GETRUSAGE
141  int64 curr;
142  getrusage(RUSAGE_SELF,&t_rec); 
143  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
144         +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
145         +(int64)5000)/(int64)10000; // unit is 1/100 sec
146  curr -= siStartTime;
147  double f =  ((double)curr) * timer_resolution / (double)100;
148#else
149  clock_t curr;
150
151  times(&t_rec);
152  curr = t_rec.tms_utime+t_rec.tms_stime - siStartTime;
153
154  double f =  ((double)curr) * timer_resolution / (double)HZ;
155#endif
156  return (int)(f+0.5);
157}
158
159/*2
160* stops timer, writes string s and the time since last call of startTimer
161* if this time is > mintime sec
162*/
163#ifdef EXTEND_TIMER_D
164extern int iiOp;
165#endif
166void writeTime(const char* v)
167{
168
169#ifdef GETRUSAGE
170  int64 curr;
171  getrusage(RUSAGE_SELF,&t_rec); 
172  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
173               +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
174               +(int64)5000)/(int64)10000; // unit is 1/100 sec
175  curr -= startl;
176  double f =  ((double)curr) * timer_resolution / (double)100;
177#else
178  clock_t curr;
179  times(&t_rec);
180  curr = t_rec.tms_utime+t_rec.tms_stime - startl;
181
182  double f =  ((double)curr) / (double)HZ;
183#endif
184  if (f > mintime)
185  {
186#ifdef EXTEND_TIMER_D
187    Print("//%s %.2f sec (%d) >>%s<<\n" ,v ,f,iiOp,my_yylinebuf);
188#else
189    Print("//%s %.2f sec\n" ,v ,f);
190#endif
191  }
192}
193
194#ifdef HAVE_RTIMER
195/*0 Real timer implementation*/
196int rtimerv = 0;
197static struct timeval  startRl;
198static struct timeval  siStartRTime;
199static struct timezone tzp;
200void initRTimer()
201{
202  gettimeofday(&startRl, &tzp);
203  gettimeofday(&siStartRTime, &tzp);
204}
205
206void startRTimer()
207{
208  gettimeofday(&siStartRTime, &tzp);
209}
210
211/*2
212* returns the time since a fixed point in resolutions
213*/
214int getRTimer()
215{
216  struct timeval now;
217
218  gettimeofday(&now, &tzp);
219
220  if (startRl.tv_usec > now.tv_usec)
221  {
222    now.tv_usec += 1000000;
223    now.tv_sec --;
224  }
225
226  double f =((double)  (now.tv_sec - startRl.tv_sec))*timer_resolution +
227    ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution /
228    (double) 1000000;
229
230  return (int)(f+0.5);
231}
232
233/*2
234* stops timer, writes string s and the time since last call of startTimer
235* if this time is > mintime
236*/
237void writeRTime(const char* v)
238{
239  struct timeval now;
240
241  gettimeofday(&now, &tzp);
242
243  if (siStartRTime.tv_usec > now.tv_usec)
244  {
245    now.tv_usec += 1000000;
246    now.tv_sec --;
247  }
248
249  double f =((double)  (now.tv_sec - siStartRTime.tv_sec)) +
250    ((double) (now.tv_usec - siStartRTime.tv_usec)) /
251    (double) 1000000;
252
253  if (f > mintime)
254   Print("//%s %.2f sec \n" ,v ,f);
255}
256#endif
Note: See TracBrowser for help on using the repository browser.