source: git/kernel/timer.cc @ e4e36c

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