From: Richard Kreckel Date: Fri, 4 Nov 2005 01:19:32 +0000 (+0000) Subject: * Avoid getrusage(2) on systems that don't have it (by ASheplyakov Alexei X-Git-Tag: release_1-4-0~138 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=0160f9ab1da453641e30539abcf0eaa4162582eb * Avoid getrusage(2) on systems that don't have it (by ASheplyakov Alexei and Richy Kreckel). --- diff --git a/acinclude.m4 b/acinclude.m4 index 09d71cb3..0061968f 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -100,3 +100,22 @@ else fi echo "Configuration of GiNaC $VERSION done. Now type \"make\"." fi]) + +AC_DEFUN([GINAC_HAVE_RUSAGE], +[AC_CACHE_CHECK([whether struct rusage is declared in ], +ac_cv_have_rusage, + [AC_TRY_COMPILE([#include + #include ], + [struct rusage resUsage; + getrusage(RUSAGE_SELF, &resUsage); + return 0;], + [ac_cv_have_rusage=yes], + [ac_cv_have_rusage=no]) +]) +CONFIG_RUSAGE="no" +if test "$ac_cv_have_rusage" = yes; then + CONFIG_RUSAGE="yes" + AC_DEFINE(HAVE_RUSAGE,,[define if struct rusage declared in ]) +fi +AC_SUBST(CONFIG_RUSAGE) +]) diff --git a/check/timer.cpp b/check/timer.cpp index 0b978a7d..b6c3a6b8 100644 --- a/check/timer.cpp +++ b/check/timer.cpp @@ -20,50 +20,83 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_RUSAGE #include #include +#include +#else +#include +#endif #include "timer.h" timer::timer() : on(false) { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } void timer::start() { on = true; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } void timer::stop() { on = false; +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used2); +#else + used2 = clock(); +#endif } void timer::reset() { +#ifdef HAVE_RUSAGE getrusage(RUSAGE_SELF, &used1); used2.ru_utime = used1.ru_utime; used2.ru_stime = used1.ru_stime; +#else + used1 = clock(); + used2 = used1; +#endif } double timer::read() { double elapsed; +#ifdef HAVE_RUSAGE if (running()) getrusage(RUSAGE_SELF, &used2); elapsed = ((used2.ru_utime.tv_sec - used1.ru_utime.tv_sec) + (used2.ru_stime.tv_sec - used1.ru_stime.tv_sec) + (used2.ru_utime.tv_usec - used1.ru_utime.tv_usec) * 1e-6 + (used2.ru_stime.tv_usec - used1.ru_stime.tv_usec) * 1e-6); +#else + if (running()) + used2 = clock(); + elapsed = double(used2 - used1)/CLOCKS_PER_SEC; +#endif // Results more accurate than 10ms are pointless: + return elapsed; return 0.01*int(elapsed*100+0.5); } diff --git a/check/timer.h b/check/timer.h index 2e76a6e1..48e884ac 100644 --- a/check/timer.h +++ b/check/timer.h @@ -23,7 +23,14 @@ #ifndef TIMER_H #define TIMER_H +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_RUSAGE #include +#else +#include +#endif class timer { public: @@ -35,7 +42,11 @@ public: bool running(); private: bool on; +#ifdef HAVE_RUSAGE struct rusage used1, used2; +#else + std::clock_t used1, used2; +#endif }; #endif // ndef TIMER_H diff --git a/configure.ac b/configure.ac index 638e25b3..1ca64109 100644 --- a/configure.ac +++ b/configure.ac @@ -82,6 +82,7 @@ AC_LANG([C++]) dnl Check for stuff needed for building the GiNaC interactive shell (ginsh). AC_CHECK_HEADERS(unistd.h) +GINAC_HAVE_RUSAGE AC_CHECK_HEADERS(readline/readline.h readline/history.h) if test "x${ac_cv_header_readline_readline_h}" != "xyes" -o "x${ac_cv_header_readline_history_h}" != "xyes"; then GINAC_WARNING([I could not find the headers for libreadline (needed for building ginsh).]) @@ -111,6 +112,9 @@ AC_CHECK_HEADER(typeinfo, , GINAC_ERROR([The standard header file cou AC_CHECK_HEADER(stdexcept, , GINAC_ERROR([The standard header file could not be found.])) AC_CHECK_HEADER(algorithm, , GINAC_ERROR([The standard header file could not be found.])) AC_CHECK_HEADER(limits, , GINAC_ERROR([The standard header file could not be found.])) +if test "x$CONFIG_RUSAGE" = "xno"; then + AC_CHECK_HEADER(ctime, , GINAC_ERROR([The standard header file could not be found.])) +fi dnl We need to have Bruno Haible's CLN installed. dnl (CLN versions >= 1.1.0 must have installed cln.m4 at a visible place, diff --git a/ginsh/ginsh_parser.yy b/ginsh/ginsh_parser.yy index 005ba9e9..9682b936 100644 --- a/ginsh/ginsh_parser.yy +++ b/ginsh/ginsh_parser.yy @@ -28,8 +28,11 @@ %{ #include "config.h" - +#ifdef HAVE_RUSAGE #include +#else +#include +#endif #if HAVE_UNISTD_H #include @@ -61,7 +64,23 @@ static void push(const ex &e); static ex exstack[3]; // Start and end time for the time() function +#ifdef HAVE_RUSAGE static struct rusage start_time, end_time; +#define START_TIMER getrusage(RUSAGE_SELF, &start_time); +#define STOP_TIMER getrusage(RUSAGE_SELF, &end_time); +#define PRINT_TIME_USED cout << \ + (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) + \ + (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) + \ + double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 + \ + double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6 \ + << 's' << endl; +#else +static std::clock_t start_time, end_time; +#define START_TIMER start_time = std::clock(); +#define STOP_TIMER end_time = std::clock(); +#define PRINT_TIME_USED \ + cout << double(end_time - start_time)/CLOCKS_PER_SEC << 's' << endl; +#endif // Table of functions (a multimap, because one function may appear with different // numbers of parameters) @@ -210,13 +229,7 @@ line : ';' } | T_REAL_SYMBOLS { symboltype = domain::real; } | T_COMPLEX_SYMBOLS { symboltype = domain::complex; } - | T_TIME {getrusage(RUSAGE_SELF, &start_time);} '(' exp ')' { - getrusage(RUSAGE_SELF, &end_time); - cout << (end_time.ru_utime.tv_sec - start_time.ru_utime.tv_sec) + - (end_time.ru_stime.tv_sec - start_time.ru_stime.tv_sec) + - double(end_time.ru_utime.tv_usec - start_time.ru_utime.tv_usec) / 1e6 + - double(end_time.ru_stime.tv_usec - start_time.ru_stime.tv_usec) / 1e6 << 's' << endl; - } + | T_TIME { START_TIMER } '(' exp ')' { STOP_TIMER PRINT_TIME_USED } | error ';' {yyclearin; yyerrok;} | error ':' {yyclearin; yyerrok;} ;