merge master

This commit is contained in:
Eric Fiselier 2015-03-17 12:35:11 -04:00
commit 7f2ef46255
11 changed files with 156 additions and 99 deletions

View File

@ -40,8 +40,7 @@ add_cxx_compiler_flag(-Werror)
add_cxx_compiler_flag(-pedantic-errors)
add_cxx_compiler_flag(-Wshorten-64-to-32)
add_cxx_compiler_flag(-Wfloat-equal)
# TODO(ericwf): enable this for g++
#add_cxx_compiler_flag(-Wzero-as-null-pointer-constant)
add_cxx_compiler_flag(-Wzero-as-null-pointer-constant)
# Release flags
add_cxx_compiler_flag(-fno-strict-aliasing RELEASE)

View File

@ -27,17 +27,7 @@ static void BM_StringCopy(benchmark::State& state) {
}
BENCHMARK(BM_StringCopy);
// Augment the main() program to invoke benchmarks if specified
// via the --benchmarks command line flag. E.g.,
// my_unittest --benchmark_filter=all
// my_unittest --benchmark_filter=BM_StringCreation
// my_unittest --benchmark_filter=String
// my_unittest --benchmark_filter='Copy|Creation'
int main(int argc, const char* argv[]) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
return 0;
}
BENCHMARK_MAIN();
```
Sometimes a family of microbenchmarks can be implemented with

View File

@ -152,7 +152,8 @@ void Initialize(int* argc, const char** argv);
// Otherwise, run all benchmarks specified by the --benchmark_filter flag,
// and exit after running the benchmarks.
void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter = NULL);
void RunSpecifiedBenchmarks();
void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter);
// If this routine is called, peak memory allocation past this point in the
// benchmark is reported at the end of the benchmark report line. (It is
@ -163,8 +164,21 @@ void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter = NULL);
namespace internal {
class Benchmark;
class BenchmarkFamilies;
}
class BenchmarkImp;
template <class T> struct Voider {
typedef void type;
};
template <class T, class = void>
struct EnableIfString {};
template <class T>
struct EnableIfString<T, typename Voider<typename T::basic_string>::type> {
typedef int type;
};
} // end namespace internal
// State is passed to a running Benchmark and contains state for the
// benchmark to use.
@ -279,7 +293,7 @@ public:
// as an injected class name in the case of std::string.
template <class StringType>
void SetLabel(StringType const & str,
typename StringType::basic_string* = 0) {
typename internal::EnableIfString<StringType>::type = 1) {
this->SetLabel(str.c_str());
}
@ -452,28 +466,12 @@ class Benchmark {
// Equivalent to ThreadRange(NumCPUs(), NumCPUs())
Benchmark* ThreadPerCpu();
// -------------------------------
// Following methods are not useful for clients
// Used inside the benchmark implementation
struct Instance;
private:
std::string name_;
Function* function_;
std::size_t registration_index_;
int arg_count_;
std::vector< std::pair<int, int> > args_; // Args for all benchmark runs
std::vector<int> thread_counts_;
// Special value placed in thread_counts_ to stand for NumCPUs()
static const int kNumCpuMarker = -1;
static void AddRange(std::vector<int>* dst, int lo, int hi, int mult);
friend class BenchmarkFamilies;
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(Benchmark);
BenchmarkImp* imp_;
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(Benchmark);
};

View File

@ -339,7 +339,6 @@ struct Benchmark::Instance {
bool multithreaded; // Is benchmark multi-threaded?
};
// Class for managing registered benchmarks. Note that each registered
// benchmark identifies a family of related benchmarks to run.
class BenchmarkFamilies {
@ -347,7 +346,7 @@ class BenchmarkFamilies {
static BenchmarkFamilies* GetInstance();
// Registers a benchmark family and returns the index assigned to it.
size_t AddBenchmark(Benchmark* family);
size_t AddBenchmark(BenchmarkImp* family);
// Unregisters a family at the given index.
void RemoveBenchmark(size_t index);
@ -360,11 +359,40 @@ class BenchmarkFamilies {
BenchmarkFamilies();
~BenchmarkFamilies();
std::vector<Benchmark*> families_;
std::vector<BenchmarkImp*> families_;
Mutex mutex_;
};
class BenchmarkImp {
public:
BenchmarkImp(const char* name, Function* func);
~BenchmarkImp();
void Arg(int x);
void Range(int start, int limit);
void DenseRange(int start, int limit);
void ArgPair(int start, int limit);
void RangePair(int lo1, int hi1, int lo2, int hi2);
void Threads(int t);
void ThreadRange(int min_threads, int max_threads);
void ThreadPerCpu();
static void AddRange(std::vector<int>* dst, int lo, int hi, int mult);
private:
friend class BenchmarkFamilies;
std::string name_;
Function* function_;
int arg_count_;
std::vector< std::pair<int, int> > args_; // Args for all benchmark runs
std::vector<int> thread_counts_;
std::size_t registration_index_;
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(BenchmarkImp);
};
BenchmarkFamilies* BenchmarkFamilies::GetInstance() {
static BenchmarkFamilies instance;
return &instance;
@ -373,12 +401,12 @@ BenchmarkFamilies* BenchmarkFamilies::GetInstance() {
BenchmarkFamilies::BenchmarkFamilies() { }
BenchmarkFamilies::~BenchmarkFamilies() {
for (internal::Benchmark* family : families_) {
for (BenchmarkImp* family : families_) {
delete family;
}
}
size_t BenchmarkFamilies::AddBenchmark(Benchmark* family) {
size_t BenchmarkFamilies::AddBenchmark(BenchmarkImp* family) {
MutexLock l(mutex_);
// This loop attempts to reuse an entry that was previously removed to avoid
// unncessary growth of the vector.
@ -416,7 +444,7 @@ bool BenchmarkFamilies::FindBenchmarks(
one_thread.push_back(1);
MutexLock l(mutex_);
for (Benchmark* family : families_) {
for (BenchmarkImp* family : families_) {
// Family was deleted or benchmark doesn't match
if (family == nullptr || !re.Match(family->name_)) continue;
@ -461,25 +489,22 @@ bool BenchmarkFamilies::FindBenchmarks(
return true;
}
Benchmark::Benchmark(const char* name,
Function* f)
: name_(name), function_(f), arg_count_(-1) {
registration_index_ = BenchmarkFamilies::GetInstance()->AddBenchmark(this);
BenchmarkImp::BenchmarkImp(const char* name, Function* func)
: name_(name), function_(func), arg_count_(-1) {
registration_index_ = BenchmarkFamilies::GetInstance()->AddBenchmark(this);
}
Benchmark::~Benchmark() {
BenchmarkImp::~BenchmarkImp() {
BenchmarkFamilies::GetInstance()->RemoveBenchmark(registration_index_);
}
Benchmark* Benchmark::Arg(int x) {
void BenchmarkImp::Arg(int x) {
CHECK(arg_count_ == -1 || arg_count_ == 1);
arg_count_ = 1;
args_.emplace_back(x, -1);
return this;
}
Benchmark* Benchmark::Range(int start, int limit) {
void BenchmarkImp::Range(int start, int limit) {
CHECK(arg_count_ == -1 || arg_count_ == 1);
arg_count_ = 1;
std::vector<int> arglist;
@ -488,10 +513,9 @@ Benchmark* Benchmark::Range(int start, int limit) {
for (int i : arglist) {
args_.emplace_back(i, -1);
}
return this;
}
Benchmark* Benchmark::DenseRange(int start, int limit) {
void BenchmarkImp::DenseRange(int start, int limit) {
CHECK(arg_count_ == -1 || arg_count_ == 1);
arg_count_ = 1;
CHECK_GE(start, 0);
@ -499,17 +523,15 @@ Benchmark* Benchmark::DenseRange(int start, int limit) {
for (int arg = start; arg <= limit; arg++) {
args_.emplace_back(arg, -1);
}
return this;
}
Benchmark* Benchmark::ArgPair(int x, int y) {
void BenchmarkImp::ArgPair(int x, int y) {
CHECK(arg_count_ == -1 || arg_count_ == 2);
arg_count_ = 2;
args_.emplace_back(x, y);
return this;
}
Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) {
void BenchmarkImp::RangePair(int lo1, int hi1, int lo2, int hi2) {
CHECK(arg_count_ == -1 || arg_count_ == 2);
arg_count_ = 2;
std::vector<int> arglist1, arglist2;
@ -521,35 +543,26 @@ Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) {
args_.emplace_back(i, j);
}
}
return this;
}
Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
custom_arguments(this);
return this;
}
Benchmark* Benchmark::Threads(int t) {
void BenchmarkImp::Threads(int t) {
CHECK_GT(t, 0);
thread_counts_.push_back(t);
return this;
}
Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
void BenchmarkImp::ThreadRange(int min_threads, int max_threads) {
CHECK_GT(min_threads, 0);
CHECK_GE(max_threads, min_threads);
AddRange(&thread_counts_, min_threads, max_threads, 2);
return this;
}
Benchmark* Benchmark::ThreadPerCpu() {
void BenchmarkImp::ThreadPerCpu() {
static int num_cpus = NumCPUs();
thread_counts_.push_back(num_cpus);
return this;
}
void Benchmark::AddRange(std::vector<int>* dst, int lo, int hi, int mult) {
void BenchmarkImp::AddRange(std::vector<int>* dst, int lo, int hi, int mult) {
CHECK_GE(lo, 0);
CHECK_GE(hi, lo);
@ -571,6 +584,60 @@ void Benchmark::AddRange(std::vector<int>* dst, int lo, int hi, int mult) {
}
}
Benchmark::Benchmark(const char* name, Function* f)
: imp_(new BenchmarkImp(name, f))
{
}
Benchmark::~Benchmark() {
delete imp_;
}
Benchmark* Benchmark::Arg(int x) {
imp_->Arg(x);
return this;
}
Benchmark* Benchmark::Range(int start, int limit) {
imp_->Range(start, limit);
return this;
}
Benchmark* Benchmark::DenseRange(int start, int limit) {
imp_->DenseRange(start, limit);
return this;
}
Benchmark* Benchmark::ArgPair(int x, int y) {
imp_->ArgPair(x, y);
return this;
}
Benchmark* Benchmark::RangePair(int lo1, int hi1, int lo2, int hi2) {
imp_->RangePair(lo1, hi1, lo2, hi2);
return this;
}
Benchmark* Benchmark::Apply(void (*custom_arguments)(Benchmark* benchmark)) {
custom_arguments(this);
return this;
}
Benchmark* Benchmark::Threads(int t) {
imp_->Threads(t);
return this;
}
Benchmark* Benchmark::ThreadRange(int min_threads, int max_threads) {
imp_->ThreadRange(min_threads, max_threads);
return this;
}
Benchmark* Benchmark::ThreadPerCpu() {
imp_->ThreadPerCpu();
return this;
}
} // end namespace internal
namespace {
@ -898,6 +965,9 @@ void RunMatchingBenchmarks(const std::string& spec,
} // end namespace internal
void RunSpecifiedBenchmarks() {
RunSpecifiedBenchmarks(nullptr);
}
void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter) {
std::string spec = FLAGS_benchmark_filter;

View File

@ -65,7 +65,7 @@ PlatformColorCode GetPlatformColorCode(LogColor color) {
case COLOR_WHITE:
return "7";
default:
return NULL;
return nullptr;
};
#endif
}

View File

@ -24,7 +24,7 @@ namespace benchmark {
// unchanged and returns false.
bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) {
// Parses the environment variable as a decimal integer.
char* end = NULL;
char* end = nullptr;
const long long_value = strtol(str, &end, 10); // NOLINT
// Has strtol() consumed all characters in the string?
@ -58,7 +58,7 @@ bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) {
// returns true; otherwise leaves *value unchanged and returns false.
bool ParseDouble(const std::string& src_text, const char* str, double* value) {
// Parses the environment variable as a decimal integer.
char* end = NULL;
char* end = nullptr;
const double double_value = strtod(str, &end); // NOLINT
// Has strtol() consumed all characters in the string?
@ -76,9 +76,9 @@ bool ParseDouble(const std::string& src_text, const char* str, double* value) {
inline const char* GetEnv(const char* name) {
#if defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
// Environment variables which we programmatically clear will be set to the
// empty string rather than unset (NULL). Handle that case.
// empty string rather than unset (nullptr). Handle that case.
const char* const env = getenv(name);
return (env != NULL && env[0] != '\0') ? env : NULL;
return (env != nullptr && env[0] != '\0') ? env : nullptr;
#else
return getenv(name);
#endif
@ -104,7 +104,7 @@ static std::string FlagToEnvVar(const char* flag) {
bool BoolFromEnv(const char* flag, bool default_value) {
const std::string env_var = FlagToEnvVar(flag);
const char* const string_value = GetEnv(env_var.c_str());
return string_value == NULL ? default_value : strcmp(string_value, "0") != 0;
return string_value == nullptr ? default_value : strcmp(string_value, "0") != 0;
}
// Reads and returns a 32-bit integer stored in the environment
@ -113,7 +113,7 @@ bool BoolFromEnv(const char* flag, bool default_value) {
int32_t Int32FromEnv(const char* flag, int32_t default_value) {
const std::string env_var = FlagToEnvVar(flag);
const char* const string_value = GetEnv(env_var.c_str());
if (string_value == NULL) {
if (string_value == nullptr) {
// The environment variable is not set.
return default_value;
}
@ -133,23 +133,23 @@ int32_t Int32FromEnv(const char* flag, int32_t default_value) {
const char* StringFromEnv(const char* flag, const char* default_value) {
const std::string env_var = FlagToEnvVar(flag);
const char* const value = GetEnv(env_var.c_str());
return value == NULL ? default_value : value;
return value == nullptr ? default_value : value;
}
// Parses a string as a command line flag. The string should have
// the format "--flag=value". When def_optional is true, the "=value"
// part can be omitted.
//
// Returns the value of the flag, or NULL if the parsing failed.
// Returns the value of the flag, or nullptr if the parsing failed.
const char* ParseFlagValue(const char* str, const char* flag,
bool def_optional) {
// str and flag must not be NULL.
if (str == NULL || flag == NULL) return NULL;
// str and flag must not be nullptr.
if (str == nullptr || flag == nullptr) return nullptr;
// The flag must start with "--".
const std::string flag_str = std::string("--") + std::string(flag);
const size_t flag_len = flag_str.length();
if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
if (strncmp(str, flag_str.c_str(), flag_len) != 0) return nullptr;
// Skips the flag name.
const char* flag_end = str + flag_len;
@ -160,7 +160,7 @@ const char* ParseFlagValue(const char* str, const char* flag,
// If def_optional is true and there are more characters after the
// flag name, or if def_optional is false, there must be a '=' after
// the flag name.
if (flag_end[0] != '=') return NULL;
if (flag_end[0] != '=') return nullptr;
// Returns the string after "=".
return flag_end + 1;
@ -171,7 +171,7 @@ bool ParseBoolFlag(const char* str, const char* flag, bool* value) {
const char* const value_str = ParseFlagValue(str, flag, true);
// Aborts if the parsing failed.
if (value_str == NULL) return false;
if (value_str == nullptr) return false;
// Converts the string value to a bool.
*value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
@ -183,7 +183,7 @@ bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) {
const char* const value_str = ParseFlagValue(str, flag, false);
// Aborts if the parsing failed.
if (value_str == NULL) return false;
if (value_str == nullptr) return false;
// Sets *value to the value of the flag.
return ParseInt32(std::string("The value of flag --") + flag, value_str,
@ -195,7 +195,7 @@ bool ParseDoubleFlag(const char* str, const char* flag, double* value) {
const char* const value_str = ParseFlagValue(str, flag, false);
// Aborts if the parsing failed.
if (value_str == NULL) return false;
if (value_str == nullptr) return false;
// Sets *value to the value of the flag.
return ParseDouble(std::string("The value of flag --") + flag, value_str,
@ -207,13 +207,13 @@ bool ParseStringFlag(const char* str, const char* flag, std::string* value) {
const char* const value_str = ParseFlagValue(str, flag, false);
// Aborts if the parsing failed.
if (value_str == NULL) return false;
if (value_str == nullptr) return false;
*value = value_str;
return true;
}
bool IsFlag(const char* str, const char* flag) {
return (ParseFlagValue(str, flag, true) != NULL);
return (ParseFlagValue(str, flag, true) != nullptr);
}
} // end namespace benchmark

View File

@ -113,13 +113,13 @@ inline BENCHMARK_ALWAYS_INLINE int64_t Now() {
}
#endif
struct timeval tv;
gettimeofday(&tv, NULL);
gettimeofday(&tv, nullptr);
return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
#elif defined(__mips__)
// mips apparently only allows rdtsc for superusers, so we fall
// back to gettimeofday. It's possible clock_gettime would be better.
struct timeval tv;
gettimeofday(&tv, NULL);
gettimeofday(&tv, nullptr);
return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
#else
// The soft failover to a generic implementation is automatic only for ARM.

View File

@ -37,7 +37,7 @@ class Regex {
// Compile a regular expression matcher from spec. Returns true on success.
//
// On failure (and if error is not NULL), error is populated with a human
// On failure (and if error is not nullptr), error is populated with a human
// readable error message if an error occurs.
bool Init(const std::string& spec, std::string* error);

View File

@ -23,7 +23,7 @@ bool Regex::Init(const std::string& spec, std::string* error) {
int ec = regcomp(&re_, spec.c_str(), REG_EXTENDED | REG_NOSUB);
if (ec != 0) {
if (error) {
size_t needed = regerror(ec, &re_, NULL, 0);
size_t needed = regerror(ec, &re_, nullptr, 0);
char* errbuf = new char[needed];
regerror(ec, &re_, errbuf, needed);
@ -53,7 +53,7 @@ bool Regex::Match(const std::string& str) {
return false;
}
return regexec(&re_, str.c_str(), 0, NULL, 0) == 0;
return regexec(&re_, str.c_str(), 0, nullptr, 0) == 0;
}
} // end namespace benchmark

View File

@ -137,7 +137,7 @@ void InitializeSystemInfo() {
memmove(line, line + oldlinelen + 1, sizeof(line) - (oldlinelen + 1));
// Terminate the new line, reading more if we can't find the newline
char* newline = strchr(line, '\n');
if (newline == NULL) {
if (newline == nullptr) {
const size_t linelen = strlen(line);
const size_t bytes_to_read = sizeof(line) - 1 - linelen;
CHECK(bytes_to_read > 0); // because the memmove recovered >=1 bytes
@ -145,7 +145,7 @@ void InitializeSystemInfo() {
line[linelen + chars_read] = '\0';
newline = strchr(line, '\n');
}
if (newline != NULL) *newline = '\0';
if (newline != nullptr) *newline = '\0';
// When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
// accept postive values. Some environments (virtual machines) report zero,
@ -216,7 +216,7 @@ void InitializeSystemInfo() {
#endif
size_t sz = sizeof(hz);
const char* sysctl_path = "machdep.tsc_freq";
if (sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0) {
if (sysctlbyname(sysctl_path, &hz, &sz, nullptr, 0) != 0) {
fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
sysctl_path, strerror(errno));
cpuinfo_cycles_per_second = EstimateCyclesPerSecond();
@ -236,7 +236,7 @@ void InitializeSystemInfo() {
SUCCEEDED(
SHGetValueA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"~MHz", NULL, &data, &data_size)))
"~MHz", nullptr, &data, &data_size)))
cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz
else
cpuinfo_cycles_per_second = EstimateCyclesPerSecond();
@ -303,7 +303,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double* cputime) {
cputime_fd = -1;
return false;
}
unsigned long long result = strtoull(buff, NULL, 0);
unsigned long long result = strtoull(buff, nullptr, 0);
if (result == (std::numeric_limits<unsigned long long>::max)()) {
close(cputime_fd);
cputime_fd = -1;

View File

@ -89,7 +89,7 @@ private:
WallTime Slow() const {
struct timeval tv;
gettimeofday(&tv, NULL);
gettimeofday(&tv, nullptr);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
@ -177,7 +177,7 @@ std::string Print(WallTime time, const char* format, bool local,
if (!SplitTimezone(time, local, &split, &subsecond)) {
snprintf(storage, sizeof(storage), "Invalid time: %f", time);
} else {
if (remainder_us != NULL) {
if (remainder_us != nullptr) {
*remainder_us = static_cast<int>((subsecond * 1000000) + 0.5);
if (*remainder_us > 999999) *remainder_us = 999999;
if (*remainder_us < 0) *remainder_us = 0;