commit 58cbdbd892c9a83369e3e46aa9700cc7df98a17b
parent 12b97053570a5bb9c4b1115eae9509cafd975eb1
Author: Mattias Andrée <maandree@kth.se>
Date: Sun, 24 Jul 2016 16:42:17 +0200
Clean up bench/util.h
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat:
M | Makefile | | | 12 | ++++++------ |
A | bench/util.c | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
M | bench/util.h | | | 76 | ++++++++++++++++++++++------------------------------------------------------ |
3 files changed, 67 insertions(+), 60 deletions(-)
diff --git a/Makefile b/Makefile
@@ -149,16 +149,16 @@ test-random.c: test-generate.py
test: test.c libzahl.a test-random.c
$(CC) $(LDFLAGS) $(CFLAGS_WITHOUT_O) -O0 $(CPPFLAGS) -o $@ test.c libzahl.a
-benchmark: bench/benchmark.c bench/util.h $(BENCHMARK_DEP_$(BENCHMARK_LIB))
- $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark.c \
+benchmark: bench/benchmark.c bench/util.c bench/util.h $(BENCHMARK_DEP_$(BENCHMARK_LIB))
+ $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark.c bench/util.c \
$(BENCHMARK_LIB_$(BENCHMARK_LIB)) $(BENCHMARK_C_$(BENCHMARK_LIB))
-benchmark-func: bench/benchmark-func.c bench/util.h $(BENCHMARK_DEP_$(BENCHMARK_LIB))
- $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark-func.c \
+benchmark-func: bench/benchmark-func.c bench/util.c bench/util.h $(BENCHMARK_DEP_$(BENCHMARK_LIB))
+ $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark-func.c bench/util.c \
$(BENCHMARK_LIB_$(BENCHMARK_LIB)) $(BENCHMARK_C_$(BENCHMARK_LIB))
-benchmark-zrand: bench/benchmark-zrand.c bench/util.h libzahl.a
- $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark-zrand.c libzahl.a
+benchmark-zrand: bench/benchmark-zrand.c bench/util.c bench/util.h libzahl.a
+ $(CC) $(LDFLAGS) $(CFLAGS) $(CPPFLAGS) -o $@ bench/benchmark-zrand.c bench/util.c libzahl.a
refsheet.pdf: doc/refsheet.tex
pdflatex doc/refsheet.tex </dev/null
diff --git a/bench/util.c b/bench/util.c
@@ -0,0 +1,39 @@
+#define COMPILING_UTIL_C
+#include "util.h"
+
+
+char timebuf[512];
+unsigned long long int freq;
+
+
+void
+benchmark_init(void)
+{
+#if defined(__linux__) && defined(USE_RDTSC)
+ cpu_set_t cpuset;
+ FILE *f;
+ char *line = 0;
+ size_t size = 0;
+ char path[PATH_MAX];
+ CPU_ZERO(&cpuset);
+ CPU_SET(USE_CPU, &cpuset);
+ sched_setaffinity(getpid(), sizeof(cpuset), &cpuset);
+ sprintf(path, "/sys/devices/system/cpu/cpu%i/cpufreq/cpuinfo_max_freq", USE_CPU);
+ f = fopen(path, "r");
+ if (getline(&line, &size, f) < 0)
+ abort();
+ fclose(f);
+ freq = strtoull(line, 0, 10);
+ free(line);
+
+#elif defined(__linux__)
+ cpu_set_t cpuset;
+ CPU_ZERO(&cpuset);
+ CPU_SET(USE_CPU, &cpuset);
+ sched_setaffinity(getpid(), sizeof(cpuset), &cpuset);
+
+#else
+ fprintf(stderr, "WARNING: Don't know how to set CPU affinity.\n");
+
+#endif
+}
diff --git a/bench/util.h b/bench/util.h
@@ -11,6 +11,7 @@
#include <unistd.h>
+
#ifdef BENCHMARK_LIB
# include BENCHMARK_LIB
#else
@@ -31,12 +32,10 @@
# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
#endif
-
#ifdef __x86_64__
# define RDTSC_MAYBE_SUPPORTED
#endif
-
#if !defined(USE_RDTSC) && !defined(USE_CLOCK) && !defined(USE_GETTIME)
# if 1 && defined(RDTSC_MAYBE_SUPPORTED) && defined(__linux__)
# define USE_RDTSC
@@ -49,17 +48,19 @@
-static struct timespec dur;
-static char timebuf[512];
+extern char timebuf[512];
+extern unsigned long long int freq;
+
+#ifndef COMPILING_UTIL_C
+static struct timespec dur;
-#if defined(USE_RDTSC) && defined(__x86_64__)
+# if defined(USE_RDTSC) && defined(__x86_64__)
typedef unsigned long long int rdtsc_t;
static unsigned int start_high, start_low, end_high, end_low;
-static unsigned long long int freq;
-
-# define TIC (rdtsc(&start_low, &start_high))
-# define TOC\
+# define rdtsc_join(low, high) ((rdtsc_t)(low) | (((rdtsc_t)(high)) << 32))
+# define TIC (rdtsc(&start_low, &start_high))
+# define TOC\
do {\
rdtsc_t dur_cycles;\
double dur_seconds;\
@@ -71,37 +72,26 @@ static unsigned long long int freq;
dur_seconds -= (double)(dur.tv_sec = (int)dur_seconds);\
dur.tv_nsec = (long int)(dur_seconds * 1000000000L);\
} while (0)
-
static inline void
rdtsc(unsigned int *low, unsigned int *high)
{
__asm__ __volatile__ ("rdtsc" : "=a"(*low), "=d"(*high));
}
-static inline rdtsc_t
-rdtsc_join(unsigned int low, unsigned int high)
-{
- return (rdtsc_t)low | (((rdtsc_t)high) << 32);
-}
-
-
-#elif defined(USE_CLOCK)
+# elif defined(USE_CLOCK)
static clock_t start, end;
-
-# define TIC (start = clock())
-# define TOC\
+# define TIC (start = clock())
+# define TOC\
do {\
end = clock();\
dur.tv_sec = (end - start) / 1000000ULL;\
dur.tv_nsec = ((end - start) % 1000000ULL) * 1000;\
} while (0)
-
-#elif defined(USE_GETTIME)
+# elif defined(USE_GETTIME)
static struct timespec start;
-
-# define TIC clock_gettime(CLOCK_MONOTONIC_RAW, &start)
-# define TOC\
+# define TIC clock_gettime(CLOCK_MONOTONIC_RAW, &start)
+# define TOC\
do {\
clock_gettime(CLOCK_MONOTONIC_RAW, &dur);\
dur.tv_sec -= start.tv_sec;\
@@ -111,36 +101,14 @@ static struct timespec start;
dur.tv_sec -= 1;\
}\
} while (0)
-#endif
+# endif
-#define TICKS ((unsigned long long int)(dur.tv_sec) * 1000000000ULL + (unsigned long long int)(dur.tv_nsec))
-#define STIME (sprintf(timebuf, "%lli.%09li", (long long)(dur.tv_sec), dur.tv_nsec), timebuf)
+# define TICKS ((unsigned long long int)(dur.tv_sec) * 1000000000ULL + (unsigned long long int)(dur.tv_nsec))
+# define STIME (sprintf(timebuf, "%lli.%09li", (long long)(dur.tv_sec), dur.tv_nsec), timebuf)
-static void
-benchmark_init(void)
-{
-#if defined(__linux__)
- cpu_set_t cpuset;
-# if defined(USE_RDTSC)
- FILE *f;
- char *line = 0;
- size_t size = 0;
- char path[PATH_MAX];
-# endif
- CPU_ZERO(&cpuset);
- CPU_SET(USE_CPU, &cpuset);
- sched_setaffinity(getpid(), sizeof(cpuset), &cpuset);
-# if defined(USE_RDTSC)
- sprintf(path, "/sys/devices/system/cpu/cpu%i/cpufreq/cpuinfo_max_freq", USE_CPU);
- f = fopen(path, "r");
- if (getline(&line, &size, f) < 0)
- abort();
- fclose(f);
- freq = strtoull(line, 0, 10);
- free(line);
-# endif
#endif
- (void) timebuf;
-}
+
+
+void benchmark_init(void);