tests/harness/cputimer.cc

Go to the documentation of this file.
00001 
00004 /* Copyright (C) 2009 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
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     // FIXME: Fallback to just using wallclock time, which is probably only
00073     // going to be used on Microsoft Windows, where nobody has implemented
00074     // the code required to get the CPU time used by a process.
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 }

Documentation for Xapian (version 1.0.20).
Generated on 28 Apr 2010 by Doxygen 1.5.2.