Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:46

0001 //  boost process_cpu_clocks.cpp  -----------------------------------------------------------//
0002 
0003 //  Copyright Beman Dawes 1994, 2006, 2008
0004 //  Copyright Vicente J. Botet Escriba 2009
0005 
0006 //  Distributed under the Boost Software License, Version 1.0.
0007 //  See http://www.boost.org/LICENSE_1_0.txt
0008 
0009 //  See http://www.boost.org/libs/chrono for documentation.
0010 
0011 //--------------------------------------------------------------------------------------//
0012 
0013 #include <boost/chrono/config.hpp>
0014 #include <boost/chrono/process_cpu_clocks.hpp>
0015 #include <boost/assert.hpp>
0016 
0017 #include <sys/time.h> //for gettimeofday and timeval
0018 #include <sys/times.h> //for times
0019 # include <unistd.h>
0020 
0021 namespace boost
0022 {
0023   namespace chrono
0024   {
0025     namespace chrono_detail
0026     {
0027 
0028       inline long tick_factor() // multiplier to convert ticks
0029       //  to nanoseconds; -1 if unknown
0030       {
0031         long factor = 0;
0032         if (!factor)
0033         {
0034           if ((factor = ::sysconf(_SC_CLK_TCK)) <= 0)
0035             factor = -1;
0036           else
0037           {
0038             BOOST_ASSERT(factor <= 1000000000l); // doesn't handle large ticks
0039             factor = 1000000000l / factor; // compute factor
0040             if (!factor)
0041               factor = -1;
0042           }
0043         }
0044         return factor;
0045       }
0046     }
0047 
0048 
0049     process_real_cpu_clock::time_point process_real_cpu_clock::now() BOOST_NOEXCEPT
0050     {
0051 #if 1
0052       tms tm;
0053       clock_t c = ::times(&tm);
0054       if (c == clock_t(-1)) // error
0055       {
0056         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0057       } else
0058       {
0059         long factor = chrono_detail::tick_factor();
0060         if (factor != -1)
0061         {
0062           return time_point(nanoseconds(c * factor));
0063         } else
0064         {
0065           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0066         }
0067       }
0068       return time_point();
0069 #else
0070       clock_t c = ::clock();
0071       if (c == clock_t(-1)) // error
0072       {
0073         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0074       } else
0075       {
0076         long factor = chrono_detail::tick_factor();
0077         if (factor != -1)
0078         {
0079           return time_point(nanoseconds(c * factor));
0080         } else
0081         {
0082           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0083         }
0084       }
0085       return time_point();
0086 #endif
0087     }
0088 
0089 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0090     process_real_cpu_clock::time_point process_real_cpu_clock::now(system::error_code & ec)
0091     {
0092 
0093 #if 1
0094       tms tm;
0095       clock_t c = ::times(&tm);
0096       if (c == clock_t(-1)) // error
0097       {
0098         if (::boost::chrono::is_throws(ec))
0099         {
0100           boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
0101         } else
0102         {
0103           ec.assign(errno, ::boost::system::system_category());
0104           return time_point();
0105         }
0106       } else
0107       {
0108         long factor = chrono_detail::tick_factor();
0109         if (factor != -1)
0110         {
0111           if (!::boost::chrono::is_throws(ec))
0112           {
0113             ec.clear();
0114           }
0115           return time_point(nanoseconds(c * factor));
0116         } else
0117         {
0118           if (::boost::chrono::is_throws(ec))
0119           {
0120             boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
0121           } else
0122           {
0123             ec.assign(errno, ::boost::system::system_category());
0124             return time_point();
0125           }
0126         }
0127       }
0128 #else
0129       clock_t c = ::clock();
0130       if (c == clock_t(-1)) // error
0131       {
0132         if (::boost::chrono::is_throws(ec))
0133         {
0134           boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
0135         } else
0136         {
0137           ec.assign(errno, ::boost::system::system_category());
0138           return time_point();
0139         }
0140       } else
0141       {
0142         long factor = chrono_detail::tick_factor();
0143         if (factor != -1)
0144         {
0145           if (!::boost::chrono::is_throws(ec))
0146           {
0147             ec.clear();
0148           }
0149           return time_point(nanoseconds(c * factor));
0150         } else
0151         {
0152           if (::boost::chrono::is_throws(ec))
0153           {
0154             boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_real_cpu_clock"));
0155           } else
0156           {
0157             ec.assign(errno, ::boost::system::system_category());
0158             return time_point();
0159           }
0160         }
0161       }
0162 #endif
0163 
0164     }
0165 #endif
0166 
0167 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0168     process_user_cpu_clock::time_point process_user_cpu_clock::now(system::error_code & ec)
0169     {
0170       tms tm;
0171       clock_t c = ::times(&tm);
0172       if (c == clock_t(-1)) // error
0173       {
0174         if (::boost::chrono::is_throws(ec))
0175         {
0176           boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_user_cpu_clock"));
0177         } else
0178         {
0179           ec.assign(errno, ::boost::system::system_category());
0180           return time_point();
0181         }
0182       } else
0183       {
0184         long factor = chrono_detail::tick_factor();
0185         if (factor != -1)
0186         {
0187           if (!::boost::chrono::is_throws(ec))
0188           {
0189             ec.clear();
0190           }
0191           return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime) * factor));
0192         } else
0193         {
0194           if (::boost::chrono::is_throws(ec))
0195           {
0196             boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_user_cpu_clock"));
0197           } else
0198           {
0199             ec.assign(errno, ::boost::system::system_category());
0200             return time_point();
0201           }
0202         }
0203       }
0204     }
0205 #endif
0206 
0207     process_user_cpu_clock::time_point process_user_cpu_clock::now() BOOST_NOEXCEPT
0208     {
0209       tms tm;
0210       clock_t c = ::times(&tm);
0211       if (c == clock_t(-1)) // error
0212       {
0213         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0214       } else
0215       {
0216         long factor = chrono_detail::tick_factor();
0217         if (factor != -1)
0218         {
0219           return time_point(nanoseconds((tm.tms_utime + tm.tms_cutime)
0220               * factor));
0221         } else
0222         {
0223           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0224         }
0225       }
0226       return time_point();
0227     }
0228     process_system_cpu_clock::time_point process_system_cpu_clock::now() BOOST_NOEXCEPT
0229     {
0230       tms tm;
0231       clock_t c = ::times(&tm);
0232       if (c == clock_t(-1)) // error
0233       {
0234         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0235       } else
0236       {
0237         long factor = chrono_detail::tick_factor();
0238         if (factor != -1)
0239         {
0240           return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime)
0241               * factor));
0242         } else
0243         {
0244           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0245         }
0246       }
0247       return time_point();
0248     }
0249 
0250 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0251     process_system_cpu_clock::time_point process_system_cpu_clock::now(system::error_code & ec)
0252     {
0253       tms tm;
0254       clock_t c = ::times(&tm);
0255       if (c == clock_t(-1)) // error
0256       {
0257         if (::boost::chrono::is_throws(ec))
0258         {
0259           boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_system_cpu_clock"));
0260         } else
0261         {
0262           ec.assign(errno, ::boost::system::system_category());
0263           return time_point();
0264         }
0265       } else
0266       {
0267         long factor = chrono_detail::tick_factor();
0268         if (factor != -1)
0269         {
0270           if (!::boost::chrono::is_throws(ec))
0271           {
0272             ec.clear();
0273           }
0274           return time_point(nanoseconds((tm.tms_stime + tm.tms_cstime) * factor));
0275         } else
0276         {
0277           if (::boost::chrono::is_throws(ec))
0278           {
0279             boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_system_cpu_clock"));
0280           } else
0281           {
0282             ec.assign(errno, ::boost::system::system_category());
0283             return time_point();
0284           }
0285         }
0286       }
0287     }
0288 #endif
0289 
0290     process_cpu_clock::time_point process_cpu_clock::now() BOOST_NOEXCEPT
0291     {
0292       tms tm;
0293       clock_t c = ::times(&tm);
0294       if (c == clock_t(-1)) // error
0295       {
0296         BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0297       } else
0298       {
0299         long factor = chrono_detail::tick_factor();
0300         if (factor != -1)
0301         {
0302           time_point::rep
0303               r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
0304                   + tm.tms_cstime) * factor);
0305           return time_point(duration(r));
0306         } else
0307         {
0308           BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
0309         }
0310       }
0311       return time_point();
0312     }
0313 
0314 #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
0315     process_cpu_clock::time_point process_cpu_clock::now(system::error_code & ec)
0316     {
0317 
0318       tms tm;
0319       clock_t c = ::times(&tm);
0320       if (c == clock_t(-1)) // error
0321       {
0322         if (::boost::chrono::is_throws(ec))
0323         {
0324           boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_clock"));
0325         } else
0326         {
0327           ec.assign(errno, ::boost::system::system_category());
0328           return time_point();
0329         }
0330       } else
0331       {
0332         long factor = chrono_detail::tick_factor();
0333         if (factor != -1)
0334         {
0335           time_point::rep
0336               r(c * factor, (tm.tms_utime + tm.tms_cutime) * factor, (tm.tms_stime
0337                   + tm.tms_cstime) * factor);
0338           return time_point(duration(r));
0339         } else
0340         {
0341           if (::boost::chrono::is_throws(ec))
0342           {
0343             boost::throw_exception(system::system_error(errno, ::boost::system::system_category(), "chrono::process_clock"));
0344           } else
0345           {
0346             ec.assign(errno, ::boost::system::system_category());
0347             return time_point();
0348           }
0349         }
0350       }
0351 
0352     }
0353 #endif
0354 
0355   }
0356 }