1 // boost/chrono/process_cpu_clocks.hpp -----------------------------------------------------------//
3 // Copyright 2009-2011 Vicente J. Botet Escriba
4 // Copyright (c) Microsoft Corporation 2014
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
9 // See http://www.boost.org/libs/system for documentation.
11 #ifndef BOOST_CHRONO_PROCESS_CPU_CLOCKS_HPP
12 #define BOOST_CHRONO_PROCESS_CPU_CLOCKS_HPP
14 #include <boost/chrono/config.hpp>
17 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
19 #include <boost/chrono/duration.hpp>
20 #include <boost/chrono/time_point.hpp>
21 #include <boost/operators.hpp>
22 #include <boost/chrono/detail/system.hpp>
24 #include <boost/type_traits/common_type.hpp>
25 #include <boost/chrono/clock_string.hpp>
27 #ifndef BOOST_CHRONO_HEADER_ONLY
28 #include <boost/config/abi_prefix.hpp> // must be the last #include
31 namespace boost { namespace chrono {
33 class BOOST_CHRONO_DECL process_real_cpu_clock {
35 typedef nanoseconds duration;
36 typedef duration::rep rep;
37 typedef duration::period period;
38 typedef chrono::time_point<process_real_cpu_clock> time_point;
39 BOOST_STATIC_CONSTEXPR bool is_steady = true;
41 static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
42 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
43 static BOOST_CHRONO_INLINE time_point now(system::error_code & ec );
47 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
48 class BOOST_CHRONO_DECL process_user_cpu_clock {
50 typedef nanoseconds duration;
51 typedef duration::rep rep;
52 typedef duration::period period;
53 typedef chrono::time_point<process_user_cpu_clock> time_point;
54 BOOST_STATIC_CONSTEXPR bool is_steady = true;
56 static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
57 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
58 static BOOST_CHRONO_INLINE time_point now(system::error_code & ec );
62 class BOOST_CHRONO_DECL process_system_cpu_clock {
64 typedef nanoseconds duration;
65 typedef duration::rep rep;
66 typedef duration::period period;
67 typedef chrono::time_point<process_system_cpu_clock> time_point;
68 BOOST_STATIC_CONSTEXPR bool is_steady = true;
70 static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
71 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
72 static BOOST_CHRONO_INLINE time_point now(system::error_code & ec );
77 template <typename Rep>
79 : arithmetic<process_times<Rep>,
80 multiplicative<process_times<Rep>, Rep,
81 less_than_comparable<process_times<Rep> > > >
83 //typedef process_real_cpu_clock::rep rep;
90 #if ! defined BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0
91 template <typename Rep2>
92 explicit process_times(
98 template <typename Rep2>
99 explicit process_times(
100 process_times<Rep2> const& rhs)
103 , system(rhs.system){}
112 rep real; // real (i.e wall clock) time
113 rep user; // user cpu time
114 rep system; // system cpu time
116 #if ! defined BOOST_CHRONO_DONT_PROVIDES_DEPRECATED_IO_SINCE_V2_0_0
122 template <typename Rep2>
123 bool operator==(process_times<Rep2> const& rhs) {
124 return (real==rhs.real &&
129 process_times& operator+=(
130 process_times const& rhs)
137 process_times& operator-=(
138 process_times const& rhs)
145 process_times& operator*=(
146 process_times const& rhs)
153 process_times& operator*=(rep const& rhs)
160 process_times& operator/=(process_times const& rhs)
167 process_times& operator/=(rep const& rhs)
174 bool operator<(process_times const & rhs) const
176 if (real < rhs.real) return true;
177 if (real > rhs.real) return false;
178 if (user < rhs.user) return true;
179 if (user > rhs.user) return false;
180 if (system < rhs.system) return true;
184 template <class CharT, class Traits>
185 void print(std::basic_ostream<CharT, Traits>& os) const
187 os << "{"<< real <<";"<< user <<";"<< system << "}";
190 template <class CharT, class Traits>
191 void read(std::basic_istream<CharT, Traits>& is)
193 typedef std::istreambuf_iterator<CharT, Traits> in_iterator;
196 if (i == e || *i++ != '{') // mandatory '{'
198 is.setstate(is.failbit | is.eofbit);
202 is >> real >> x >> user >> y >> system >> z;
203 if (!is.good() || (x != ';')|| (y != ';')|| (z != '}'))
205 is.setstate(is.failbit);
210 template <class Rep1, class Rep2>
212 chrono::process_times<Rep1>,
213 chrono::process_times<Rep2>
216 typedef chrono::process_times<typename common_type<Rep1, Rep2>::type> type;
219 template <class Rep1, class Rep2>
221 chrono::process_times<Rep1>,
225 typedef chrono::process_times<typename common_type<Rep1, Rep2>::type> type;
228 template <class Rep1, class Rep2>
231 chrono::process_times<Rep2>
234 typedef chrono::process_times<typename common_type<Rep1, Rep2>::type> type;
240 template <class Rep1, class Period1, class Rep2, class Period2>
241 inline BOOST_CONSTEXPR
243 operator==(const duration<process_times<Rep1>, Period1>& lhs,
244 const duration<process_times<Rep2>, Period2>& rhs)
246 return boost::chrono::detail::duration_eq<
247 duration<Rep1, Period1>, duration<Rep2, Period2>
248 >()(duration<Rep1, Period1>(lhs.count().real), duration<Rep2, Period2>(rhs.count().real));
251 template <class Rep1, class Period1, class Rep2, class Period2>
252 inline BOOST_CONSTEXPR
254 operator==(const duration<process_times<Rep1>, Period1>& lhs,
255 const duration<Rep2, Period2>& rhs)
257 return boost::chrono::detail::duration_eq<
258 duration<Rep1, Period1>, duration<Rep2, Period2> >()(duration<Rep1, Period1>(lhs.count().real), rhs);
261 template <class Rep1, class Period1, class Rep2, class Period2>
262 inline BOOST_CONSTEXPR
264 operator==(const duration<Rep1, Period1>& lhs,
265 const duration<process_times<Rep2>, Period2>& rhs)
273 template <class Rep1, class Period1, class Rep2, class Period2>
274 inline BOOST_CONSTEXPR
276 operator< (const duration<process_times<Rep1>, Period1>& lhs,
277 const duration<Rep2, Period2>& rhs)
279 return boost::chrono::detail::duration_lt<
280 duration<Rep1, Period1>, duration<Rep2, Period2> >()(duration<Rep1, Period1>(lhs.count().real), rhs);
283 template <class Rep1, class Period1, class Rep2, class Period2>
284 inline BOOST_CONSTEXPR
286 operator< (const duration<Rep1, Period1>& lhs,
287 const duration<process_times<Rep2>, Period2>& rhs)
289 return boost::chrono::detail::duration_lt<
290 duration<Rep1, Period1>, duration<Rep2, Period2> >()(lhs, duration<Rep2, Period2>(rhs.count().real));
293 template <class Rep1, class Period1, class Rep2, class Period2>
294 inline BOOST_CONSTEXPR
296 operator< (const duration<process_times<Rep1>, Period1>& lhs,
297 const duration<process_times<Rep2>, Period2>& rhs)
299 return boost::chrono::detail::duration_lt<
300 duration<Rep1, Period1>, duration<Rep2, Period2>
301 >()(duration<Rep1, Period1>(lhs.count().real), duration<Rep2, Period2>(rhs.count().real));
305 typedef process_times<nanoseconds::rep> process_cpu_clock_times;
306 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
307 class BOOST_CHRONO_DECL process_cpu_clock
311 typedef process_cpu_clock_times times;
312 typedef boost::chrono::duration<times, nano> duration;
313 typedef duration::rep rep;
314 typedef duration::period period;
315 typedef chrono::time_point<process_cpu_clock> time_point;
316 BOOST_STATIC_CONSTEXPR bool is_steady = true;
318 static BOOST_CHRONO_INLINE time_point now() BOOST_NOEXCEPT;
319 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
320 static BOOST_CHRONO_INLINE time_point now(system::error_code & ec );
325 template <class CharT, class Traits, typename Rep>
326 std::basic_ostream<CharT, Traits>&
327 operator<<(std::basic_ostream<CharT, Traits>& os,
328 process_times<Rep> const& rhs)
334 template <class CharT, class Traits, typename Rep>
335 std::basic_istream<CharT, Traits>&
336 operator>>(std::basic_istream<CharT, Traits>& is,
337 process_times<Rep>& rhs)
343 template <typename Rep>
344 struct duration_values<process_times<Rep> >
346 typedef process_times<Rep> Res;
352 static Res max BOOST_PREVENT_MACRO_SUBSTITUTION ()
354 return Res((std::numeric_limits<Rep>::max)(),
355 (std::numeric_limits<Rep>::max)(),
356 (std::numeric_limits<Rep>::max)());
358 static Res min BOOST_PREVENT_MACRO_SUBSTITUTION ()
360 return Res((std::numeric_limits<Rep>::min)(),
361 (std::numeric_limits<Rep>::min)(),
362 (std::numeric_limits<Rep>::min)());
366 template<class CharT>
367 struct clock_string<process_real_cpu_clock, CharT>
369 static std::basic_string<CharT> name()
373 { 'p', 'r', 'o', 'c', 'e', 's', 's', '_', 'r', 'e', 'a', 'l', '_', 'c', 'l', 'o', 'c', 'k' };
374 static const std::basic_string<CharT> str(u, u + sizeof(u)
378 static std::basic_string<CharT> since()
382 { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 's', 't', 'a', 'r', 't', '-', 'u', 'p' };
383 const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
388 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
389 template<class CharT>
390 struct clock_string<process_user_cpu_clock, CharT>
392 static std::basic_string<CharT> name()
396 { 'p', 'r', 'o', 'c', 'e', 's', 's', '_', 'u', 's', 'e', 'r', '_', 'c', 'l', 'o', 'c', 'k' };
397 static const std::basic_string<CharT> str(u, u + sizeof(u)
401 static std::basic_string<CharT> since()
405 { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 's', 't', 'a', 'r', 't', '-', 'u', 'p' };
406 const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
411 template<class CharT>
412 struct clock_string<process_system_cpu_clock, CharT>
414 static std::basic_string<CharT> name()
418 { 'p', 'r', 'o', 'c', 'e', 's', 's', '_', 's', 'y', 's', 't', 'e', 'm', '_', 'c', 'l', 'o', 'c', 'k' };
419 static const std::basic_string<CharT> str(u, u + sizeof(u)
423 static std::basic_string<CharT> since()
427 { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 's', 't', 'a', 'r', 't', '-', 'u', 'p' };
428 const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
433 template<class CharT>
434 struct clock_string<process_cpu_clock, CharT>
436 static std::basic_string<CharT> name()
438 static const CharT u[] =
439 { 'p', 'r', 'o', 'c', 'e', 's', 's', '_', 'c', 'l', 'o', 'c', 'k' };
440 static const std::basic_string<CharT> str(u, u + sizeof(u)
444 static std::basic_string<CharT> since()
448 { ' ', 's', 'i', 'n', 'c', 'e', ' ', 'p', 'r', 'o', 'c', 'e', 's', 's', ' ', 's', 't', 'a', 'r', 't', '-', 'u', 'p' };
449 const std::basic_string<CharT> str(u, u + sizeof(u) / sizeof(u[0]));
455 } // namespace chrono
460 template <typename Rep>
461 struct numeric_limits<boost::chrono::process_times<Rep> >
463 typedef boost::chrono::process_times<Rep> Res;
466 static const bool is_specialized = true;
467 static Res min BOOST_PREVENT_MACRO_SUBSTITUTION ()
469 return Res((std::numeric_limits<Rep>::min)(),
470 (std::numeric_limits<Rep>::min)(),
471 (std::numeric_limits<Rep>::min)());
473 static Res max BOOST_PREVENT_MACRO_SUBSTITUTION ()
475 return Res((std::numeric_limits<Rep>::max)(),
476 (std::numeric_limits<Rep>::max)(),
477 (std::numeric_limits<Rep>::max)());
479 static Res lowest() throw()
483 static const int digits = std::numeric_limits<Rep>::digits+
484 std::numeric_limits<Rep>::digits+
485 std::numeric_limits<Rep>::digits;
486 static const int digits10 = std::numeric_limits<Rep>::digits10+
487 std::numeric_limits<Rep>::digits10+
488 std::numeric_limits<Rep>::digits10;
489 static const bool is_signed = Rep::is_signed;
490 static const bool is_integer = Rep::is_integer;
491 static const bool is_exact = Rep::is_exact;
492 static const int radix = 0;
493 //~ static Res epsilon() throw() { return 0; }
494 //~ static Res round_error() throw() { return 0; }
495 //~ static const int min_exponent = 0;
496 //~ static const int min_exponent10 = 0;
497 //~ static const int max_exponent = 0;
498 //~ static const int max_exponent10 = 0;
499 //~ static const bool has_infinity = false;
500 //~ static const bool has_quiet_NaN = false;
501 //~ static const bool has_signaling_NaN = false;
502 //~ static const float_denorm_style has_denorm = denorm_absent;
503 //~ static const bool has_denorm_loss = false;
504 //~ static Res infinity() throw() { return 0; }
505 //~ static Res quiet_NaN() throw() { return 0; }
506 //~ static Res signaling_NaN() throw() { return 0; }
507 //~ static Res denorm_min() throw() { return 0; }
508 //~ static const bool is_iec559 = false;
509 //~ static const bool is_bounded = true;
510 //~ static const bool is_modulo = false;
511 //~ static const bool traps = false;
512 //~ static const bool tinyness_before = false;
513 //~ static const float_round_style round_style = round_toward_zero;
518 #ifndef BOOST_CHRONO_HEADER_ONLY
519 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
521 #include <boost/chrono/detail/inlined/process_cpu_clocks.hpp>
525 #endif // BOOST_CHRONO_PROCESS_CPU_CLOCKS_HPP