diff --git a/include/benchmark/macros.h b/include/benchmark/macros.h index 8c2df946..ac3d963a 100644 --- a/include/benchmark/macros.h +++ b/include/benchmark/macros.h @@ -34,69 +34,6 @@ char (&ArraySizeHelper(const T (&array)[N]))[N]; #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 -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 -// -// 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_EQ(a, b) CHECK((a) == (b)) #define CHECK_GE(a, b) CHECK((a) >= (b)) diff --git a/src/benchmark.cc b/src/benchmark.cc index 7a4de8e3..3394044a 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -80,10 +80,10 @@ static const char kBigIECUnits[] = "KMGTPEZY"; static const char kSmallSIUnits[] = "munpfazy"; // We require that all three arrays have the same size. -STATIC_ASSERT(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), - SI_and_IEC_unit_arrays_must_be_the_same_size); -STATIC_ASSERT(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), - Small_SI_and_Big_SI_unit_arrays_must_be_the_same_size); +static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), + "SI and IEC unit arrays must be the same size"); +static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), + "Small SI and Big SI unit arrays must be the same size"); static const int kUnitsSize = arraysize(kBigSIUnits); void ToExponentAndMantissa(double val, double thresh, @@ -428,11 +428,11 @@ void UseRealTime() { void PrintUsageAndExit() { fprintf(stdout, "benchmark [--benchmark_filter=]\n" - " [--benchmark_min_iters=]\n" - " [--benchmark_max_iters=]\n" - " [--benchmark_min_time=]\n" +// TODO " [--benchmark_min_iters=]\n" +// TODO " [--benchmark_max_iters=]\n" +// TODO " [--benchmark_min_time=]\n" // " [--benchmark_memory_usage]\n" - " [--benchmark_repetitions=]\n" +// TODO " [--benchmark_repetitions=]\n" " [--color_print={true|false}]\n" " [--v=]\n"); exit(0); @@ -442,6 +442,7 @@ void ParseCommandLineFlags(int* argc, const char** argv) { for (int i = 1; i < *argc; ++i) { if (ParseStringFlag(argv[i], "benchmark_filter", &FLAGS_benchmark_filter) || + /* TODO(dominic) ParseInt32Flag(argv[i], "benchmark_min_iters", &FLAGS_benchmark_min_iters) || ParseInt32Flag(argv[i], "benchmark_max_iters", @@ -453,6 +454,7 @@ void ParseCommandLineFlags(int* argc, const char** argv) { // &FLAGS_gbenchmark_memory_usage) || ParseInt32Flag(argv[i], "benchmark_repetitions", &FLAGS_benchmark_repetitions) || + */ ParseBoolFlag(argv[i], "color_print", &FLAGS_color_print) || ParseInt32Flag(argv[i], "v", &FLAGS_v)) { 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); thread_stats = new internal::Benchmark::ThreadStats(); walltime::Initialize(); - internal::Benchmark::MeasureOverhead(); internal::ParseCommandLineFlags(argc, argv); + internal::Benchmark::MeasureOverhead(); } - } // end namespace benchmark diff --git a/src/colorprint.cc b/src/colorprint.cc index 42d69cb6..b51ac6bb 100644 --- a/src/colorprint.cc +++ b/src/colorprint.cc @@ -6,6 +6,7 @@ DECLARE_bool(color_print); +namespace benchmark { namespace { #ifdef OS_WINDOWS typedef WORD PlatformColorCode; @@ -78,5 +79,5 @@ void ColorPrintf(LogColor color, const char* fmt, ...) { #endif va_end(args); } - +} // end namespace benchmark diff --git a/src/colorprint.h b/src/colorprint.h index 5789c2e7..54d1f664 100644 --- a/src/colorprint.h +++ b/src/colorprint.h @@ -1,6 +1,7 @@ #ifndef BENCHMARK_COLORPRINT_H_ #define BENCHMARK_COLORPRINT_H_ +namespace benchmark { enum LogColor { COLOR_DEFAULT, COLOR_RED, @@ -13,5 +14,6 @@ enum LogColor { }; void ColorPrintf(LogColor color, const char* fmt, ...); +} // end namespace benchmark #endif // BENCHMARK_COLORPRINT_H_ diff --git a/src/commandlineflags.h b/src/commandlineflags.h index 056d9fc0..e9c11753 100644 --- a/src/commandlineflags.h +++ b/src/commandlineflags.h @@ -24,7 +24,6 @@ std::string FLAG(name) = (default_val) namespace benchmark { - // Parses 'str' for a 32-bit signed integer. If successful, writes the result // to *value and returns true; otherwise leaves *value unchanged and returns // false. diff --git a/src/cycleclock.h b/src/cycleclock.h index d5ba314f..1b7d4c99 100644 --- a/src/cycleclock.h +++ b/src/cycleclock.h @@ -44,7 +44,8 @@ extern "C" uint64_t __rdtsc(); // http://peter.kuscsik.com/wordpress/?p=14 // with modifications by m3b. See also // 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. static inline int64_t Now() { #if defined(OS_MACOSX) @@ -124,6 +125,7 @@ struct CycleClock { #error You need to define CycleTimer for your OS and CPU #endif } -}; +} // end namespace cycleclock +} // end namespace benchmark #endif // BENCHMARK_CYCLECLOCK_H_ diff --git a/src/mutex_lock.h b/src/mutex_lock.h index 40f0fdee..7b0131e8 100644 --- a/src/mutex_lock.h +++ b/src/mutex_lock.h @@ -3,6 +3,7 @@ #include +namespace benchmark { class mutex_lock { public: explicit mutex_lock(pthread_mutex_t* mu) : mu_(mu) { @@ -16,5 +17,6 @@ class mutex_lock { private: pthread_mutex_t* mu_; }; +} // end namespace benchmark #endif // BENCHMARK_MUTEX_LOCK_H_ diff --git a/src/port.h b/src/port.h deleted file mode 100644 index 7d8fe1cc..00000000 --- a/src/port.h +++ /dev/null @@ -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_ diff --git a/src/sleep.cc b/src/sleep.cc index 82292919..f1bdf1fc 100644 --- a/src/sleep.cc +++ b/src/sleep.cc @@ -3,6 +3,7 @@ #include #include +namespace benchmark { #ifdef OS_WINDOWS // Window's _sleep takes milliseconds argument. @@ -37,6 +38,5 @@ void SleepForSeconds(double seconds) { } #endif // OS_WINDOWS - - +} // end namespace benchmark diff --git a/src/sleep.h b/src/sleep.h index 35b4263b..1b60a45a 100644 --- a/src/sleep.h +++ b/src/sleep.h @@ -3,8 +3,10 @@ #include +namespace benchmark { void SleepForMicroseconds(int64_t microseconds); void SleepForMilliseconds(int milliseconds); void SleepForSeconds(double seconds); +} // end namespace benchmark #endif // BENCHMARK_SLEEP_H_ diff --git a/src/stat.h b/src/stat.h index b121d47e..c36cf059 100644 --- a/src/stat.h +++ b/src/stat.h @@ -5,6 +5,7 @@ #include #include +namespace benchmark { template class Stat1; @@ -302,5 +303,6 @@ inline std::ostream& operator <<(std::ostream& out, << " max = " << s.Max() << "}"; return out; } +} // end namespace benchmark #endif // BENCHMARK_STAT_H_ diff --git a/src/sysinfo.cc b/src/sysinfo.cc index 644b66f0..b8dc3cbd 100644 --- a/src/sysinfo.cc +++ b/src/sysinfo.cc @@ -18,6 +18,7 @@ #include "mutex_lock.h" #include "sleep.h" +namespace benchmark { namespace { pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT; double cpuinfo_cycles_per_second = 1.0; @@ -30,9 +31,9 @@ int64_t EstimateCyclesPerSecond(const int estimate_time_ms) { CHECK(estimate_time_ms > 0); 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); - const int64_t guess = int64_t(multiplier * (CycleClock::Now() - start_ticks)); + const int64_t guess = int64_t(multiplier * (cycleclock::Now() - start_ticks)); return guess; } @@ -334,4 +335,4 @@ int NumCPUs(void) { pthread_once(&cpuinfo_init, &InitializeSystemInfo); return cpuinfo_num_cpus; } - +} // end namespace benchmark diff --git a/src/sysinfo.h b/src/sysinfo.h index 0b85d5cc..f9f63ee2 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -1,9 +1,11 @@ #ifndef BENCHMARK_SYSINFO_H_ #define BENCHMARK_SYSINFO_H_ +namespace benchmark { double MyCPUUsage(); double ChildrenCPUUsage(); int NumCPUs(); double CyclesPerSecond(); +} // end namespace benchmark #endif // BENCHMARK_SYSINFO_H_ diff --git a/src/walltime.cc b/src/walltime.cc index 85384aac..1c95fd55 100644 --- a/src/walltime.cc +++ b/src/walltime.cc @@ -12,6 +12,7 @@ #include "macros.h" #include "sysinfo.h" +namespace benchmark { namespace walltime { namespace { const double kMaxErrorInterval = 100e-6; @@ -77,9 +78,9 @@ void Initialize() { max_interval_cycles = static_cast( cycles_per_second * kMaxErrorInterval); do { - base_cycletime = CycleClock::Now(); + base_cycletime = cycleclock::Now(); 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 // within kMaxErrorInterval of one another. @@ -97,7 +98,7 @@ WallTime Now() { int64_t ct = 0; uint32_t top_bits = 0; do { - ct = CycleClock::Now(); + ct = cycleclock::Now(); int64_t cycle_delta = ct - base_cycletime; result = base_walltime + cycle_delta * seconds_per_cycle; @@ -109,7 +110,7 @@ WallTime Now() { } 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 // kMaxErrorInterval of one another. @@ -135,3 +136,4 @@ const char* Print(WallTime time, const char *format, bool local, return storage; } } // end namespace walltime +} // end namespace benchmark diff --git a/src/walltime.h b/src/walltime.h index 13cda806..660e2ba6 100644 --- a/src/walltime.h +++ b/src/walltime.h @@ -1,6 +1,7 @@ #ifndef BENCHMARK_WALLTIME_H_ #define BENCHMARK_WALLTIME_H_ +namespace benchmark { typedef double WallTime; namespace walltime { @@ -15,5 +16,6 @@ WallTime Now(); const char* Print(WallTime time, const char *format, bool local, char* storage, int *remainder_us); } // end namespace walltime +} // end namespace benchmark #endif // BENCHMARK_WALLTIME_H_