summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_Chronometer.cxx
blob: 5e1159ab86bf63aa1a8300f49696644655fed538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// File:        OSD_Chronometer.cxx
// Created:     Mon Nov 16 15:20:41 1992
// Author:      Mireille MERCIEN
//              <mip@sdsun3>

#include <OSD_Chronometer.ixx>
#include <Standard_Stream.hxx>

// ====================== PLATFORM-SPECIFIC PART ========================

#ifndef WNT

//---------- Systemes autres que WNT : ----------------------------------

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_SYS_TIMES_H
# include <sys/times.h>
#endif

#ifdef SOLARIS
# include <sys/resource.h>
#endif

//=======================================================================
//Selon les plateformes on doit avoir le nombre de clicks par secondes
//qui est l unite de mesure du temps. 
//=======================================================================
#ifndef sysconf 
# define _sysconf sysconf
#endif

#if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
# include <time.h>
#endif

#  ifndef CLK_TCK
#   define CLK_TCK	CLOCKS_PER_SEC
#  endif

#ifdef OCE_HAVE_LIMITS
# include <limits>
#elif defined (OCE_HAVE_LIMITS_H)
# include <limits.h>
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include "gettime_osx.h"
#endif

//=======================================================================
//function : GetProcessCPU
//purpose  : 
//=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{
#if defined(LIN) || defined(linux) || defined(__FreeBSD__)
  static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
#else
  static const long aCLK_TCK = CLK_TCK;
#endif
      
  tms CurrentTMS;
  times (&CurrentTMS);

  UserSeconds   = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
  SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
}

//=======================================================================
//function : GetThreadCPU
//purpose  : 
//=======================================================================
void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{
  UserSeconds = SystemSeconds = 0.;

#if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
  // on Linux, only user times are available for threads via clock_gettime()
  struct timespec t;
  if ( ! clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t) )
  {
    UserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
  }
#elif defined(SOLARIS)
  // on Solaris, both user and system times are available as LWP times
  struct rusage rut;
  if ( ! getrusage (RUSAGE_LWP, &rut) )
  {
    UserSeconds   = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
    SystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
  }
#else
#pragma error "OS is not supported yet; code to be ported"
#endif      
}

#else

//---------------------------- Systeme WNT --------------------------------

#define STRICT
#include <windows.h>

//=======================================================================
//function : EncodeFILETIME
//purpose  : Encode time defined by FILETIME structure
//           (100s nanoseconds since January 1, 1601) to 64-bit integer
//=======================================================================
static inline __int64 EncodeFILETIME (PFILETIME pFt) 
{
  __int64 qw;

  qw   = pFt -> dwHighDateTime;
  qw <<= 32;
  qw  |= pFt -> dwLowDateTime;

  return qw;
}  

//=======================================================================
//function : GetProcessCPU
//purpose  : 
//=======================================================================
void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{
  FILETIME ftStart, ftExit, ftKernel, ftUser;
  ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
  UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
  SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
}

//=======================================================================
//function : GetThreadCPU
//purpose  : 
//=======================================================================
void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
{
  FILETIME ftStart, ftExit, ftKernel, ftUser;
  ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
  UserSeconds   = 0.0000001 * EncodeFILETIME (&ftUser);
  SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
}

#endif /* WNT */

// ====================== PLATFORM-INDEPENDENT PART ========================

//=======================================================================
//function : OSD_Chronometer
//purpose  : 
//=======================================================================
OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
{
  ThreadOnly = ThisThreadOnly;
  Start_user = Start_sys = 0.;
  Cumul_user = Cumul_sys = 0.;
  Stopped    = Standard_True; 
}

//=======================================================================
//function :  Destroy
//purpose  : 
//=======================================================================
void OSD_Chronometer::Destroy () 
{
}

//=======================================================================
//function : Reset
//purpose  : 
//=======================================================================
void OSD_Chronometer::Reset ()
{
  Stopped    = Standard_True;
  Start_user = Start_sys = 0.;
  Cumul_user = Cumul_sys = 0.;
}

//=======================================================================
//function : Stop
//purpose  : 
//=======================================================================
void OSD_Chronometer::Stop () 
{
  if ( !Stopped ) {
    Standard_Real Curr_user, Curr_sys;
    if ( ThreadOnly )
      GetThreadCPU (Curr_user, Curr_sys);
    else
      GetProcessCPU (Curr_user, Curr_sys);

    Cumul_user += Curr_user - Start_user;
    Cumul_sys  += Curr_sys  - Start_sys;

    Stopped = Standard_True; 
  } 
//  else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
}

//=======================================================================
//function : Start
//purpose  : 
//=======================================================================
void OSD_Chronometer::Start () 
{
  if ( Stopped ) {
    if ( ThreadOnly )
      GetThreadCPU (Start_user, Start_sys);
    else
      GetProcessCPU (Start_user, Start_sys);
 
    Stopped = Standard_False;
  } 
//  else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
}

//=======================================================================
//function : Show
//purpose  : 
//=======================================================================
void OSD_Chronometer::Show ()
{
  Show (cout);
}

//=======================================================================
//function : Show
//purpose  : 
//=======================================================================
void OSD_Chronometer::Show (Standard_OStream& os)
{
  Standard_Boolean StopSav = Stopped;
  if (!StopSav) Stop();
  std::streamsize prec = os.precision (12); 
  os << "CPU user time: "   << Cumul_user  << " seconds " << endl;
  os << "CPU system time: " << Cumul_sys   << " seconds " << endl;
  os.precision (prec);
  if (!StopSav) Start();
}

//=======================================================================
//function : Show
//purpose  : Returns cpu user time
//=======================================================================
void OSD_Chronometer::Show (Standard_Real& second)
{
  Standard_Boolean StopSav = Stopped;
  if (!StopSav) Stop();
  second = Cumul_user; 
  if (!StopSav) Start();
}
//=======================================================================
//function : Show
//purpose  : Returns both user and system cpu times
//=======================================================================
void OSD_Chronometer::Show (Standard_Real& user,
                            Standard_Real& system)
{
  Standard_Boolean StopSav = Stopped;
  if (!StopSav) Stop();
  user = Cumul_user; 
  system = Cumul_sys; 
  if (!StopSav) Start();
}