2 * @brief Measure CPU time.
4 /* Copyright (C) 2009,2015 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 "testsuite.h"
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #elif defined HAVE_TIMES
31 # include <sys/times.h>
33 # include "safeunistd.h"
35 #elif defined HAVE_FTIME
36 # include <sys/timeb.h>
49 CPUTimer::get_current_cputime() const
51 static bool skip
= (getenv("AUTOMATED_TESTING") != NULL
);
53 SKIP_TEST("Skipping timed test because $AUTOMATED_TESTING is set");
59 if (getrusage(RUSAGE_SELF
, &r
) == -1) {
60 FAIL_TEST("Couldn't measure CPU for self: " << strerror(errno
));
63 t
= r
.ru_utime
.tv_sec
+ r
.ru_stime
.tv_sec
;
64 t
+= (r
.ru_utime
.tv_usec
+ r
.ru_stime
.tv_usec
) * 0.000001;
65 #elif defined HAVE_TIMES
67 if (times(&b
) == clock_t(-1)) {
68 FAIL_TEST("Couldn't measure CPU: " << strerror(errno
));
70 t
= (double)(b
.tms_utime
+ b
.tms_stime
);
72 t
/= sysconf(_SC_CLK_TCK
);
77 // FIXME: Fallback to just using wallclock time, which is probably only
78 // going to be used on Microsoft Windows, where nobody has implemented
79 // the code required to get the CPU time used by a process.
82 # ifdef FTIME_RETURNS_VOID
84 t
= tb
.time
+ (tb
.millitm
* 0.001);
86 if (ftime(&tb
) == -1) {
89 t
= tb
.time
+ (tb
.millitm
* 0.001);