// ms_windows_cpu_timer.hpp header file // Copyright by Yuriy Koblents-Mishke, 2007. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) // ??? // See http://www.??????? for documentation. #ifndef BOOST_MS_WINDOWS_CPU_TIMER_HPP #define BOOST_MS_WINDOWS_CPU_TIMER_HPP // MS Windows is so widespread that it deserves a native implementation #include #include // pair // ?? #include #include // BOOST_TYPEOF namespace boost { namespace detail { namespace { #include } // avoids pollution of ::boost::detail by symbols defined in windows.h struct cpu_timer_impl // implementation of cpu_timer { public: cpu_timer_impl(); // sets cpu to 0 void restart(); // resets cpu() to 0 double cpu() const; // return cpu time in seconds double cpu_max() const; // return the maximum possible value for cpu() double cpu_min() const // return the minimum possible value for cpu() { return double(1)/double(clock_per_sec_); } private: typedef std::pair< __int64, __int64 > CPUclock; // kernel, user CPUclock start_clock_; // CPU clock on the moment when timing started static const __int64 clock_per_sec_ = 10000000; // 100 nanoseconds static const CPUclock get_cpu_time(); }; // cpu_timer_impl inline const cpu_timer_impl::CPUclock cpu_timer_impl::get_cpu_time() { // Attention: // It was tested only on on Windows 2000 Pro and Windows XP Pro. // I expect it to work also on all versions of Windows Vista, // Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, // Windows XP Home and other versions of Windows XP, // Windows NT (Workstation and Server), version 3.5 and later, // and on many implementations of Windows CE, version 2.12 and later. // !!! The current implementation will not work on Windows 95 / 98 / ME !!! // Reasons: // The implementation uses function GetThreadTimes // described in Microsoft documentation available on the following websites // http://msdn2.microsoft.com/en-us/library/ms683237.aspx // http://msdn2.microsoft.com/en-us/library/ms908464.aspx // See also an useful discussion on a Microsoft forum // http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=990282&SiteID=1 // According to the Microsoft documentation, GetThreadTimes is supported // on Windows NT, starting from ver. 3.51, and its descendants // including all versions of Windows 2000, Windows XP, Windows Vista, and // Windows Server 2003. // Windows CE, strating from versions 3.0 or even 2.12, also supports // this function, but vendors can remove it from vendor-specific builds. // The function is not supported on Windows 95 / 98 / ME CPUclock current_clock; FILETIME dummy1, dummy2; GetThreadTimes( GetCurrentThread(), &dummy1, &dummy2, (FILETIME*)¤t_clock.first, (FILETIME*)¤t_clock.second ); return current_clock; } inline cpu_timer_impl::cpu_timer_impl() : start_clock_( get_cpu_time() ){} inline void cpu_timer_impl::restart() { start_clock_ = get_cpu_time(); } inline double cpu_timer_impl::cpu_max() const // return estimated maximum value for cpu() { return double( (::std::numeric_limits< __int64 >::max)() - start_clock_.first - start_clock_.second) / double(clock_per_sec_); } inline double cpu_timer_impl::cpu() const { const CPUclock current_clock = get_cpu_time(); // calculates CPU times as a difference of current ans saved CPU "clocks" // adds CPU time spent in user code and in kernel in behalf of user return double( ( current_clock.first - start_clock_.first ) + ( current_clock.second - start_clock_.second ) ) / double( clock_per_sec_ ); } }} // namespace boost::detail #endif // BOOST_MS_WINDOWS_CPU_TIMER_HPP guard