00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include "cputimer.h"
00024
00025 #include "testsuite.h"
00026
00027 #include "safeerrno.h"
00028
00029 #ifdef HAVE_GETRUSAGE
00030 # include <sys/time.h>
00031 # include <sys/resource.h>
00032 #elif defined HAVE_TIMES
00033 # include <sys/times.h>
00034 # ifdef HAVE_SYSCONF
00035 # include "safeunistd.h"
00036 # endif
00037 #elif defined HAVE_FTIME
00038 # include <sys/timeb.h>
00039 #else
00040 # include <ctime>
00041 #endif
00042
00043 #include <cstring>
00044 #include <string>
00045
00046 using namespace std;
00047
00048 double
00049 CPUTimer::get_current_cputime() const
00050 {
00051 double t = 0;
00052 #ifdef HAVE_GETRUSAGE
00053 struct rusage r;
00054 if (getrusage(RUSAGE_SELF, &r) == -1) {
00055 FAIL_TEST(string("Couldn't measure CPU for self: ") + strerror(errno));
00056 }
00057
00058 t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
00059 t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
00060 #elif defined HAVE_TIMES
00061 struct tms b;
00062 if (times(&b) == (clock_t)-1) {
00063 FAIL_TEST(string("Couldn't measure CPU: ") + strerror(errno));
00064 }
00065 t = (double)(b.tms_utime + b.tms_stime);
00066 # ifdef HAVE_SYSCONF
00067 t /= sysconf(_SC_CLK_TCK);
00068 # else
00069 t /= CLK_TCK;
00070 # endif
00071 #else
00072
00073
00074
00075 # ifdef HAVE_FTIME
00076 struct timeb tb;
00077 # ifdef FTIME_RETURNS_VOID
00078 ftime(&tb);
00079 t = tb.time + (tb.millitm * 0.001);
00080 # else
00081 if (ftime(&tb) == -1) {
00082 t = time(NULL);
00083 } else {
00084 t = tb.time + (tb.millitm * 0.001);
00085 }
00086 # endif
00087 # else
00088 t = time(NULL);
00089 # endif
00090 #endif
00091
00092 return t;
00093 }