1 // boost process_timer.cpp -----------------------------------------------------------//
3 // Copyright Beman Dawes 1994, 2006, 2008
4 // Copyright 2009-2010 Vicente J. Botet Escriba
5 // Copyright (c) Microsoft Corporation 2014
7 // Distributed under the Boost Software License, Version 1.0.
8 // See http://www.boost.org/LICENSE_1_0.txt
10 // See http://www.boost.org/libs/chrono for documentation.
12 //--------------------------------------------------------------------------------------//
13 #ifndef BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
14 #define BOOST_CHRONO_DETAIL_INLINED_WIN_PROCESS_CLOCK_HPP
16 #include <boost/chrono/config.hpp>
17 #include <boost/chrono/process_cpu_clocks.hpp>
20 #include <boost/assert.hpp>
22 #include <boost/winapi/get_last_error.hpp>
23 #include <boost/winapi/get_current_process.hpp>
24 #if BOOST_PLAT_WINDOWS_DESKTOP
25 #include <boost/winapi/get_process_times.hpp>
33 process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
35 clock_t c = ::clock();
36 if ( c == clock_t(-1) ) // error
38 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
40 typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
42 duration(static_cast<rep>(c)*R::num/R::den)
46 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
47 process_real_cpu_clock::time_point process_real_cpu_clock::now(
48 system::error_code & ec)
50 clock_t c = ::clock();
51 if ( c == clock_t(-1) ) // error
53 boost::throw_exception(
56 ::boost::system::system_category(),
57 "chrono::process_real_cpu_clock" ));
59 if (!::boost::chrono::is_throws(ec))
63 typedef ratio_divide<giga, ratio<CLOCKS_PER_SEC> >::type R;
65 duration(static_cast<rep>(c)*R::num/R::den)
70 #if BOOST_PLAT_WINDOWS_DESKTOP
71 process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
74 // note that Windows uses 100 nanosecond ticks for FILETIME
75 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
77 if ( boost::winapi::GetProcessTimes(
78 boost::winapi::GetCurrentProcess(), &creation, &exit,
79 &system_time, &user_time ) )
81 return time_point(duration(
82 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
83 | user_time.dwLowDateTime) * 100
88 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
94 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
95 process_user_cpu_clock::time_point process_user_cpu_clock::now(
96 system::error_code & ec)
99 // note that Windows uses 100 nanosecond ticks for FILETIME
100 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
102 if ( boost::winapi::GetProcessTimes(
103 boost::winapi::GetCurrentProcess(), &creation, &exit,
104 &system_time, &user_time ) )
106 if (!::boost::chrono::is_throws(ec))
110 return time_point(duration(
111 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
112 | user_time.dwLowDateTime) * 100
117 boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
118 if (::boost::chrono::is_throws(ec))
120 boost::throw_exception(
121 system::system_error(
123 ::boost::system::system_category(),
124 "chrono::process_user_cpu_clock" ));
128 ec.assign( cause, ::boost::system::system_category() );
136 process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
139 // note that Windows uses 100 nanosecond ticks for FILETIME
140 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
142 if ( boost::winapi::GetProcessTimes(
143 boost::winapi::GetCurrentProcess(), &creation, &exit,
144 &system_time, &user_time ) )
146 return time_point(duration(
147 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
148 | system_time.dwLowDateTime) * 100
153 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
159 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
160 process_system_cpu_clock::time_point process_system_cpu_clock::now(
161 system::error_code & ec)
164 // note that Windows uses 100 nanosecond ticks for FILETIME
165 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
167 if ( boost::winapi::GetProcessTimes(
168 boost::winapi::GetCurrentProcess(), &creation, &exit,
169 &system_time, &user_time ) )
171 if (!::boost::chrono::is_throws(ec))
175 return time_point(duration(
176 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
177 | system_time.dwLowDateTime) * 100
182 boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
183 if (::boost::chrono::is_throws(ec))
185 boost::throw_exception(
186 system::system_error(
188 ::boost::system::system_category(),
189 "chrono::process_system_cpu_clock" ));
193 ec.assign( cause, ::boost::system::system_category() );
201 process_cpu_clock::time_point process_cpu_clock::now() BOOST_NOEXCEPT
204 // note that Windows uses 100 nanosecond ticks for FILETIME
205 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
207 if ( boost::winapi::GetProcessTimes(
208 boost::winapi::GetCurrentProcess(), &creation, &exit,
209 &system_time, &user_time ) )
211 time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
213 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
214 | user_time.dwLowDateTime
216 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
217 | system_time.dwLowDateTime
220 return time_point(duration(r));
224 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
230 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
231 process_cpu_clock::time_point process_cpu_clock::now(
232 system::error_code & ec )
235 // note that Windows uses 100 nanosecond ticks for FILETIME
236 boost::winapi::FILETIME_ creation, exit, user_time, system_time;
238 if ( boost::winapi::GetProcessTimes(
239 boost::winapi::GetCurrentProcess(), &creation, &exit,
240 &system_time, &user_time ) )
242 if (!::boost::chrono::is_throws(ec))
246 time_point::rep r(process_real_cpu_clock::now().time_since_epoch().count()
248 ((static_cast<process_user_cpu_clock::rep>(user_time.dwHighDateTime) << 32)
249 | user_time.dwLowDateTime
251 ((static_cast<process_system_cpu_clock::rep>(system_time.dwHighDateTime) << 32)
252 | system_time.dwLowDateTime
255 return time_point(duration(r));
259 boost::winapi::DWORD_ cause = boost::winapi::GetLastError();
260 if (::boost::chrono::is_throws(ec))
262 boost::throw_exception(
263 system::system_error(
265 ::boost::system::system_category(),
266 "chrono::process_cpu_clock" ));
270 ec.assign( cause, ::boost::system::system_category() );
278 } // namespace chrono