4949#include < sys/time.h>
5050#endif
5151
52+ #include " base/types.h"
53+
54+ // this must be the last file included
55+ #include " base/povdebug.h"
56+
5257#if !defined(HAVE_CLOCKID_T)
5358typedef int clockid_t ;
5459#endif
@@ -104,11 +109,11 @@ static inline bool ClockGettimeMillisec(POV_ULONG& result, clockid_t source)
104109static inline bool GetrusageMillisec (POV_ULONG& result, int source)
105110{
106111#if defined(HAVE_GETRUSAGE)
107- bool success ;
108- success = (getrusage (source, &ru) == 0 );
112+ struct rusage ru ;
113+ bool success = (getrusage (source, &ru) == 0 );
109114 if (success)
110- result = static_cast <POV_ULONG>(ru.ru_utime .tv_sec ) + static_cast <POV_ULONG>(ru.ru_stime .tv_sec )) *1000
111- + static_cast <POV_ULONG>(ru.ru_utime .tv_usec ) + static_cast <POV_ULONG>(ru.ru_stime .tv_usec )) /1000 ;
115+ result = ( static_cast <POV_ULONG>(ru.ru_utime .tv_sec ) + static_cast <POV_ULONG>(ru.ru_stime .tv_sec )) *1000
116+ + ( static_cast <POV_ULONG>(ru.ru_utime .tv_usec ) + static_cast <POV_ULONG>(ru.ru_stime .tv_usec )) /1000 ;
112117 return success;
113118#else
114119 return false ;
@@ -131,7 +136,6 @@ static inline bool GettimeofdayMillisec(POV_ULONG& result)
131136}
132137
133138Timer::Timer () :
134- mThreadTimeOnly (CPUTimeIsThreadOnly),
135139 mWallTimeUseClockGettimeMonotonic (false ),
136140 mWallTimeUseClockGettimeRealtime (false ),
137141 mWallTimeUseGettimeofday (false ),
@@ -141,17 +145,17 @@ Timer::Timer () :
141145 mThreadTimeUseGetrusageThread (false ),
142146 mThreadTimeUseGetrusageLwp (false ),
143147 mThreadTimeUseClockGettimeThread (false ),
144- mThreadTimeUseFallback (false ),
148+ mThreadTimeUseFallback (false )
145149{
146150 // Figure out which timer source to use for wall clock time.
147151 bool haveWallTime = false ;
148- #if defined(HAVE_CLOCK_MONOTONIC)
152+ #if defined(HAVE_DECL_CLOCK_MONOTONIC) && HAVE_DECL_CLOCK_MONOTONIC
149153 if (!haveWallTime)
150154 haveWallTime = mWallTimeUseClockGettimeMonotonic = ClockGettimeMillisec (mWallTimeStart , CLOCK_MONOTONIC);
151155#endif
152156 // we prefer CLOCK_MONOTONIC over CLOCK_REALTIME because the former will not be affected if someone adjusts the
153157 // system's real-time clock.
154- #if defined(HAVE_CLOCK_REALTIME)
158+ #if defined(HAVE_DECL_CLOCK_REALTIME) && HAVE_DECL_CLOCK_REALTIME
155159 if (!haveWallTime)
156160 haveWallTime = mWallTimeUseClockGettimeRealtime = ClockGettimeMillisec (mWallTimeStart , CLOCK_REALTIME);
157161#endif
@@ -168,15 +172,15 @@ Timer::Timer () :
168172
169173 // Figure out which timer source to use for per-process CPU time.
170174 bool haveProcessTime = false ;
171- #if defined(HAVE_RUSAGE_SELF)
175+ #if defined(HAVE_DECL_RUSAGE_SELF) && HAVE_DECL_RUSAGE_SELF
172176 if (!haveProcessTime)
173177 haveProcessTime = mProcessTimeUseGetrusageSelf = GetrusageMillisec (mProcessTimeStart , RUSAGE_SELF);
174178#endif
175179 // We prefer getrusage() over clock_gettime() because the latter may be inaccurate on systems
176180 // with multiple physical processors.
177- #if defined(HAVE_CLOCK_PROCESS_CPUTIME_ID)
181+ #if defined(HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID) && HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID
178182 if (!haveProcessTime)
179- haveProcessTime = mProcessTimeUseGettimeProcess = ClockGettimeMillisec (mProcessTimeStart , CLOCK_PROCESS_CPUTIME_ID);
183+ haveProcessTime = mProcessTimeUseClockGettimeProcess = ClockGettimeMillisec (mProcessTimeStart , CLOCK_PROCESS_CPUTIME_ID);
180184#endif
181185 if (!haveProcessTime)
182186 {
@@ -186,20 +190,20 @@ Timer::Timer () :
186190
187191 // Figure out which timer source to use for per-thread CPU time.
188192 bool haveThreadTime = false ;
189- #if defined(HAVE_RUSAGE_THREAD)
193+ #if defined(HAVE_DECL_RUSAGE_THREAD) && HAVE_DECL_RUSAGE_THREAD
190194 if (!haveThreadTime)
191195 haveThreadTime = mThreadTimeUseGetrusageThread = GetrusageMillisec (mThreadTimeStart , RUSAGE_THREAD);
192- #elif defined(HAVE_RUSAGE_LWP) // should be alias of RUSAGE_THREAD on systems that support both
196+ #elif defined(HAVE_DECL_RUSAGE_LWP) && HAVE_DECL_RUSAGE_LWP // should be alias of RUSAGE_THREAD on systems that support both
193197 if (!haveThreadTime)
194198 haveThreadTime = mThreadTimeUseGetrusageLwp = GetrusageMillisec (mThreadTimeStart , RUSAGE_LWP);
195199#endif
196200 // We prefer getrusage() over clock_gettime() because the latter may be inaccurate on systems
197201 // with multiple physical processors.
198- #if defined(HAVE_CLOCK_THREAD_CPUTIME_ID)
202+ #if defined(HAVE_DECL_CLOCK_THREAD_CPUTIME_ID) && HAVE_DECL_CLOCK_THREAD_CPUTIME_ID
199203 if (!haveThreadTime)
200- haveThreadTime = mThreadTimeUseGettimeThread = ClockGettimeMillisec (mThreadTimeStart , CLOCK_THREAD_CPUTIME_ID);
204+ haveThreadTime = mThreadTimeUseClockGettimeThread = ClockGettimeMillisec (mThreadTimeStart , CLOCK_THREAD_CPUTIME_ID);
201205#endif
202- if (!haveProcessTime )
206+ if (!haveThreadTime )
203207 {
204208 haveThreadTime = mThreadTimeUseFallback = haveProcessTime;
205209 mThreadTimeStart = mProcessTimeStart ;
@@ -214,11 +218,11 @@ Timer::~Timer ()
214218POV_ULONG Timer::GetWallTime () const
215219{
216220 POV_ULONG result;
217- #if defined(HAVE_CLOCK_MONOTONIC)
221+ #if defined(HAVE_DECL_CLOCK_MONOTONIC) && HAVE_DECL_CLOCK_MONOTONIC
218222 if (mWallTimeUseClockGettimeMonotonic )
219223 return (ClockGettimeMillisec (result, CLOCK_MONOTONIC) ? result : 0 );
220224#endif
221- #if defined(HAVE_CLOCK_REALTIME)
225+ #if defined(HAVE_DECL_CLOCK_REALTIME) && HAVE_DECL_CLOCK_REALTIME
222226 if (mWallTimeUseClockGettimeRealtime )
223227 return (ClockGettimeMillisec (result, CLOCK_REALTIME) ? result : 0 );
224228#endif
@@ -227,34 +231,34 @@ POV_ULONG Timer::GetWallTime () const
227231 return 0 ;
228232}
229233
230- POV_ULONG Timer::GetProcessCPUTime () const
234+ POV_ULONG Timer::GetProcessTime () const
231235{
232236 POV_ULONG result;
233- #if defined(HAVE_RUSAGE_SELF)
237+ #if defined(HAVE_DECL_RUSAGE_SELF) && HAVE_DECL_RUSAGE_SELF
234238 if (mProcessTimeUseGetrusageSelf )
235239 return (GetrusageMillisec (result, RUSAGE_SELF) ? result : 0 );
236240#endif
237- #if defined(HAVE_CLOCK_PROCESS_CPUTIME_ID)
238- if (mProcessTimeUseGettimeProcess )
241+ #if defined(HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID) && HAVE_DECL_CLOCK_PROCESS_CPUTIME_ID
242+ if (mProcessTimeUseClockGettimeProcess )
239243 return (ClockGettimeMillisec (result, CLOCK_PROCESS_CPUTIME_ID) ? result : 0 );
240244#endif
241245 if (mProcessTimeUseFallback )
242246 return GetWallTime ();
243247 return 0 ;
244248}
245249
246- POV_ULONG Timer::GetThreadCPUTime () const
250+ POV_ULONG Timer::GetThreadTime () const
247251{
248252 POV_ULONG result;
249- #if defined(HAVE_RUSAGE_THREAD)
253+ #if defined(HAVE_DECL_RUSAGE_THREAD) && HAVE_DECL_RUSAGE_THREAD
250254 if (mThreadTimeUseGetrusageThread )
251255 return (GetrusageMillisec (result, RUSAGE_THREAD) ? result : 0 );
252256#endif
253- #if defined(HAVE_RUSAGE_LWP)
257+ #if defined(HAVE_DECL_RUSAGE_LWP) && HAVE_DECL_RUSAGE_LWP
254258 if (mThreadTimeUseGetrusageLwp )
255259 return (GetrusageMillisec (result, RUSAGE_LWP) ? result : 0 );
256260#endif
257- #if defined(HAVE_CLOCK_THREAD_CPUTIME_ID)
261+ #if defined(HAVE_DECL_CLOCK_THREAD_CPUTIME_ID) && HAVE_DECL_CLOCK_THREAD_CPUTIME_ID
258262 if (mThreadTimeUseClockGettimeThread )
259263 return (ClockGettimeMillisec (result, CLOCK_THREAD_CPUTIME_ID) ? result : 0 );
260264#endif
@@ -281,8 +285,8 @@ POV_LONG Timer::ElapsedThreadCPUTime () const
281285void Timer::Reset ()
282286{
283287 mWallTimeStart = GetWallTime ();
284- mProcessTimeStart = ( mProcessTimeUseFallback ? mWallTimeStart : GetProcessTime () );
285- mThreadTimeStart = ( mThreadTimeUseFallback ? mProcessTimeStart : GetThreadTime () );
288+ mProcessTimeStart = GetProcessTime ();
289+ mThreadTimeStart = GetThreadTime ();
286290}
287291
288292bool Timer::HasValidProcessCPUTime () const
0 commit comments