123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- // boost process_timer.cpp -----------------------------------------------------------//
- // Copyright Beman Dawes 1994, 2006, 2008
- // Copyright 2009-2010 Vicente J. Botet Escriba
- // Copyright (c) Microsoft Corporation 2014
- // Distributed under the Boost Software License, Version 1.0.
- // See http://www.boost.org/LICENSE_1_0.txt
- // See http://www.boost.org/libs/chrono for documentation.
- //--------------------------------------------------------------------------------------//
- #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
- #define BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
- #include <boost/chrono/config.hpp>
- #include <boost/chrono/process_cpu_clocks.hpp>
- #include <cassert>
- #include <time.h>
- #include <boost/assert.hpp>
- #include <boost/winapi/get_last_error.hpp>
- #include <boost/winapi/get_current_process.hpp>
- #if BOOST_PLAT_WINDOWS_DESKTOP
- #include <boost/winapi/get_process_times.hpp>
- #endif
- namespace boost
- {
- namespace chrono
- {
- process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
- {
- clock_t c = ::clock();
- if ( c == clock_t(-1) ) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- }
- typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
- return time_point(
- duration(static_cast<rep>(c)*R::num/R::den)
- );
- }
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_real_cpu_clock::time_point process_real_cpu_clock::now(
- system::error_code & ec)
- {
- clock_t c = ::clock();
- if ( c == clock_t(-1) ) // error
- {
- boost::throw_exception(
- system::system_error(
- errno,
- ::boost::system::system_category(),
- "chrono::process_real_cpu_clock" ));
- }
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
- return time_point(
- duration(static_cast<rep>(c)*R::num/R::den)
- );
- }
- #endif
- #if BOOST_PLAT_WINDOWS_DESKTOP
- process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- return time_point(duration(
- ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
- | user_time.dwLowDateTime) * 100
- ));
- }
- else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- return time_point();
- }
- }
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_user_cpu_clock::time_point process_user_cpu_clock::now(
- system::error_code & ec)
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(duration(
- ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
- | user_time.dwLowDateTime) * 100
- ));
- }
- else
- {
- boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(
- system::system_error(
- cause,
- ::boost::system::system_category(),
- "chrono::process_user_cpu_clock" ));
- }
- else
- {
- ec.assign( cause, ::boost::system::system_category() );
- return time_point();
- }
- }
- }
- #endif
- process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- return time_point(duration(
- ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
- | system_time.dwLowDateTime) * 100
- ));
- }
- else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- return time_point();
- }
- }
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_system_cpu_clock::time_point process_system_cpu_clock::now(
- system::error_code & ec)
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(duration(
- ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
- | system_time.dwLowDateTime) * 100
- ));
- }
- else
- {
- boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(
- system::system_error(
- cause,
- ::boost::system::system_category(),
- "chrono::process_system_cpu_clock" ));
- }
- else
- {
- ec.assign( cause, ::boost::system::system_category() );
- return time_point();
- }
- }
- }
- #endif
- process_cpu_clock::time_point process_cpu_clock::now() BOOST_NOEXCEPT
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
- ,
- ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
- | user_time.dwLowDateTime
- ) * 100,
- ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
- | system_time.dwLowDateTime
- ) * 100
- );
- return time_point(duration(r));
- }
- else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- return time_point();
- }
- }
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_cpu_clock::time_point process_cpu_clock::now(
- system::error_code & ec )
- {
- // note that Windows uses 100 nanosecond ticks for FILETIME
- boost::winapi::FILETIME_ creation, exit, user_time, system_time;
- if ( boost::winapi::GetProcessTimes(
- boost::winapi::GetCurrentProcess(), &creation, &exit,
- &system_time, &user_time ) )
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
- ,
- ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
- | user_time.dwLowDateTime
- ) * 100,
- ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
- | system_time.dwLowDateTime
- ) * 100
- );
- return time_point(duration(r));
- }
- else
- {
- boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(
- system::system_error(
- cause,
- ::boost::system::system_category(),
- "chrono::process_cpu_clock" ));
- }
- else
- {
- ec.assign( cause, ::boost::system::system_category() );
- return time_point();
- }
- }
- }
- #endif
- #endif
- } // namespace chrono
- } // namespace boost
- #endif
|