source: git/kernel/timer.cc @ a82c308

spielwiese
Last change on this file since a82c308 was 762407, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
config.h is for sources files only FIX: config.h should only be used by source (not from inside kernel/mod2.h!) NOTE: each source file should better include mod2.h right after config.h, while headers should better not include mod2.h.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7*  ABSTRACT - get the computing time
8*/
9#include <sys/resource.h>
10#include <time.h>
11#include <sys/time.h>
12#include <unistd.h>
13
14#include "config.h"
15#include <kernel/mod2.h>
16
17int        timerv = 0;
18static double timer_resolution = TIMER_RESOLUTION;
19
20static double mintime = 0.5;
21
22void SetTimerResolution(int res)
23{
24  timer_resolution = (double) res;
25}
26
27void 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 <kernel/timer.h>
53#include <kernel/febase.h>
54
55/*3
56* the start time of the timer
57*/
58static int64 siStartTime;
59static int64 startl;
60
61/*3
62* temp structure to get the time
63*/
64static struct rusage t_rec;
65/*0 implementation*/
66
67int 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
80void 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*/
95int 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
116extern int iiOp;
117#endif
118
119void 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*/
146int rtimerv = 0;
147static struct timeval  startRl;
148static struct timeval  siStartRTime;
149static struct timezone tzp;
150
151void startRTimer()
152{
153  gettimeofday(&siStartRTime, &tzp);
154}
155
156void initRTimer()
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*/
170int getRTimer()
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*/
193void 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}
Note: See TracBrowser for help on using the repository browser.