You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
12 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/performance/processtimes_counter.hpp
  3. *
  4. * Purpose: WinSTL process-time performance counter class.
  5. *
  6. * Created: 22nd March 2002
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/performance/processtimes_counter.hpp
  40. *
  41. * \brief [C++ only] Definition of the
  42. * \link winstl::processtimes_counter processtimes_counter\endlink class
  43. * (\ref group__library__performance "Performance" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER
  46. #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MAJOR 4
  49. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MINOR 0
  50. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_REVISION 3
  51. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_EDIT 54
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  57. # include <winstl/winstl.h>
  58. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  59. /* /////////////////////////////////////////////////////////////////////////
  60. * Namespace
  61. */
  62. #ifndef _WINSTL_NO_NAMESPACE
  63. # if defined(_STLSOFT_NO_NAMESPACE) || \
  64. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  65. /* There is no stlsoft namespace, so must define ::winstl */
  66. namespace winstl
  67. {
  68. # else
  69. /* Define stlsoft::winstl_project */
  70. namespace stlsoft
  71. {
  72. namespace winstl_project
  73. {
  74. # endif /* _STLSOFT_NO_NAMESPACE */
  75. #endif /* !_WINSTL_NO_NAMESPACE */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Classes
  78. */
  79. // class processtimes_counter
  80. /** \brief A performance counter that provides process-specific performance timings
  81. *
  82. * \ingroup group__library__performance
  83. *
  84. * This class uses the operating system's performance monitoring facilities to provide timing
  85. * information pertaining to the calling process only, irrespective of the activities of other
  86. * processes on the system. This class does not provide meaningful timing information on operating
  87. * systems that do not provide process-specific monitoring.
  88. */
  89. class processtimes_counter
  90. {
  91. public:
  92. /// \brief The epoch type
  93. ///
  94. /// The type of the interval measurement, a 64-bit signed integer.
  95. typedef ws_sint64_t epoch_type;
  96. /// \brief The interval type
  97. ///
  98. /// The type of the interval measurement, a 64-bit signed integer.
  99. typedef ws_sint64_t interval_type;
  100. /// \brief The class type.
  101. typedef processtimes_counter class_type;
  102. // Construction
  103. public:
  104. processtimes_counter();
  105. // Operations
  106. public:
  107. /// \brief Starts measurement
  108. ///
  109. /// Begins the measurement period
  110. void start();
  111. /// \brief Ends measurement
  112. ///
  113. /// Ends the measurement period
  114. void stop();
  115. // Attributes
  116. public:
  117. // Kernel
  118. /// \brief The elapsed count in the measurement period for kernel mode activity
  119. ///
  120. /// This represents the extent, in machine-specific increments, of the measurement period for kernel mode activity
  121. interval_type get_kernel_period_count() const;
  122. /// \brief The number of whole seconds in the measurement period for kernel mode activity
  123. ///
  124. /// This represents the extent, in whole seconds, of the measurement period for kernel mode activity
  125. interval_type get_kernel_seconds() const;
  126. /// \brief The number of whole milliseconds in the measurement period for kernel mode activity
  127. ///
  128. /// This represents the extent, in whole milliseconds, of the measurement period for kernel mode activity
  129. interval_type get_kernel_milliseconds() const;
  130. /// \brief The number of whole microseconds in the measurement period for kernel mode activity
  131. ///
  132. /// This represents the extent, in whole microseconds, of the measurement period for kernel mode activity
  133. interval_type get_kernel_microseconds() const;
  134. // User
  135. /// \brief The elapsed count in the measurement period for user mode activity
  136. ///
  137. /// This represents the extent, in machine-specific increments, of the measurement period for user mode activity
  138. interval_type get_user_period_count() const;
  139. /// \brief The number of whole seconds in the measurement period for user mode activity
  140. ///
  141. /// This represents the extent, in whole seconds, of the measurement period for user mode activity
  142. interval_type get_user_seconds() const;
  143. /// \brief The number of whole milliseconds in the measurement period for user mode activity
  144. ///
  145. /// This represents the extent, in whole milliseconds, of the measurement period for user mode activity
  146. interval_type get_user_milliseconds() const;
  147. /// \brief The number of whole microseconds in the measurement period for user mode activity
  148. ///
  149. /// This represents the extent, in whole microseconds, of the measurement period for user mode activity
  150. interval_type get_user_microseconds() const;
  151. // Total
  152. /// \brief The elapsed count in the measurement period
  153. ///
  154. /// This represents the extent, in machine-specific increments, of the measurement period
  155. interval_type get_period_count() const;
  156. /// \brief The number of whole seconds in the measurement period
  157. ///
  158. /// This represents the extent, in whole seconds, of the measurement period
  159. interval_type get_seconds() const;
  160. /// \brief The number of whole milliseconds in the measurement period
  161. ///
  162. /// This represents the extent, in whole milliseconds, of the measurement period
  163. interval_type get_milliseconds() const;
  164. /// \brief The number of whole microseconds in the measurement period
  165. ///
  166. /// This represents the extent, in whole microseconds, of the measurement period
  167. interval_type get_microseconds() const;
  168. // Implementation
  169. private:
  170. static HANDLE get_process_handle_();
  171. // Members
  172. private:
  173. epoch_type m_kernelStart;
  174. epoch_type m_kernelEnd;
  175. epoch_type m_userStart;
  176. epoch_type m_userEnd;
  177. };
  178. ////////////////////////////////////////////////////////////////////////////
  179. // Unit-testing
  180. #ifdef STLSOFT_UNITTEST
  181. # include "./unittest/processtimes_counter_unittest_.h"
  182. #endif /* STLSOFT_UNITTEST */
  183. /* ////////////////////////////////////////////////////////////////////// */
  184. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  185. inline /* static */ HANDLE processtimes_counter::get_process_handle_()
  186. {
  187. #if !defined(STLSOFT_STRICT) && \
  188. defined(STLSOFT_COMPILER_IS_MSVC) && \
  189. _MSC_VER >= 1310
  190. # pragma warning(push)
  191. # pragma warning(disable : 4640) /* "construction of local static object is not thread-safe" - since it is here! (As long as one uses a 'conformant' allocator) - maybe use a spin_mutex in future */
  192. #endif /* compiler */
  193. static HANDLE s_hProcess = ::GetCurrentProcess();
  194. #if !defined(STLSOFT_STRICT) && \
  195. defined(STLSOFT_COMPILER_IS_MSVC) && \
  196. _MSC_VER >= 1310
  197. # pragma warning(pop)
  198. #endif /* compiler */
  199. return s_hProcess;
  200. }
  201. inline processtimes_counter::processtimes_counter()
  202. {
  203. // Note that the constructor does nothing, for performance reasons. Calling
  204. // any of the Attribute methods before having gone through a start()-stop()
  205. // cycle will yield undefined results.
  206. }
  207. // Operations
  208. inline void processtimes_counter::start()
  209. {
  210. FILETIME creationTime;
  211. FILETIME exitTime;
  212. ::GetProcessTimes(get_process_handle_(), &creationTime, &exitTime, reinterpret_cast<LPFILETIME>(&m_kernelStart), reinterpret_cast<LPFILETIME>(&m_userStart));
  213. }
  214. inline void processtimes_counter::stop()
  215. {
  216. FILETIME creationTime;
  217. FILETIME exitTime;
  218. ::GetProcessTimes(get_process_handle_(), &creationTime, &exitTime, reinterpret_cast<LPFILETIME>(&m_kernelEnd), reinterpret_cast<LPFILETIME>(&m_userEnd));
  219. }
  220. // Kernel
  221. inline processtimes_counter::interval_type processtimes_counter::get_kernel_period_count() const
  222. {
  223. return static_cast<interval_type>(m_kernelEnd - m_kernelStart);
  224. }
  225. inline processtimes_counter::interval_type processtimes_counter::get_kernel_seconds() const
  226. {
  227. return get_kernel_period_count() / interval_type(10000000);
  228. }
  229. inline processtimes_counter::interval_type processtimes_counter::get_kernel_milliseconds() const
  230. {
  231. return get_kernel_period_count() / interval_type(10000);
  232. }
  233. inline processtimes_counter::interval_type processtimes_counter::get_kernel_microseconds() const
  234. {
  235. return get_kernel_period_count() / interval_type(10);
  236. }
  237. // User
  238. inline processtimes_counter::interval_type processtimes_counter::get_user_period_count() const
  239. {
  240. return static_cast<interval_type>(m_userEnd - m_userStart);
  241. }
  242. inline processtimes_counter::interval_type processtimes_counter::get_user_seconds() const
  243. {
  244. return get_user_period_count() / interval_type(10000000);
  245. }
  246. inline processtimes_counter::interval_type processtimes_counter::get_user_milliseconds() const
  247. {
  248. return get_user_period_count() / interval_type(10000);
  249. }
  250. inline processtimes_counter::interval_type processtimes_counter::get_user_microseconds() const
  251. {
  252. return get_user_period_count() / interval_type(10);
  253. }
  254. // Total
  255. inline processtimes_counter::interval_type processtimes_counter::get_period_count() const
  256. {
  257. return get_kernel_period_count() + get_user_period_count();
  258. }
  259. inline processtimes_counter::interval_type processtimes_counter::get_seconds() const
  260. {
  261. return get_period_count() / interval_type(10000000);
  262. }
  263. inline processtimes_counter::interval_type processtimes_counter::get_milliseconds() const
  264. {
  265. return get_period_count() / interval_type(10000);
  266. }
  267. inline processtimes_counter::interval_type processtimes_counter::get_microseconds() const
  268. {
  269. return get_period_count() / interval_type(10);
  270. }
  271. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  272. /* ////////////////////////////////////////////////////////////////////// */
  273. #ifndef _WINSTL_NO_NAMESPACE
  274. # if defined(_STLSOFT_NO_NAMESPACE) || \
  275. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  276. } // namespace winstl
  277. # else
  278. } // namespace winstl_project
  279. } // namespace stlsoft
  280. # endif /* _STLSOFT_NO_NAMESPACE */
  281. #endif /* !_WINSTL_NO_NAMESPACE */
  282. /* ////////////////////////////////////////////////////////////////////// */
  283. #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER */
  284. /* ///////////////////////////// end of file //////////////////////////// */