source: git/kernel/timer.cc @ 528f5b7

spielwiese
Last change on this file since 528f5b7 was 4b889d, checked in by Hans Schoenemann <hannes@…>, 13 years ago
RTIMER simplification git-svn-id: file:///usr/local/Singular/svn/trunk@13480 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.0 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 <unistd.h>
11
12#include <kernel/mod2.h>
13
14int        timerv = 0;
15static double timer_resolution = TIMER_RESOLUTION;
16
17static double mintime = 0.5;
18
19void SetTimerResolution(int res)
20{
21  timer_resolution = (double) res;
22}
23
24void SetMinDisplayTime(double mtime)
25{
26  mintime = mtime;
27}
28
29#include <stdio.h>
30
31#ifdef TIME_WITH_SYS_TIME
32# include <time.h>
33# ifdef HAVE_SYS_TIME_H
34#   include <sys/time.h>
35# endif
36#else
37# ifdef HAVE_SYS_TIME_H
38#   include <sys/time.h>
39# else
40#   include <time.h>
41# endif
42#endif
43
44#ifdef HAVE_SYS_TIMES_H
45#include <sys/times.h>
46#endif
47
48
49#include <kernel/timer.h>
50#include <kernel/febase.h>
51
52/*3
53* the start time of the timer
54*/
55static int64 siStartTime;
56static int64 startl;
57
58/*3
59* temp structure to get the time
60*/
61static struct rusage t_rec;
62/*0 implementation*/
63
64int initTimer()
65{
66  getrusage(RUSAGE_SELF,&t_rec);
67  siStartTime = (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec
68               +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
69               +5000)/10000; // unit is 1/100 sec
70  getrusage(RUSAGE_CHILDREN,&t_rec);
71  siStartTime += (t_rec.ru_utime.tv_sec*1000000+t_rec.ru_utime.tv_usec
72               +t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
73               +5000)/10000; // unit is 1/100 sec
74  return (int)time(NULL);
75}
76
77void startTimer()
78{
79  getrusage(RUSAGE_SELF,&t_rec);
80  startl = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
81               +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
82               +(int64)5000)/(int64)10000; // unit is 1/100 sec
83  getrusage(RUSAGE_CHILDREN,&t_rec);
84  startl += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
85               +(int64)t_rec.ru_stime.tv_sec*1000000+t_rec.ru_stime.tv_usec
86               +(int64)5000)/(int64)10000; // unit is 1/100 sec
87}
88
89/*2
90* returns the time since a fixed point in seconds
91*/
92int getTimer()
93{
94  int64 curr;
95  getrusage(RUSAGE_SELF,&t_rec);
96  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
97         +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
98         +(int64)5000)/(int64)10000; // unit is 1/100 sec
99  getrusage(RUSAGE_CHILDREN,&t_rec);
100  curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
101         +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
102         +(int64)5000)/(int64)10000; // unit is 1/100 sec
103  curr -= siStartTime;
104  double f =  ((double)curr) * timer_resolution / (double)100;
105  return (int)(f+0.5);
106}
107
108/*2
109* stops timer, writes string s and the time since last call of startTimer
110* if this time is > mintime sec
111*/
112#ifdef EXTEND_TIMER_D
113extern int iiOp;
114#endif
115
116void writeTime(const char* v)
117{
118  int64 curr;
119  getrusage(RUSAGE_SELF,&t_rec);
120  curr = ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
121               +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
122               +(int64)5000)/(int64)10000; // unit is 1/100 sec
123  getrusage(RUSAGE_CHILDREN,&t_rec);
124  curr += ((int64)t_rec.ru_utime.tv_sec*1000000+(int64)t_rec.ru_utime.tv_usec
125               +(int64)t_rec.ru_stime.tv_sec*1000000+(int64)t_rec.ru_stime.tv_usec
126               +(int64)5000)/(int64)10000; // unit is 1/100 sec
127  curr -= startl;
128  double f =  ((double)curr) * timer_resolution / (double)100;
129  if (f/timer_resolution > mintime)
130  {
131#ifdef EXTEND_TIMER_D
132    Print("//%s %.2f/%d sec (%d) >>%s<<\n" ,v ,f,(int)timer_resolution,iiOp,my_yylinebuf);
133#else
134    if (timer_resolution==(double)1.0)
135      Print("//%s %.2f sec\n" ,v ,f);
136    else
137      Print("//%s %.2f/%d sec\n" ,v ,f,(int)timer_resolution);
138#endif
139  }
140}
141
142/*0 Real timer implementation*/
143int rtimerv = 0;
144static struct timeval  startRl;
145static struct timeval  siStartRTime;
146static struct timezone tzp;
147void initRTimer()
148{
149#ifdef HAVE_GETTIMEOFDAY
150  gettimeofday(&startRl, &tzp);
151  gettimeofday(&siStartRTime, &tzp);
152#else
153  memset(&startRl,0,sizeof(startRl));
154  memset(&startRTime,0,sizeof(startRTime));
155#endif
156}
157
158void startRTimer()
159{
160  gettimeofday(&siStartRTime, &tzp);
161}
162
163/*2
164* returns the time since a fixed point in resolutions
165*/
166int getRTimer()
167{
168  struct timeval now;
169
170  gettimeofday(&now, &tzp);
171
172  if (startRl.tv_usec > now.tv_usec)
173  {
174    now.tv_usec += 1000000;
175    now.tv_sec --;
176  }
177
178  double f =((double)  (now.tv_sec - startRl.tv_sec))*timer_resolution +
179    ((double) (now.tv_usec - startRl.tv_usec))*timer_resolution /
180    (double) 1000000;
181
182  return (int)(f+0.5);
183}
184
185/*2
186* stops timer, writes string s and the time since last call of startTimer
187* if this time is > mintime
188*/
189void writeRTime(const char* v)
190{
191  struct timeval now;
192
193  gettimeofday(&now, &tzp);
194
195  if (siStartRTime.tv_usec > now.tv_usec)
196  {
197    now.tv_usec += 1000000;
198    now.tv_sec --;
199  }
200
201  double f =((double)  (now.tv_sec - siStartRTime.tv_sec)) +
202    ((double) (now.tv_usec - siStartRTime.tv_usec)) /
203    (double) 1000000;
204
205  if (f > mintime)
206   Print("//%s %.2f sec \n" ,v ,f);
207}
Note: See TracBrowser for help on using the repository browser.