1 #include "ace/Profile_Timer.h"
3 #if !defined (__ACE_INLINE__)
4 # include "ace/Profile_Timer.inl"
5 #endif /* __ACE_INLINE__ */
7 #include "ace/Log_Category.h"
8 #include "ace/OS_NS_string.h"
10 #if defined (ACE_HAS_GETRUSAGE) && !defined (ACE_WIN32)
12 #include "ace/OS_NS_stdio.h"
13 #if defined (ACE_HAS_ALLOC_HOOKS)
14 # include "ace/Malloc_Base.h"
15 #endif /* ACE_HAS_ALLOC_HOOKS */
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 ACE_ALLOC_HOOK_DEFINE(ACE_Profile_Timer
)
22 ACE_Profile_Timer::dump () const
24 #if defined (ACE_HAS_DUMP)
25 ACE_TRACE ("ACE_Profile_Timer::dump");
26 #endif /* ACE_HAS_DUMP */
29 // Initialize interval timer.
31 ACE_Profile_Timer::ACE_Profile_Timer ()
33 ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
34 ACE_OS::memset (&this->end_usage_
, 0, sizeof this->end_usage_
);
35 ACE_OS::memset (&this->begin_usage_
, 0, sizeof this->begin_usage_
);
36 ACE_OS::memset (&this->last_usage_
, 0, sizeof this->last_usage_
);
38 # if defined (ACE_HAS_GETRUSAGE)
39 ACE_OS::memset (&this->begin_time_
, 0, sizeof this->begin_time_
);
40 ACE_OS::memset (&this->end_time_
, 0, sizeof this->end_time_
);
41 ACE_OS::memset (&this->last_time_
, 0, sizeof this->last_time_
);
42 # endif /* ACE_HAS_GETRUSAGE */
45 // Terminate the interval timer.
46 ACE_Profile_Timer::~ACE_Profile_Timer ()
48 ACE_TRACE ("ACE_Profile_Timer::~ACE_Profile_Timer");
51 // Return the resource utilization.
54 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage
&usage
)
56 ACE_TRACE ("ACE_Profile_Timer::get_rusage");
57 usage
= this->end_usage_
;
60 # if defined (ACE_HAS_GETRUSAGE)
61 // Compute the amount of resource utilization since the start time.
64 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage
&usage
)
66 ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
67 // integral shared memory size
69 this->end_usage_
.ru_ixrss
- this->last_usage_
.ru_ixrss
;
70 // integral unshared data "
72 this->end_usage_
.ru_idrss
- this->last_usage_
.ru_idrss
;
73 // integral unshared stack "
75 this->end_usage_
.ru_isrss
- this->last_usage_
.ru_isrss
;
76 // page reclaims - total vmfaults
78 this->end_usage_
.ru_minflt
- this->last_usage_
.ru_minflt
;
81 this->end_usage_
.ru_majflt
- this->last_usage_
.ru_majflt
;
84 this->end_usage_
.ru_nswap
- this->last_usage_
.ru_nswap
;
85 // block input operations
87 this->end_usage_
.ru_inblock
- this->last_usage_
.ru_inblock
;
88 // block output operations
90 this->end_usage_
.ru_oublock
- this->last_usage_
.ru_oublock
;
93 this->end_usage_
.ru_msgsnd
- this->last_usage_
.ru_msgsnd
;
96 this->end_usage_
.ru_msgrcv
- this->last_usage_
.ru_msgrcv
;
99 this->end_usage_
.ru_nsignals
- this->last_usage_
.ru_nsignals
;
100 // voluntary context switches
102 this->end_usage_
.ru_nvcsw
- this->last_usage_
.ru_nvcsw
;
103 // involuntary context switches
105 this->end_usage_
.ru_nivcsw
- this->last_usage_
.ru_nivcsw
;
106 this->subtract (usage
.ru_utime
,
107 this->end_usage_
.ru_utime
,
108 this->last_usage_
.ru_utime
);
109 this->subtract (usage
.ru_stime
,
110 this->end_usage_
.ru_stime
,
111 this->last_usage_
.ru_stime
);
115 ACE_Profile_Timer::compute_times (ACE_Elapsed_Time
&et
)
117 ACE_TRACE ("ACE_Profile_Timer::compute_times");
121 this->subtract (td
, this->end_time_
, this->begin_time_
);
122 et
.real_time
= td
.tv_sec
+ ((double) td
.tv_usec
) / ACE_ONE_SECOND_IN_USECS
;
124 this->subtract (td
, this->end_usage_
.ru_utime
, this->begin_usage_
.ru_utime
);
125 et
.user_time
= td
.tv_sec
+ ((double) td
.tv_usec
) / ACE_ONE_SECOND_IN_USECS
;
127 this->subtract (td
, this->end_usage_
.ru_stime
, this->begin_usage_
.ru_stime
);
128 et
.system_time
= td
.tv_sec
+ ((double) td
.tv_usec
) / ACE_ONE_SECOND_IN_USECS
;
131 // Determine the difference between T1 and T2.
134 ACE_Profile_Timer::subtract (timeval
&tdiff
, timeval
&t1
, timeval
&t0
)
136 ACE_TRACE ("ACE_Profile_Timer::subtract");
137 tdiff
.tv_sec
= t1
.tv_sec
- t0
.tv_sec
;
138 tdiff
.tv_usec
= t1
.tv_usec
- t0
.tv_usec
;
140 // Normalize the time.
142 while (tdiff
.tv_usec
< 0)
145 tdiff
.tv_usec
+= ACE_ONE_SECOND_IN_USECS
;
149 # endif /* ACE_HAS_GETRUSAGE */
151 // Compute the amount of time that has elapsed between start and stop.
154 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time
&et
)
156 ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
157 this->compute_times (et
);
161 ACE_END_VERSIONED_NAMESPACE_DECL
163 #elif defined (ACE_WIN32)
165 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
168 ACE_Profile_Timer::dump () const
170 #if defined (ACE_HAS_DUMP)
171 ACE_TRACE ("ACE_Profile_Timer::dump");
173 #endif /* ACE_HAS_DUMP */
176 // Initialize interval timer.
177 ACE_Profile_Timer::ACE_Profile_Timer ()
180 ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
181 # if defined (ACE_HAS_GETRUSAGE)
183 ACE_OS::memset (&this->end_usage_
, 0, sizeof this->end_usage_
);
184 ACE_OS::memset (&this->begin_usage_
, 0, sizeof this->begin_usage_
);
185 ACE_OS::memset (&this->last_usage_
, 0, sizeof this->last_usage_
);
187 ACE_OS::memset (&this->begin_time_
, 0, sizeof this->begin_time_
);
188 ACE_OS::memset (&this->end_time_
, 0, sizeof this->end_time_
);
189 ACE_OS::memset (&this->last_time_
, 0, sizeof this->last_time_
);
190 # endif /* ACE_HAS_GETRUSAGE */
194 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time
&et
)
196 ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
198 ACE_hrtime_t delta_t
; // nanoseconds
199 timer_
.elapsed_time (delta_t
);
200 et
.real_time
= (__int64
) delta_t
/ (double) ACE_ONE_SECOND_IN_NSECS
;
201 # if defined (ACE_HAS_GETRUSAGE)
202 ACE_Time_Value atv
= ACE_Time_Value (this->end_usage_
.ru_utime
)
203 - ACE_Time_Value (this->begin_usage_
.ru_utime
);
204 et
.user_time
= atv
.sec () + ((double) atv
.usec ()) / ACE_ONE_SECOND_IN_USECS
;
206 atv
= ACE_Time_Value (this->end_usage_
.ru_stime
)
207 - ACE_Time_Value (this->begin_usage_
.ru_stime
);
208 et
.system_time
= atv
.sec () + ((double) atv
.usec ()) / ACE_ONE_SECOND_IN_USECS
;
209 # else /* ACE_HAS_GETRUSAGE */
212 # endif /* ACE_HAS_GETRUSAGE */
217 // Return the resource utilization.
220 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage
&usage
)
222 ACE_TRACE ("ACE_Profile_Timer::get_rusage");
223 # if defined (ACE_HAS_GETRUSAGE)
224 usage
= this->end_usage_
;
225 # else /* ACE_HAS_GETRUSAGE */
227 # endif /* ACE_HAS_GETRUSAGE */
230 // Compute the amount of resource utilization since the start time.
233 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage
&usage
)
235 ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
237 # if defined (ACE_HAS_GETRUSAGE)
238 // Use ACE_Time_Value's as intermediate because the type of ru_utime can
239 // be multiple types and using the - operator is not safe when this are
240 // 64bit FILETIMEs on Windows
241 ACE_Time_Value
end_ru_utime (this->end_usage_
.ru_utime
);
242 ACE_Time_Value
begin_ru_utime (this->begin_usage_
.ru_utime
);
243 usage
.ru_utime
= end_ru_utime
- begin_ru_utime
;
245 ACE_Time_Value
end_ru_stime (this->end_usage_
.ru_stime
);
246 ACE_Time_Value
begin_ru_stime (this->begin_usage_
.ru_stime
);
247 usage
.ru_stime
= end_ru_stime
- begin_ru_stime
;
248 # else /* ACE_HAS_GETRUSAGE */
250 # endif /* ACE_HAS_GETRUSAGE */
253 # if defined (ACE_HAS_GETRUSAGE)
254 // Determine the difference between T1 and T2.
257 ACE_Profile_Timer::subtract (timeval
&tdiff
, timeval
&t1
, timeval
&t0
)
259 ACE_TRACE ("ACE_Profile_Timer::subtract");
260 tdiff
.tv_sec
= t1
.tv_sec
- t0
.tv_sec
;
261 tdiff
.tv_usec
= t1
.tv_usec
- t0
.tv_usec
;
263 // Normalize the time.
265 while (tdiff
.tv_usec
< 0)
268 tdiff
.tv_usec
+= ACE_ONE_SECOND_IN_USECS
;
271 # endif /* ACE_HAS_GETRUSAGE */
273 ACE_END_VERSIONED_NAMESPACE_DECL
277 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
280 ACE_Profile_Timer::dump () const
282 #if defined (ACE_HAS_DUMP)
283 ACE_TRACE ("ACE_Profile_Timer::dump");
285 #endif /* ACE_HAS_DUMP */
288 ACE_Profile_Timer::ACE_Profile_Timer ()
291 ACE_TRACE ("ACE_Profile_Timer::ACE_Profile_Timer");
295 ACE_Profile_Timer::elapsed_time (ACE_Elapsed_Time
&et
)
297 ACE_TRACE ("ACE_Profile_Timer::elapsed_time");
299 ACE_hrtime_t delta_t
; /* nanoseconds */
300 timer_
.elapsed_time (delta_t
);
302 et
.real_time
= delta_t
/ (double) ACE_ONE_SECOND_IN_NSECS
;
311 ACE_Profile_Timer::get_rusage (ACE_Profile_Timer::Rusage
&usage
)
313 ACE_TRACE ("ACE_Profile_Timer::get_rusage");
319 ACE_Profile_Timer::elapsed_rusage (ACE_Profile_Timer::Rusage
&usage
)
321 ACE_TRACE ("ACE_Profile_Timer::elapsed_rusage");
325 ACE_END_VERSIONED_NAMESPACE_DECL
327 #endif /* defined (ACE_HAS_GETRUSAGE) && !defined (ACE_WIN32) */