flag_benchmark.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // Copyright 2020 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include <stdint.h>
  16. #include <string>
  17. #include <vector>
  18. #include "absl/flags/flag.h"
  19. #include "absl/flags/marshalling.h"
  20. #include "absl/flags/parse.h"
  21. #include "absl/flags/reflection.h"
  22. #include "absl/strings/string_view.h"
  23. #include "absl/time/time.h"
  24. #include "absl/types/optional.h"
  25. #include "benchmark/benchmark.h"
  26. namespace {
  27. using String = std::string;
  28. using VectorOfStrings = std::vector<std::string>;
  29. using AbslDuration = absl::Duration;
  30. // We do not want to take over marshalling for the types absl::optional<int>,
  31. // absl::optional<std::string> which we do not own. Instead we introduce unique
  32. // "aliases" to these types, which we do.
  33. using AbslOptionalInt = absl::optional<int>;
  34. struct OptionalInt : AbslOptionalInt {
  35. using AbslOptionalInt::AbslOptionalInt;
  36. };
  37. // Next two functions represent Abseil Flags marshalling for OptionalInt.
  38. bool AbslParseFlag(absl::string_view src, OptionalInt* flag,
  39. std::string* error) {
  40. int val;
  41. if (src.empty())
  42. flag->reset();
  43. else if (!absl::ParseFlag(src, &val, error))
  44. return false;
  45. *flag = val;
  46. return true;
  47. }
  48. std::string AbslUnparseFlag(const OptionalInt& flag) {
  49. return !flag ? "" : absl::UnparseFlag(*flag);
  50. }
  51. using AbslOptionalString = absl::optional<std::string>;
  52. struct OptionalString : AbslOptionalString {
  53. using AbslOptionalString::AbslOptionalString;
  54. };
  55. // Next two functions represent Abseil Flags marshalling for OptionalString.
  56. bool AbslParseFlag(absl::string_view src, OptionalString* flag,
  57. std::string* error) {
  58. std::string val;
  59. if (src.empty())
  60. flag->reset();
  61. else if (!absl::ParseFlag(src, &val, error))
  62. return false;
  63. *flag = val;
  64. return true;
  65. }
  66. std::string AbslUnparseFlag(const OptionalString& flag) {
  67. return !flag ? "" : absl::UnparseFlag(*flag);
  68. }
  69. struct UDT {
  70. UDT() = default;
  71. UDT(const UDT&) {}
  72. UDT& operator=(const UDT&) { return *this; }
  73. };
  74. // Next two functions represent Abseil Flags marshalling for UDT.
  75. bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
  76. std::string AbslUnparseFlag(const UDT&) { return ""; }
  77. } // namespace
  78. #define BENCHMARKED_TYPES(A) \
  79. A(bool) \
  80. A(int16_t) \
  81. A(uint16_t) \
  82. A(int32_t) \
  83. A(uint32_t) \
  84. A(int64_t) \
  85. A(uint64_t) \
  86. A(double) \
  87. A(float) \
  88. A(String) \
  89. A(VectorOfStrings) \
  90. A(OptionalInt) \
  91. A(OptionalString) \
  92. A(AbslDuration) \
  93. A(UDT)
  94. #define REPLICATE_0(A, T, name, index) A(T, name, index)
  95. #define REPLICATE_1(A, T, name, index) \
  96. REPLICATE_0(A, T, name, index##0) REPLICATE_0(A, T, name, index##1)
  97. #define REPLICATE_2(A, T, name, index) \
  98. REPLICATE_1(A, T, name, index##0) REPLICATE_1(A, T, name, index##1)
  99. #define REPLICATE_3(A, T, name, index) \
  100. REPLICATE_2(A, T, name, index##0) REPLICATE_2(A, T, name, index##1)
  101. #define REPLICATE_4(A, T, name, index) \
  102. REPLICATE_3(A, T, name, index##0) REPLICATE_3(A, T, name, index##1)
  103. #define REPLICATE_5(A, T, name, index) \
  104. REPLICATE_4(A, T, name, index##0) REPLICATE_4(A, T, name, index##1)
  105. #define REPLICATE_6(A, T, name, index) \
  106. REPLICATE_5(A, T, name, index##0) REPLICATE_5(A, T, name, index##1)
  107. #define REPLICATE_7(A, T, name, index) \
  108. REPLICATE_6(A, T, name, index##0) REPLICATE_6(A, T, name, index##1)
  109. #define REPLICATE_8(A, T, name, index) \
  110. REPLICATE_7(A, T, name, index##0) REPLICATE_7(A, T, name, index##1)
  111. #define REPLICATE_9(A, T, name, index) \
  112. REPLICATE_8(A, T, name, index##0) REPLICATE_8(A, T, name, index##1)
  113. #if defined(_MSC_VER)
  114. #define REPLICATE(A, T, name) \
  115. REPLICATE_7(A, T, name, 0) REPLICATE_7(A, T, name, 1)
  116. #define SINGLE_FLAG(T) FLAGS_##T##_flag_00000000
  117. #else
  118. #define REPLICATE(A, T, name) \
  119. REPLICATE_9(A, T, name, 0) REPLICATE_9(A, T, name, 1)
  120. #define SINGLE_FLAG(T) FLAGS_##T##_flag_0000000000
  121. #endif
  122. #define REPLICATE_ALL(A, T, name) \
  123. REPLICATE_9(A, T, name, 0) REPLICATE_9(A, T, name, 1)
  124. #define COUNT(T, name, index) +1
  125. constexpr size_t kNumFlags = 0 REPLICATE(COUNT, _, _);
  126. #if defined(__clang__) && defined(__linux__)
  127. // Force the flags used for benchmarks into a separate ELF section.
  128. // This ensures that, even when other parts of the code might change size,
  129. // the layout of the flags across cachelines is kept constant. This makes
  130. // benchmark results more reproducible across unrelated code changes.
  131. #pragma clang section data = ".benchmark_flags"
  132. #endif
  133. #define DEFINE_FLAG(T, name, index) ABSL_FLAG(T, name##_##index, {}, "");
  134. #define FLAG_DEF(T) REPLICATE(DEFINE_FLAG, T, T##_flag);
  135. BENCHMARKED_TYPES(FLAG_DEF)
  136. #if defined(__clang__) && defined(__linux__)
  137. #pragma clang section data = ""
  138. #endif
  139. // Register thousands of flags to bloat up the size of the registry.
  140. // This mimics real life production binaries.
  141. #define BLOAT_FLAG(_unused1, _unused2, index) \
  142. ABSL_FLAG(int, bloat_flag_##index, 0, "");
  143. REPLICATE_ALL(BLOAT_FLAG, _, _)
  144. namespace {
  145. #define FLAG_PTR(T, name, index) &FLAGS_##name##_##index,
  146. #define FLAG_PTR_ARR(T) \
  147. static constexpr absl::Flag<T>* FlagPtrs_##T[] = { \
  148. REPLICATE(FLAG_PTR, T, T##_flag)};
  149. BENCHMARKED_TYPES(FLAG_PTR_ARR)
  150. #define BM_SingleGetFlag(T) \
  151. void BM_SingleGetFlag_##T(benchmark::State& state) { \
  152. for (auto _ : state) { \
  153. benchmark::DoNotOptimize(absl::GetFlag(SINGLE_FLAG(T))); \
  154. } \
  155. } \
  156. BENCHMARK(BM_SingleGetFlag_##T)->ThreadRange(1, 16);
  157. BENCHMARKED_TYPES(BM_SingleGetFlag)
  158. template <typename T>
  159. struct Accumulator {
  160. using type = T;
  161. };
  162. template <>
  163. struct Accumulator<String> {
  164. using type = size_t;
  165. };
  166. template <>
  167. struct Accumulator<VectorOfStrings> {
  168. using type = size_t;
  169. };
  170. template <>
  171. struct Accumulator<OptionalInt> {
  172. using type = bool;
  173. };
  174. template <>
  175. struct Accumulator<OptionalString> {
  176. using type = bool;
  177. };
  178. template <>
  179. struct Accumulator<UDT> {
  180. using type = bool;
  181. };
  182. template <typename T>
  183. void Accumulate(typename Accumulator<T>::type& a, const T& f) {
  184. a += f;
  185. }
  186. void Accumulate(bool& a, bool f) { a = a || f; }
  187. void Accumulate(size_t& a, const std::string& f) { a += f.size(); }
  188. void Accumulate(size_t& a, const std::vector<std::string>& f) { a += f.size(); }
  189. void Accumulate(bool& a, const OptionalInt& f) { a |= f.has_value(); }
  190. void Accumulate(bool& a, const OptionalString& f) { a |= f.has_value(); }
  191. void Accumulate(bool& a, const UDT& f) {
  192. a |= reinterpret_cast<int64_t>(&f) & 0x1;
  193. }
  194. #define BM_ManyGetFlag(T) \
  195. void BM_ManyGetFlag_##T(benchmark::State& state) { \
  196. Accumulator<T>::type res = {}; \
  197. while (state.KeepRunningBatch(kNumFlags)) { \
  198. for (auto* flag_ptr : FlagPtrs_##T) { \
  199. Accumulate(res, absl::GetFlag(*flag_ptr)); \
  200. } \
  201. } \
  202. benchmark::DoNotOptimize(res); \
  203. } \
  204. BENCHMARK(BM_ManyGetFlag_##T)->ThreadRange(1, 8);
  205. BENCHMARKED_TYPES(BM_ManyGetFlag)
  206. void BM_ThreadedFindCommandLineFlag(benchmark::State& state) {
  207. char dummy[] = "dummy";
  208. char* argv[] = {dummy};
  209. // We need to ensure that flags have been parsed. That is where the registry
  210. // is finalized.
  211. absl::ParseCommandLine(1, argv);
  212. while (state.KeepRunningBatch(kNumFlags)) {
  213. for (auto* flag_ptr : FlagPtrs_bool) {
  214. benchmark::DoNotOptimize(absl::FindCommandLineFlag(flag_ptr->Name()));
  215. }
  216. }
  217. }
  218. BENCHMARK(BM_ThreadedFindCommandLineFlag)->ThreadRange(1, 16);
  219. } // namespace
  220. #define InvokeGetFlag(T) \
  221. T AbslInvokeGetFlag##T() { return absl::GetFlag(SINGLE_FLAG(T)); } \
  222. int odr##T = (benchmark::DoNotOptimize(AbslInvokeGetFlag##T), 1);
  223. BENCHMARKED_TYPES(InvokeGetFlag)
  224. // To veiw disassembly use: gdb ${BINARY} -batch -ex "disassemble /s $FUNC"