bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc
Go to the documentation of this file.
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 
16 #include <stdint.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include "absl/flags/flag.h"
22 #include "absl/flags/marshalling.h"
23 #include "absl/flags/parse.h"
24 #include "absl/flags/reflection.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/time/time.h"
27 #include "absl/types/optional.h"
28 #include "benchmark/benchmark.h"
29 
30 namespace {
31 using String = std::string;
32 using VectorOfStrings = std::vector<std::string>;
33 using AbslDuration = absl::Duration;
34 
35 // We do not want to take over marshalling for the types absl::optional<int>,
36 // absl::optional<std::string> which we do not own. Instead we introduce unique
37 // "aliases" to these types, which we do.
38 using AbslOptionalInt = absl::optional<int>;
39 struct OptionalInt : AbslOptionalInt {
40  using AbslOptionalInt::AbslOptionalInt;
41 };
42 // Next two functions represent Abseil Flags marshalling for OptionalInt.
43 bool AbslParseFlag(absl::string_view src, OptionalInt* flag,
44  std::string* error) {
45  int val;
46  if (src.empty())
47  flag->reset();
48  else if (!absl::ParseFlag(src, &val, error))
49  return false;
50  *flag = val;
51  return true;
52 }
53 std::string AbslUnparseFlag(const OptionalInt& flag) {
54  return !flag ? "" : absl::UnparseFlag(*flag);
55 }
56 
57 using AbslOptionalString = absl::optional<std::string>;
58 struct OptionalString : AbslOptionalString {
59  using AbslOptionalString::AbslOptionalString;
60 };
61 // Next two functions represent Abseil Flags marshalling for OptionalString.
62 bool AbslParseFlag(absl::string_view src, OptionalString* flag,
63  std::string* error) {
64  std::string val;
65  if (src.empty())
66  flag->reset();
67  else if (!absl::ParseFlag(src, &val, error))
68  return false;
69  *flag = val;
70  return true;
71 }
72 std::string AbslUnparseFlag(const OptionalString& flag) {
73  return !flag ? "" : absl::UnparseFlag(*flag);
74 }
75 
76 struct UDT {
77  UDT() = default;
78  UDT(const UDT&) {}
79  UDT& operator=(const UDT&) { return *this; }
80 };
81 // Next two functions represent Abseil Flags marshalling for UDT.
82 bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
83 std::string AbslUnparseFlag(const UDT&) { return ""; }
84 
85 } // namespace
86 
87 #define BENCHMARKED_TYPES(A) \
88  A(bool) \
89  A(int16_t) \
90  A(uint16_t) \
91  A(int32_t) \
92  A(uint32_t) \
93  A(int64_t) \
94  A(uint64_t) \
95  A(double) \
96  A(float) \
97  A(String) \
98  A(VectorOfStrings) \
99  A(OptionalInt) \
100  A(OptionalString) \
101  A(AbslDuration) \
102  A(UDT)
103 
104 #define FLAG_DEF(T) ABSL_FLAG(T, T##_flag, {}, "");
105 
106 #if defined(__clang__) && defined(__linux__)
107 // Force the flags used for benchmarks into a separate ELF section.
108 // This ensures that, even when other parts of the code might change size,
109 // the layout of the flags across cachelines is kept constant. This makes
110 // benchmark results more reproducible across unrelated code changes.
111 #pragma clang section data = ".benchmark_flags"
112 #endif
114 #if defined(__clang__) && defined(__linux__)
115 #pragma clang section data = ""
116 #endif
117 // Register thousands of flags to bloat up the size of the registry.
118 // This mimics real life production binaries.
119 #define DEFINE_FLAG_0(name) ABSL_FLAG(int, name, 0, "");
120 #define DEFINE_FLAG_1(name) DEFINE_FLAG_0(name##0) DEFINE_FLAG_0(name##1)
121 #define DEFINE_FLAG_2(name) DEFINE_FLAG_1(name##0) DEFINE_FLAG_1(name##1)
122 #define DEFINE_FLAG_3(name) DEFINE_FLAG_2(name##0) DEFINE_FLAG_2(name##1)
123 #define DEFINE_FLAG_4(name) DEFINE_FLAG_3(name##0) DEFINE_FLAG_3(name##1)
124 #define DEFINE_FLAG_5(name) DEFINE_FLAG_4(name##0) DEFINE_FLAG_4(name##1)
125 #define DEFINE_FLAG_6(name) DEFINE_FLAG_5(name##0) DEFINE_FLAG_5(name##1)
126 #define DEFINE_FLAG_7(name) DEFINE_FLAG_6(name##0) DEFINE_FLAG_6(name##1)
127 #define DEFINE_FLAG_8(name) DEFINE_FLAG_7(name##0) DEFINE_FLAG_7(name##1)
128 #define DEFINE_FLAG_9(name) DEFINE_FLAG_8(name##0) DEFINE_FLAG_8(name##1)
129 #define DEFINE_FLAG_10(name) DEFINE_FLAG_9(name##0) DEFINE_FLAG_9(name##1)
130 #define DEFINE_FLAG_11(name) DEFINE_FLAG_10(name##0) DEFINE_FLAG_10(name##1)
131 #define DEFINE_FLAG_12(name) DEFINE_FLAG_11(name##0) DEFINE_FLAG_11(name##1)
132 DEFINE_FLAG_12(bloat_flag_);
133 
134 namespace {
135 
136 #define BM_GetFlag(T) \
137  void BM_GetFlag_##T(benchmark::State& state) { \
138  for (auto _ : state) { \
139  benchmark::DoNotOptimize(absl::GetFlag(FLAGS_##T##_flag)); \
140  } \
141  } \
142  BENCHMARK(BM_GetFlag_##T)->ThreadRange(1, 16);
143 
145 
146 void BM_ThreadedFindCommandLineFlag(benchmark::State& state) {
147  char dummy[] = "dummy";
148  char* argv[] = {dummy};
149  // We need to ensure that flags have been parsed. That is where the registry
150  // is finalized.
151  absl::ParseCommandLine(1, argv);
152 
153  for (auto s : state) {
155  absl::FindCommandLineFlag("bloat_flag_010101010101"));
156  }
157 }
158 BENCHMARK(BM_ThreadedFindCommandLineFlag)->ThreadRange(1, 16);
159 
160 } // namespace
161 
162 #define InvokeGetFlag(T) \
163  T AbslInvokeGetFlag##T() { return absl::GetFlag(FLAGS_##T##_flag); } \
164  int odr##T = (benchmark::DoNotOptimize(AbslInvokeGetFlag##T), 1);
165 
167 
168 // To veiw disassembly use: gdb ${BINARY} -batch -ex "disassemble /s $FUNC"
flag
uint32_t flag
Definition: ssl_versions.cc:162
BM_GetFlag
#define BM_GetFlag(T)
Definition: bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc:136
UDT::operator=
UDT & operator=(const UDT &)=default
absl::ParseFlag
ABSL_NAMESPACE_BEGIN bool ParseFlag(absl::string_view input, T *dst, std::string *error)
Definition: abseil-cpp/absl/flags/marshalling.h:328
BENCHMARKED_TYPES
#define BENCHMARKED_TYPES(A)
Definition: bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc:87
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
absl::ParseCommandLine
std::vector< char * > ParseCommandLine(int argc, char *argv[])
Definition: abseil-cpp/absl/flags/parse.cc:815
absl::FindCommandLineFlag
CommandLineFlag * FindCommandLineFlag(absl::string_view name)
Definition: abseil-cpp/absl/flags/reflection.cc:336
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
absl::UnparseFlag
std::string UnparseFlag(const T &v)
Definition: abseil-cpp/absl/flags/marshalling.h:342
DEFINE_FLAG_12
#define DEFINE_FLAG_12(name)
Definition: bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc:131
AbslUnparseFlag
std::string AbslUnparseFlag(const UDT &)
Definition: bloaty/third_party/abseil-cpp/absl/flags/internal/usage_test.cc:50
UDT::UDT
UDT()=default
absl::optional
Definition: abseil-cpp/absl/types/internal/optional.h:61
stdint.h
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
FLAG_DEF
#define FLAG_DEF(T)
Definition: bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc:104
InvokeGetFlag
#define InvokeGetFlag(T)
Definition: bloaty/third_party/abseil-cpp/absl/flags/flag_benchmark.cc:162
AbslParseFlag
bool AbslParseFlag(absl::string_view, UDT *, std::string *)
Definition: bloaty/third_party/abseil-cpp/absl/flags/internal/usage_test.cc:49
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
absl::ABSL_NAMESPACE_BEGIN::dummy
int dummy
Definition: function_type_benchmark.cc:28
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170
Duration
struct Duration Duration
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:640


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:24