00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: winstl/performance/processtimes_counter.hpp 00003 * 00004 * Purpose: WinSTL process-time performance counter class. 00005 * 00006 * Created: 22nd March 2002 00007 * Updated: 10th August 2009 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2002-2009, Matthew Wilson and Synesis Software 00012 * All rights reserved. 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted provided that the following conditions are met: 00016 * 00017 * - Redistributions of source code must retain the above copyright notice, this 00018 * list of conditions and the following disclaimer. 00019 * - Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of 00023 * any contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 * 00038 * ////////////////////////////////////////////////////////////////////// */ 00039 00040 00048 #ifndef WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER 00049 #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER 00050 00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00052 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MAJOR 4 00053 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MINOR 0 00054 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_REVISION 3 00055 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_EDIT 54 00056 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00057 00058 /* ///////////////////////////////////////////////////////////////////////// 00059 * Includes 00060 */ 00061 00062 #ifndef WINSTL_INCL_WINSTL_H_WINSTL 00063 # include <winstl/winstl.h> 00064 #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */ 00065 00066 /* ///////////////////////////////////////////////////////////////////////// 00067 * Namespace 00068 */ 00069 00070 #ifndef _WINSTL_NO_NAMESPACE 00071 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00072 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00073 /* There is no stlsoft namespace, so must define ::winstl */ 00074 namespace winstl 00075 { 00076 # else 00077 /* Define stlsoft::winstl_project */ 00078 00079 namespace stlsoft 00080 { 00081 00082 namespace winstl_project 00083 { 00084 00085 # endif /* _STLSOFT_NO_NAMESPACE */ 00086 #endif /* !_WINSTL_NO_NAMESPACE */ 00087 00088 /* ///////////////////////////////////////////////////////////////////////// 00089 * Classes 00090 */ 00091 00092 // class processtimes_counter 00102 class processtimes_counter 00103 { 00104 public: 00108 typedef ws_sint64_t epoch_type; 00112 typedef ws_sint64_t interval_type; 00114 typedef processtimes_counter class_type; 00115 00116 // Construction 00117 public: 00118 processtimes_counter(); 00119 00120 // Operations 00121 public: 00125 void start(); 00129 void stop(); 00130 00131 // Attributes 00132 public: 00133 // Kernel 00134 00138 interval_type get_kernel_period_count() const; 00142 interval_type get_kernel_seconds() const; 00146 interval_type get_kernel_milliseconds() const; 00150 interval_type get_kernel_microseconds() const; 00151 00152 // User 00153 00157 interval_type get_user_period_count() const; 00161 interval_type get_user_seconds() const; 00165 interval_type get_user_milliseconds() const; 00169 interval_type get_user_microseconds() const; 00170 00171 // Total 00172 00176 interval_type get_period_count() const; 00180 interval_type get_seconds() const; 00184 interval_type get_milliseconds() const; 00188 interval_type get_microseconds() const; 00189 00190 // Implementation 00191 private: 00192 static HANDLE get_process_handle_(); 00193 00194 // Members 00195 private: 00196 epoch_type m_kernelStart; 00197 epoch_type m_kernelEnd; 00198 epoch_type m_userStart; 00199 epoch_type m_userEnd; 00200 }; 00201 00203 // Unit-testing 00204 00205 #ifdef STLSOFT_UNITTEST 00206 # include "./unittest/processtimes_counter_unittest_.h" 00207 #endif /* STLSOFT_UNITTEST */ 00208 00209 /* ////////////////////////////////////////////////////////////////////// */ 00210 00211 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00212 00213 inline /* static */ HANDLE processtimes_counter::get_process_handle_() 00214 { 00215 #if !defined(STLSOFT_STRICT) && \ 00216 defined(STLSOFT_COMPILER_IS_MSVC) && \ 00217 _MSC_VER >= 1310 00218 # pragma warning(push) 00219 # 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 */ 00220 #endif /* compiler */ 00221 00222 static HANDLE s_hProcess = ::GetCurrentProcess(); 00223 00224 #if !defined(STLSOFT_STRICT) && \ 00225 defined(STLSOFT_COMPILER_IS_MSVC) && \ 00226 _MSC_VER >= 1310 00227 # pragma warning(pop) 00228 #endif /* compiler */ 00229 00230 return s_hProcess; 00231 } 00232 00233 inline processtimes_counter::processtimes_counter() 00234 { 00235 // Note that the constructor does nothing, for performance reasons. Calling 00236 // any of the Attribute methods before having gone through a start()-stop() 00237 // cycle will yield undefined results. 00238 } 00239 00240 // Operations 00241 inline void processtimes_counter::start() 00242 { 00243 FILETIME creationTime; 00244 FILETIME exitTime; 00245 00246 ::GetProcessTimes(get_process_handle_(), &creationTime, &exitTime, reinterpret_cast<LPFILETIME>(&m_kernelStart), reinterpret_cast<LPFILETIME>(&m_userStart)); 00247 } 00248 00249 inline void processtimes_counter::stop() 00250 { 00251 FILETIME creationTime; 00252 FILETIME exitTime; 00253 00254 ::GetProcessTimes(get_process_handle_(), &creationTime, &exitTime, reinterpret_cast<LPFILETIME>(&m_kernelEnd), reinterpret_cast<LPFILETIME>(&m_userEnd)); 00255 } 00256 00257 // Kernel 00258 inline processtimes_counter::interval_type processtimes_counter::get_kernel_period_count() const 00259 { 00260 return static_cast<interval_type>(m_kernelEnd - m_kernelStart); 00261 } 00262 00263 inline processtimes_counter::interval_type processtimes_counter::get_kernel_seconds() const 00264 { 00265 return get_kernel_period_count() / interval_type(10000000); 00266 } 00267 00268 inline processtimes_counter::interval_type processtimes_counter::get_kernel_milliseconds() const 00269 { 00270 return get_kernel_period_count() / interval_type(10000); 00271 } 00272 00273 inline processtimes_counter::interval_type processtimes_counter::get_kernel_microseconds() const 00274 { 00275 return get_kernel_period_count() / interval_type(10); 00276 } 00277 00278 // User 00279 inline processtimes_counter::interval_type processtimes_counter::get_user_period_count() const 00280 { 00281 return static_cast<interval_type>(m_userEnd - m_userStart); 00282 } 00283 00284 inline processtimes_counter::interval_type processtimes_counter::get_user_seconds() const 00285 { 00286 return get_user_period_count() / interval_type(10000000); 00287 } 00288 00289 inline processtimes_counter::interval_type processtimes_counter::get_user_milliseconds() const 00290 { 00291 return get_user_period_count() / interval_type(10000); 00292 } 00293 00294 inline processtimes_counter::interval_type processtimes_counter::get_user_microseconds() const 00295 { 00296 return get_user_period_count() / interval_type(10); 00297 } 00298 00299 // Total 00300 inline processtimes_counter::interval_type processtimes_counter::get_period_count() const 00301 { 00302 return get_kernel_period_count() + get_user_period_count(); 00303 } 00304 00305 inline processtimes_counter::interval_type processtimes_counter::get_seconds() const 00306 { 00307 return get_period_count() / interval_type(10000000); 00308 } 00309 00310 inline processtimes_counter::interval_type processtimes_counter::get_milliseconds() const 00311 { 00312 return get_period_count() / interval_type(10000); 00313 } 00314 00315 inline processtimes_counter::interval_type processtimes_counter::get_microseconds() const 00316 { 00317 return get_period_count() / interval_type(10); 00318 } 00319 00320 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00321 00322 /* ////////////////////////////////////////////////////////////////////// */ 00323 00324 #ifndef _WINSTL_NO_NAMESPACE 00325 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00326 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00327 } // namespace winstl 00328 # else 00329 } // namespace winstl_project 00330 } // namespace stlsoft 00331 # endif /* _STLSOFT_NO_NAMESPACE */ 00332 #endif /* !_WINSTL_NO_NAMESPACE */ 00333 00334 /* ////////////////////////////////////////////////////////////////////// */ 00335 00336 #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER */ 00337 00338 /* ///////////////////////////// end of file //////////////////////////// */