Adding benchmark namespace and removing broken flags

This commit is contained in:
Dominic Hamon 2013-12-19 16:18:09 -08:00
parent 403f354423
commit e390e4ebc3
15 changed files with 41 additions and 94 deletions

View File

@ -34,69 +34,6 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array))) #define arraysize(array) (sizeof(ArraySizeHelper(array)))
// The STATIC_ASSERT macro can be used to verify that a compile time
// expression is true. For example, you could use it to verify the
// size of a static array:
//
// STATIC_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
// content_type_names_incorrect_size);
//
// or to make sure a struct is smaller than a certain size:
//
// STATIC_ASSERT(sizeof(foo) < 128, foo_too_large);
//
// The second argument to the macro is the name of the variable. If
// the expression is false, most compilers will issue a warning/error
// containing the name of the variable.
template <bool>
struct StaticAssert {
};
#define STATIC_ASSERT(expr, msg) \
typedef StaticAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
// Implementation details of STATIC_ASSERT:
//
// - STATIC_ASSERT works by defining an array type that has -1
// elements (and thus is invalid) when the expression is false.
//
// - The simpler definition
//
// #define STATIC_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
//
// does not work, as gcc supports variable-length arrays whose sizes
// are determined at run-time (this is gcc's extension and not part
// of the C++ standard). As a result, gcc fails to reject the
// following code with the simple definition:
//
// int foo;
// STATIC_ASSERT(foo, msg); // not supposed to compile as foo is
// // not a compile-time constant.
//
// - By using the type StaticAssert<(bool(expr))>, we ensures that
// expr is a compile-time constant. (Template arguments must be
// determined at compile-time.)
//
// - The outer parentheses in StaticAssert<(bool(expr))> are necessary
// to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
//
// StaticAssert<bool(expr)>
//
// instead, these compilers will refuse to compile
//
// STATIC_ASSERT(5 > 0, some_message);
//
// (They seem to think the ">" in "5 > 0" marks the end of the
// template argument list.)
//
// - The array size is (bool(expr) ? 1 : -1), instead of simply
//
// ((expr) ? 1 : -1).
//
// This is to avoid running into a bug in MS VC 7.1, which
// causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
#define CHECK(b) do { if (!(b)) assert(false); } while(0) #define CHECK(b) do { if (!(b)) assert(false); } while(0)
#define CHECK_EQ(a, b) CHECK((a) == (b)) #define CHECK_EQ(a, b) CHECK((a) == (b))
#define CHECK_GE(a, b) CHECK((a) >= (b)) #define CHECK_GE(a, b) CHECK((a) >= (b))

View File

