| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "commandlineflags.h" | ||
|
|
||
| #include <cctype> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <limits> | ||
|
|
||
| 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. | ||
| bool ParseInt32(const std::string& src_text, const char* str, int32_t* value) { | ||
| // Parses the environment variable as a decimal integer. | ||
| char* end = nullptr; | ||
| const long long_value = strtol(str, &end, 10); // NOLINT | ||
|
|
||
| // Has strtol() consumed all characters in the string? | ||
| if (*end != '\0') { | ||
| // No - an invalid character was encountered. | ||
| std::cerr << src_text << " is expected to be a 32-bit integer, " | ||
| << "but actually has value \"" << str << "\".\n"; | ||
| return false; | ||
| } | ||
|
|
||
| // Is the parsed value in the range of an Int32? | ||
| const int32_t result = static_cast<int32_t>(long_value); | ||
| if (long_value == std::numeric_limits<long>::max() || | ||
| long_value == std::numeric_limits<long>::min() || | ||
| // The parsed value overflows as a long. (strtol() returns | ||
| // LONG_MAX or LONG_MIN when the input overflows.) | ||
| result != long_value | ||
| // The parsed value overflows as an Int32. | ||
| ) { | ||
| std::cerr << src_text << " is expected to be a 32-bit integer, " | ||
| << "but actually has value \"" << str << "\", " | ||
| << "which overflows.\n"; | ||
| return false; | ||
| } | ||
|
|
||
| *value = result; | ||
| return true; | ||
| } | ||
|
|
||
| // Parses 'str' for a double. If successful, writes the result to *value and | ||
| // 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 = nullptr; | ||
| const double double_value = strtod(str, &end); // NOLINT | ||
|
|
||
| // Has strtol() consumed all characters in the string? | ||
| if (*end != '\0') { | ||
| // No - an invalid character was encountered. | ||
| std::cerr << src_text << " is expected to be a double, " | ||
| << "but actually has value \"" << str << "\".\n"; | ||
| return false; | ||
| } | ||
|
|
||
| *value = double_value; | ||
| return true; | ||
| } | ||
|
|
||
| // Returns the name of the environment variable corresponding to the | ||
| // given flag. For example, FlagToEnvVar("foo") will return | ||
| // "BENCHMARK_FOO" in the open-source version. | ||
| static std::string FlagToEnvVar(const char* flag) { | ||
| const std::string flag_str(flag); | ||
|
|
||
| std::string env_var; | ||
| for (size_t i = 0; i != flag_str.length(); ++i) | ||
| env_var += static_cast<char>(::toupper(flag_str.c_str()[i])); | ||
|
|
||
| return "BENCHMARK_" + env_var; | ||
| } | ||
|
|
||
| // Reads and returns the Boolean environment variable corresponding to | ||
| // the given flag; if it's not set, returns default_value. | ||
| // | ||
| // The value is considered true iff it's not "0". | ||
| 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 == nullptr ? default_value | ||
| : strcmp(string_value, "0") != 0; | ||
| } | ||
|
|
||
| // Reads and returns a 32-bit integer stored in the environment | ||
| // variable corresponding to the given flag; if it isn't set or | ||
| // doesn't represent a valid 32-bit integer, returns 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 == nullptr) { | ||
| // The environment variable is not set. | ||
| return default_value; | ||
| } | ||
|
|
||
| int32_t result = default_value; | ||
| if (!ParseInt32(std::string("Environment variable ") + env_var, string_value, | ||
| &result)) { | ||
| std::cout << "The default value " << default_value << " is used.\n"; | ||
| return default_value; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Reads and returns the string environment variable corresponding to | ||
| // the given flag; if it's not set, returns 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 == 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 nullptr if the parsing failed. | ||
| const char* ParseFlagValue(const char* str, const char* flag, | ||
| bool def_optional) { | ||
| // 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 nullptr; | ||
|
|
||
| // Skips the flag name. | ||
| const char* flag_end = str + flag_len; | ||
|
|
||
| // When def_optional is true, it's OK to not have a "=value" part. | ||
| if (def_optional && (flag_end[0] == '\0')) return flag_end; | ||
|
|
||
| // 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 nullptr; | ||
|
|
||
| // Returns the string after "=". | ||
| return flag_end + 1; | ||
| } | ||
|
|
||
| bool ParseBoolFlag(const char* str, const char* flag, bool* value) { | ||
| // Gets the value of the flag as a string. | ||
| const char* const value_str = ParseFlagValue(str, flag, true); | ||
|
|
||
| // Aborts if the parsing failed. | ||
| if (value_str == nullptr) return false; | ||
|
|
||
| // Converts the string value to a bool. | ||
| *value = IsTruthyFlagValue(value_str); | ||
| return true; | ||
| } | ||
|
|
||
| bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) { | ||
| // Gets the value of the flag as a string. | ||
| const char* const value_str = ParseFlagValue(str, flag, false); | ||
|
|
||
| // Aborts if the parsing failed. | ||
| 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, | ||
| value); | ||
| } | ||
|
|
||
| bool ParseDoubleFlag(const char* str, const char* flag, double* value) { | ||
| // Gets the value of the flag as a string. | ||
| const char* const value_str = ParseFlagValue(str, flag, false); | ||
|
|
||
| // Aborts if the parsing failed. | ||
| 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, | ||
| value); | ||
| } | ||
|
|
||
| bool ParseStringFlag(const char* str, const char* flag, std::string* value) { | ||
| // Gets the value of the flag as a string. | ||
| const char* const value_str = ParseFlagValue(str, flag, false); | ||
|
|
||
| // Aborts if the parsing failed. | ||
| if (value_str == nullptr) return false; | ||
|
|
||
| *value = value_str; | ||
| return true; | ||
| } | ||
|
|
||
| bool IsFlag(const char* str, const char* flag) { | ||
| return (ParseFlagValue(str, flag, true) != nullptr); | ||
| } | ||
|
|
||
| bool IsTruthyFlagValue(const std::string& value) { | ||
| if (value.empty()) return true; | ||
| char ch = value[0]; | ||
| return isalnum(ch) && | ||
| !(ch == '0' || ch == 'f' || ch == 'F' || ch == 'n' || ch == 'N'); | ||
| } | ||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #ifndef BENCHMARK_COMMANDLINEFLAGS_H_ | ||
| #define BENCHMARK_COMMANDLINEFLAGS_H_ | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
|
|
||
| // Macro for referencing flags. | ||
| #define FLAG(name) FLAGS_##name | ||
|
|
||
| // Macros for declaring flags. | ||
| #define DECLARE_bool(name) extern bool FLAG(name) | ||
| #define DECLARE_int32(name) extern int32_t FLAG(name) | ||
| #define DECLARE_int64(name) extern int64_t FLAG(name) | ||
| #define DECLARE_double(name) extern double FLAG(name) | ||
| #define DECLARE_string(name) extern std::string FLAG(name) | ||
|
|
||
| // Macros for defining flags. | ||
| #define DEFINE_bool(name, default_val, doc) bool FLAG(name) = (default_val) | ||
| #define DEFINE_int32(name, default_val, doc) int32_t FLAG(name) = (default_val) | ||
| #define DEFINE_int64(name, default_val, doc) int64_t FLAG(name) = (default_val) | ||
| #define DEFINE_double(name, default_val, doc) double FLAG(name) = (default_val) | ||
| #define DEFINE_string(name, default_val, doc) \ | ||
| 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. | ||
| bool ParseInt32(const std::string& src_text, const char* str, int32_t* value); | ||
|
|
||
| // Parses a bool/Int32/string from the environment variable | ||
| // corresponding to the given Google Test flag. | ||
| bool BoolFromEnv(const char* flag, bool default_val); | ||
| int32_t Int32FromEnv(const char* flag, int32_t default_val); | ||
| double DoubleFromEnv(const char* flag, double default_val); | ||
| const char* StringFromEnv(const char* flag, const char* default_val); | ||
|
|
||
| // Parses a string for a bool flag, in the form of either | ||
| // "--flag=value" or "--flag". | ||
| // | ||
| // In the former case, the value is taken as true if it passes IsTruthyValue(). | ||
| // | ||
| // In the latter case, the value is taken as true. | ||
| // | ||
| // On success, stores the value of the flag in *value, and returns | ||
| // true. On failure, returns false without changing *value. | ||
| bool ParseBoolFlag(const char* str, const char* flag, bool* value); | ||
|
|
||
| // Parses a string for an Int32 flag, in the form of | ||
| // "--flag=value". | ||
| // | ||
| // On success, stores the value of the flag in *value, and returns | ||
| // true. On failure, returns false without changing *value. | ||
| bool ParseInt32Flag(const char* str, const char* flag, int32_t* value); | ||
|
|
||
| // Parses a string for a Double flag, in the form of | ||
| // "--flag=value". | ||
| // | ||
| // On success, stores the value of the flag in *value, and returns | ||
| // true. On failure, returns false without changing *value. | ||
| bool ParseDoubleFlag(const char* str, const char* flag, double* value); | ||
|
|
||
| // Parses a string for a string flag, in the form of | ||
| // "--flag=value". | ||
| // | ||
| // On success, stores the value of the flag in *value, and returns | ||
| // true. On failure, returns false without changing *value. | ||
| bool ParseStringFlag(const char* str, const char* flag, std::string* value); | ||
|
|
||
| // Returns true if the string matches the flag. | ||
| bool IsFlag(const char* str, const char* flag); | ||
|
|
||
| // Returns true unless value starts with one of: '0', 'f', 'F', 'n' or 'N', or | ||
| // some non-alphanumeric character. As a special case, also returns true if | ||
| // value is the empty string. | ||
| bool IsTruthyFlagValue(const std::string& value); | ||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_COMMANDLINEFLAGS_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| // Copyright 2016 Ismael Jimenez Martinez. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Source project : https://github.com/ismaelJimenez/cpp.leastsq | ||
| // Adapted to be used with google benchmark | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include "check.h" | ||
| #include "complexity.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // Internal function to calculate the different scalability forms | ||
| BigOFunc* FittingCurve(BigO complexity) { | ||
| switch (complexity) { | ||
| case oN: | ||
| return [](int64_t n) -> double { return static_cast<double>(n); }; | ||
| case oNSquared: | ||
| return [](int64_t n) -> double { return std::pow(n, 2); }; | ||
| case oNCubed: | ||
| return [](int64_t n) -> double { return std::pow(n, 3); }; | ||
| case oLogN: | ||
| return [](int64_t n) { return log2(n); }; | ||
| case oNLogN: | ||
| return [](int64_t n) { return n * log2(n); }; | ||
| case o1: | ||
| default: | ||
| return [](int64_t) { return 1.0; }; | ||
| } | ||
| } | ||
|
|
||
| // Function to return an string for the calculated complexity | ||
| std::string GetBigOString(BigO complexity) { | ||
| switch (complexity) { | ||
| case oN: | ||
| return "N"; | ||
| case oNSquared: | ||
| return "N^2"; | ||
| case oNCubed: | ||
| return "N^3"; | ||
| case oLogN: | ||
| return "lgN"; | ||
| case oNLogN: | ||
| return "NlgN"; | ||
| case o1: | ||
| return "(1)"; | ||
| default: | ||
| return "f(N)"; | ||
| } | ||
| } | ||
|
|
||
| // Find the coefficient for the high-order term in the running time, by | ||
| // minimizing the sum of squares of relative error, for the fitting curve | ||
| // given by the lambda expression. | ||
| // - n : Vector containing the size of the benchmark tests. | ||
| // - time : Vector containing the times for the benchmark tests. | ||
| // - fitting_curve : lambda expression (e.g. [](int64_t n) {return n; };). | ||
|
|
||
| // For a deeper explanation on the algorithm logic, look the README file at | ||
| // http://github.com/ismaelJimenez/Minimal-Cpp-Least-Squared-Fit | ||
|
|
||
| LeastSq MinimalLeastSq(const std::vector<int64_t>& n, | ||
| const std::vector<double>& time, | ||
| BigOFunc* fitting_curve) { | ||
| double sigma_gn_squared = 0.0; | ||
| double sigma_time = 0.0; | ||
| double sigma_time_gn = 0.0; | ||
|
|
||
| // Calculate least square fitting parameter | ||
| for (size_t i = 0; i < n.size(); ++i) { | ||
| double gn_i = fitting_curve(n[i]); | ||
| sigma_gn_squared += gn_i * gn_i; | ||
| sigma_time += time[i]; | ||
| sigma_time_gn += time[i] * gn_i; | ||
| } | ||
|
|
||
| LeastSq result; | ||
| result.complexity = oLambda; | ||
|
|
||
| // Calculate complexity. | ||
| result.coef = sigma_time_gn / sigma_gn_squared; | ||
|
|
||
| // Calculate RMS | ||
| double rms = 0.0; | ||
| for (size_t i = 0; i < n.size(); ++i) { | ||
| double fit = result.coef * fitting_curve(n[i]); | ||
| rms += pow((time[i] - fit), 2); | ||
| } | ||
|
|
||
| // Normalized RMS by the mean of the observed values | ||
| double mean = sigma_time / n.size(); | ||
| result.rms = sqrt(rms / n.size()) / mean; | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Find the coefficient for the high-order term in the running time, by | ||
| // minimizing the sum of squares of relative error. | ||
| // - n : Vector containing the size of the benchmark tests. | ||
| // - time : Vector containing the times for the benchmark tests. | ||
| // - complexity : If different than oAuto, the fitting curve will stick to | ||
| // this one. If it is oAuto, it will be calculated the best | ||
| // fitting curve. | ||
| LeastSq MinimalLeastSq(const std::vector<int64_t>& n, | ||
| const std::vector<double>& time, const BigO complexity) { | ||
| CHECK_EQ(n.size(), time.size()); | ||
| CHECK_GE(n.size(), 2); // Do not compute fitting curve is less than two | ||
| // benchmark runs are given | ||
| CHECK_NE(complexity, oNone); | ||
|
|
||
| LeastSq best_fit; | ||
|
|
||
| if (complexity == oAuto) { | ||
| std::vector<BigO> fit_curves = {oLogN, oN, oNLogN, oNSquared, oNCubed}; | ||
|
|
||
| // Take o1 as default best fitting curve | ||
| best_fit = MinimalLeastSq(n, time, FittingCurve(o1)); | ||
| best_fit.complexity = o1; | ||
|
|
||
| // Compute all possible fitting curves and stick to the best one | ||
| for (const auto& fit : fit_curves) { | ||
| LeastSq current_fit = MinimalLeastSq(n, time, FittingCurve(fit)); | ||
| if (current_fit.rms < best_fit.rms) { | ||
| best_fit = current_fit; | ||
| best_fit.complexity = fit; | ||
| } | ||
| } | ||
| } else { | ||
| best_fit = MinimalLeastSq(n, time, FittingCurve(complexity)); | ||
| best_fit.complexity = complexity; | ||
| } | ||
|
|
||
| return best_fit; | ||
| } | ||
|
|
||
| std::vector<BenchmarkReporter::Run> ComputeBigO( | ||
| const std::vector<BenchmarkReporter::Run>& reports) { | ||
| typedef BenchmarkReporter::Run Run; | ||
| std::vector<Run> results; | ||
|
|
||
| if (reports.size() < 2) return results; | ||
|
|
||
| // Accumulators. | ||
| std::vector<int64_t> n; | ||
| std::vector<double> real_time; | ||
| std::vector<double> cpu_time; | ||
|
|
||
| // Populate the accumulators. | ||
| for (const Run& run : reports) { | ||
| CHECK_GT(run.complexity_n, 0) << "Did you forget to call SetComplexityN?"; | ||
| n.push_back(run.complexity_n); | ||
| real_time.push_back(run.real_accumulated_time / run.iterations); | ||
| cpu_time.push_back(run.cpu_accumulated_time / run.iterations); | ||
| } | ||
|
|
||
| LeastSq result_cpu; | ||
| LeastSq result_real; | ||
|
|
||
| if (reports[0].complexity == oLambda) { | ||
| result_cpu = MinimalLeastSq(n, cpu_time, reports[0].complexity_lambda); | ||
| result_real = MinimalLeastSq(n, real_time, reports[0].complexity_lambda); | ||
| } else { | ||
| result_cpu = MinimalLeastSq(n, cpu_time, reports[0].complexity); | ||
| result_real = MinimalLeastSq(n, real_time, result_cpu.complexity); | ||
| } | ||
| std::string benchmark_name = | ||
| reports[0].benchmark_name.substr(0, reports[0].benchmark_name.find('/')); | ||
|
|
||
| // Get the data from the accumulator to BenchmarkReporter::Run's. | ||
| Run big_o; | ||
| big_o.benchmark_name = benchmark_name + "_BigO"; | ||
| big_o.iterations = 0; | ||
| big_o.real_accumulated_time = result_real.coef; | ||
| big_o.cpu_accumulated_time = result_cpu.coef; | ||
| big_o.report_big_o = true; | ||
| big_o.complexity = result_cpu.complexity; | ||
|
|
||
| // All the time results are reported after being multiplied by the | ||
| // time unit multiplier. But since RMS is a relative quantity it | ||
| // should not be multiplied at all. So, here, we _divide_ it by the | ||
| // multiplier so that when it is multiplied later the result is the | ||
| // correct one. | ||
| double multiplier = GetTimeUnitMultiplier(reports[0].time_unit); | ||
|
|
||
| // Only add label to mean/stddev if it is same for all runs | ||
| Run rms; | ||
| big_o.report_label = reports[0].report_label; | ||
| rms.benchmark_name = benchmark_name + "_RMS"; | ||
| rms.report_label = big_o.report_label; | ||
| rms.iterations = 0; | ||
| rms.real_accumulated_time = result_real.rms / multiplier; | ||
| rms.cpu_accumulated_time = result_cpu.rms / multiplier; | ||
| rms.report_rms = true; | ||
| rms.complexity = result_cpu.complexity; | ||
| // don't forget to keep the time unit, or we won't be able to | ||
| // recover the correct value. | ||
| rms.time_unit = reports[0].time_unit; | ||
|
|
||
| results.push_back(big_o); | ||
| results.push_back(rms); | ||
| return results; | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2016 Ismael Jimenez Martinez. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Source project : https://github.com/ismaelJimenez/cpp.leastsq | ||
| // Adapted to be used with google benchmark | ||
|
|
||
| #ifndef COMPLEXITY_H_ | ||
| #define COMPLEXITY_H_ | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // Return a vector containing the bigO and RMS information for the specified | ||
| // list of reports. If 'reports.size() < 2' an empty vector is returned. | ||
| std::vector<BenchmarkReporter::Run> ComputeBigO( | ||
| const std::vector<BenchmarkReporter::Run>& reports); | ||
|
|
||
| // This data structure will contain the result returned by MinimalLeastSq | ||
| // - coef : Estimated coeficient for the high-order term as | ||
| // interpolated from data. | ||
| // - rms : Normalized Root Mean Squared Error. | ||
| // - complexity : Scalability form (e.g. oN, oNLogN). In case a scalability | ||
| // form has been provided to MinimalLeastSq this will return | ||
| // the same value. In case BigO::oAuto has been selected, this | ||
| // parameter will return the best fitting curve detected. | ||
|
|
||
| struct LeastSq { | ||
| LeastSq() : coef(0.0), rms(0.0), complexity(oNone) {} | ||
|
|
||
| double coef; | ||
| double rms; | ||
| BigO complexity; | ||
| }; | ||
|
|
||
| // Function to return an string for the calculated complexity | ||
| std::string GetBigOString(BigO complexity); | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // COMPLEXITY_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "complexity.h" | ||
| #include "counter.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "check.h" | ||
| #include "colorprint.h" | ||
| #include "commandlineflags.h" | ||
| #include "internal_macros.h" | ||
| #include "string_util.h" | ||
| #include "timers.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| bool ConsoleReporter::ReportContext(const Context& context) { | ||
| name_field_width_ = context.name_field_width; | ||
| printed_header_ = false; | ||
| prev_counters_.clear(); | ||
|
|
||
| PrintBasicContext(&GetErrorStream(), context); | ||
|
|
||
| #ifdef BENCHMARK_OS_WINDOWS | ||
| if ((output_options_ & OO_Color) && &std::cout != &GetOutputStream()) { | ||
| GetErrorStream() | ||
| << "Color printing is only supported for stdout on windows." | ||
| " Disabling color printing\n"; | ||
| output_options_ = static_cast< OutputOptions >(output_options_ & ~OO_Color); | ||
| } | ||
| #endif | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void ConsoleReporter::PrintHeader(const Run& run) { | ||
| std::string str = FormatString("%-*s %13s %13s %10s", static_cast<int>(name_field_width_), | ||
| "Benchmark", "Time", "CPU", "Iterations"); | ||
| if(!run.counters.empty()) { | ||
| if(output_options_ & OO_Tabular) { | ||
| for(auto const& c : run.counters) { | ||
| str += FormatString(" %10s", c.first.c_str()); | ||
| } | ||
| } else { | ||
| str += " UserCounters..."; | ||
| } | ||
| } | ||
| str += "\n"; | ||
| std::string line = std::string(str.length(), '-'); | ||
| GetOutputStream() << line << "\n" << str << line << "\n"; | ||
| } | ||
|
|
||
| void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) { | ||
| for (const auto& run : reports) { | ||
| // print the header: | ||
| // --- if none was printed yet | ||
| bool print_header = !printed_header_; | ||
| // --- or if the format is tabular and this run | ||
| // has different fields from the prev header | ||
| print_header |= (output_options_ & OO_Tabular) && | ||
| (!internal::SameNames(run.counters, prev_counters_)); | ||
| if (print_header) { | ||
| printed_header_ = true; | ||
| prev_counters_ = run.counters; | ||
| PrintHeader(run); | ||
| } | ||
| // As an alternative to printing the headers like this, we could sort | ||
| // the benchmarks by header and then print. But this would require | ||
| // waiting for the full results before printing, or printing twice. | ||
| PrintRunData(run); | ||
| } | ||
| } | ||
|
|
||
| static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt, | ||
| ...) { | ||
| va_list args; | ||
| va_start(args, fmt); | ||
| out << FormatString(fmt, args); | ||
| va_end(args); | ||
| } | ||
|
|
||
| void ConsoleReporter::PrintRunData(const Run& result) { | ||
| typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...); | ||
| auto& Out = GetOutputStream(); | ||
| PrinterFn* printer = (output_options_ & OO_Color) ? | ||
| (PrinterFn*)ColorPrintf : IgnoreColorPrint; | ||
| auto name_color = | ||
| (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN; | ||
| printer(Out, name_color, "%-*s ", name_field_width_, | ||
| result.benchmark_name.c_str()); | ||
|
|
||
| if (result.error_occurred) { | ||
| printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'", | ||
| result.error_message.c_str()); | ||
| printer(Out, COLOR_DEFAULT, "\n"); | ||
| return; | ||
| } | ||
| // Format bytes per second | ||
| std::string rate; | ||
| if (result.bytes_per_second > 0) { | ||
| rate = StrCat(" ", HumanReadableNumber(result.bytes_per_second), "B/s"); | ||
| } | ||
|
|
||
| // Format items per second | ||
| std::string items; | ||
| if (result.items_per_second > 0) { | ||
| items = | ||
| StrCat(" ", HumanReadableNumber(result.items_per_second), " items/s"); | ||
| } | ||
|
|
||
| const double real_time = result.GetAdjustedRealTime(); | ||
| const double cpu_time = result.GetAdjustedCPUTime(); | ||
|
|
||
| if (result.report_big_o) { | ||
| std::string big_o = GetBigOString(result.complexity); | ||
| printer(Out, COLOR_YELLOW, "%10.2f %s %10.2f %s ", real_time, big_o.c_str(), | ||
| cpu_time, big_o.c_str()); | ||
| } else if (result.report_rms) { | ||
| printer(Out, COLOR_YELLOW, "%10.0f %% %10.0f %% ", real_time * 100, | ||
| cpu_time * 100); | ||
| } else { | ||
| const char* timeLabel = GetTimeUnitString(result.time_unit); | ||
| printer(Out, COLOR_YELLOW, "%10.0f %s %10.0f %s ", real_time, timeLabel, | ||
| cpu_time, timeLabel); | ||
| } | ||
|
|
||
| if (!result.report_big_o && !result.report_rms) { | ||
| printer(Out, COLOR_CYAN, "%10lld", result.iterations); | ||
| } | ||
|
|
||
| for (auto& c : result.counters) { | ||
| const std::size_t cNameLen = std::max(std::string::size_type(10), | ||
| c.first.length()); | ||
| auto const& s = HumanReadableNumber(c.second.value, 1000); | ||
| if (output_options_ & OO_Tabular) { | ||
| if (c.second.flags & Counter::kIsRate) { | ||
| printer(Out, COLOR_DEFAULT, " %*s/s", cNameLen - 2, s.c_str()); | ||
| } else { | ||
| printer(Out, COLOR_DEFAULT, " %*s", cNameLen, s.c_str()); | ||
| } | ||
| } else { | ||
| const char* unit = (c.second.flags & Counter::kIsRate) ? "/s" : ""; | ||
| printer(Out, COLOR_DEFAULT, " %s=%s%s", c.first.c_str(), s.c_str(), | ||
| unit); | ||
| } | ||
| } | ||
|
|
||
| if (!rate.empty()) { | ||
| printer(Out, COLOR_DEFAULT, " %*s", 13, rate.c_str()); | ||
| } | ||
|
|
||
| if (!items.empty()) { | ||
| printer(Out, COLOR_DEFAULT, " %*s", 18, items.c_str()); | ||
| } | ||
|
|
||
| if (!result.report_label.empty()) { | ||
| printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str()); | ||
| } | ||
|
|
||
| printer(Out, COLOR_DEFAULT, "\n"); | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "counter.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace internal { | ||
|
|
||
| double Finish(Counter const& c, double cpu_time, double num_threads) { | ||
| double v = c.value; | ||
| if (c.flags & Counter::kIsRate) { | ||
| v /= cpu_time; | ||
| } | ||
| if (c.flags & Counter::kAvgThreads) { | ||
| v /= num_threads; | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| void Finish(UserCounters *l, double cpu_time, double num_threads) { | ||
| for (auto &c : *l) { | ||
| c.second.value = Finish(c.second, cpu_time, num_threads); | ||
| } | ||
| } | ||
|
|
||
| void Increment(UserCounters *l, UserCounters const& r) { | ||
| // add counters present in both or just in *l | ||
| for (auto &c : *l) { | ||
| auto it = r.find(c.first); | ||
| if (it != r.end()) { | ||
| c.second.value = c.second + it->second; | ||
| } | ||
| } | ||
| // add counters present in r, but not in *l | ||
| for (auto const &tc : r) { | ||
| auto it = l->find(tc.first); | ||
| if (it == l->end()) { | ||
| (*l)[tc.first] = tc.second; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool SameNames(UserCounters const& l, UserCounters const& r) { | ||
| if (&l == &r) return true; | ||
| if (l.size() != r.size()) { | ||
| return false; | ||
| } | ||
| for (auto const& c : l) { | ||
| if (r.find(c.first) == r.end()) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // end namespace internal | ||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // these counter-related functions are hidden to reduce API surface. | ||
| namespace internal { | ||
| void Finish(UserCounters *l, double time, double num_threads); | ||
| void Increment(UserCounters *l, UserCounters const& r); | ||
| bool SameNames(UserCounters const& l, UserCounters const& r); | ||
| } // end namespace internal | ||
|
|
||
| } //end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "complexity.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "string_util.h" | ||
| #include "timers.h" | ||
| #include "check.h" | ||
|
|
||
| // File format reference: http://edoceo.com/utilitas/csv-file-format. | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| namespace { | ||
| std::vector<std::string> elements = { | ||
| "name", "iterations", "real_time", "cpu_time", | ||
| "time_unit", "bytes_per_second", "items_per_second", "label", | ||
| "error_occurred", "error_message"}; | ||
| } // namespace | ||
|
|
||
| bool CSVReporter::ReportContext(const Context& context) { | ||
| PrintBasicContext(&GetErrorStream(), context); | ||
| return true; | ||
| } | ||
|
|
||
| void CSVReporter::ReportRuns(const std::vector<Run> & reports) { | ||
| std::ostream& Out = GetOutputStream(); | ||
|
|
||
| if (!printed_header_) { | ||
| // save the names of all the user counters | ||
| for (const auto& run : reports) { | ||
| for (const auto& cnt : run.counters) { | ||
| user_counter_names_.insert(cnt.first); | ||
| } | ||
| } | ||
|
|
||
| // print the header | ||
| for (auto B = elements.begin(); B != elements.end();) { | ||
| Out << *B++; | ||
| if (B != elements.end()) Out << ","; | ||
| } | ||
| for (auto B = user_counter_names_.begin(); B != user_counter_names_.end();) { | ||
| Out << ",\"" << *B++ << "\""; | ||
| } | ||
| Out << "\n"; | ||
|
|
||
| printed_header_ = true; | ||
| } else { | ||
| // check that all the current counters are saved in the name set | ||
| for (const auto& run : reports) { | ||
| for (const auto& cnt : run.counters) { | ||
| CHECK(user_counter_names_.find(cnt.first) != user_counter_names_.end()) | ||
| << "All counters must be present in each run. " | ||
| << "Counter named \"" << cnt.first | ||
| << "\" was not in a run after being added to the header"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // print results for each run | ||
| for (const auto& run : reports) { | ||
| PrintRunData(run); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| void CSVReporter::PrintRunData(const Run & run) { | ||
| std::ostream& Out = GetOutputStream(); | ||
|
|
||
| // Field with embedded double-quote characters must be doubled and the field | ||
| // delimited with double-quotes. | ||
| std::string name = run.benchmark_name; | ||
| ReplaceAll(&name, "\"", "\"\""); | ||
| Out << '"' << name << "\","; | ||
| if (run.error_occurred) { | ||
| Out << std::string(elements.size() - 3, ','); | ||
| Out << "true,"; | ||
| std::string msg = run.error_message; | ||
| ReplaceAll(&msg, "\"", "\"\""); | ||
| Out << '"' << msg << "\"\n"; | ||
| return; | ||
| } | ||
|
|
||
| // Do not print iteration on bigO and RMS report | ||
| if (!run.report_big_o && !run.report_rms) { | ||
| Out << run.iterations; | ||
| } | ||
| Out << ","; | ||
|
|
||
| Out << run.GetAdjustedRealTime() << ","; | ||
| Out << run.GetAdjustedCPUTime() << ","; | ||
|
|
||
| // Do not print timeLabel on bigO and RMS report | ||
| if (run.report_big_o) { | ||
| Out << GetBigOString(run.complexity); | ||
| } else if (!run.report_rms) { | ||
| Out << GetTimeUnitString(run.time_unit); | ||
| } | ||
| Out << ","; | ||
|
|
||
| if (run.bytes_per_second > 0.0) { | ||
| Out << run.bytes_per_second; | ||
| } | ||
| Out << ","; | ||
| if (run.items_per_second > 0.0) { | ||
| Out << run.items_per_second; | ||
| } | ||
| Out << ","; | ||
| if (!run.report_label.empty()) { | ||
| // Field with embedded double-quote characters must be doubled and the field | ||
| // delimited with double-quotes. | ||
| std::string label = run.report_label; | ||
| ReplaceAll(&label, "\"", "\"\""); | ||
| Out << "\"" << label << "\""; | ||
| } | ||
| Out << ",,"; // for error_occurred and error_message | ||
|
|
||
| // Print user counters | ||
| for (const auto &ucn : user_counter_names_) { | ||
| auto it = run.counters.find(ucn); | ||
| if(it == run.counters.end()) { | ||
| Out << ","; | ||
| } else { | ||
| Out << "," << it->second; | ||
| } | ||
| } | ||
| Out << '\n'; | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // ---------------------------------------------------------------------- | ||
| // CycleClock | ||
| // A CycleClock tells you the current time in Cycles. The "time" | ||
| // is actually time since power-on. This is like time() but doesn't | ||
| // involve a system call and is much more precise. | ||
| // | ||
| // NOTE: Not all cpu/platform/kernel combinations guarantee that this | ||
| // clock increments at a constant rate or is synchronized across all logical | ||
| // cpus in a system. | ||
| // | ||
| // If you need the above guarantees, please consider using a different | ||
| // API. There are efforts to provide an interface which provides a millisecond | ||
| // granularity and implemented as a memory read. A memory read is generally | ||
| // cheaper than the CycleClock for many architectures. | ||
| // | ||
| // Also, in some out of order CPU implementations, the CycleClock is not | ||
| // serializing. So if you're trying to count at cycles granularity, your | ||
| // data might be inaccurate due to out of order instruction execution. | ||
| // ---------------------------------------------------------------------- | ||
|
|
||
| #ifndef BENCHMARK_CYCLECLOCK_H_ | ||
| #define BENCHMARK_CYCLECLOCK_H_ | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "internal_macros.h" | ||
|
|
||
| #if defined(BENCHMARK_OS_MACOSX) | ||
| #include <mach/mach_time.h> | ||
| #endif | ||
| // For MSVC, we want to use '_asm rdtsc' when possible (since it works | ||
| // with even ancient MSVC compilers), and when not possible the | ||
| // __rdtsc intrinsic, declared in <intrin.h>. Unfortunately, in some | ||
| // environments, <windows.h> and <intrin.h> have conflicting | ||
| // declarations of some other intrinsics, breaking compilation. | ||
| // Therefore, we simply declare __rdtsc ourselves. See also | ||
| // http://connect.microsoft.com/VisualStudio/feedback/details/262047 | ||
| #if defined(COMPILER_MSVC) && !defined(_M_IX86) | ||
| extern "C" uint64_t __rdtsc(); | ||
| #pragma intrinsic(__rdtsc) | ||
| #endif | ||
|
|
||
| #if !defined(BENCHMARK_OS_WINDOWS) || defined(BENCHMARK_OS_MINGW) | ||
| #include <sys/time.h> | ||
| #include <time.h> | ||
| #endif | ||
|
|
||
| #ifdef BENCHMARK_OS_EMSCRIPTEN | ||
| #include <emscripten.h> | ||
| #endif | ||
|
|
||
| namespace benchmark { | ||
| // NOTE: only i386 and x86_64 have been well tested. | ||
| // PPC, sparc, alpha, and ia64 are based on | ||
| // 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 | ||
| namespace cycleclock { | ||
| // This should return the number of cycles since power-on. Thread-safe. | ||
| inline BENCHMARK_ALWAYS_INLINE int64_t Now() { | ||
| #if defined(BENCHMARK_OS_MACOSX) | ||
| // this goes at the top because we need ALL Macs, regardless of | ||
| // architecture, to return the number of "mach time units" that | ||
| // have passed since startup. See sysinfo.cc where | ||
| // InitializeSystemInfo() sets the supposed cpu clock frequency of | ||
| // macs to the number of mach time units per second, not actual | ||
| // CPU clock frequency (which can change in the face of CPU | ||
| // frequency scaling). Also note that when the Mac sleeps, this | ||
| // counter pauses; it does not continue counting, nor does it | ||
| // reset to zero. | ||
| return mach_absolute_time(); | ||
| #elif defined(BENCHMARK_OS_EMSCRIPTEN) | ||
| // this goes above x86-specific code because old versions of Emscripten | ||
| // define __x86_64__, although they have nothing to do with it. | ||
| return static_cast<int64_t>(emscripten_get_now() * 1e+6); | ||
| #elif defined(__i386__) | ||
| int64_t ret; | ||
| __asm__ volatile("rdtsc" : "=A"(ret)); | ||
| return ret; | ||
| #elif defined(__x86_64__) || defined(__amd64__) | ||
| uint64_t low, high; | ||
| __asm__ volatile("rdtsc" : "=a"(low), "=d"(high)); | ||
| return (high << 32) | low; | ||
| #elif defined(__powerpc__) || defined(__ppc__) | ||
| // This returns a time-base, which is not always precisely a cycle-count. | ||
| #if defined(__powerpc64__) || defined(__ppc64__) | ||
| int64_t tb; | ||
| asm volatile("mfspr %0, 268" : "=r"(tb)); | ||
| return tb; | ||
| #else | ||
| uint32_t tbl, tbu0, tbu1; | ||
| asm volatile( | ||
| "mftbu %0\n" | ||
| "mftb %1\n" | ||
| "mftbu %2" | ||
| : "=r"(tbu0), "=r"(tbl), "=r"(tbu1)); | ||
| tbl &= -static_cast<int32_t>(tbu0 == tbu1); | ||
| // high 32 bits in tbu1; low 32 bits in tbl (tbu0 is no longer needed) | ||
| return (static_cast<uint64_t>(tbu1) << 32) | tbl; | ||
| #endif | ||
| #elif defined(__sparc__) | ||
| int64_t tick; | ||
| asm(".byte 0x83, 0x41, 0x00, 0x00"); | ||
| asm("mov %%g1, %0" : "=r"(tick)); | ||
| return tick; | ||
| #elif defined(__ia64__) | ||
| int64_t itc; | ||
| asm("mov %0 = ar.itc" : "=r"(itc)); | ||
| return itc; | ||
| #elif defined(COMPILER_MSVC) && defined(_M_IX86) | ||
| // Older MSVC compilers (like 7.x) don't seem to support the | ||
| // __rdtsc intrinsic properly, so I prefer to use _asm instead | ||
| // when I know it will work. Otherwise, I'll use __rdtsc and hope | ||
| // the code is being compiled with a non-ancient compiler. | ||
| _asm rdtsc | ||
| #elif defined(COMPILER_MSVC) | ||
| return __rdtsc(); | ||
| #elif defined(BENCHMARK_OS_NACL) | ||
| // Native Client validator on x86/x86-64 allows RDTSC instructions, | ||
| // and this case is handled above. Native Client validator on ARM | ||
| // rejects MRC instructions (used in the ARM-specific sequence below), | ||
| // so we handle it here. Portable Native Client compiles to | ||
| // architecture-agnostic bytecode, which doesn't provide any | ||
| // cycle counter access mnemonics. | ||
|
|
||
| // Native Client does not provide any API to access cycle counter. | ||
| // Use clock_gettime(CLOCK_MONOTONIC, ...) instead of gettimeofday | ||
| // because is provides nanosecond resolution (which is noticable at | ||
| // least for PNaCl modules running on x86 Mac & Linux). | ||
| // Initialize to always return 0 if clock_gettime fails. | ||
| struct timespec ts = { 0, 0 }; | ||
| clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| return static_cast<int64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec; | ||
| #elif defined(__aarch64__) | ||
| // System timer of ARMv8 runs at a different frequency than the CPU's. | ||
| // The frequency is fixed, typically in the range 1-50MHz. It can be | ||
| // read at CNTFRQ special register. We assume the OS has set up | ||
| // the virtual timer properly. | ||
| int64_t virtual_timer_value; | ||
| asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value)); | ||
| return virtual_timer_value; | ||
| #elif defined(__ARM_ARCH) | ||
| // V6 is the earliest arch that has a standard cyclecount | ||
| // Native Client validator doesn't allow MRC instructions. | ||
| #if (__ARM_ARCH >= 6) | ||
| uint32_t pmccntr; | ||
| uint32_t pmuseren; | ||
| uint32_t pmcntenset; | ||
| // Read the user mode perf monitor counter access permissions. | ||
| asm volatile("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren)); | ||
| if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. | ||
| asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset)); | ||
| if (pmcntenset & 0x80000000ul) { // Is it counting? | ||
| asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr)); | ||
| // The counter is set up to count every 64th cycle | ||
| return static_cast<int64_t>(pmccntr) * 64; // Should optimize to << 6 | ||
| } | ||
| } | ||
| #endif | ||
| struct timeval tv; | ||
| gettimeofday(&tv, nullptr); | ||
| return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; | ||
| #elif defined(__mips__) || defined(__m68k__) | ||
| // 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, nullptr); | ||
| return static_cast<int64_t>(tv.tv_sec) * 1000000 + tv.tv_usec; | ||
| #elif defined(__s390__) // Covers both s390 and s390x. | ||
| // Return the CPU clock. | ||
| uint64_t tsc; | ||
| asm("stck %0" : "=Q" (tsc) : : "cc"); | ||
| return tsc; | ||
| #elif defined(__riscv) // RISC-V | ||
| // Use RDCYCLE (and RDCYCLEH on riscv32) | ||
| #if __riscv_xlen == 32 | ||
| uint32_t cycles_lo, cycles_hi0, cycles_hi1; | ||
| // This asm also includes the PowerPC overflow handling strategy, as above. | ||
| // Implemented in assembly because Clang insisted on branching. | ||
| asm volatile( | ||
| "rdcycleh %0\n" | ||
| "rdcycle %1\n" | ||
| "rdcycleh %2\n" | ||
| "sub %0, %0, %2\n" | ||
| "seqz %0, %0\n" | ||
| "sub %0, zero, %0\n" | ||
| "and %1, %1, %0\n" | ||
| : "=r"(cycles_hi0), "=r"(cycles_lo), "=r"(cycles_hi1)); | ||
| return (static_cast<uint64_t>(cycles_hi1) << 32) | cycles_lo; | ||
| #else | ||
| uint64_t cycles; | ||
| asm volatile("rdcycle %0" : "=r"(cycles)); | ||
| return cycles; | ||
| #endif | ||
| #else | ||
| // The soft failover to a generic implementation is automatic only for ARM. | ||
| // For other platforms the developer is expected to make an attempt to create | ||
| // a fast implementation and use generic version if nothing better is available. | ||
| #error You need to define CycleTimer for your OS and CPU | ||
| #endif | ||
| } | ||
| } // end namespace cycleclock | ||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_CYCLECLOCK_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #ifndef BENCHMARK_INTERNAL_MACROS_H_ | ||
| #define BENCHMARK_INTERNAL_MACROS_H_ | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| #ifndef __has_feature | ||
| #define __has_feature(x) 0 | ||
| #endif | ||
|
|
||
| #if defined(__clang__) | ||
| #if !defined(COMPILER_CLANG) | ||
| #define COMPILER_CLANG | ||
| #endif | ||
| #elif defined(_MSC_VER) | ||
| #if !defined(COMPILER_MSVC) | ||
| #define COMPILER_MSVC | ||
| #endif | ||
| #elif defined(__GNUC__) | ||
| #if !defined(COMPILER_GCC) | ||
| #define COMPILER_GCC | ||
| #endif | ||
| #endif | ||
|
|
||
| #if __has_feature(cxx_attributes) | ||
| #define BENCHMARK_NORETURN [[noreturn]] | ||
| #elif defined(__GNUC__) | ||
| #define BENCHMARK_NORETURN __attribute__((noreturn)) | ||
| #elif defined(COMPILER_MSVC) | ||
| #define BENCHMARK_NORETURN __declspec(noreturn) | ||
| #else | ||
| #define BENCHMARK_NORETURN | ||
| #endif | ||
|
|
||
| #if defined(__CYGWIN__) | ||
| #define BENCHMARK_OS_CYGWIN 1 | ||
| #elif defined(_WIN32) | ||
| #define BENCHMARK_OS_WINDOWS 1 | ||
| #if defined(__MINGW32__) | ||
| #define BENCHMARK_OS_MINGW 1 | ||
| #endif | ||
| #elif defined(__APPLE__) | ||
| #define BENCHMARK_OS_APPLE 1 | ||
| #include "TargetConditionals.h" | ||
| #if defined(TARGET_OS_MAC) | ||
| #define BENCHMARK_OS_MACOSX 1 | ||
| #if defined(TARGET_OS_IPHONE) | ||
| #define BENCHMARK_OS_IOS 1 | ||
| #endif | ||
| #endif | ||
| #elif defined(__FreeBSD__) | ||
| #define BENCHMARK_OS_FREEBSD 1 | ||
| #elif defined(__NetBSD__) | ||
| #define BENCHMARK_OS_NETBSD 1 | ||
| #elif defined(__OpenBSD__) | ||
| #define BENCHMARK_OS_OPENBSD 1 | ||
| #elif defined(__linux__) | ||
| #define BENCHMARK_OS_LINUX 1 | ||
| #elif defined(__native_client__) | ||
| #define BENCHMARK_OS_NACL 1 | ||
| #elif defined(__EMSCRIPTEN__) | ||
| #define BENCHMARK_OS_EMSCRIPTEN 1 | ||
| #elif defined(__rtems__) | ||
| #define BENCHMARK_OS_RTEMS 1 | ||
| #elif defined(__Fuchsia__) | ||
| #define BENCHMARK_OS_FUCHSIA 1 | ||
| #elif defined (__SVR4) && defined (__sun) | ||
| #define BENCHMARK_OS_SOLARIS 1 | ||
| #endif | ||
|
|
||
| #if !__has_feature(cxx_exceptions) && !defined(__cpp_exceptions) \ | ||
| && !defined(__EXCEPTIONS) | ||
| #define BENCHMARK_HAS_NO_EXCEPTIONS | ||
| #endif | ||
|
|
||
| #if defined(COMPILER_CLANG) || defined(COMPILER_GCC) | ||
| #define BENCHMARK_MAYBE_UNUSED __attribute__((unused)) | ||
| #else | ||
| #define BENCHMARK_MAYBE_UNUSED | ||
| #endif | ||
|
|
||
| #endif // BENCHMARK_INTERNAL_MACROS_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "complexity.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
| #include <iomanip> // for setprecision | ||
| #include <limits> | ||
|
|
||
| #include "string_util.h" | ||
| #include "timers.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| namespace { | ||
|
|
||
| std::string FormatKV(std::string const& key, std::string const& value) { | ||
| return StrFormat("\"%s\": \"%s\"", key.c_str(), value.c_str()); | ||
| } | ||
|
|
||
| std::string FormatKV(std::string const& key, const char* value) { | ||
| return StrFormat("\"%s\": \"%s\"", key.c_str(), value); | ||
| } | ||
|
|
||
| std::string FormatKV(std::string const& key, bool value) { | ||
| return StrFormat("\"%s\": %s", key.c_str(), value ? "true" : "false"); | ||
| } | ||
|
|
||
| std::string FormatKV(std::string const& key, int64_t value) { | ||
| std::stringstream ss; | ||
| ss << '"' << key << "\": " << value; | ||
| return ss.str(); | ||
| } | ||
|
|
||
| std::string FormatKV(std::string const& key, double value) { | ||
| std::stringstream ss; | ||
| ss << '"' << key << "\": "; | ||
|
|
||
| const auto max_digits10 = std::numeric_limits<decltype (value)>::max_digits10; | ||
| const auto max_fractional_digits10 = max_digits10 - 1; | ||
|
|
||
| ss << std::scientific << std::setprecision(max_fractional_digits10) << value; | ||
| return ss.str(); | ||
| } | ||
|
|
||
| int64_t RoundDouble(double v) { return static_cast<int64_t>(v + 0.5); } | ||
|
|
||
| } // end namespace | ||
|
|
||
| bool JSONReporter::ReportContext(const Context& context) { | ||
| std::ostream& out = GetOutputStream(); | ||
|
|
||
| out << "{\n"; | ||
| std::string inner_indent(2, ' '); | ||
|
|
||
| // Open context block and print context information. | ||
| out << inner_indent << "\"context\": {\n"; | ||
| std::string indent(4, ' '); | ||
|
|
||
| std::string walltime_value = LocalDateTimeString(); | ||
| out << indent << FormatKV("date", walltime_value) << ",\n"; | ||
|
|
||
| if (Context::executable_name) { | ||
| out << indent << FormatKV("executable", Context::executable_name) << ",\n"; | ||
| } | ||
|
|
||
| CPUInfo const& info = context.cpu_info; | ||
| out << indent << FormatKV("num_cpus", static_cast<int64_t>(info.num_cpus)) | ||
| << ",\n"; | ||
| out << indent | ||
| << FormatKV("mhz_per_cpu", | ||
| RoundDouble(info.cycles_per_second / 1000000.0)) | ||
| << ",\n"; | ||
| out << indent << FormatKV("cpu_scaling_enabled", info.scaling_enabled) | ||
| << ",\n"; | ||
|
|
||
| out << indent << "\"caches\": [\n"; | ||
| indent = std::string(6, ' '); | ||
| std::string cache_indent(8, ' '); | ||
| for (size_t i = 0; i < info.caches.size(); ++i) { | ||
| auto& CI = info.caches[i]; | ||
| out << indent << "{\n"; | ||
| out << cache_indent << FormatKV("type", CI.type) << ",\n"; | ||
| out << cache_indent << FormatKV("level", static_cast<int64_t>(CI.level)) | ||
| << ",\n"; | ||
| out << cache_indent | ||
| << FormatKV("size", static_cast<int64_t>(CI.size) * 1000u) << ",\n"; | ||
| out << cache_indent | ||
| << FormatKV("num_sharing", static_cast<int64_t>(CI.num_sharing)) | ||
| << "\n"; | ||
| out << indent << "}"; | ||
| if (i != info.caches.size() - 1) out << ","; | ||
| out << "\n"; | ||
| } | ||
| indent = std::string(4, ' '); | ||
| out << indent << "],\n"; | ||
|
|
||
| #if defined(NDEBUG) | ||
| const char build_type[] = "release"; | ||
| #else | ||
| const char build_type[] = "debug"; | ||
| #endif | ||
| out << indent << FormatKV("library_build_type", build_type) << "\n"; | ||
| // Close context block and open the list of benchmarks. | ||
| out << inner_indent << "},\n"; | ||
| out << inner_indent << "\"benchmarks\": [\n"; | ||
| return true; | ||
| } | ||
|
|
||
| void JSONReporter::ReportRuns(std::vector<Run> const& reports) { | ||
| if (reports.empty()) { | ||
| return; | ||
| } | ||
| std::string indent(4, ' '); | ||
| std::ostream& out = GetOutputStream(); | ||
| if (!first_report_) { | ||
| out << ",\n"; | ||
| } | ||
| first_report_ = false; | ||
|
|
||
| for (auto it = reports.begin(); it != reports.end(); ++it) { | ||
| out << indent << "{\n"; | ||
| PrintRunData(*it); | ||
| out << indent << '}'; | ||
| auto it_cp = it; | ||
| if (++it_cp != reports.end()) { | ||
| out << ",\n"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void JSONReporter::Finalize() { | ||
| // Close the list of benchmarks and the top level object. | ||
| GetOutputStream() << "\n ]\n}\n"; | ||
| } | ||
|
|
||
| void JSONReporter::PrintRunData(Run const& run) { | ||
| std::string indent(6, ' '); | ||
| std::ostream& out = GetOutputStream(); | ||
| out << indent << FormatKV("name", run.benchmark_name) << ",\n"; | ||
| if (run.error_occurred) { | ||
| out << indent << FormatKV("error_occurred", run.error_occurred) << ",\n"; | ||
| out << indent << FormatKV("error_message", run.error_message) << ",\n"; | ||
| } | ||
| if (!run.report_big_o && !run.report_rms) { | ||
| out << indent << FormatKV("iterations", run.iterations) << ",\n"; | ||
| out << indent | ||
| << FormatKV("real_time", run.GetAdjustedRealTime()) | ||
| << ",\n"; | ||
| out << indent | ||
| << FormatKV("cpu_time", run.GetAdjustedCPUTime()); | ||
| out << ",\n" | ||
| << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit)); | ||
| } else if (run.report_big_o) { | ||
| out << indent | ||
| << FormatKV("cpu_coefficient", run.GetAdjustedCPUTime()) | ||
| << ",\n"; | ||
| out << indent | ||
| << FormatKV("real_coefficient", run.GetAdjustedRealTime()) | ||
| << ",\n"; | ||
| out << indent << FormatKV("big_o", GetBigOString(run.complexity)) << ",\n"; | ||
| out << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit)); | ||
| } else if (run.report_rms) { | ||
| out << indent | ||
| << FormatKV("rms", run.GetAdjustedCPUTime()); | ||
| } | ||
| if (run.bytes_per_second > 0.0) { | ||
| out << ",\n" | ||
| << indent | ||
| << FormatKV("bytes_per_second", run.bytes_per_second); | ||
| } | ||
| if (run.items_per_second > 0.0) { | ||
| out << ",\n" | ||
| << indent | ||
| << FormatKV("items_per_second", run.items_per_second); | ||
| } | ||
| for(auto &c : run.counters) { | ||
| out << ",\n" | ||
| << indent | ||
| << FormatKV(c.first, c.second); | ||
| } | ||
| if (!run.report_label.empty()) { | ||
| out << ",\n" << indent << FormatKV("label", run.report_label); | ||
| } | ||
| out << '\n'; | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #ifndef BENCHMARK_LOG_H_ | ||
| #define BENCHMARK_LOG_H_ | ||
|
|
||
| #include <iostream> | ||
| #include <ostream> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace internal { | ||
|
|
||
| typedef std::basic_ostream<char>&(EndLType)(std::basic_ostream<char>&); | ||
|
|
||
| class LogType { | ||
| friend LogType& GetNullLogInstance(); | ||
| friend LogType& GetErrorLogInstance(); | ||
|
|
||
| // FIXME: Add locking to output. | ||
| template <class Tp> | ||
| friend LogType& operator<<(LogType&, Tp const&); | ||
| friend LogType& operator<<(LogType&, EndLType*); | ||
|
|
||
| private: | ||
| LogType(std::ostream* out) : out_(out) {} | ||
| std::ostream* out_; | ||
| BENCHMARK_DISALLOW_COPY_AND_ASSIGN(LogType); | ||
| }; | ||
|
|
||
| template <class Tp> | ||
| LogType& operator<<(LogType& log, Tp const& value) { | ||
| if (log.out_) { | ||
| *log.out_ << value; | ||
| } | ||
| return log; | ||
| } | ||
|
|
||
| inline LogType& operator<<(LogType& log, EndLType* m) { | ||
| if (log.out_) { | ||
| *log.out_ << m; | ||
| } | ||
| return log; | ||
| } | ||
|
|
||
| inline int& LogLevel() { | ||
| static int log_level = 0; | ||
| return log_level; | ||
| } | ||
|
|
||
| inline LogType& GetNullLogInstance() { | ||
| static LogType log(nullptr); | ||
| return log; | ||
| } | ||
|
|
||
| inline LogType& GetErrorLogInstance() { | ||
| static LogType log(&std::clog); | ||
| return log; | ||
| } | ||
|
|
||
| inline LogType& GetLogInstanceForLevel(int level) { | ||
| if (level <= LogLevel()) { | ||
| return GetErrorLogInstance(); | ||
| } | ||
| return GetNullLogInstance(); | ||
| } | ||
|
|
||
| } // end namespace internal | ||
| } // end namespace benchmark | ||
|
|
||
| #define VLOG(x) \ | ||
| (::benchmark::internal::GetLogInstanceForLevel(x) << "-- LOG(" << x << "):" \ | ||
| " ") | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #ifndef BENCHMARK_MUTEX_H_ | ||
| #define BENCHMARK_MUTEX_H_ | ||
|
|
||
| #include <condition_variable> | ||
| #include <mutex> | ||
|
|
||
| #include "check.h" | ||
|
|
||
| // Enable thread safety attributes only with clang. | ||
| // The attributes can be safely erased when compiling with other compilers. | ||
| #if defined(HAVE_THREAD_SAFETY_ATTRIBUTES) | ||
| #define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x)) | ||
| #else | ||
| #define THREAD_ANNOTATION_ATTRIBUTE__(x) // no-op | ||
| #endif | ||
|
|
||
| #define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(capability(x)) | ||
|
|
||
| #define SCOPED_CAPABILITY THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable) | ||
|
|
||
| #define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x)) | ||
|
|
||
| #define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x)) | ||
|
|
||
| #define ACQUIRED_BEFORE(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__)) | ||
|
|
||
| #define ACQUIRED_AFTER(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__)) | ||
|
|
||
| #define REQUIRES(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__)) | ||
|
|
||
| #define REQUIRES_SHARED(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__)) | ||
|
|
||
| #define ACQUIRE(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__)) | ||
|
|
||
| #define ACQUIRE_SHARED(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__)) | ||
|
|
||
| #define RELEASE(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__)) | ||
|
|
||
| #define RELEASE_SHARED(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__)) | ||
|
|
||
| #define TRY_ACQUIRE(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__)) | ||
|
|
||
| #define TRY_ACQUIRE_SHARED(...) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__)) | ||
|
|
||
| #define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__)) | ||
|
|
||
| #define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x)) | ||
|
|
||
| #define ASSERT_SHARED_CAPABILITY(x) \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x)) | ||
|
|
||
| #define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x)) | ||
|
|
||
| #define NO_THREAD_SAFETY_ANALYSIS \ | ||
| THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis) | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| typedef std::condition_variable Condition; | ||
|
|
||
| // NOTE: Wrappers for std::mutex and std::unique_lock are provided so that | ||
| // we can annotate them with thread safety attributes and use the | ||
| // -Wthread-safety warning with clang. The standard library types cannot be | ||
| // used directly because they do not provided the required annotations. | ||
| class CAPABILITY("mutex") Mutex { | ||
| public: | ||
| Mutex() {} | ||
|
|
||
| void lock() ACQUIRE() { mut_.lock(); } | ||
| void unlock() RELEASE() { mut_.unlock(); } | ||
| std::mutex& native_handle() { return mut_; } | ||
|
|
||
| private: | ||
| std::mutex mut_; | ||
| }; | ||
|
|
||
| class SCOPED_CAPABILITY MutexLock { | ||
| typedef std::unique_lock<std::mutex> MutexLockImp; | ||
|
|
||
| public: | ||
| MutexLock(Mutex& m) ACQUIRE(m) : ml_(m.native_handle()) {} | ||
| ~MutexLock() RELEASE() {} | ||
| MutexLockImp& native_handle() { return ml_; } | ||
|
|
||
| private: | ||
| MutexLockImp ml_; | ||
| }; | ||
|
|
||
| class Barrier { | ||
| public: | ||
| Barrier(int num_threads) : running_threads_(num_threads) {} | ||
|
|
||
| // Called by each thread | ||
| bool wait() EXCLUDES(lock_) { | ||
| bool last_thread = false; | ||
| { | ||
| MutexLock ml(lock_); | ||
| last_thread = createBarrier(ml); | ||
| } | ||
| if (last_thread) phase_condition_.notify_all(); | ||
| return last_thread; | ||
| } | ||
|
|
||
| void removeThread() EXCLUDES(lock_) { | ||
| MutexLock ml(lock_); | ||
| --running_threads_; | ||
| if (entered_ != 0) phase_condition_.notify_all(); | ||
| } | ||
|
|
||
| private: | ||
| Mutex lock_; | ||
| Condition phase_condition_; | ||
| int running_threads_; | ||
|
|
||
| // State for barrier management | ||
| int phase_number_ = 0; | ||
| int entered_ = 0; // Number of threads that have entered this barrier | ||
|
|
||
| // Enter the barrier and wait until all other threads have also | ||
| // entered the barrier. Returns iff this is the last thread to | ||
| // enter the barrier. | ||
| bool createBarrier(MutexLock& ml) REQUIRES(lock_) { | ||
| CHECK_LT(entered_, running_threads_); | ||
| entered_++; | ||
| if (entered_ < running_threads_) { | ||
| // Wait for all threads to enter | ||
| int phase_number_cp = phase_number_; | ||
| auto cb = [this, phase_number_cp]() { | ||
| return this->phase_number_ > phase_number_cp || | ||
| entered_ == running_threads_; // A thread has aborted in error | ||
| }; | ||
| phase_condition_.wait(ml.native_handle(), cb); | ||
| if (phase_number_ > phase_number_cp) return false; | ||
| // else (running_threads_ == entered_) and we are the last thread. | ||
| } | ||
| // Last thread has reached the barrier | ||
| phase_number_++; | ||
| entered_ = 0; | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_MUTEX_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef BENCHMARK_RE_H_ | ||
| #define BENCHMARK_RE_H_ | ||
|
|
||
| #include "internal_macros.h" | ||
|
|
||
| #if !defined(HAVE_STD_REGEX) && \ | ||
| !defined(HAVE_GNU_POSIX_REGEX) && \ | ||
| !defined(HAVE_POSIX_REGEX) | ||
| // No explicit regex selection; detect based on builtin hints. | ||
| #if defined(BENCHMARK_OS_LINUX) || defined(BENCHMARK_OS_APPLE) | ||
| #define HAVE_POSIX_REGEX 1 | ||
| #elif __cplusplus >= 199711L | ||
| #define HAVE_STD_REGEX 1 | ||
| #endif | ||
| #endif | ||
|
|
||
| // Prefer C regex libraries when compiling w/o exceptions so that we can | ||
| // correctly report errors. | ||
| #if defined(BENCHMARK_HAS_NO_EXCEPTIONS) && \ | ||
| defined(BENCHMARK_HAVE_STD_REGEX) && \ | ||
| (defined(HAVE_GNU_POSIX_REGEX) || defined(HAVE_POSIX_REGEX)) | ||
| #undef HAVE_STD_REGEX | ||
| #endif | ||
|
|
||
| #if defined(HAVE_STD_REGEX) | ||
| #include <regex> | ||
| #elif defined(HAVE_GNU_POSIX_REGEX) | ||
| #include <gnuregex.h> | ||
| #elif defined(HAVE_POSIX_REGEX) | ||
| #include <regex.h> | ||
| #else | ||
| #error No regular expression backend was found! | ||
| #endif | ||
| #include <string> | ||
|
|
||
| #include "check.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // A wrapper around the POSIX regular expression API that provides automatic | ||
| // cleanup | ||
| class Regex { | ||
| public: | ||
| Regex() : init_(false) {} | ||
|
|
||
| ~Regex(); | ||
|
|
||
| // Compile a regular expression matcher from spec. Returns true on success. | ||
| // | ||
| // 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); | ||
|
|
||
| // Returns whether str matches the compiled regular expression. | ||
| bool Match(const std::string& str); | ||
|
|
||
| private: | ||
| bool init_; | ||
| // Underlying regular expression object | ||
| #if defined(HAVE_STD_REGEX) | ||
| std::regex re_; | ||
| #elif defined(HAVE_POSIX_REGEX) || defined(HAVE_GNU_POSIX_REGEX) | ||
| regex_t re_; | ||
| #else | ||
| #error No regular expression backend implementation available | ||
| #endif | ||
| }; | ||
|
|
||
| #if defined(HAVE_STD_REGEX) | ||
|
|
||
| inline bool Regex::Init(const std::string& spec, std::string* error) { | ||
| #ifdef BENCHMARK_HAS_NO_EXCEPTIONS | ||
| ((void)error); // suppress unused warning | ||
| #else | ||
| try { | ||
| #endif | ||
| re_ = std::regex(spec, std::regex_constants::extended); | ||
| init_ = true; | ||
| #ifndef BENCHMARK_HAS_NO_EXCEPTIONS | ||
| } catch (const std::regex_error& e) { | ||
| if (error) { | ||
| *error = e.what(); | ||
| } | ||
| } | ||
| #endif | ||
| return init_; | ||
| } | ||
|
|
||
| inline Regex::~Regex() {} | ||
|
|
||
| inline bool Regex::Match(const std::string& str) { | ||
| if (!init_) { | ||
| return false; | ||
| } | ||
| return std::regex_search(str, re_); | ||
| } | ||
|
|
||
| #else | ||
| inline 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_, nullptr, 0); | ||
| char* errbuf = new char[needed]; | ||
| regerror(ec, &re_, errbuf, needed); | ||
|
|
||
| // regerror returns the number of bytes necessary to null terminate | ||
| // the string, so we move that when assigning to error. | ||
| CHECK_NE(needed, 0); | ||
| error->assign(errbuf, needed - 1); | ||
|
|
||
| delete[] errbuf; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| init_ = true; | ||
| return true; | ||
| } | ||
|
|
||
| inline Regex::~Regex() { | ||
| if (init_) { | ||
| regfree(&re_); | ||
| } | ||
| } | ||
|
|
||
| inline bool Regex::Match(const std::string& str) { | ||
| if (!init_) { | ||
| return false; | ||
| } | ||
| return regexec(&re_, str.c_str(), 0, nullptr, 0) == 0; | ||
| } | ||
| #endif | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_RE_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "timers.h" | ||
|
|
||
| #include <cstdlib> | ||
|
|
||
| #include <iostream> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "check.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| BenchmarkReporter::BenchmarkReporter() | ||
| : output_stream_(&std::cout), error_stream_(&std::cerr) {} | ||
|
|
||
| BenchmarkReporter::~BenchmarkReporter() {} | ||
|
|
||
| void BenchmarkReporter::PrintBasicContext(std::ostream *out, | ||
| Context const &context) { | ||
| CHECK(out) << "cannot be null"; | ||
| auto &Out = *out; | ||
|
|
||
| Out << LocalDateTimeString() << "\n"; | ||
|
|
||
| if (context.executable_name) | ||
| Out << "Running " << context.executable_name << "\n"; | ||
|
|
||
| const CPUInfo &info = context.cpu_info; | ||
| Out << "Run on (" << info.num_cpus << " X " | ||
| << (info.cycles_per_second / 1000000.0) << " MHz CPU " | ||
| << ((info.num_cpus > 1) ? "s" : "") << ")\n"; | ||
| if (info.caches.size() != 0) { | ||
| Out << "CPU Caches:\n"; | ||
| for (auto &CInfo : info.caches) { | ||
| Out << " L" << CInfo.level << " " << CInfo.type << " " | ||
| << (CInfo.size / 1000) << "K"; | ||
| if (CInfo.num_sharing != 0) | ||
| Out << " (x" << (info.num_cpus / CInfo.num_sharing) << ")"; | ||
| Out << "\n"; | ||
| } | ||
| } | ||
|
|
||
| if (info.scaling_enabled) { | ||
| Out << "***WARNING*** CPU scaling is enabled, the benchmark " | ||
| "real time measurements may be noisy and will incur extra " | ||
| "overhead.\n"; | ||
| } | ||
|
|
||
| #ifndef NDEBUG | ||
| Out << "***WARNING*** Library was built as DEBUG. Timings may be " | ||
| "affected.\n"; | ||
| #endif | ||
| } | ||
|
|
||
| // No initializer because it's already initialized to NULL. | ||
| const char* BenchmarkReporter::Context::executable_name; | ||
|
|
||
| BenchmarkReporter::Context::Context() : cpu_info(CPUInfo::Get()) {} | ||
|
|
||
| double BenchmarkReporter::Run::GetAdjustedRealTime() const { | ||
| double new_time = real_accumulated_time * GetTimeUnitMultiplier(time_unit); | ||
| if (iterations != 0) new_time /= static_cast<double>(iterations); | ||
| return new_time; | ||
| } | ||
|
|
||
| double BenchmarkReporter::Run::GetAdjustedCPUTime() const { | ||
| double new_time = cpu_accumulated_time * GetTimeUnitMultiplier(time_unit); | ||
| if (iterations != 0) new_time /= static_cast<double>(iterations); | ||
| return new_time; | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "sleep.h" | ||
|
|
||
| #include <cerrno> | ||
| #include <cstdlib> | ||
| #include <ctime> | ||
|
|
||
| #include "internal_macros.h" | ||
|
|
||
| #ifdef BENCHMARK_OS_WINDOWS | ||
| #include <windows.h> | ||
| #endif | ||
|
|
||
| namespace benchmark { | ||
| #ifdef BENCHMARK_OS_WINDOWS | ||
| // Window's Sleep takes milliseconds argument. | ||
| void SleepForMilliseconds(int milliseconds) { Sleep(milliseconds); } | ||
| void SleepForSeconds(double seconds) { | ||
| SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds)); | ||
| } | ||
| #else // BENCHMARK_OS_WINDOWS | ||
| void SleepForMicroseconds(int microseconds) { | ||
| struct timespec sleep_time; | ||
| sleep_time.tv_sec = microseconds / kNumMicrosPerSecond; | ||
| sleep_time.tv_nsec = (microseconds % kNumMicrosPerSecond) * kNumNanosPerMicro; | ||
| while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) | ||
| ; // Ignore signals and wait for the full interval to elapse. | ||
| } | ||
|
|
||
| void SleepForMilliseconds(int milliseconds) { | ||
| SleepForMicroseconds(milliseconds * kNumMicrosPerMilli); | ||
| } | ||
|
|
||
| void SleepForSeconds(double seconds) { | ||
| SleepForMicroseconds(static_cast<int>(seconds * kNumMicrosPerSecond)); | ||
| } | ||
| #endif // BENCHMARK_OS_WINDOWS | ||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #ifndef BENCHMARK_SLEEP_H_ | ||
| #define BENCHMARK_SLEEP_H_ | ||
|
|
||
| namespace benchmark { | ||
| const int kNumMillisPerSecond = 1000; | ||
| const int kNumMicrosPerMilli = 1000; | ||
| const int kNumMicrosPerSecond = kNumMillisPerSecond * 1000; | ||
| const int kNumNanosPerMicro = 1000; | ||
| const int kNumNanosPerSecond = kNumNanosPerMicro * kNumMicrosPerSecond; | ||
|
|
||
| void SleepForMilliseconds(int milliseconds); | ||
| void SleepForSeconds(double seconds); | ||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_SLEEP_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright 2016 Ismael Jimenez Martinez. All rights reserved. | ||
| // Copyright 2017 Roman Lebedev. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <string> | ||
| #include <vector> | ||
| #include <numeric> | ||
| #include "check.h" | ||
| #include "statistics.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| auto StatisticsSum = [](const std::vector<double>& v) { | ||
| return std::accumulate(v.begin(), v.end(), 0.0); | ||
| }; | ||
|
|
||
| double StatisticsMean(const std::vector<double>& v) { | ||
| if (v.empty()) return 0.0; | ||
| return StatisticsSum(v) * (1.0 / v.size()); | ||
| } | ||
|
|
||
| double StatisticsMedian(const std::vector<double>& v) { | ||
| if (v.size() < 3) return StatisticsMean(v); | ||
| std::vector<double> copy(v); | ||
|
|
||
| auto center = copy.begin() + v.size() / 2; | ||
| std::nth_element(copy.begin(), center, copy.end()); | ||
|
|
||
| // did we have an odd number of samples? | ||
| // if yes, then center is the median | ||
| // it no, then we are looking for the average between center and the value before | ||
| if(v.size() % 2 == 1) | ||
| return *center; | ||
| auto center2 = copy.begin() + v.size() / 2 - 1; | ||
| std::nth_element(copy.begin(), center2, copy.end()); | ||
| return (*center + *center2) / 2.0; | ||
| } | ||
|
|
||
| // Return the sum of the squares of this sample set | ||
| auto SumSquares = [](const std::vector<double>& v) { | ||
| return std::inner_product(v.begin(), v.end(), v.begin(), 0.0); | ||
| }; | ||
|
|
||
| auto Sqr = [](const double dat) { return dat * dat; }; | ||
| auto Sqrt = [](const double dat) { | ||
| // Avoid NaN due to imprecision in the calculations | ||
| if (dat < 0.0) return 0.0; | ||
| return std::sqrt(dat); | ||
| }; | ||
|
|
||
| double StatisticsStdDev(const std::vector<double>& v) { | ||
| const auto mean = StatisticsMean(v); | ||
| if (v.empty()) return mean; | ||
|
|
||
| // Sample standard deviation is undefined for n = 1 | ||
| if (v.size() == 1) | ||
| return 0.0; | ||
|
|
||
| const double avg_squares = SumSquares(v) * (1.0 / v.size()); | ||
| return Sqrt(v.size() / (v.size() - 1.0) * (avg_squares - Sqr(mean))); | ||
| } | ||
|
|
||
| std::vector<BenchmarkReporter::Run> ComputeStats( | ||
| const std::vector<BenchmarkReporter::Run>& reports) { | ||
| typedef BenchmarkReporter::Run Run; | ||
| std::vector<Run> results; | ||
|
|
||
| auto error_count = | ||
| std::count_if(reports.begin(), reports.end(), | ||
| [](Run const& run) { return run.error_occurred; }); | ||
|
|
||
| if (reports.size() - error_count < 2) { | ||
| // We don't report aggregated data if there was a single run. | ||
| return results; | ||
| } | ||
|
|
||
| // Accumulators. | ||
| std::vector<double> real_accumulated_time_stat; | ||
| std::vector<double> cpu_accumulated_time_stat; | ||
| std::vector<double> bytes_per_second_stat; | ||
| std::vector<double> items_per_second_stat; | ||
|
|
||
| real_accumulated_time_stat.reserve(reports.size()); | ||
| cpu_accumulated_time_stat.reserve(reports.size()); | ||
| bytes_per_second_stat.reserve(reports.size()); | ||
| items_per_second_stat.reserve(reports.size()); | ||
|
|
||
| // All repetitions should be run with the same number of iterations so we | ||
| // can take this information from the first benchmark. | ||
| int64_t const run_iterations = reports.front().iterations; | ||
| // create stats for user counters | ||
| struct CounterStat { | ||
| Counter c; | ||
| std::vector<double> s; | ||
| }; | ||
| std::map< std::string, CounterStat > counter_stats; | ||
| for(Run const& r : reports) { | ||
| for(auto const& cnt : r.counters) { | ||
| auto it = counter_stats.find(cnt.first); | ||
| if(it == counter_stats.end()) { | ||
| counter_stats.insert({cnt.first, {cnt.second, std::vector<double>{}}}); | ||
| it = counter_stats.find(cnt.first); | ||
| it->second.s.reserve(reports.size()); | ||
| } else { | ||
| CHECK_EQ(counter_stats[cnt.first].c.flags, cnt.second.flags); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Populate the accumulators. | ||
| for (Run const& run : reports) { | ||
| CHECK_EQ(reports[0].benchmark_name, run.benchmark_name); | ||
| CHECK_EQ(run_iterations, run.iterations); | ||
| if (run.error_occurred) continue; | ||
| real_accumulated_time_stat.emplace_back(run.real_accumulated_time); | ||
| cpu_accumulated_time_stat.emplace_back(run.cpu_accumulated_time); | ||
| items_per_second_stat.emplace_back(run.items_per_second); | ||
| bytes_per_second_stat.emplace_back(run.bytes_per_second); | ||
| // user counters | ||
| for(auto const& cnt : run.counters) { | ||
| auto it = counter_stats.find(cnt.first); | ||
| CHECK_NE(it, counter_stats.end()); | ||
| it->second.s.emplace_back(cnt.second); | ||
| } | ||
| } | ||
|
|
||
| // Only add label if it is same for all runs | ||
| std::string report_label = reports[0].report_label; | ||
| for (std::size_t i = 1; i < reports.size(); i++) { | ||
| if (reports[i].report_label != report_label) { | ||
| report_label = ""; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| for(const auto& Stat : *reports[0].statistics) { | ||
| // Get the data from the accumulator to BenchmarkReporter::Run's. | ||
| Run data; | ||
| data.benchmark_name = reports[0].benchmark_name + "_" + Stat.name_; | ||
| data.report_label = report_label; | ||
| data.iterations = run_iterations; | ||
|
|
||
| data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat); | ||
| data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat); | ||
| data.bytes_per_second = Stat.compute_(bytes_per_second_stat); | ||
| data.items_per_second = Stat.compute_(items_per_second_stat); | ||
|
|
||
| data.time_unit = reports[0].time_unit; | ||
|
|
||
| // user counters | ||
| for(auto const& kv : counter_stats) { | ||
| const auto uc_stat = Stat.compute_(kv.second.s); | ||
| auto c = Counter(uc_stat, counter_stats[kv.first].c.flags); | ||
| data.counters[kv.first] = c; | ||
| } | ||
|
|
||
| results.push_back(data); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2016 Ismael Jimenez Martinez. All rights reserved. | ||
| // Copyright 2017 Roman Lebedev. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef STATISTICS_H_ | ||
| #define STATISTICS_H_ | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // Return a vector containing the mean, median and standard devation information | ||
| // (and any user-specified info) for the specified list of reports. If 'reports' | ||
| // contains less than two non-errored runs an empty vector is returned | ||
| std::vector<BenchmarkReporter::Run> ComputeStats( | ||
| const std::vector<BenchmarkReporter::Run>& reports); | ||
|
|
||
| double StatisticsMean(const std::vector<double>& v); | ||
| double StatisticsMedian(const std::vector<double>& v); | ||
| double StatisticsStdDev(const std::vector<double>& v); | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // STATISTICS_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| #include "string_util.h" | ||
|
|
||
| #include <array> | ||
| #include <cmath> | ||
| #include <cstdarg> | ||
| #include <cstdio> | ||
| #include <memory> | ||
| #include <sstream> | ||
|
|
||
| #include "arraysize.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace { | ||
|
|
||
| // kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta. | ||
| const char kBigSIUnits[] = "kMGTPEZY"; | ||
| // Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi. | ||
| const char kBigIECUnits[] = "KMGTPEZY"; | ||
| // milli, micro, nano, pico, femto, atto, zepto, yocto. | ||
| 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 const int64_t kUnitsSize = arraysize(kBigSIUnits); | ||
|
|
||
| void ToExponentAndMantissa(double val, double thresh, int precision, | ||
| double one_k, std::string* mantissa, | ||
| int64_t* exponent) { | ||
| std::stringstream mantissa_stream; | ||
|
|
||
| if (val < 0) { | ||
| mantissa_stream << "-"; | ||
| val = -val; | ||
| } | ||
|
|
||
| // Adjust threshold so that it never excludes things which can't be rendered | ||
| // in 'precision' digits. | ||
| const double adjusted_threshold = | ||
| std::max(thresh, 1.0 / std::pow(10.0, precision)); | ||
| const double big_threshold = adjusted_threshold * one_k; | ||
| const double small_threshold = adjusted_threshold; | ||
| // Values in ]simple_threshold,small_threshold[ will be printed as-is | ||
| const double simple_threshold = 0.01; | ||
|
|
||
| if (val > big_threshold) { | ||
| // Positive powers | ||
| double scaled = val; | ||
| for (size_t i = 0; i < arraysize(kBigSIUnits); ++i) { | ||
| scaled /= one_k; | ||
| if (scaled <= big_threshold) { | ||
| mantissa_stream << scaled; | ||
| *exponent = i + 1; | ||
| *mantissa = mantissa_stream.str(); | ||
| return; | ||
| } | ||
| } | ||
| mantissa_stream << val; | ||
| *exponent = 0; | ||
| } else if (val < small_threshold) { | ||
| // Negative powers | ||
| if (val < simple_threshold) { | ||
| double scaled = val; | ||
| for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) { | ||
| scaled *= one_k; | ||
| if (scaled >= small_threshold) { | ||
| mantissa_stream << scaled; | ||
| *exponent = -static_cast<int64_t>(i + 1); | ||
| *mantissa = mantissa_stream.str(); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| mantissa_stream << val; | ||
| *exponent = 0; | ||
| } else { | ||
| mantissa_stream << val; | ||
| *exponent = 0; | ||
| } | ||
| *mantissa = mantissa_stream.str(); | ||
| } | ||
|
|
||
| std::string ExponentToPrefix(int64_t exponent, bool iec) { | ||
| if (exponent == 0) return ""; | ||
|
|
||
| const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1); | ||
| if (index >= kUnitsSize) return ""; | ||
|
|
||
| const char* array = | ||
| (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits); | ||
| if (iec) | ||
| return array[index] + std::string("i"); | ||
| else | ||
| return std::string(1, array[index]); | ||
| } | ||
|
|
||
| std::string ToBinaryStringFullySpecified(double value, double threshold, | ||
| int precision, double one_k = 1024.0) { | ||
| std::string mantissa; | ||
| int64_t exponent; | ||
| ToExponentAndMantissa(value, threshold, precision, one_k, &mantissa, | ||
| &exponent); | ||
| return mantissa + ExponentToPrefix(exponent, false); | ||
| } | ||
|
|
||
| } // end namespace | ||
|
|
||
| void AppendHumanReadable(int n, std::string* str) { | ||
| std::stringstream ss; | ||
| // Round down to the nearest SI prefix. | ||
| ss << ToBinaryStringFullySpecified(n, 1.0, 0); | ||
| *str += ss.str(); | ||
| } | ||
|
|
||
| std::string HumanReadableNumber(double n, double one_k) { | ||
| // 1.1 means that figures up to 1.1k should be shown with the next unit down; | ||
| // this softens edge effects. | ||
| // 1 means that we should show one decimal place of precision. | ||
| return ToBinaryStringFullySpecified(n, 1.1, 1, one_k); | ||
| } | ||
|
|
||
| std::string StrFormatImp(const char* msg, va_list args) { | ||
| // we might need a second shot at this, so pre-emptivly make a copy | ||
| va_list args_cp; | ||
| va_copy(args_cp, args); | ||
|
|
||
| // TODO(ericwf): use std::array for first attempt to avoid one memory | ||
| // allocation guess what the size might be | ||
| std::array<char, 256> local_buff; | ||
| std::size_t size = local_buff.size(); | ||
| // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation | ||
| // in the android-ndk | ||
| auto ret = vsnprintf(local_buff.data(), size, msg, args_cp); | ||
|
|
||
| va_end(args_cp); | ||
|
|
||
| // handle empty expansion | ||
| if (ret == 0) return std::string{}; | ||
| if (static_cast<std::size_t>(ret) < size) | ||
| return std::string(local_buff.data()); | ||
|
|
||
| // we did not provide a long enough buffer on our first attempt. | ||
| // add 1 to size to account for null-byte in size cast to prevent overflow | ||
| size = static_cast<std::size_t>(ret) + 1; | ||
| auto buff_ptr = std::unique_ptr<char[]>(new char[size]); | ||
| // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation | ||
| // in the android-ndk | ||
| ret = vsnprintf(buff_ptr.get(), size, msg, args); | ||
| return std::string(buff_ptr.get()); | ||
| } | ||
|
|
||
| std::string StrFormat(const char* format, ...) { | ||
| va_list args; | ||
| va_start(args, format); | ||
| std::string tmp = StrFormatImp(format, args); | ||
| va_end(args); | ||
| return tmp; | ||
| } | ||
|
|
||
| void ReplaceAll(std::string* str, const std::string& from, | ||
| const std::string& to) { | ||
| std::size_t start = 0; | ||
| while ((start = str->find(from, start)) != std::string::npos) { | ||
| str->replace(start, from.length(), to); | ||
| start += to.length(); | ||
| } | ||
| } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #ifndef BENCHMARK_STRING_UTIL_H_ | ||
| #define BENCHMARK_STRING_UTIL_H_ | ||
|
|
||
| #include <sstream> | ||
| #include <string> | ||
| #include <utility> | ||
| #include "internal_macros.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| void AppendHumanReadable(int n, std::string* str); | ||
|
|
||
| std::string HumanReadableNumber(double n, double one_k = 1024.0); | ||
|
|
||
| std::string StrFormat(const char* format, ...); | ||
|
|
||
| inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT { | ||
| return out; | ||
| } | ||
|
|
||
| template <class First, class... Rest> | ||
| inline std::ostream& StrCatImp(std::ostream& out, First&& f, | ||
| Rest&&... rest) { | ||
| out << std::forward<First>(f); | ||
| return StrCatImp(out, std::forward<Rest>(rest)...); | ||
| } | ||
|
|
||
| template <class... Args> | ||
| inline std::string StrCat(Args&&... args) { | ||
| std::ostringstream ss; | ||
| StrCatImp(ss, std::forward<Args>(args)...); | ||
| return ss.str(); | ||
| } | ||
|
|
||
| void ReplaceAll(std::string* str, const std::string& from, | ||
| const std::string& to); | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_STRING_UTIL_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #ifndef BENCHMARK_THREAD_MANAGER_H | ||
| #define BENCHMARK_THREAD_MANAGER_H | ||
|
|
||
| #include <atomic> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "mutex.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace internal { | ||
|
|
||
| class ThreadManager { | ||
| public: | ||
| ThreadManager(int num_threads) | ||
| : alive_threads_(num_threads), start_stop_barrier_(num_threads) {} | ||
|
|
||
| Mutex& GetBenchmarkMutex() const RETURN_CAPABILITY(benchmark_mutex_) { | ||
| return benchmark_mutex_; | ||
| } | ||
|
|
||
| bool StartStopBarrier() EXCLUDES(end_cond_mutex_) { | ||
| return start_stop_barrier_.wait(); | ||
| } | ||
|
|
||
| void NotifyThreadComplete() EXCLUDES(end_cond_mutex_) { | ||
| start_stop_barrier_.removeThread(); | ||
| if (--alive_threads_ == 0) { | ||
| MutexLock lock(end_cond_mutex_); | ||
| end_condition_.notify_all(); | ||
| } | ||
| } | ||
|
|
||
| void WaitForAllThreads() EXCLUDES(end_cond_mutex_) { | ||
| MutexLock lock(end_cond_mutex_); | ||
| end_condition_.wait(lock.native_handle(), | ||
| [this]() { return alive_threads_ == 0; }); | ||
| } | ||
|
|
||
| public: | ||
| struct Result { | ||
| int64_t iterations = 0; | ||
| double real_time_used = 0; | ||
| double cpu_time_used = 0; | ||
| double manual_time_used = 0; | ||
| int64_t bytes_processed = 0; | ||
| int64_t items_processed = 0; | ||
| int64_t complexity_n = 0; | ||
| std::string report_label_; | ||
| std::string error_message_; | ||
| bool has_error_ = false; | ||
| UserCounters counters; | ||
| }; | ||
| GUARDED_BY(GetBenchmarkMutex()) Result results; | ||
|
|
||
| private: | ||
| mutable Mutex benchmark_mutex_; | ||
| std::atomic<int> alive_threads_; | ||
| Barrier start_stop_barrier_; | ||
| Mutex end_cond_mutex_; | ||
| Condition end_condition_; | ||
| }; | ||
|
|
||
| } // namespace internal | ||
| } // namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_THREAD_MANAGER_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #ifndef BENCHMARK_THREAD_TIMER_H | ||
| #define BENCHMARK_THREAD_TIMER_H | ||
|
|
||
| #include "check.h" | ||
| #include "timers.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace internal { | ||
|
|
||
| class ThreadTimer { | ||
| public: | ||
| ThreadTimer() = default; | ||
|
|
||
| // Called by each thread | ||
| void StartTimer() { | ||
| running_ = true; | ||
| start_real_time_ = ChronoClockNow(); | ||
| start_cpu_time_ = ThreadCPUUsage(); | ||
| } | ||
|
|
||
| // Called by each thread | ||
| void StopTimer() { | ||
| CHECK(running_); | ||
| running_ = false; | ||
| real_time_used_ += ChronoClockNow() - start_real_time_; | ||
| // Floating point error can result in the subtraction producing a negative | ||
| // time. Guard against that. | ||
| cpu_time_used_ += std::max<double>(ThreadCPUUsage() - start_cpu_time_, 0); | ||
| } | ||
|
|
||
| // Called by each thread | ||
| void SetIterationTime(double seconds) { manual_time_used_ += seconds; } | ||
|
|
||
| bool running() const { return running_; } | ||
|
|
||
| // REQUIRES: timer is not running | ||
| double real_time_used() { | ||
| CHECK(!running_); | ||
| return real_time_used_; | ||
| } | ||
|
|
||
| // REQUIRES: timer is not running | ||
| double cpu_time_used() { | ||
| CHECK(!running_); | ||
| return cpu_time_used_; | ||
| } | ||
|
|
||
| // REQUIRES: timer is not running | ||
| double manual_time_used() { | ||
| CHECK(!running_); | ||
| return manual_time_used_; | ||
| } | ||
|
|
||
| private: | ||
| bool running_ = false; // Is the timer running | ||
| double start_real_time_ = 0; // If running_ | ||
| double start_cpu_time_ = 0; // If running_ | ||
|
|
||
| // Accumulated time so far (does not contain current slice if running_) | ||
| double real_time_used_ = 0; | ||
| double cpu_time_used_ = 0; | ||
| // Manually set iteration time. User sets this with SetIterationTime(seconds). | ||
| double manual_time_used_ = 0; | ||
| }; | ||
|
|
||
| } // namespace internal | ||
| } // namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_THREAD_TIMER_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| // Copyright 2015 Google Inc. All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "timers.h" | ||
| #include "internal_macros.h" | ||
|
|
||
| #ifdef BENCHMARK_OS_WINDOWS | ||
| #include <shlwapi.h> | ||
| #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA | ||
| #include <versionhelpers.h> | ||
| #include <windows.h> | ||
| #else | ||
| #include <fcntl.h> | ||
| #ifndef BENCHMARK_OS_FUCHSIA | ||
| #include <sys/resource.h> | ||
| #endif | ||
| #include <sys/time.h> | ||
| #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD | ||
| #include <unistd.h> | ||
| #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX | ||
| #include <sys/sysctl.h> | ||
| #endif | ||
| #if defined(BENCHMARK_OS_MACOSX) | ||
| #include <mach/mach_init.h> | ||
| #include <mach/mach_port.h> | ||
| #include <mach/thread_act.h> | ||
| #endif | ||
| #endif | ||
|
|
||
| #ifdef BENCHMARK_OS_EMSCRIPTEN | ||
| #include <emscripten.h> | ||
| #endif | ||
|
|
||
| #include <cerrno> | ||
| #include <cstdint> | ||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <ctime> | ||
| #include <iostream> | ||
| #include <limits> | ||
| #include <mutex> | ||
|
|
||
| #include "check.h" | ||
| #include "log.h" | ||
| #include "sleep.h" | ||
| #include "string_util.h" | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // Suppress unused warnings on helper functions. | ||
| #if defined(__GNUC__) | ||
| #pragma GCC diagnostic ignored "-Wunused-function" | ||
| #endif | ||
|
|
||
| namespace { | ||
| #if defined(BENCHMARK_OS_WINDOWS) | ||
| double MakeTime(FILETIME const& kernel_time, FILETIME const& user_time) { | ||
| ULARGE_INTEGER kernel; | ||
| ULARGE_INTEGER user; | ||
| kernel.HighPart = kernel_time.dwHighDateTime; | ||
| kernel.LowPart = kernel_time.dwLowDateTime; | ||
| user.HighPart = user_time.dwHighDateTime; | ||
| user.LowPart = user_time.dwLowDateTime; | ||
| return (static_cast<double>(kernel.QuadPart) + | ||
| static_cast<double>(user.QuadPart)) * | ||
| 1e-7; | ||
| } | ||
| #elif !defined(BENCHMARK_OS_FUCHSIA) | ||
| double MakeTime(struct rusage const& ru) { | ||
| return (static_cast<double>(ru.ru_utime.tv_sec) + | ||
| static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 + | ||
| static_cast<double>(ru.ru_stime.tv_sec) + | ||
| static_cast<double>(ru.ru_stime.tv_usec) * 1e-6); | ||
| } | ||
| #endif | ||
| #if defined(BENCHMARK_OS_MACOSX) | ||
| double MakeTime(thread_basic_info_data_t const& info) { | ||
| return (static_cast<double>(info.user_time.seconds) + | ||
| static_cast<double>(info.user_time.microseconds) * 1e-6 + | ||
| static_cast<double>(info.system_time.seconds) + | ||
| static_cast<double>(info.system_time.microseconds) * 1e-6); | ||
| } | ||
| #endif | ||
| #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID) | ||
| double MakeTime(struct timespec const& ts) { | ||
| return ts.tv_sec + (static_cast<double>(ts.tv_nsec) * 1e-9); | ||
| } | ||
| #endif | ||
|
|
||
| BENCHMARK_NORETURN static void DiagnoseAndExit(const char* msg) { | ||
| std::cerr << "ERROR: " << msg << std::endl; | ||
| std::exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| } // end namespace | ||
|
|
||
| double ProcessCPUUsage() { | ||
| #if defined(BENCHMARK_OS_WINDOWS) | ||
| HANDLE proc = GetCurrentProcess(); | ||
| FILETIME creation_time; | ||
| FILETIME exit_time; | ||
| FILETIME kernel_time; | ||
| FILETIME user_time; | ||
| if (GetProcessTimes(proc, &creation_time, &exit_time, &kernel_time, | ||
| &user_time)) | ||
| return MakeTime(kernel_time, user_time); | ||
| DiagnoseAndExit("GetProccessTimes() failed"); | ||
| #elif defined(BENCHMARK_OS_EMSCRIPTEN) | ||
| // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) returns 0 on Emscripten. | ||
| // Use Emscripten-specific API. Reported CPU time would be exactly the | ||
| // same as total time, but this is ok because there aren't long-latency | ||
| // syncronous system calls in Emscripten. | ||
| return emscripten_get_now() * 1e-3; | ||
| #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) | ||
| // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See | ||
| // https://github.com/google/benchmark/pull/292 | ||
| struct timespec spec; | ||
| if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) | ||
| return MakeTime(spec); | ||
| DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed"); | ||
| #else | ||
| struct rusage ru; | ||
| if (getrusage(RUSAGE_SELF, &ru) == 0) return MakeTime(ru); | ||
| DiagnoseAndExit("getrusage(RUSAGE_SELF, ...) failed"); | ||
| #endif | ||
| } | ||
|
|
||
| double ThreadCPUUsage() { | ||
| #if defined(BENCHMARK_OS_WINDOWS) | ||
| HANDLE this_thread = GetCurrentThread(); | ||
| FILETIME creation_time; | ||
| FILETIME exit_time; | ||
| FILETIME kernel_time; | ||
| FILETIME user_time; | ||
| GetThreadTimes(this_thread, &creation_time, &exit_time, &kernel_time, | ||
| &user_time); | ||
| return MakeTime(kernel_time, user_time); | ||
| #elif defined(BENCHMARK_OS_MACOSX) | ||
| // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See | ||
| // https://github.com/google/benchmark/pull/292 | ||
| mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | ||
| thread_basic_info_data_t info; | ||
| mach_port_t thread = pthread_mach_thread_np(pthread_self()); | ||
| if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) == | ||
| KERN_SUCCESS) { | ||
| return MakeTime(info); | ||
| } | ||
| DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info"); | ||
| #elif defined(BENCHMARK_OS_EMSCRIPTEN) | ||
| // Emscripten doesn't support traditional threads | ||
| return ProcessCPUUsage(); | ||
| #elif defined(BENCHMARK_OS_RTEMS) | ||
| // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See | ||
| // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c | ||
| return ProcessCPUUsage(); | ||
| #elif defined(BENCHMARK_OS_SOLARIS) | ||
| struct rusage ru; | ||
| if (getrusage(RUSAGE_LWP, &ru) == 0) return MakeTime(ru); | ||
| DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed"); | ||
| #elif defined(CLOCK_THREAD_CPUTIME_ID) | ||
| struct timespec ts; | ||
| if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) return MakeTime(ts); | ||
| DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed"); | ||
| #else | ||
| #error Per-thread timing is not available on your system. | ||
| #endif | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| std::string DateTimeString(bool local) { | ||
| typedef std::chrono::system_clock Clock; | ||
| std::time_t now = Clock::to_time_t(Clock::now()); | ||
| const std::size_t kStorageSize = 128; | ||
| char storage[kStorageSize]; | ||
| std::size_t written; | ||
|
|
||
| if (local) { | ||
| #if defined(BENCHMARK_OS_WINDOWS) | ||
| written = | ||
| std::strftime(storage, sizeof(storage), "%x %X", ::localtime(&now)); | ||
| #else | ||
| std::tm timeinfo; | ||
| ::localtime_r(&now, &timeinfo); | ||
| written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo); | ||
| #endif | ||
| } else { | ||
| #if defined(BENCHMARK_OS_WINDOWS) | ||
| written = std::strftime(storage, sizeof(storage), "%x %X", ::gmtime(&now)); | ||
| #else | ||
| std::tm timeinfo; | ||
| ::gmtime_r(&now, &timeinfo); | ||
| written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo); | ||
| #endif | ||
| } | ||
| CHECK(written < kStorageSize); | ||
| ((void)written); // prevent unused variable in optimized mode. | ||
| return std::string(storage); | ||
| } | ||
|
|
||
| } // end namespace | ||
|
|
||
| std::string LocalDateTimeString() { return DateTimeString(true); } | ||
|
|
||
| } // end namespace benchmark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #ifndef BENCHMARK_TIMERS_H | ||
| #define BENCHMARK_TIMERS_H | ||
|
|
||
| #include <chrono> | ||
| #include <string> | ||
|
|
||
| namespace benchmark { | ||
|
|
||
| // Return the CPU usage of the current process | ||
| double ProcessCPUUsage(); | ||
|
|
||
| // Return the CPU usage of the children of the current process | ||
| double ChildrenCPUUsage(); | ||
|
|
||
| // Return the CPU usage of the current thread | ||
| double ThreadCPUUsage(); | ||
|
|
||
| #if defined(HAVE_STEADY_CLOCK) | ||
| template <bool HighResIsSteady = std::chrono::high_resolution_clock::is_steady> | ||
| struct ChooseSteadyClock { | ||
| typedef std::chrono::high_resolution_clock type; | ||
| }; | ||
|
|
||
| template <> | ||
| struct ChooseSteadyClock<false> { | ||
| typedef std::chrono::steady_clock type; | ||
| }; | ||
| #endif | ||
|
|
||
| struct ChooseClockType { | ||
| #if defined(HAVE_STEADY_CLOCK) | ||
| typedef ChooseSteadyClock<>::type type; | ||
| #else | ||
| typedef std::chrono::high_resolution_clock type; | ||
| #endif | ||
| }; | ||
|
|
||
| inline double ChronoClockNow() { | ||
| typedef ChooseClockType::type ClockType; | ||
| using FpSeconds = std::chrono::duration<double, std::chrono::seconds::period>; | ||
| return FpSeconds(ClockType::now().time_since_epoch()).count(); | ||
| } | ||
|
|
||
| std::string LocalDateTimeString(); | ||
|
|
||
| } // end namespace benchmark | ||
|
|
||
| #endif // BENCHMARK_TIMERS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
|
|
||
| include(split_list) | ||
|
|
||
| set(ASM_TEST_FLAGS "") | ||
| check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG) | ||
| if (BENCHMARK_HAS_O3_FLAG) | ||
| list(APPEND ASM_TEST_FLAGS -O3) | ||
| endif() | ||
|
|
||
| check_cxx_compiler_flag(-g0 BENCHMARK_HAS_G0_FLAG) | ||
| if (BENCHMARK_HAS_G0_FLAG) | ||
| list(APPEND ASM_TEST_FLAGS -g0) | ||
| endif() | ||
|
|
||
| check_cxx_compiler_flag(-fno-stack-protector BENCHMARK_HAS_FNO_STACK_PROTECTOR_FLAG) | ||
| if (BENCHMARK_HAS_FNO_STACK_PROTECTOR_FLAG) | ||
| list(APPEND ASM_TEST_FLAGS -fno-stack-protector) | ||
| endif() | ||
|
|
||
| split_list(ASM_TEST_FLAGS) | ||
| string(TOUPPER "${CMAKE_CXX_COMPILER_ID}" ASM_TEST_COMPILER) | ||
|
|
||
| macro(add_filecheck_test name) | ||
| cmake_parse_arguments(ARG "" "" "CHECK_PREFIXES" ${ARGV}) | ||
| add_library(${name} OBJECT ${name}.cc) | ||
| set_target_properties(${name} PROPERTIES COMPILE_FLAGS "-S ${ASM_TEST_FLAGS}") | ||
| set(ASM_OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${name}.s") | ||
| add_custom_target(copy_${name} ALL | ||
| COMMAND ${PROJECT_SOURCE_DIR}/tools/strip_asm.py | ||
| $<TARGET_OBJECTS:${name}> | ||
| ${ASM_OUTPUT_FILE} | ||
| BYPRODUCTS ${ASM_OUTPUT_FILE}) | ||
| add_dependencies(copy_${name} ${name}) | ||
| if (NOT ARG_CHECK_PREFIXES) | ||
| set(ARG_CHECK_PREFIXES "CHECK") | ||
| endif() | ||
| foreach(prefix ${ARG_CHECK_PREFIXES}) | ||
| add_test(NAME run_${name}_${prefix} | ||
| COMMAND | ||
| ${LLVM_FILECHECK_EXE} ${name}.cc | ||
| --input-file=${ASM_OUTPUT_FILE} | ||
| --check-prefixes=CHECK,CHECK-${ASM_TEST_COMPILER} | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) | ||
| endforeach() | ||
| endmacro() |