123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- // boost process_cpu_clocks.cpp -----------------------------------------------------------//
- // Copyright Beman Dawes 1994, 2006, 2008
- // Copyright Vicente J. Botet Escriba 2009
- // 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.
- //--------------------------------------------------------------------------------------//
- #include <boost/chrono/config.hpp>
- #include <boost/chrono/process_cpu_clocks.hpp>
- #include <boost/assert.hpp>
- #include <sys/time.h> //for gettimeofday and timeval
- #include <sys/times.h> //for times
- # include <unistd.h>
- namespace boost
- {
- namespace chrono
- {
- namespace chrono_detail
- {
- inline long tick_factor() // multiplier to convert ticks
- // to nanoseconds; -1 if unknown
- {
- long factor = 0;
- if (!factor)
- {
- if ((factor = ::sysconf(_SC_CLK_TCK)) <= 0)
- factor = -1;
- else
- {
- BOOST_ASSERT(factor <= 1000000000l); // doesn't handle large ticks
- factor = 1000000000l / factor; // compute factor
- if (!factor)
- factor = -1;
- }
- }
- return factor;
- }
- }
- process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
- {
- #if 1
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- return time_point(nanoseconds(c * factor));
- } else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- }
- }
- return time_point();
- #else
- clock_t c = ::clock();
- if (c == clock_t(-1)) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- return time_point(nanoseconds(c * factor));
- } else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- }
- }
- return time_point();
- #endif
- }
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_real_cpu_clock::time_point process_real_cpu_clock::now(system::error_code & ec)
- {
- #if 1
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(nanoseconds(c * factor));
- } else
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- }
- }
- #else
- clock_t c = ::clock();
- if (c == clock_t(-1)) // error
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(nanoseconds(c * factor));
- } else
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- }
- }
- #endif
- }
- #endif
- #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
- process_user_cpu_clock::time_point process_user_cpu_clock::now(system::error_code & ec)
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_user_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime) * factor));
- } else
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_user_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- }
- }
- }
- #endif
- process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime)
- * factor));
- } else
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- }
- }
- return time_point();
- }
- process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime)
- * factor));
- } 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)
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_system_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- if (!::boost::chrono::is_throws(ec))
- {
- ec.clear();
- }
- return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime) * factor));
- } else
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_system_cpu_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- }
- }
- }
- #endif
- process_cpu_clock::time_point process_cpu_clock::now() BOOST_NOEXCEPT
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- time_point::rep
- r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
- + tm.tms_cstime) * factor);
- 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)
- {
- tms tm;
- clock_t c = ::times(&tm);
- if (c == clock_t(-1)) // error
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- } else
- {
- long factor = chrono_detail::tick_factor();
- if (factor != -1)
- {
- time_point::rep
- r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
- + tm.tms_cstime) * factor);
- return time_point(duration(r));
- } else
- {
- if (::boost::chrono::is_throws(ec))
- {
- boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_clock"));
- } else
- {
- ec.assign(errno, ::boost::system::system_category());
- return time_point();
- }
- }
- }
- }
- #endif
- }
- }
|