Skip to content

Commit

Permalink
Use correct time library depending on target platform.
Browse files Browse the repository at this point in the history
librt doesn't exist on apple platforms, so detect that
and use mach time instead.
  • Loading branch information
spkrka committed Sep 3, 2013
1 parent 9f11214 commit ec80630
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
9 changes: 7 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ AM_CONDITIONAL([HAVE_DOXYGEN],
AC_SEARCH_LIBS([snappy_compress],
[snappy],,[AC_MSG_ERROR([Could not find snappy])
])
AC_SEARCH_LIBS([clock_gettime],
[rt],,[AC_MSG_ERROR([Could not find rt])

AM_CONDITIONAL([APPLE], [test x$build_vendor = xapple])
AM_COND_IF([APPLE], [
],[
AC_SEARCH_LIBS([clock_gettime],
[rt],,[AC_MSG_ERROR([Could not find rt])
])
])
AC_CONFIG_FILES([
Makefile
Expand Down
35 changes: 29 additions & 6 deletions src/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
Expand Down Expand Up @@ -106,17 +105,41 @@ static size_t total_file_size(const char** files) {
}
}

float wall() {
#ifdef __APPLE__
#include <mach/mach_time.h>
static float wall() {
static double multiplier = 0;
if (multiplier <= 0) {
mach_timebase_info_data_t info;
mach_timebase_info(&info);
multiplier = (double) info.numer / (double) info.denom / 1000000000.0;
}
return (float) (multiplier * mach_absolute_time());
}
static float cpu() {
return wall();
}

#else

#include <time.h>
#ifdef CLOCK_MONOTONIC_RAW
#define CLOCK_SUITABLE CLOCK_MONOTONIC_RAW
#else
#define CLOCK_SUITABLE CLOCK_MONOTONIC
#endif
static float wall() {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
clock_gettime(CLOCK_SUITABLE, &tp);
return tp.tv_sec + 1e-9 * tp.tv_nsec;
}

float cpu() {
static float cpu() {
struct timespec tp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
return tp.tv_sec + 1e-9 * tp.tv_nsec;
}
#endif

typedef struct {
char *name;
Expand Down Expand Up @@ -194,7 +217,7 @@ static void sparkey_create_compressed(int n) {
sparkey_create(n, SPARKEY_COMPRESSION_SNAPPY, 1024);
}

static const char* sparkey_list[] = {"test.spi", "test.spl", NULL};
static const char* sparkey_list[] = {"test.spi", "test.spl", NULL};

static const char** sparkey_files() {
return sparkey_list;
Expand All @@ -217,7 +240,7 @@ void test(candidate *c, int n, int lookups) {
rm_all_rec(c->files());

float t1_wall = wall();
float t1_cpu = cpu();
float t1_cpu = cpu();

c->create(n);

Expand Down

0 comments on commit ec80630

Please sign in to comment.