* Avoid getrusage(2) on systems that don't have it (by ASheplyakov Alexei
authorRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Fri, 4 Nov 2005 01:19:32 +0000 (01:19 +0000)
committerRichard Kreckel <Richard.Kreckel@uni-mainz.de>
Fri, 4 Nov 2005 01:19:32 +0000 (01:19 +0000)
  and Richy Kreckel).

acinclude.m4
check/timer.cpp
check/timer.h
configure.ac
ginsh/ginsh_parser.yy

index 09d71cb..0061968 100644 (file)
@@ -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 <sys/resource.h>],
+ac_cv_have_rusage,
+ [AC_TRY_COMPILE([#include <sys/times.h>
+                  #include <sys/resource.h>],
+                  [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 <sys/resource.h>])
+fi
+AC_SUBST(CONFIG_RUSAGE)
+])
index 0b978a7..b6c3a6b 100644 (file)
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <sys/time.h>
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#ifdef HAVE_RUSAGE
 #include <sys/resource.h>
 #include <unistd.h>
+#include <sys/time.h>
+#else
+#include <ctime>
+#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);
 }
 
index 2e76a6e..48e884a 100644 (file)
 #ifndef TIMER_H
 #define TIMER_H
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#ifdef HAVE_RUSAGE
 #include <sys/resource.h>
+#else
+#include <ctime>
+#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
index 638e25b..1ca6410 100644 (file)
@@ -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 <typeinfo> header file cou
 AC_CHECK_HEADER(stdexcept, , GINAC_ERROR([The standard <stdexcept> header file could not be found.]))
 AC_CHECK_HEADER(algorithm, , GINAC_ERROR([The standard <algorithm> header file could not be found.]))
 AC_CHECK_HEADER(limits, , GINAC_ERROR([The standard <limits> header file could not be found.]))
+if test "x$CONFIG_RUSAGE" = "xno"; then
+    AC_CHECK_HEADER(ctime, , GINAC_ERROR([The standard <ctime> 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,
index 005ba9e..9682b93 100644 (file)
 
 %{
 #include "config.h"
-
+#ifdef HAVE_RUSAGE
 #include <sys/resource.h>
+#else
+#include <ctime>
+#endif
 
 #if HAVE_UNISTD_H
 #include <sys/types.h>
@@ -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;}
        ;