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