xapian-core  1.4.26
cputimer.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009,2015,2018,2020 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "cputimer.h"
24 
25 #include "errno_to_string.h"
26 #include "testsuite.h"
27 
28 #ifdef HAVE_GETRUSAGE
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #elif defined HAVE_TIMES
32 # include <sys/times.h>
33 # ifdef HAVE_SYSCONF
34 # include "safeunistd.h"
35 # endif
36 #else
37 # include "realtime.h"
38 #endif
39 
40 #include <cerrno>
41 #include <cstdlib>
42 #include <cstring>
43 #include <string>
44 
45 using namespace std;
46 
47 double
49 {
50 #ifdef XAPIAN_DEBUG_LOG
51  SKIP_TEST("Skipping timed test because configured with --enable-log");
52 #else
53  static bool skip = (getenv("AUTOMATED_TESTING") != NULL);
54  if (skip) {
55  SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
56  }
57 
58  double t = 0;
59 #ifdef HAVE_GETRUSAGE
60  struct rusage r;
61  if (getrusage(RUSAGE_SELF, &r) == -1) {
62  FAIL_TEST("Couldn't measure CPU for self: " << errno_to_string(errno));
63  }
64 
65  t = r.ru_utime.tv_sec + r.ru_stime.tv_sec;
66  t += (r.ru_utime.tv_usec + r.ru_stime.tv_usec) * 0.000001;
67 #elif defined HAVE_TIMES
68  struct tms b;
69  if (times(&b) == clock_t(-1)) {
70  FAIL_TEST("Couldn't measure CPU: " << errno_to_string(errno));
71  }
72  t = (double)(b.tms_utime + b.tms_stime);
73 # ifdef HAVE_SYSCONF
74  t /= sysconf(_SC_CLK_TCK);
75 # else
76  t /= CLK_TCK;
77 # endif
78 #else
79  // FIXME: Fallback to just using wallclock time, which is probably only
80  // going to be used on Microsoft Windows, where nobody has implemented
81  // the code required to get the CPU time used by a process.
82  t = RealTime::now();
83 #endif
84 
85  return t;
86 #endif
87 }
Convert errno value to std::string, thread-safe if possible.
a generic test suite engine
STL namespace.
static double get_current_cputime()
Definition: cputimer.cc:48
void errno_to_string(int e, string &s)
#define FAIL_TEST(MSG)
Fail the current testcase with message MSG.
Definition: testsuite.h:68
Measure CPU time.
#define SKIP_TEST(MSG)
Skip the current testcase with message MSG.
Definition: testsuite.h:74
double now()
Return the current time.
Definition: realtime.h:49
<unistd.h>, but with compat.
Functions for handling a time or time interval in a double.