Fix public issue #29: Write CPU timing code for Windows, based on GetProcessTimes()

instead of getursage().

I thought I'd already committed this patch, so that the 1.0.1 release already
would have a Windows-compatible snappy_unittest, but I'd seemingly deleted it
instead, so this is a reconstruction.

R=csilvers
DELTA=43  (39 added, 3 deleted, 1 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1295


git-svn-id: https://snappy.googlecode.com/svn/trunk@28 03e5f5b5-db94-4691-08a0-1a8bf15f6143
This commit is contained in:
snappy.mirrorbot@gmail.com 2011-04-11 09:07:01 +00:00
parent c67fa0c755
commit fb7e0eade4
3 changed files with 40 additions and 4 deletions

View File

@ -18,7 +18,7 @@ AC_SUBST([LIBTOOL_DEPS])
AC_PROG_CXX
AC_LANG([C++])
AC_C_BIGENDIAN
AC_CHECK_HEADERS([stdint.h stddef.h sys/mman.h])
AC_CHECK_HEADERS([stdint.h stddef.h sys/mman.h sys/resource.h windows.h])
# Don't use AC_FUNC_MMAP, as it checks for mappings of already-mapped memory,
# which we don't need (and does not exist on Windows).

View File

@ -30,6 +30,10 @@
#include "snappy-test.h"
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include <algorithm>
DEFINE_bool(run_microbenchmarks, true,
@ -64,20 +68,31 @@ int64 benchmark_cpu_time_us = 0;
string *benchmark_label = NULL;
int64 benchmark_bytes_processed = 0;
struct timeval benchmark_start_real;
struct rusage benchmark_start_cpu;
void ResetBenchmarkTiming() {
benchmark_real_time_us = 0;
benchmark_cpu_time_us = 0;
}
struct timeval benchmark_start_real;
#ifdef WIN32
FILETIME benchmark_start_cpu;
#else // WIN32
struct rusage benchmark_start_cpu;
#endif // WIN32
void StartBenchmarkTiming() {
gettimeofday(&benchmark_start_real, NULL);
#ifdef WIN32
FILETIME dummy;
CHECK(GetProcessTimes(
GetCurrentProcess(), &dummy, &dummy, &dummy, &benchmark_start_cpu));
#else
if (getrusage(RUSAGE_SELF, &benchmark_start_cpu) == -1) {
perror("getrusage(RUSAGE_SELF)");
exit(1);
}
#endif
benchmark_running = true;
}
@ -92,6 +107,22 @@ void StopBenchmarkTiming() {
benchmark_real_time_us +=
(benchmark_stop_real.tv_usec - benchmark_start_real.tv_usec);
#ifdef WIN32
FILETIME benchmark_stop_cpu, dummy;
CHECK(GetProcessTimes(
GetCurrentProcess(), &dummy, &dummy, &dummy, &benchmark_stop_cpu));
ULARGE_INTEGER start_ulargeint;
start_ulargeint.LowPart = benchmark_start_cpu.dwLowDateTime;
start_ulargeint.HighPart = benchmark_start_cpu.dwHighDateTime;
ULARGE_INTEGER stop_ulargeint;
stop_ulargeint.LowPart = benchmark_stop_cpu.dwLowDateTime;
stop_ulargeint.HighPart = benchmark_stop_cpu.dwHighDateTime;
benchmark_cpu_time_us +=
(stop_ulargeint.QuadPart - start_ulargeint.QuadPart + 5) / 10;
#else // WIN32
struct rusage benchmark_stop_cpu;
if (getrusage(RUSAGE_SELF, &benchmark_stop_cpu) == -1) {
perror("getrusage(RUSAGE_SELF)");
@ -101,6 +132,8 @@ void StopBenchmarkTiming() {
benchmark_start_cpu.ru_utime.tv_sec);
benchmark_cpu_time_us += (benchmark_stop_cpu.ru_utime.tv_usec -
benchmark_start_cpu.ru_utime.tv_usec);
#endif // WIN32
benchmark_running = false;
}

View File

@ -40,7 +40,10 @@
#include <sys/mman.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#include <sys/time.h>
#include <string>