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.

228 lines
7.3 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/performance/multimedia_counter.hpp
  3. *
  4. * Purpose: WinSTL multimedia performance counter class.
  5. *
  6. * Created: 31st July 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/multimedia_counter.hpp
  40. *
  41. * \brief [C++ only] Definition of the
  42. * \link winstl::multimedia_counter multimedia_counter\endlink class
  43. * (\ref group__library__performance "Performance" Library).
  44. */
  45. #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER
  46. #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER_MAJOR 4
  49. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER_MINOR 0
  50. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER_REVISION 3
  51. # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER_EDIT 42
  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. #ifndef STLSOFT_INCL_H_MMSYSTEM
  60. # define STLSOFT_INCL_H_MMSYSTEM
  61. # include <mmsystem.h> // Windows MultiMedia API
  62. #endif /* !STLSOFT_INCL_H_MMSYSTEM */
  63. /* /////////////////////////////////////////////////////////////////////////
  64. * Namespace
  65. */
  66. #ifndef _WINSTL_NO_NAMESPACE
  67. # if defined(_STLSOFT_NO_NAMESPACE) || \
  68. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  69. /* There is no stlsoft namespace, so must define ::winstl */
  70. namespace winstl
  71. {
  72. # else
  73. /* Define stlsoft::winstl_project */
  74. namespace stlsoft
  75. {
  76. namespace winstl_project
  77. {
  78. # endif /* _STLSOFT_NO_NAMESPACE */
  79. #endif /* !_WINSTL_NO_NAMESPACE */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Classes
  82. */
  83. // class multimedia_counter
  84. /** \brief A low-cost, low-resolution performance counter
  85. *
  86. * \ingroup group__library__performance
  87. *
  88. * This class provides low-resolution, but low-latency, performance monitoring
  89. * based on the multimedia timer.
  90. */
  91. class multimedia_counter
  92. {
  93. public:
  94. /// This type
  95. typedef multimedia_counter class_type;
  96. private:
  97. typedef ws_sint64_t epoch_type;
  98. public:
  99. /// \brief The interval type
  100. ///
  101. /// The type of the interval measurement, a 64-bit signed integer
  102. typedef ws_sint64_t interval_type;
  103. // Construction
  104. public:
  105. multimedia_counter();
  106. // Operations
  107. public:
  108. /// \brief Starts measurement
  109. ///
  110. /// Begins the measurement period
  111. void start();
  112. /// \brief Ends measurement
  113. ///
  114. /// Ends the measurement period
  115. void stop();
  116. // Attributes
  117. public:
  118. /// \brief The elapsed count in the measurement period
  119. ///
  120. /// This represents the extent, in machine-specific increments, of the measurement period
  121. interval_type get_period_count() const;
  122. /// \brief The number of whole seconds in the measurement period
  123. ///
  124. /// This represents the extent, in whole seconds, of the measurement period
  125. interval_type get_seconds() const;
  126. /// \brief The number of whole milliseconds in the measurement period
  127. ///
  128. /// This represents the extent, in whole milliseconds, of the measurement period
  129. interval_type get_milliseconds() const;
  130. /// \brief The number of whole microseconds in the measurement period
  131. ///
  132. /// This represents the extent, in whole microseconds, of the measurement period
  133. interval_type get_microseconds() const;
  134. // Members
  135. private:
  136. ws_dword_t m_start; // start of measurement period
  137. ws_dword_t m_end; // End of measurement period
  138. };
  139. ////////////////////////////////////////////////////////////////////////////
  140. // Unit-testing
  141. #ifdef STLSOFT_UNITTEST
  142. # include "./unittest/multimedia_counter_unittest_.h"
  143. #endif /* STLSOFT_UNITTEST */
  144. ////////////////////////////////////////////////////////////////////////////
  145. // Implementation
  146. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  147. inline multimedia_counter::multimedia_counter()
  148. {
  149. // Note that the constructor does nothing, for performance reasons. Calling
  150. // any of the Attribute methods before having gone through a start()-stop()
  151. // cycle will yield undefined results.
  152. }
  153. // Operations
  154. inline void multimedia_counter::start()
  155. {
  156. m_start = ::timeGetTime();
  157. }
  158. inline void multimedia_counter::stop()
  159. {
  160. m_end = ::timeGetTime();
  161. }
  162. // Attributes
  163. inline multimedia_counter::interval_type multimedia_counter::get_period_count() const
  164. {
  165. return static_cast<interval_type>(m_end - m_start);
  166. }
  167. inline multimedia_counter::interval_type multimedia_counter::get_seconds() const
  168. {
  169. return get_period_count() / interval_type(1000);
  170. }
  171. inline multimedia_counter::interval_type multimedia_counter::get_milliseconds() const
  172. {
  173. return get_period_count();
  174. }
  175. inline multimedia_counter::interval_type multimedia_counter::get_microseconds() const
  176. {
  177. return get_period_count() * interval_type(1000);
  178. }
  179. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  180. /* ////////////////////////////////////////////////////////////////////// */
  181. #ifndef _WINSTL_NO_NAMESPACE
  182. # if defined(_STLSOFT_NO_NAMESPACE) || \
  183. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  184. } // namespace winstl
  185. # else
  186. } // namespace winstl_project
  187. } // namespace stlsoft
  188. # endif /* _STLSOFT_NO_NAMESPACE */
  189. #endif /* !_WINSTL_NO_NAMESPACE */
  190. /* ////////////////////////////////////////////////////////////////////// */
  191. #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_MULTIMEDIA_COUNTER */
  192. /* ///////////////////////////// end of file //////////////////////////// */