@ -80,10 +80,10 @@ static const char kBigIECUnits[] = "KMGTPEZY";
static const char kSmallSIUnits[] = "munpfazy"; static const char kSmallSIUnits[] = "munpfazy";
// We require that all three arrays have the same size. // We require that all three arrays have the same size.
STATIC_ASSERT(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits),
SI_and_IEC_unit_arrays_must_be_the_same_size); "SI and IEC unit arrays must be the same size");
STATIC_ASSERT(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits),
Small_SI_and_Big_SI_unit_arrays_must_be_the_same_size); "Small SI and Big SI unit arrays must be the same size");
static const int kUnitsSize = arraysize(kBigSIUnits); static const int kUnitsSize = arraysize(kBigSIUnits);
void ToExponentAndMantissa(double val, double thresh, void ToExponentAndMantissa(double val, double thresh,
@ -428,11 +428,11 @@ void UseRealTime() {
void PrintUsageAndExit() { void PrintUsageAndExit() {
fprintf(stdout, "benchmark [--benchmark_filter=<regex>]\n" fprintf(stdout, "benchmark [--benchmark_filter=<regex>]\n"
" [--benchmark_min_iters=<min_iters>]\n" // TODO " [--benchmark_min_iters=<min_iters>]\n"
" [--benchmark_max_iters=<max_iters>]\n" // TODO " [--benchmark_max_iters=<max_iters>]\n"
" [--benchmark_min_time=<min_time>]\n" // TODO " [--benchmark_min_time=<min_time>]\n"
// " [--benchmark_memory_usage]\n" // " [--benchmark_memory_usage]\n"
" [--benchmark_repetitions=<num_repetitions>]\n" // TODO " [--benchmark_repetitions=<num_repetitions>]\n"
" [--color_print={true|false}]\n" " [--color_print={true|false}]\n"
" [--v=<verbosity>]\n"); " [--v=<verbosity>]\n");
exit(0); exit(0);
@ -442,6 +442,7 @@ void ParseCommandLineFlags(int* argc, const char** argv) {
for (int i = 1; i < *argc; ++i) { for (int i = 1; i < *argc; ++i) {
if (ParseStringFlag(argv[i], "benchmark_filter", if (ParseStringFlag(argv[i], "benchmark_filter",
&FLAGS_benchmark_filter) || &FLAGS_benchmark_filter) ||
/* TODO(dominic)
ParseInt32Flag(argv[i], "benchmark_min_iters", ParseInt32Flag(argv[i], "benchmark_min_iters",
&FLAGS_benchmark_min_iters) || &FLAGS_benchmark_min_iters) ||
ParseInt32Flag(argv[i], "benchmark_max_iters", ParseInt32Flag(argv[i], "benchmark_max_iters",
@ -453,6 +454,7 @@ void ParseCommandLineFlags(int* argc, const char** argv) {
// &FLAGS_gbenchmark_memory_usage) || // &FLAGS_gbenchmark_memory_usage) ||
ParseInt32Flag(argv[i], "benchmark_repetitions", ParseInt32Flag(argv[i], "benchmark_repetitions",
&FLAGS_benchmark_repetitions) || &FLAGS_benchmark_repetitions) ||
*/
ParseBoolFlag(argv[i], "color_print", &FLAGS_color_print) || ParseBoolFlag(argv[i], "color_print", &FLAGS_color_print) ||
ParseInt32Flag(argv[i], "v", &FLAGS_v)) { ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
for (int j = i; j != *argc; ++j) for (int j = i; j != *argc; ++j)
@ -1189,9 +1191,8 @@ void Initialize(int* argc, const char** argv) {
pthread_key_create(&thread_stats_key, DeleteThreadStats); pthread_key_create(&thread_stats_key, DeleteThreadStats);
thread_stats = new internal::Benchmark::ThreadStats(); thread_stats = new internal::Benchmark::ThreadStats();
walltime::Initialize(); walltime::Initialize();
internal::Benchmark::MeasureOverhead();
internal::ParseCommandLineFlags(argc, argv); internal::ParseCommandLineFlags(argc, argv);
internal::Benchmark::MeasureOverhead();
} }
} // end namespace benchmark } // end namespace benchmark

View File

@ -6,6 +6,7 @@
DECLARE_bool(color_print); DECLARE_bool(color_print);
namespace benchmark {
namespace { namespace {
#ifdef OS_WINDOWS #ifdef OS_WINDOWS
typedef WORD PlatformColorCode; typedef WORD PlatformColorCode;
@ -78,5 +79,5 @@ void ColorPrintf(LogColor color, const char* fmt, ...) {
#endif #endif
va_end(args); va_end(args);
} }
} // end namespace benchmark

View File

@ -1,6 +1,7 @@
#ifndef BENCHMARK_COLORPRINT_H_ #ifndef BENCHMARK_COLORPRINT_H_
#define BENCHMARK_COLORPRINT_H_ #define BENCHMARK_COLORPRINT_H_
namespace benchmark {
enum LogColor { enum LogColor {
COLOR_DEFAULT, COLOR_DEFAULT,
COLOR_RED, COLOR_RED,
@ -13,5 +14,6 @@ enum LogColor {
}; };
void ColorPrintf(LogColor color, const char* fmt, ...); void ColorPrintf(LogColor color, const char* fmt, ...);
} // end namespace benchmark
#endif // BENCHMARK_COLORPRINT_H_ #endif // BENCHMARK_COLORPRINT_H_

View File

@ -24,7 +24,6 @@
std::string FLAG(name) = (default_val) std::string FLAG(name) = (default_val)
namespace benchmark { namespace benchmark {
// Parses 'str' for a 32-bit signed integer. If successful, writes the result // Parses 'str' for a 32-bit signed integer. If successful, writes the result
// to *value and returns true; otherwise leaves *value unchanged and returns // to *value and returns true; otherwise leaves *value unchanged and returns
// false. // false.

View File

@ -44,7 +44,8 @@ extern "C" uint64_t __rdtsc();
// http://peter.kuscsik.com/wordpress/?p=14 // http://peter.kuscsik.com/wordpress/?p=14
// with modifications by m3b. See also // with modifications by m3b. See also
// https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
struct CycleClock { namespace benchmark {
namespace cycleclock {
// This should return the number of cycles since power-on. Thread-safe. // This should return the number of cycles since power-on. Thread-safe.
static inline int64_t Now() { static inline int64_t Now() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
@ -124,6 +125,7 @@ struct CycleClock {
#error You need to define CycleTimer for your OS and CPU #error You need to define CycleTimer for your OS and CPU
#endif #endif
} }
}; } // end namespace cycleclock
} // end namespace benchmark
#endif // BENCHMARK_CYCLECLOCK_H_ #endif // BENCHMARK_CYCLECLOCK_H_

View File

@ -3,6 +3,7 @@
#include <pthread.h> #include <pthread.h>
namespace benchmark {
class mutex_lock { class mutex_lock {
public: public:
explicit mutex_lock(pthread_mutex_t* mu) : mu_(mu) { explicit mutex_lock(pthread_mutex_t* mu) : mu_(mu) {
@ -16,5 +17,6 @@ class mutex_lock {
private: private:
pthread_mutex_t* mu_; pthread_mutex_t* mu_;
}; };
} // end namespace benchmark
#endif // BENCHMARK_MUTEX_LOCK_H_ #endif // BENCHMARK_MUTEX_LOCK_H_

View File

@ -1,8 +0,0 @@
#ifndef BENCHMARK_PORT_H_
#define BENCHMARK_PORT_H_
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&);
#endif // BENCHMARK_PORT_H_

View File

@ -3,6 +3,7 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
namespace benchmark {
#ifdef OS_WINDOWS #ifdef OS_WINDOWS
// Window's _sleep takes milliseconds argument. // Window's _sleep takes milliseconds argument.
@ -37,6 +38,5 @@ void SleepForSeconds(double seconds) {
} }
#endif // OS_WINDOWS #endif // OS_WINDOWS
} // end namespace benchmark

View File

@ -3,8 +3,10 @@
#include <stdint.h> #include <stdint.h>
namespace benchmark {
void SleepForMicroseconds(int64_t microseconds); void SleepForMicroseconds(int64_t microseconds);
void SleepForMilliseconds(int milliseconds); void SleepForMilliseconds(int milliseconds);
void SleepForSeconds(double seconds); void SleepForSeconds(double seconds);
} // end namespace benchmark
#endif // BENCHMARK_SLEEP_H_ #endif // BENCHMARK_SLEEP_H_

View File

@ -5,6 +5,7 @@
#include <iostream> #include <iostream>
#include <limits> #include <limits>
namespace benchmark {
template <typename VType, typename NumType> template <typename VType, typename NumType>
class Stat1; class Stat1;
@ -302,5 +303,6 @@ inline std::ostream& operator <<(std::ostream& out,
<< " max = " << s.Max() << "}"; << " max = " << s.Max() << "}";
return out; return out;
} }
} // end namespace benchmark
#endif // BENCHMARK_STAT_H_ #endif // BENCHMARK_STAT_H_

View File

@ -18,6 +18,7 @@
#include "mutex_lock.h" #include "mutex_lock.h"
#include "sleep.h" #include "sleep.h"
namespace benchmark {
namespace { namespace {
pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT; pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT;
double cpuinfo_cycles_per_second = 1.0; double cpuinfo_cycles_per_second = 1.0;
@ -30,9 +31,9 @@ int64_t EstimateCyclesPerSecond(const int estimate_time_ms) {
CHECK(estimate_time_ms > 0); CHECK(estimate_time_ms > 0);
double multiplier = 1000.0 / (double)estimate_time_ms; // scale by this much double multiplier = 1000.0 / (double)estimate_time_ms; // scale by this much
const int64_t start_ticks = CycleClock::Now(); const int64_t start_ticks = cycleclock::Now();
SleepForMilliseconds(estimate_time_ms); SleepForMilliseconds(estimate_time_ms);
const int64_t guess = int64_t(multiplier * (CycleClock::Now() - start_ticks)); const int64_t guess = int64_t(multiplier * (cycleclock::Now() - start_ticks));
return guess; return guess;
} }
@ -334,4 +335,4 @@ int NumCPUs(void) {
pthread_once(&cpuinfo_init, &InitializeSystemInfo); pthread_once(&cpuinfo_init, &InitializeSystemInfo);
return cpuinfo_num_cpus; return cpuinfo_num_cpus;
} }
} // end namespace benchmark

View File

@ -1,9 +1,11 @@
#ifndef BENCHMARK_SYSINFO_H_ #ifndef BENCHMARK_SYSINFO_H_
#define BENCHMARK_SYSINFO_H_ #define BENCHMARK_SYSINFO_H_
namespace benchmark {
double MyCPUUsage(); double MyCPUUsage();
double ChildrenCPUUsage(); double ChildrenCPUUsage();
int NumCPUs(); int NumCPUs();
double CyclesPerSecond(); double CyclesPerSecond();
} // end namespace benchmark
#endif // BENCHMARK_SYSINFO_H_ #endif // BENCHMARK_SYSINFO_H_

View File

@ -12,6 +12,7 @@
#include "macros.h" #include "macros.h"
#include "sysinfo.h" #include "sysinfo.h"
namespace benchmark {
namespace walltime { namespace walltime {
namespace { namespace {
const double kMaxErrorInterval = 100e-6; const double kMaxErrorInterval = 100e-6;
@ -77,9 +78,9 @@ void Initialize() {
max_interval_cycles = static_cast<int64_t>( max_interval_cycles = static_cast<int64_t>(
cycles_per_second * kMaxErrorInterval); cycles_per_second * kMaxErrorInterval);
do { do {
base_cycletime = CycleClock::Now(); base_cycletime = cycleclock::Now();
base_walltime = Slow(); base_walltime = Slow();
} while (CycleClock::Now() - base_cycletime > max_interval_cycles); } while (cycleclock::Now() - base_cycletime > max_interval_cycles);
// We are now sure that "base_walltime" and "base_cycletime" were produced // We are now sure that "base_walltime" and "base_cycletime" were produced
// within kMaxErrorInterval of one another. // within kMaxErrorInterval of one another.
@ -97,7 +98,7 @@ WallTime Now() {
int64_t ct = 0; int64_t ct = 0;
uint32_t top_bits = 0; uint32_t top_bits = 0;
do { do {
ct = CycleClock::Now(); ct = cycleclock::Now();
int64_t cycle_delta = ct - base_cycletime; int64_t cycle_delta = ct - base_cycletime;
result = base_walltime + cycle_delta * seconds_per_cycle; result = base_walltime + cycle_delta * seconds_per_cycle;
@ -109,7 +110,7 @@ WallTime Now() {
} }
now = Slow(); now = Slow();
} while (CycleClock::Now() - ct > max_interval_cycles); } while (cycleclock::Now() - ct > max_interval_cycles);
// We are now sure that "now" and "result" were produced within // We are now sure that "now" and "result" were produced within
// kMaxErrorInterval of one another. // kMaxErrorInterval of one another.
@ -135,3 +136,4 @@ const char* Print(WallTime time, const char *format, bool local,
return storage; return storage;
} }
} // end namespace walltime } // end namespace walltime
} // end namespace benchmark

View File

@ -1,6 +1,7 @@
#ifndef BENCHMARK_WALLTIME_H_ #ifndef BENCHMARK_WALLTIME_H_
#define BENCHMARK_WALLTIME_H_ #define BENCHMARK_WALLTIME_H_
namespace benchmark {
typedef double WallTime; typedef double WallTime;
namespace walltime { namespace walltime {
@ -15,5 +16,6 @@ WallTime Now();
const char* Print(WallTime time, const char *format, bool local, const char* Print(WallTime time, const char *format, bool local,
char* storage, int *remainder_us); char* storage, int *remainder_us);
} // end namespace walltime } // end namespace walltime
} // end namespace benchmark
#endif // BENCHMARK_WALLTIME_H_ #endif // BENCHMARK_WALLTIME_H_