COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
coTimer.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef _TIMER_H_
9 #define _TIMER_H_
10 
11 #ifdef _MSC_VER
12 #include <sys/timeb.h>
13 #else
14 #include <sys/time.h>
15 #endif
16 #include "coSignal.h"
17 
18 #include <iostream>
19 #include <cstdlib>
20 #include <cstring>
21 
22 // define different time measurement methods
23 #if defined(__sgi)
24 #define POSIX_TIME
25 //#define CLOCK_ID CLOCK_SGI_CYCLE
26 #define CLOCK_ID CLOCK_REALTIME
27 #endif
28 
29 namespace covise
30 {
31 
32 // +++++++ POSIX time-calls
33 #if defined(POSIX_TIME)
34 typedef struct timespec TimeType;
35 
36 // this is a macro to save time (inlined by Preprocesor)
37 #define TIME_CALL(x) clock_gettime(CLOCK_ID, x)
38 
39 inline void TIME_SET(TimeType &x, double ti)
40 {
41  x.tv_sec = (long)(ti);
42  x.tv_nsec = (long)((ti - x.tv_sec) * 1.0e9);
43 }
44 
45 inline double coTimerGetFloat(const TimeType &x)
46 {
47  return (double)x.tv_sec + 1.0e-9 * x.tv_nsec;
48 }
49 
50 // +++++++ SysV time-calls
51 #else
52 #ifdef _MSC_VER
53 
54 typedef struct __timeb64 TimeType;
55 #define TIME_CALL(x) \
56  { \
57  _ftime64(x); \
58  }
59 
60 inline void TIME_SET(TimeType &x, double ti)
61 {
62  x.time = (long)(ti);
63  x.millitm = (unsigned short)((ti - x.time) * 1.0e6f);
64 }
65 
66 inline double coTimerGetFloat(const TimeType &x)
67 {
68  return x.time + 1e-6 * x.millitm;
69 }
70 
71 #else
72 typedef struct timeval TimeType;
73 #define TIME_CALL(x) gettimeofday(x, NULL)
74 
75 inline void TIME_SET(TimeType &x, double ti)
76 {
77  x.tv_sec = (long)(ti);
78  x.tv_usec = (long)((ti - x.tv_sec) * 1.0e6);
79 }
80 
81 inline double coTimerGetFloat(const TimeType &x)
82 {
83  return x.tv_sec + 1e-6 * x.tv_usec;
84 }
85 #endif
86 #endif
87 
89 #ifndef CREATE_TIMEBASE_PROG
90 
91 #ifdef NO_TIMER
92 #else
93 #define MARK0(text) \
94  { \
95  coTimer::mark(text); \
96  }
97 #define MARK1(mask, d1) \
98  { \
99  sprintf(coTimer::mark(), mask, (d1)); \
100  }
101 #define MARK2(mask, d1, d2) \
102  { \
103  sprintf(coTimer::mark(), mask, (d1), (d2)); \
104  }
105 #define MARK3(mask, d1, d2, d3) \
106  { \
107  sprintf(coTimer::mark(), mask, (d1), (d2), (d3)); \
108  }
109 
110 #define MARK4(mask, d1, d2, d3, d4) \
111  { \
112  sprintf(coTimer::mark(), mask, (d1), (d2), (d3), (d4)); \
113  }
114 
115 #define MARK5(mask, d1, d2, d3, d4, d5) \
116  { \
117  sprintf(coTimer::mark(), mask, (d1), (d2), (d3), (d4), (d5)); \
118  }
119 
120 #define MARK6(mask, d1, d2, d3, d4, d5, d6) \
121  { \
122  sprintf(coTimer::mark(), mask, (d1), (d2), (d3), (d4), (d5), (d6)); \
123  }
124 #endif
125 
127 {
128 private:
129  enum
130  {
131  MAX_LEN = 128
132  };
133 
134  int maxElem, actElem;
135  char *descField;
136  TimeType *timeField, *actTval, startTime;
137  std::ofstream *stream;
138 
140  static char s_buffer[MAX_LEN];
141 
142  virtual void sigHandler(int sigNo);
143  virtual const char *sigHandlerName()
144  {
145  return "coTimer";
146  }
147 
148 protected:
149  coTimer(const char *fileNameBase, int length, bool handleSignals);
150 
151  char *int_mark()
152  {
153 #ifndef NOTIMER
154  if (actElem >= maxElem)
155  flush();
156  actTval++;
157  actElem++;
158  TIME_CALL(actTval);
159  return descField + MAX_LEN * actElem;
160 #else
161  static char dummy[MAX_LEN] return dummy;
162 #endif
163  }
164 
165 #ifndef NOTIMER
166  void int_mark(const char *string)
167  {
168  if (actElem >= maxElem)
169  flush();
170  actTval++;
171  actElem++;
172  TIME_CALL(actTval);
173  strcpy(descField + MAX_LEN * actElem, string);
174  }
175 #else
176  void mark(const char *)
177  {
178  }
179 #endif
180 
181  void flush();
182 
183 public:
184  virtual ~coTimer();
185 
186  static void init(const char *fileNameBase, int length, bool handleSignals = true);
187  static void quit();
188 
189  static char *mark()
190  {
191  char *res;
192  if (s_timerObj)
193  res = s_timerObj->int_mark();
194  else
195  res = s_buffer;
196  return res;
197  }
198 
199  static void mark(const char *string)
200  {
201  if (s_timerObj)
202  s_timerObj->int_mark(string);
203  }
204 };
205 #endif // getTimeBase
206 }
207 #endif
double coTimerGetFloat(const TimeType &x)
Definition: coTimer.h:81
static coTimer * s_timerObj
we create the getTimeBase program in here, too
Definition: coTimer.h:139
static Repl dummy("","")
Definition: coTimer.h:126
GLuint res
Definition: khronos-glext.h:10588
void int_mark(const char *string)
Definition: coTimer.h:166
struct timeval TimeType
Definition: coTimer.h:72
std::ofstream * stream
Definition: coTimer.h:137
void TIME_SET(TimeType &x, double ti)
Definition: coTimer.h:75
char * int_mark()
Definition: coTimer.h:151
GLint GLint GLint GLint GLint x
Definition: khronos-glext.h:6346
#define TIME_CALL(x)
Definition: coTimer.h:73
virtual const char * sigHandlerName()
Definition: coTimer.h:143
Definition: coSignal.h:102
int maxElem
Definition: coTimer.h:134
static char * mark()
Definition: coTimer.h:189
char * descField
Definition: coTimer.h:135
#define UTILEXPORT
Definition: coExport.h:182
GLenum GLuint GLenum GLsizei length
Definition: khronos-glext.h:6279
TimeType * timeField
Definition: coTimer.h:136
static void mark(const char *string)
Definition: coTimer.h:199