2 * @brief Measure CPU time.
4 /* Copyright (C) 2009,2015,2018,2020 Olly Betts
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.
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.
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
25 #include "errno_to_string.h"
26 #include "testsuite.h"
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #elif defined HAVE_TIMES
32 # include <sys/times.h>
34 # include "safeunistd.h"
37 # include "realtime.h"
48 CPUTimer::get_current_cputime()
50 #ifdef XAPIAN_DEBUG_LOG
51 SKIP_TEST("Skipping timed test because configured with --enable-log");
53 static bool skip
= (getenv("AUTOMATED_TESTING") != NULL
);
55 SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
61 if (getrusage(RUSAGE_SELF
, &r
) == -1) {
62 FAIL_TEST("Couldn't measure CPU for self: " << errno_to_string(errno
));
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
69 if (times(&b
) == clock_t(-1)) {
70 FAIL_TEST("Couldn't measure CPU: " << errno_to_string(errno
));
72 t
= (double)(b
.tms_utime
+ b
.tms_stime
);
74 t
/= sysconf(_SC_CLK_TCK
);
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.