1 // boost process_cpu_clocks.cpp -----------------------------------------------------------//
3 // Copyright Beman Dawes 1994, 2006, 2008
4 // Copyright Vicente J. Botet Escriba 2009
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/chrono for documentation.
11 //--------------------------------------------------------------------------------------//
13 #include <boost/chrono/config.hpp>
14 #include <boost/chrono/process_cpu_clocks.hpp>
15 #include <boost/assert.hpp>
17 #include <sys/time.h> //for gettimeofday and timeval
18 #include <sys/times.h> //for times
25 namespace chrono_detail
28 inline long tick_factor() // multiplier to convert ticks
29 // to nanoseconds; -1 if unknown
34 if ((factor = ::sysconf(_SC_CLK_TCK)) <= 0)
38 BOOST_ASSERT(factor <= 1000000000l); // doesn't handle large ticks
39 factor = 1000000000l / factor; // compute factor
49 process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
53 clock_t c = ::times(&tm);
54 if (c == clock_t(-1)) // error
56 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
59 long factor = chrono_detail::tick_factor();
62 return time_point(nanoseconds(c * factor));
65 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
70 clock_t c = ::clock();
71 if (c == clock_t(-1)) // error
73 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
76 long factor = chrono_detail::tick_factor();
79 return time_point(nanoseconds(c * factor));
82 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
89 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
90 process_real_cpu_clock::time_point process_real_cpu_clock::now(system::error_code & ec)
95 clock_t c = ::times(&tm);
96 if (c == clock_t(-1)) // error
98 if (BOOST_CHRONO_IS_THROWS(ec))
100 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
103 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
108 long factor = chrono_detail::tick_factor();
111 if (!BOOST_CHRONO_IS_THROWS(ec))
115 return time_point(nanoseconds(c * factor));
118 if (BOOST_CHRONO_IS_THROWS(ec))
120 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
123 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
129 clock_t c = ::clock();
130 if (c == clock_t(-1)) // error
132 if (BOOST_CHRONO_IS_THROWS(ec))
134 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
137 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
142 long factor = chrono_detail::tick_factor();
145 if (!BOOST_CHRONO_IS_THROWS(ec))
149 return time_point(nanoseconds(c * factor));
152 if (BOOST_CHRONO_IS_THROWS(ec))
154 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_real_cpu_clock"));
157 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
167 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
168 process_user_cpu_clock::time_point process_user_cpu_clock::now(system::error_code & ec)
171 clock_t c = ::times(&tm);
172 if (c == clock_t(-1)) // error
174 if (BOOST_CHRONO_IS_THROWS(ec))
176 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_user_cpu_clock"));
179 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
184 long factor = chrono_detail::tick_factor();
187 if (!BOOST_CHRONO_IS_THROWS(ec))
191 return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime) * factor));
194 if (BOOST_CHRONO_IS_THROWS(ec))
196 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_user_cpu_clock"));
199 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
207 process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
210 clock_t c = ::times(&tm);
211 if (c == clock_t(-1)) // error
213 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
216 long factor = chrono_detail::tick_factor();
219 return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime)
223 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
228 process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
231 clock_t c = ::times(&tm);
232 if (c == clock_t(-1)) // error
234 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
237 long factor = chrono_detail::tick_factor();
240 return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime)
244 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
250 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
251 process_system_cpu_clock::time_point process_system_cpu_clock::now(system::error_code & ec)
254 clock_t c = ::times(&tm);
255 if (c == clock_t(-1)) // error
257 if (BOOST_CHRONO_IS_THROWS(ec))
259 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock"));
262 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
267 long factor = chrono_detail::tick_factor();
270 if (!BOOST_CHRONO_IS_THROWS(ec))
274 return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime) * factor));
277 if (BOOST_CHRONO_IS_THROWS(ec))
279 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_system_cpu_clock"));
282 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
290 process_cpu_clock::time_point process_cpu_clock::now() BOOST_NOEXCEPT
293 clock_t c = ::times(&tm);
294 if (c == clock_t(-1)) // error
296 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
299 long factor = chrono_detail::tick_factor();
303 r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
304 + tm.tms_cstime) * factor);
305 return time_point(duration(r));
308 BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
314 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
315 process_cpu_clock::time_point process_cpu_clock::now(system::error_code & ec)
319 clock_t c = ::times(&tm);
320 if (c == clock_t(-1)) // error
322 if (BOOST_CHRONO_IS_THROWS(ec))
324 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock"));
327 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);
332 long factor = chrono_detail::tick_factor();
336 r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
337 + tm.tms_cstime) * factor);
338 return time_point(duration(r));
341 if (BOOST_CHRONO_IS_THROWS(ec))
343 boost::throw_exception(system::system_error(errno, BOOST_CHRONO_SYSTEM_CATEGORY, "chrono::process_clock"));
346 ec.assign(errno, BOOST_CHRONO_SYSTEM_CATEGORY);