From 5697f6d699dbd1658c0a1c8f235e4f9c446c7490 Mon Sep 17 00:00:00 2001 From: Johann Dreo Date: Thu, 16 Dec 2010 15:50:26 +0100 Subject: [PATCH] evaluator that throw an exception if a maximum CPU user time has been reached, for POSIX systems --- eo/NEWS | 5 ++- eo/src/eo | 1 + eo/src/eoEvalUserTimeThrowException.h | 61 +++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 eo/src/eoEvalUserTimeThrowException.h diff --git a/eo/NEWS b/eo/NEWS index 50e256087..0f067e003 100644 --- a/eo/NEWS +++ b/eo/NEWS @@ -1,4 +1,7 @@ -* release 1.1 (not yet released) +* current + - evaluators that throw an exception if a maximum time has en reached (wallclock and CPU user time for POSIX systems), independently of the number of generations + +* release 1.1 - provide cmake build system, remove the old autotools one - package generation system - GCC 4.3 compatibility diff --git a/eo/src/eo b/eo/src/eo index 7c988f1c9..89ed7da98 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -77,6 +77,7 @@ #include #include #include +#include // Continuators - all include eoContinue.h #include diff --git a/eo/src/eoEvalUserTimeThrowException.h b/eo/src/eoEvalUserTimeThrowException.h new file mode 100644 index 000000000..d47c064ae --- /dev/null +++ b/eo/src/eoEvalUserTimeThrowException.h @@ -0,0 +1,61 @@ +/* +(c) Thales group, 2010 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; + version 2 of the License. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +Contact: http://eodev.sourceforge.net + +Authors: +Johann Dréo +*/ + +#include +#include + +#include + +/** Check at each evaluation if a given CPU user time contract has been reached. + * + * Throw an eoMaxTimeException if the given max time has been reached. + * Usefull if you want to end the search independently of generations. + * This class uses (almost-)POSIX headers. + * It uses a computation of the user time used on the CPU. For a wallclock time measure, see eoEvalTimeThrowException + * + * @ingroup Evaluation + */ +template< class EOT > +class eoEvalUserTimeThrowException : public eoEvalFuncCounter< EOT > +{ +public: + eoEvalUserTimeThrowException( eoEvalFunc & func, long max ) : _max(max), eoEvalFuncCounter( func, "CPU-user") {} + + virtual void operator() ( EOT & eo ) + { + if( eo.invalid() ) { + + getrusage(RUSAGE_SELF,&_usage); + + if( _usage.ru_utime.tv_sec >= _max ) { + throw eoMaxTimeException( _usage.ru_utime.tv_sec ); + } else { + func(eo); + } + } + } + +protected: + long _max; + struct rusage _usage; +};