00001 /* ///////////////////////////////////////////////////////////////////////// 00002 * File: winstl/performance/threadtimes_counter.hpp 00003 * 00004 * Purpose: WinSTL thread-time performance counter class. 00005 * 00006 * Created: 22nd March 2002 00007 * Updated: 6th May 2010 00008 * 00009 * Home: http://stlsoft.org/ 00010 * 00011 * Copyright (c) 2002-2010, 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_THREADTIMES_COUNTER 00049 #define WINSTL_INCL_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER 00050 00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00052 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER_MAJOR 4 00053 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER_MINOR 0 00054 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER_REVISION 3 00055 # define WINSTL_VER_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER_EDIT 48 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 threadtimes_counter 00102 class threadtimes_counter 00103 { 00104 public: 00106 typedef threadtimes_counter class_type; 00107 00108 private: 00109 typedef ws_sint64_t epoch_type; 00110 public: 00114 typedef ws_sint64_t interval_type; 00115 00116 // Construction 00117 public: 00122 threadtimes_counter(); 00123 00124 // Operations 00125 public: 00129 void start(); 00133 void stop(); 00134 00135 // Attributes 00136 public: 00137 // Kernel 00138 00142 interval_type get_kernel_period_count() const; 00146 interval_type get_kernel_seconds() const; 00150 interval_type get_kernel_milliseconds() const; 00154 interval_type get_kernel_microseconds() const; 00155 00156 // User 00157 00161 interval_type get_user_period_count() const; 00165 interval_type get_user_seconds() const; 00169 interval_type get_user_milliseconds() const; 00173 interval_type get_user_microseconds() const; 00174 00175 // Total 00176 00180 interval_type get_period_count() const; 00184 interval_type get_seconds() const; 00188 interval_type get_milliseconds() const; 00192 interval_type get_microseconds() const; 00193 00194 private: // Implementation 00195 static epoch_type convert_(FILETIME const& ft); 00196 00197 // Members 00198 private: 00199 epoch_type m_kernelStart; 00200 epoch_type m_kernelEnd; 00201 epoch_type m_userStart; 00202 epoch_type m_userEnd; 00203 HANDLE m_thread; 00204 }; 00205 00207 // Unit-testing 00208 00209 #ifdef STLSOFT_UNITTEST 00210 # include "./unittest/threadtimes_counter_unittest_.h" 00211 #endif /* STLSOFT_UNITTEST */ 00212 00214 // Implementation 00215 00216 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION 00217 00218 inline /* static */ threadtimes_counter::epoch_type threadtimes_counter::convert_(FILETIME const& ft) 00219 { 00220 epoch_type r = ft.dwHighDateTime; 00221 00222 r <<= 32; 00223 00224 r += ft.dwLowDateTime; 00225 00226 return r; 00227 } 00228 00229 inline threadtimes_counter::threadtimes_counter() 00230 : m_thread(::GetCurrentThread()) 00231 { 00232 // Note that the constructor does nothing, for performance reasons. Calling 00233 // any of the Attribute methods before having gone through a start()-stop() 00234 // cycle will yield undefined results. 00235 } 00236 00237 // Operations 00238 inline void threadtimes_counter::start() 00239 { 00240 FILETIME creationTime; 00241 FILETIME exitTime; 00242 FILETIME kernelTime; 00243 FILETIME userTime; 00244 00245 if(!::GetThreadTimes(m_thread, &creationTime, &exitTime, &kernelTime, &userTime)) 00246 { 00247 m_kernelStart = 0; 00248 m_userStart = 0; 00249 00250 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT 00251 ; // TODO: throw 00252 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */ 00253 } 00254 else 00255 { 00256 m_kernelStart = convert_(kernelTime); 00257 m_userStart = convert_(userTime); 00258 } 00259 } 00260 00261 inline void threadtimes_counter::stop() 00262 { 00263 FILETIME creationTime; 00264 FILETIME exitTime; 00265 FILETIME kernelTime; 00266 FILETIME userTime; 00267 00268 if(!::GetThreadTimes(m_thread, &creationTime, &exitTime, &kernelTime, &userTime)) 00269 { 00270 m_kernelEnd = 0; 00271 m_userEnd = 0; 00272 00273 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT 00274 ; // TODO: throw 00275 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */ 00276 } 00277 else 00278 { 00279 m_kernelEnd = convert_(kernelTime); 00280 m_userEnd = convert_(userTime); 00281 } 00282 } 00283 00284 // Attributes 00285 00286 // Kernel 00287 inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_period_count() const 00288 { 00289 return static_cast<interval_type>(m_kernelEnd - m_kernelStart); 00290 } 00291 00292 inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_seconds() const 00293 { 00294 return get_kernel_period_count() / interval_type(10000000); 00295 } 00296 00297 inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_milliseconds() const 00298 { 00299 return get_kernel_period_count() / interval_type(10000); 00300 } 00301 00302 inline threadtimes_counter::interval_type threadtimes_counter::get_kernel_microseconds() const 00303 { 00304 return get_kernel_period_count() / interval_type(10); 00305 } 00306 00307 // User 00308 inline threadtimes_counter::interval_type threadtimes_counter::get_user_period_count() const 00309 { 00310 return static_cast<interval_type>(m_userEnd - m_userStart); 00311 } 00312 00313 inline threadtimes_counter::interval_type threadtimes_counter::get_user_seconds() const 00314 { 00315 return get_user_period_count() / interval_type(10000000); 00316 } 00317 00318 inline threadtimes_counter::interval_type threadtimes_counter::get_user_milliseconds() const 00319 { 00320 return get_user_period_count() / interval_type(10000); 00321 } 00322 00323 inline threadtimes_counter::interval_type threadtimes_counter::get_user_microseconds() const 00324 { 00325 return get_user_period_count() / interval_type(10); 00326 } 00327 00328 // Total 00329 inline threadtimes_counter::interval_type threadtimes_counter::get_period_count() const 00330 { 00331 return get_kernel_period_count() + get_user_period_count(); 00332 } 00333 00334 inline threadtimes_counter::interval_type threadtimes_counter::get_seconds() const 00335 { 00336 return get_period_count() / interval_type(10000000); 00337 } 00338 00339 inline threadtimes_counter::interval_type threadtimes_counter::get_milliseconds() const 00340 { 00341 return get_period_count() / interval_type(10000); 00342 } 00343 00344 inline threadtimes_counter::interval_type threadtimes_counter::get_microseconds() const 00345 { 00346 return get_period_count() / interval_type(10); 00347 } 00348 00349 #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */ 00350 00351 /* ////////////////////////////////////////////////////////////////////// */ 00352 00353 #ifndef _WINSTL_NO_NAMESPACE 00354 # if defined(_STLSOFT_NO_NAMESPACE) || \ 00355 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) 00356 } // namespace winstl 00357 # else 00358 } // namespace winstl_project 00359 } // namespace stlsoft 00360 # endif /* _STLSOFT_NO_NAMESPACE */ 00361 #endif /* !_WINSTL_NO_NAMESPACE */ 00362 00363 /* ////////////////////////////////////////////////////////////////////// */ 00364 00365 #endif /* !WINSTL_INCL_WINSTL_PERFORMANCE_HPP_THREADTIMES_COUNTER */ 00366 00367 /* ///////////////////////////// end of file //////////////////////////// */