xref: /llvm-project/third-party/benchmark/include/benchmark/benchmark.h (revision a5b797172cc902db166e9a695716fb81405f86e4)
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Support for registering benchmarks for functions.
16 
17 /* Example usage:
18 // Define a function that executes the code to be measured a
19 // specified number of times:
20 static void BM_StringCreation(benchmark::State& state) {
21   for (auto _ : state)
22     std::string empty_string;
23 }
24 
25 // Register the function as a benchmark
26 BENCHMARK(BM_StringCreation);
27 
28 // Define another benchmark
29 static void BM_StringCopy(benchmark::State& state) {
30   std::string x = "hello";
31   for (auto _ : state)
32     std::string copy(x);
33 }
34 BENCHMARK(BM_StringCopy);
35 
36 // Augment the main() program to invoke benchmarks if specified
37 // via the --benchmark_filter command line flag.  E.g.,
38 //       my_unittest --benchmark_filter=all
39 //       my_unittest --benchmark_filter=BM_StringCreation
40 //       my_unittest --benchmark_filter=String
41 //       my_unittest --benchmark_filter='Copy|Creation'
42 int main(int argc, char** argv) {
43   benchmark::Initialize(&argc, argv);
44   benchmark::RunSpecifiedBenchmarks();
45   benchmark::Shutdown();
46   return 0;
47 }
48 
49 // Sometimes a family of microbenchmarks can be implemented with
50 // just one routine that takes an extra argument to specify which
51 // one of the family of benchmarks to run.  For example, the following
52 // code defines a family of microbenchmarks for measuring the speed
53 // of memcpy() calls of different lengths:
54 
55 static void BM_memcpy(benchmark::State& state) {
56   char* src = new char[state.range(0)]; char* dst = new char[state.range(0)];
57   memset(src, 'x', state.range(0));
58   for (auto _ : state)
59     memcpy(dst, src, state.range(0));
60   state.SetBytesProcessed(state.iterations() * state.range(0));
61   delete[] src; delete[] dst;
62 }
63 BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);
64 
65 // The preceding code is quite repetitive, and can be replaced with the
66 // following short-hand.  The following invocation will pick a few
67 // appropriate arguments in the specified range and will generate a
68 // microbenchmark for each such argument.
69 BENCHMARK(BM_memcpy)->Range(8, 8<<10);
70 
71 // You might have a microbenchmark that depends on two inputs.  For
72 // example, the following code defines a family of microbenchmarks for
73 // measuring the speed of set insertion.
74 static void BM_SetInsert(benchmark::State& state) {
75   set<int> data;
76   for (auto _ : state) {
77     state.PauseTiming();
78     data = ConstructRandomSet(state.range(0));
79     state.ResumeTiming();
80     for (int j = 0; j < state.range(1); ++j)
81       data.insert(RandomNumber());
82   }
83 }
84 BENCHMARK(BM_SetInsert)
85    ->Args({1<<10, 128})
86    ->Args({2<<10, 128})
87    ->Args({4<<10, 128})
88    ->Args({8<<10, 128})
89    ->Args({1<<10, 512})
90    ->Args({2<<10, 512})
91    ->Args({4<<10, 512})
92    ->Args({8<<10, 512});
93 
94 // The preceding code is quite repetitive, and can be replaced with
95 // the following short-hand.  The following macro will pick a few
96 // appropriate arguments in the product of the two specified ranges
97 // and will generate a microbenchmark for each such pair.
98 BENCHMARK(BM_SetInsert)->Ranges({{1<<10, 8<<10}, {128, 512}});
99 
100 // For more complex patterns of inputs, passing a custom function
101 // to Apply allows programmatic specification of an
102 // arbitrary set of arguments to run the microbenchmark on.
103 // The following example enumerates a dense range on
104 // one parameter, and a sparse range on the second.
105 static void CustomArguments(benchmark::internal::Benchmark* b) {
106   for (int i = 0; i <= 10; ++i)
107     for (int j = 32; j <= 1024*1024; j *= 8)
108       b->Args({i, j});
109 }
110 BENCHMARK(BM_SetInsert)->Apply(CustomArguments);
111 
112 // Templated microbenchmarks work the same way:
113 // Produce then consume 'size' messages 'iters' times
114 // Measures throughput in the absence of multiprogramming.
115 template <class Q> int BM_Sequential(benchmark::State& state) {
116   Q q;
117   typename Q::value_type v;
118   for (auto _ : state) {
119     for (int i = state.range(0); i--; )
120       q.push(v);
121     for (int e = state.range(0); e--; )
122       q.Wait(&v);
123   }
124   // actually messages, not bytes:
125   state.SetBytesProcessed(state.iterations() * state.range(0));
126 }
127 BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
128 
129 Use `Benchmark::MinTime(double t)` to set the minimum time used to run the
130 benchmark. This option overrides the `benchmark_min_time` flag.
131 
132 void BM_test(benchmark::State& state) {
133  ... body ...
134 }
135 BENCHMARK(BM_test)->MinTime(2.0); // Run for at least 2 seconds.
136 
137 In a multithreaded test, it is guaranteed that none of the threads will start
138 until all have reached the loop start, and all will have finished before any
139 thread exits the loop body. As such, any global setup or teardown you want to
140 do can be wrapped in a check against the thread index:
141 
142 static void BM_MultiThreaded(benchmark::State& state) {
143   if (state.thread_index() == 0) {
144     // Setup code here.
145   }
146   for (auto _ : state) {
147     // Run the test as normal.
148   }
149   if (state.thread_index() == 0) {
150     // Teardown code here.
151   }
152 }
153 BENCHMARK(BM_MultiThreaded)->Threads(4);
154 
155 
156 If a benchmark runs a few milliseconds it may be hard to visually compare the
157 measured times, since the output data is given in nanoseconds per default. In
158 order to manually set the time unit, you can specify it manually:
159 
160 BENCHMARK(BM_test)->Unit(benchmark::kMillisecond);
161 */
162 
163 #ifndef BENCHMARK_BENCHMARK_H_
164 #define BENCHMARK_BENCHMARK_H_
165 
166 // The _MSVC_LANG check should detect Visual Studio 2015 Update 3 and newer.
167 #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
168 #define BENCHMARK_HAS_CXX11
169 #endif
170 
171 // This _MSC_VER check should detect VS 2017 v15.3 and newer.
172 #if __cplusplus >= 201703L || \
173     (defined(_MSC_VER) && _MSC_VER >= 1911 && _MSVC_LANG >= 201703L)
174 #define BENCHMARK_HAS_CXX17
175 #endif
176 
177 #include <stdint.h>
178 
179 #include <algorithm>
180 #include <cassert>
181 #include <cstddef>
182 #include <iosfwd>
183 #include <limits>
184 #include <map>
185 #include <set>
186 #include <string>
187 #include <utility>
188 #include <vector>
189 
190 #include "benchmark/export.h"
191 
192 #if defined(BENCHMARK_HAS_CXX11)
193 #include <atomic>
194 #include <initializer_list>
195 #include <type_traits>
196 #include <utility>
197 #endif
198 
199 #if defined(_MSC_VER)
200 #include <intrin.h>  // for _ReadWriteBarrier
201 #endif
202 
203 #ifndef BENCHMARK_HAS_CXX11
204 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
205   TypeName(const TypeName&);                         \
206   TypeName& operator=(const TypeName&)
207 #else
208 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
209   TypeName(const TypeName&) = delete;                \
210   TypeName& operator=(const TypeName&) = delete
211 #endif
212 
213 #ifdef BENCHMARK_HAS_CXX17
214 #define BENCHMARK_UNUSED [[maybe_unused]]
215 #elif defined(__GNUC__) || defined(__clang__)
216 #define BENCHMARK_UNUSED __attribute__((unused))
217 #else
218 #define BENCHMARK_UNUSED
219 #endif
220 
221 // Used to annotate functions, methods and classes so they
222 // are not optimized by the compiler. Useful for tests
223 // where you expect loops to stay in place churning cycles
224 #if defined(__clang__)
225 #define BENCHMARK_DONT_OPTIMIZE __attribute__((optnone))
226 #elif defined(__GNUC__) || defined(__GNUG__)
227 #define BENCHMARK_DONT_OPTIMIZE __attribute__((optimize(0)))
228 #else
229 // MSVC & Intel do not have a no-optimize attribute, only line pragmas
230 #define BENCHMARK_DONT_OPTIMIZE
231 #endif
232 
233 #if defined(__GNUC__) || defined(__clang__)
234 #define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
235 #elif defined(_MSC_VER) && !defined(__clang__)
236 #define BENCHMARK_ALWAYS_INLINE __forceinline
237 #define __func__ __FUNCTION__
238 #else
239 #define BENCHMARK_ALWAYS_INLINE
240 #endif
241 
242 #define BENCHMARK_INTERNAL_TOSTRING2(x) #x
243 #define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
244 
245 // clang-format off
246 #if (defined(__GNUC__) && !defined(__NVCC__) && !defined(__NVCOMPILER)) || defined(__clang__)
247 #define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
248 #define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
249 #define BENCHMARK_DISABLE_DEPRECATED_WARNING \
250   _Pragma("GCC diagnostic push")             \
251   _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
252 #define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("GCC diagnostic pop")
253 #elif defined(__NVCOMPILER)
254 #define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
255 #define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
256 #define BENCHMARK_DISABLE_DEPRECATED_WARNING \
257   _Pragma("diagnostic push") \
258   _Pragma("diag_suppress deprecated_entity_with_custom_message")
259 #define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("diagnostic pop")
260 #else
261 #define BENCHMARK_BUILTIN_EXPECT(x, y) x
262 #define BENCHMARK_DEPRECATED_MSG(msg)
263 #define BENCHMARK_WARNING_MSG(msg)                           \
264   __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
265       __LINE__) ") : warning note: " msg))
266 #define BENCHMARK_DISABLE_DEPRECATED_WARNING
267 #define BENCHMARK_RESTORE_DEPRECATED_WARNING
268 #endif
269 // clang-format on
270 
271 #if defined(__GNUC__) && !defined(__clang__)
272 #define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
273 #endif
274 
275 #ifndef __has_builtin
276 #define __has_builtin(x) 0
277 #endif
278 
279 #if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
280 #define BENCHMARK_UNREACHABLE() __builtin_unreachable()
281 #elif defined(_MSC_VER)
282 #define BENCHMARK_UNREACHABLE() __assume(false)
283 #else
284 #define BENCHMARK_UNREACHABLE() ((void)0)
285 #endif
286 
287 #ifdef BENCHMARK_HAS_CXX11
288 #define BENCHMARK_OVERRIDE override
289 #else
290 #define BENCHMARK_OVERRIDE
291 #endif
292 
293 #if defined(_MSC_VER)
294 #pragma warning(push)
295 // C4251: <symbol> needs to have dll-interface to be used by clients of class
296 #pragma warning(disable : 4251)
297 #endif
298 
299 namespace benchmark {
300 class BenchmarkReporter;
301 
302 // Default number of minimum benchmark running time in seconds.
303 const char kDefaultMinTimeStr[] = "0.5s";
304 
305 // Returns the version of the library.
306 BENCHMARK_EXPORT std::string GetBenchmarkVersion();
307 
308 BENCHMARK_EXPORT void PrintDefaultHelp();
309 
310 BENCHMARK_EXPORT void Initialize(int* argc, char** argv,
311                                  void (*HelperPrinterf)() = PrintDefaultHelp);
312 BENCHMARK_EXPORT void Shutdown();
313 
314 // Report to stdout all arguments in 'argv' as unrecognized except the first.
315 // Returns true there is at least on unrecognized argument (i.e. 'argc' > 1).
316 BENCHMARK_EXPORT bool ReportUnrecognizedArguments(int argc, char** argv);
317 
318 // Returns the current value of --benchmark_filter.
319 BENCHMARK_EXPORT std::string GetBenchmarkFilter();
320 
321 // Sets a new value to --benchmark_filter. (This will override this flag's
322 // current value).
323 // Should be called after `benchmark::Initialize()`, as
324 // `benchmark::Initialize()` will override the flag's value.
325 BENCHMARK_EXPORT void SetBenchmarkFilter(std::string value);
326 
327 // Returns the current value of --v (command line value for verbosity).
328 BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
329 
330 // Creates a default display reporter. Used by the library when no display
331 // reporter is provided, but also made available for external use in case a
332 // custom reporter should respect the `--benchmark_format` flag as a fallback
333 BENCHMARK_EXPORT BenchmarkReporter* CreateDefaultDisplayReporter();
334 
335 // Generate a list of benchmarks matching the specified --benchmark_filter flag
336 // and if --benchmark_list_tests is specified return after printing the name
337 // of each matching benchmark. Otherwise run each matching benchmark and
338 // report the results.
339 //
340 // spec : Specify the benchmarks to run. If users do not specify this arg,
341 //        then the value of FLAGS_benchmark_filter
342 //        will be used.
343 //
344 // The second and third overload use the specified 'display_reporter' and
345 //  'file_reporter' respectively. 'file_reporter' will write to the file
346 //  specified
347 //   by '--benchmark_out'. If '--benchmark_out' is not given the
348 //  'file_reporter' is ignored.
349 //
350 // RETURNS: The number of matching benchmarks.
351 BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks();
352 BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks(std::string spec);
353 
354 BENCHMARK_EXPORT size_t
355 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
356 BENCHMARK_EXPORT size_t
357 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
358 
359 BENCHMARK_EXPORT size_t RunSpecifiedBenchmarks(
360     BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
361 BENCHMARK_EXPORT size_t
362 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
363                        BenchmarkReporter* file_reporter, std::string spec);
364 
365 // TimeUnit is passed to a benchmark in order to specify the order of magnitude
366 // for the measured time.
367 enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
368 
369 BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
370 
371 // Sets the default time unit the benchmarks use
372 // Has to be called before the benchmark loop to take effect
373 BENCHMARK_EXPORT void SetDefaultTimeUnit(TimeUnit unit);
374 
375 // If a MemoryManager is registered (via RegisterMemoryManager()),
376 // it can be used to collect and report allocation metrics for a run of the
377 // benchmark.
378 class MemoryManager {
379  public:
380   static const int64_t TombstoneValue;
381 
382   struct Result {
ResultResult383     Result()
384         : num_allocs(0),
385           max_bytes_used(0),
386           total_allocated_bytes(TombstoneValue),
387           net_heap_growth(TombstoneValue) {}
388 
389     // The number of allocations made in total between Start and Stop.
390     int64_t num_allocs;
391 
392     // The peak memory use between Start and Stop.
393     int64_t max_bytes_used;
394 
395     // The total memory allocated, in bytes, between Start and Stop.
396     // Init'ed to TombstoneValue if metric not available.
397     int64_t total_allocated_bytes;
398 
399     // The net changes in memory, in bytes, between Start and Stop.
400     // ie., total_allocated_bytes - total_deallocated_bytes.
401     // Init'ed to TombstoneValue if metric not available.
402     int64_t net_heap_growth;
403   };
404 
~MemoryManager()405   virtual ~MemoryManager() {}
406 
407   // Implement this to start recording allocation information.
408   virtual void Start() = 0;
409 
410   // Implement this to stop recording and fill out the given Result structure.
411   virtual void Stop(Result& result) = 0;
412 };
413 
414 // Register a MemoryManager instance that will be used to collect and report
415 // allocation measurements for benchmark runs.
416 BENCHMARK_EXPORT
417 void RegisterMemoryManager(MemoryManager* memory_manager);
418 
419 // Add a key-value pair to output as part of the context stanza in the report.
420 BENCHMARK_EXPORT
421 void AddCustomContext(const std::string& key, const std::string& value);
422 
423 namespace internal {
424 class Benchmark;
425 class BenchmarkImp;
426 class BenchmarkFamilies;
427 
428 BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext();
429 
430 BENCHMARK_EXPORT
431 void UseCharPointer(char const volatile*);
432 
433 // Take ownership of the pointer and register the benchmark. Return the
434 // registered benchmark.
435 BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*);
436 
437 // Ensure that the standard streams are properly initialized in every TU.
438 BENCHMARK_EXPORT int InitializeStreams();
439 BENCHMARK_UNUSED static int stream_init_anchor = InitializeStreams();
440 
441 }  // namespace internal
442 
443 #if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
444     defined(__EMSCRIPTEN__)
445 #define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
446 #endif
447 
448 // Force the compiler to flush pending writes to global memory. Acts as an
449 // effective read/write barrier
450 #ifdef BENCHMARK_HAS_CXX11
ClobberMemory()451 inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() {
452   std::atomic_signal_fence(std::memory_order_acq_rel);
453 }
454 #endif
455 
456 // The DoNotOptimize(...) function can be used to prevent a value or
457 // expression from being optimized away by the compiler. This function is
458 // intended to add little to no overhead.
459 // See: https://youtu.be/nXaxk27zwlk?t=2441
460 #ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
461 #if !defined(__GNUC__) || defined(__llvm__) || defined(__INTEL_COMPILER)
462 template <class Tp>
463 BENCHMARK_DEPRECATED_MSG(
464     "The const-ref version of this method can permit "
465     "undesired compiler optimizations in benchmarks")
DoNotOptimize(Tp const & value)466 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
467   asm volatile("" : : "r,m"(value) : "memory");
468 }
469 
470 template <class Tp>
DoNotOptimize(Tp & value)471 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
472 #if defined(__clang__)
473   asm volatile("" : "+r,m"(value) : : "memory");
474 #else
475   asm volatile("" : "+m,r"(value) : : "memory");
476 #endif
477 }
478 
479 #ifdef BENCHMARK_HAS_CXX11
480 template <class Tp>
DoNotOptimize(Tp && value)481 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp&& value) {
482 #if defined(__clang__)
483   asm volatile("" : "+r,m"(value) : : "memory");
484 #else
485   asm volatile("" : "+m,r"(value) : : "memory");
486 #endif
487 }
488 #endif
489 #elif defined(BENCHMARK_HAS_CXX11) && (__GNUC__ >= 5)
490 // Workaround for a bug with full argument copy overhead with GCC.
491 // See: #1340 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105519
492 template <class Tp>
493 BENCHMARK_DEPRECATED_MSG(
494     "The const-ref version of this method can permit "
495     "undesired compiler optimizations in benchmarks")
496 inline BENCHMARK_ALWAYS_INLINE
497     typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
498                             (sizeof(Tp) <= sizeof(Tp*))>::type
DoNotOptimize(Tp const & value)499     DoNotOptimize(Tp const& value) {
500   asm volatile("" : : "r,m"(value) : "memory");
501 }
502 
503 template <class Tp>
504 BENCHMARK_DEPRECATED_MSG(
505     "The const-ref version of this method can permit "
506     "undesired compiler optimizations in benchmarks")
507 inline BENCHMARK_ALWAYS_INLINE
508     typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
509                             (sizeof(Tp) > sizeof(Tp*))>::type
DoNotOptimize(Tp const & value)510     DoNotOptimize(Tp const& value) {
511   asm volatile("" : : "m"(value) : "memory");
512 }
513 
514 template <class Tp>
515 inline BENCHMARK_ALWAYS_INLINE
516     typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
517                             (sizeof(Tp) <= sizeof(Tp*))>::type
DoNotOptimize(Tp & value)518     DoNotOptimize(Tp& value) {
519   asm volatile("" : "+m,r"(value) : : "memory");
520 }
521 
522 template <class Tp>
523 inline BENCHMARK_ALWAYS_INLINE
524     typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
525                             (sizeof(Tp) > sizeof(Tp*))>::type
DoNotOptimize(Tp & value)526     DoNotOptimize(Tp& value) {
527   asm volatile("" : "+m"(value) : : "memory");
528 }
529 
530 template <class Tp>
531 inline BENCHMARK_ALWAYS_INLINE
532     typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
533                             (sizeof(Tp) <= sizeof(Tp*))>::type
DoNotOptimize(Tp && value)534     DoNotOptimize(Tp&& value) {
535   asm volatile("" : "+m,r"(value) : : "memory");
536 }
537 
538 template <class Tp>
539 inline BENCHMARK_ALWAYS_INLINE
540     typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
541                             (sizeof(Tp) > sizeof(Tp*))>::type
DoNotOptimize(Tp && value)542     DoNotOptimize(Tp&& value) {
543   asm volatile("" : "+m"(value) : : "memory");
544 }
545 
546 #else
547 // Fallback for GCC < 5. Can add some overhead because the compiler is forced
548 // to use memory operations instead of operations with registers.
549 // TODO: Remove if GCC < 5 will be unsupported.
550 template <class Tp>
551 BENCHMARK_DEPRECATED_MSG(
552     "The const-ref version of this method can permit "
553     "undesired compiler optimizations in benchmarks")
DoNotOptimize(Tp const & value)554 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
555   asm volatile("" : : "m"(value) : "memory");
556 }
557 
558 template <class Tp>
DoNotOptimize(Tp & value)559 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
560   asm volatile("" : "+m"(value) : : "memory");
561 }
562 
563 #ifdef BENCHMARK_HAS_CXX11
564 template <class Tp>
DoNotOptimize(Tp && value)565 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp&& value) {
566   asm volatile("" : "+m"(value) : : "memory");
567 }
568 #endif
569 #endif
570 
571 #ifndef BENCHMARK_HAS_CXX11
ClobberMemory()572 inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() {
573   asm volatile("" : : : "memory");
574 }
575 #endif
576 #elif defined(_MSC_VER)
577 template <class Tp>
578 BENCHMARK_DEPRECATED_MSG(
579     "The const-ref version of this method can permit "
580     "undesired compiler optimizations in benchmarks")
DoNotOptimize(Tp const & value)581 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
582   internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
583   _ReadWriteBarrier();
584 }
585 
586 #ifndef BENCHMARK_HAS_CXX11
ClobberMemory()587 inline BENCHMARK_ALWAYS_INLINE void ClobberMemory() { _ReadWriteBarrier(); }
588 #endif
589 #else
590 #ifdef BENCHMARK_HAS_CXX11
591 template <class Tp>
DoNotOptimize(Tp && value)592 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp&& value) {
593   internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
594 }
595 #else
596 template <class Tp>
597 BENCHMARK_DEPRECATED_MSG(
598     "The const-ref version of this method can permit "
599     "undesired compiler optimizations in benchmarks")
DoNotOptimize(Tp const & value)600 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
601   internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
602 }
603 
604 template <class Tp>
DoNotOptimize(Tp & value)605 inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
606   internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
607 }
608 #endif
609 // FIXME Add ClobberMemory() for non-gnu and non-msvc compilers, before C++11.
610 #endif
611 
612 // This class is used for user-defined counters.
613 class Counter {
614  public:
615   enum Flags {
616     kDefaults = 0,
617     // Mark the counter as a rate. It will be presented divided
618     // by the duration of the benchmark.
619     kIsRate = 1 << 0,
620     // Mark the counter as a thread-average quantity. It will be
621     // presented divided by the number of threads.
622     kAvgThreads = 1 << 1,
623     // Mark the counter as a thread-average rate. See above.
624     kAvgThreadsRate = kIsRate | kAvgThreads,
625     // Mark the counter as a constant value, valid/same for *every* iteration.
626     // When reporting, it will be *multiplied* by the iteration count.
627     kIsIterationInvariant = 1 << 2,
628     // Mark the counter as a constant rate.
629     // When reporting, it will be *multiplied* by the iteration count
630     // and then divided by the duration of the benchmark.
631     kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
632     // Mark the counter as a iteration-average quantity.
633     // It will be presented divided by the number of iterations.
634     kAvgIterations = 1 << 3,
635     // Mark the counter as a iteration-average rate. See above.
636     kAvgIterationsRate = kIsRate | kAvgIterations,
637 
638     // In the end, invert the result. This is always done last!
639     kInvert = 1 << 31
640   };
641 
642   enum OneK {
643     // 1'000 items per 1k
644     kIs1000 = 1000,
645     // 1'024 items per 1k
646     kIs1024 = 1024
647   };
648 
649   double value;
650   Flags flags;
651   OneK oneK;
652 
653   BENCHMARK_ALWAYS_INLINE
654   Counter(double v = 0., Flags f = kDefaults, OneK k = kIs1000)
value(v)655       : value(v), flags(f), oneK(k) {}
656 
657   BENCHMARK_ALWAYS_INLINE operator double const &() const { return value; }
658   BENCHMARK_ALWAYS_INLINE operator double&() { return value; }
659 };
660 
661 // A helper for user code to create unforeseen combinations of Flags, without
662 // having to do this cast manually each time, or providing this operator.
663 Counter::Flags inline operator|(const Counter::Flags& LHS,
664                                 const Counter::Flags& RHS) {
665   return static_cast<Counter::Flags>(static_cast<int>(LHS) |
666                                      static_cast<int>(RHS));
667 }
668 
669 // This is the container for the user-defined counters.
670 typedef std::map<std::string, Counter> UserCounters;
671 
672 // BigO is passed to a benchmark in order to specify the asymptotic
673 // computational
674 // complexity for the benchmark. In case oAuto is selected, complexity will be
675 // calculated automatically to the best fit.
676 enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
677 
678 typedef int64_t ComplexityN;
679 
680 typedef int64_t IterationCount;
681 
682 enum StatisticUnit { kTime, kPercentage };
683 
684 // BigOFunc is passed to a benchmark in order to specify the asymptotic
685 // computational complexity for the benchmark.
686 typedef double(BigOFunc)(ComplexityN);
687 
688 // StatisticsFunc is passed to a benchmark in order to compute some descriptive
689 // statistics over all the measurements of some type
690 typedef double(StatisticsFunc)(const std::vector<double>&);
691 
692 namespace internal {
693 struct Statistics {
694   std::string name_;
695   StatisticsFunc* compute_;
696   StatisticUnit unit_;
697 
698   Statistics(const std::string& name, StatisticsFunc* compute,
699              StatisticUnit unit = kTime)
name_Statistics700       : name_(name), compute_(compute), unit_(unit) {}
701 };
702 
703 class BenchmarkInstance;
704 class ThreadTimer;
705 class ThreadManager;
706 class PerfCountersMeasurement;
707 
708 enum AggregationReportMode
709 #if defined(BENCHMARK_HAS_CXX11)
710     : unsigned
711 #else
712 #endif
713 {
714   // The mode has not been manually specified
715   ARM_Unspecified = 0,
716   // The mode is user-specified.
717   // This may or may not be set when the following bit-flags are set.
718   ARM_Default = 1U << 0U,
719   // File reporter should only output aggregates.
720   ARM_FileReportAggregatesOnly = 1U << 1U,
721   // Display reporter should only output aggregates
722   ARM_DisplayReportAggregatesOnly = 1U << 2U,
723   // Both reporters should only display aggregates.
724   ARM_ReportAggregatesOnly =
725       ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
726 };
727 
728 enum Skipped
729 #if defined(BENCHMARK_HAS_CXX11)
730     : unsigned
731 #endif
732 {
733   NotSkipped = 0,
734   SkippedWithMessage,
735   SkippedWithError
736 };
737 
738 }  // namespace internal
739 
740 // State is passed to a running Benchmark and contains state for the
741 // benchmark to use.
742 class BENCHMARK_EXPORT State {
743  public:
744   struct StateIterator;
745   friend struct StateIterator;
746 
747   // Returns iterators used to run each iteration of a benchmark using a
748   // C++11 ranged-based for loop. These functions should not be called directly.
749   //
750   // REQUIRES: The benchmark has not started running yet. Neither begin nor end
751   // have been called previously.
752   //
753   // NOTE: KeepRunning may not be used after calling either of these functions.
754   inline BENCHMARK_ALWAYS_INLINE StateIterator begin();
755   inline BENCHMARK_ALWAYS_INLINE StateIterator end();
756 
757   // Returns true if the benchmark should continue through another iteration.
758   // NOTE: A benchmark may not return from the test until KeepRunning() has
759   // returned false.
760   inline bool KeepRunning();
761 
762   // Returns true iff the benchmark should run n more iterations.
763   // REQUIRES: 'n' > 0.
764   // NOTE: A benchmark must not return from the test until KeepRunningBatch()
765   // has returned false.
766   // NOTE: KeepRunningBatch() may overshoot by up to 'n' iterations.
767   //
768   // Intended usage:
769   //   while (state.KeepRunningBatch(1000)) {
770   //     // process 1000 elements
771   //   }
772   inline bool KeepRunningBatch(IterationCount n);
773 
774   // REQUIRES: timer is running and 'SkipWithMessage(...)' or
775   //   'SkipWithError(...)' has not been called by the current thread.
776   // Stop the benchmark timer.  If not called, the timer will be
777   // automatically stopped after the last iteration of the benchmark loop.
778   //
779   // For threaded benchmarks the PauseTiming() function only pauses the timing
780   // for the current thread.
781   //
782   // NOTE: The "real time" measurement is per-thread. If different threads
783   // report different measurements the largest one is reported.
784   //
785   // NOTE: PauseTiming()/ResumeTiming() are relatively
786   // heavyweight, and so their use should generally be avoided
787   // within each benchmark iteration, if possible.
788   void PauseTiming();
789 
790   // REQUIRES: timer is not running and 'SkipWithMessage(...)' or
791   //   'SkipWithError(...)' has not been called by the current thread.
792   // Start the benchmark timer.  The timer is NOT running on entrance to the
793   // benchmark function. It begins running after control flow enters the
794   // benchmark loop.
795   //
796   // NOTE: PauseTiming()/ResumeTiming() are relatively
797   // heavyweight, and so their use should generally be avoided
798   // within each benchmark iteration, if possible.
799   void ResumeTiming();
800 
801   // REQUIRES: 'SkipWithMessage(...)' or 'SkipWithError(...)' has not been
802   //            called previously by the current thread.
803   // Report the benchmark as resulting in being skipped with the specified
804   // 'msg'.
805   // After this call the user may explicitly 'return' from the benchmark.
806   //
807   // If the ranged-for style of benchmark loop is used, the user must explicitly
808   // break from the loop, otherwise all future iterations will be run.
809   // If the 'KeepRunning()' loop is used the current thread will automatically
810   // exit the loop at the end of the current iteration.
811   //
812   // For threaded benchmarks only the current thread stops executing and future
813   // calls to `KeepRunning()` will block until all threads have completed
814   // the `KeepRunning()` loop. If multiple threads report being skipped only the
815   // first skip message is used.
816   //
817   // NOTE: Calling 'SkipWithMessage(...)' does not cause the benchmark to exit
818   // the current scope immediately. If the function is called from within
819   // the 'KeepRunning()' loop the current iteration will finish. It is the users
820   // responsibility to exit the scope as needed.
821   void SkipWithMessage(const std::string& msg);
822 
823   // REQUIRES: 'SkipWithMessage(...)' or 'SkipWithError(...)' has not been
824   //            called previously by the current thread.
825   // Report the benchmark as resulting in an error with the specified 'msg'.
826   // After this call the user may explicitly 'return' from the benchmark.
827   //
828   // If the ranged-for style of benchmark loop is used, the user must explicitly
829   // break from the loop, otherwise all future iterations will be run.
830   // If the 'KeepRunning()' loop is used the current thread will automatically
831   // exit the loop at the end of the current iteration.
832   //
833   // For threaded benchmarks only the current thread stops executing and future
834   // calls to `KeepRunning()` will block until all threads have completed
835   // the `KeepRunning()` loop. If multiple threads report an error only the
836   // first error message is used.
837   //
838   // NOTE: Calling 'SkipWithError(...)' does not cause the benchmark to exit
839   // the current scope immediately. If the function is called from within
840   // the 'KeepRunning()' loop the current iteration will finish. It is the users
841   // responsibility to exit the scope as needed.
842   void SkipWithError(const std::string& msg);
843 
844   // Returns true if 'SkipWithMessage(...)' or 'SkipWithError(...)' was called.
skipped()845   bool skipped() const { return internal::NotSkipped != skipped_; }
846 
847   // Returns true if an error has been reported with 'SkipWithError(...)'.
error_occurred()848   bool error_occurred() const { return internal::SkippedWithError == skipped_; }
849 
850   // REQUIRES: called exactly once per iteration of the benchmarking loop.
851   // Set the manually measured time for this benchmark iteration, which
852   // is used instead of automatically measured time if UseManualTime() was
853   // specified.
854   //
855   // For threaded benchmarks the final value will be set to the largest
856   // reported values.
857   void SetIterationTime(double seconds);
858 
859   // Set the number of bytes processed by the current benchmark
860   // execution.  This routine is typically called once at the end of a
861   // throughput oriented benchmark.
862   //
863   // REQUIRES: a benchmark has exited its benchmarking loop.
864   BENCHMARK_ALWAYS_INLINE
SetBytesProcessed(int64_t bytes)865   void SetBytesProcessed(int64_t bytes) {
866     counters["bytes_per_second"] =
867         Counter(static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
868   }
869 
870   BENCHMARK_ALWAYS_INLINE
bytes_processed()871   int64_t bytes_processed() const {
872     if (counters.find("bytes_per_second") != counters.end())
873       return static_cast<int64_t>(counters.at("bytes_per_second"));
874     return 0;
875   }
876 
877   // If this routine is called with complexity_n > 0 and complexity report is
878   // requested for the
879   // family benchmark, then current benchmark will be part of the computation
880   // and complexity_n will
881   // represent the length of N.
882   BENCHMARK_ALWAYS_INLINE
SetComplexityN(ComplexityN complexity_n)883   void SetComplexityN(ComplexityN complexity_n) {
884     complexity_n_ = complexity_n;
885   }
886 
887   BENCHMARK_ALWAYS_INLINE
complexity_length_n()888   ComplexityN complexity_length_n() const { return complexity_n_; }
889 
890   // If this routine is called with items > 0, then an items/s
891   // label is printed on the benchmark report line for the currently
892   // executing benchmark. It is typically called at the end of a processing
893   // benchmark where a processing items/second output is desired.
894   //
895   // REQUIRES: a benchmark has exited its benchmarking loop.
896   BENCHMARK_ALWAYS_INLINE
SetItemsProcessed(int64_t items)897   void SetItemsProcessed(int64_t items) {
898     counters["items_per_second"] =
899         Counter(static_cast<double>(items), benchmark::Counter::kIsRate);
900   }
901 
902   BENCHMARK_ALWAYS_INLINE
items_processed()903   int64_t items_processed() const {
904     if (counters.find("items_per_second") != counters.end())
905       return static_cast<int64_t>(counters.at("items_per_second"));
906     return 0;
907   }
908 
909   // If this routine is called, the specified label is printed at the
910   // end of the benchmark report line for the currently executing
911   // benchmark.  Example:
912   //  static void BM_Compress(benchmark::State& state) {
913   //    ...
914   //    double compress = input_size / output_size;
915   //    state.SetLabel(StrFormat("compress:%.1f%%", 100.0*compression));
916   //  }
917   // Produces output that looks like:
918   //  BM_Compress   50         50   14115038  compress:27.3%
919   //
920   // REQUIRES: a benchmark has exited its benchmarking loop.
921   void SetLabel(const std::string& label);
922 
923   // Range arguments for this run. CHECKs if the argument has been set.
924   BENCHMARK_ALWAYS_INLINE
925   int64_t range(std::size_t pos = 0) const {
926     assert(range_.size() > pos);
927     return range_[pos];
928   }
929 
930   BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead")
range_x()931   int64_t range_x() const { return range(0); }
932 
933   BENCHMARK_DEPRECATED_MSG("use 'range(1)' instead")
range_y()934   int64_t range_y() const { return range(1); }
935 
936   // Number of threads concurrently executing the benchmark.
937   BENCHMARK_ALWAYS_INLINE
threads()938   int threads() const { return threads_; }
939 
940   // Index of the executing thread. Values from [0, threads).
941   BENCHMARK_ALWAYS_INLINE
thread_index()942   int thread_index() const { return thread_index_; }
943 
944   BENCHMARK_ALWAYS_INLINE
iterations()945   IterationCount iterations() const {
946     if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
947       return 0;
948     }
949     return max_iterations - total_iterations_ + batch_leftover_;
950   }
951 
952   BENCHMARK_ALWAYS_INLINE
name()953   std::string name() const { return name_; }
954 
955  private:
956   // items we expect on the first cache line (ie 64 bytes of the struct)
957   // When total_iterations_ is 0, KeepRunning() and friends will return false.
958   // May be larger than max_iterations.
959   IterationCount total_iterations_;
960 
961   // When using KeepRunningBatch(), batch_leftover_ holds the number of
962   // iterations beyond max_iters that were run. Used to track
963   // completed_iterations_ accurately.
964   IterationCount batch_leftover_;
965 
966  public:
967   const IterationCount max_iterations;
968 
969  private:
970   bool started_;
971   bool finished_;
972   internal::Skipped skipped_;
973 
974   // items we don't need on the first cache line
975   std::vector<int64_t> range_;
976 
977   ComplexityN complexity_n_;
978 
979  public:
980   // Container for user-defined counters.
981   UserCounters counters;
982 
983  private:
984   State(std::string name, IterationCount max_iters,
985         const std::vector<int64_t>& ranges, int thread_i, int n_threads,
986         internal::ThreadTimer* timer, internal::ThreadManager* manager,
987         internal::PerfCountersMeasurement* perf_counters_measurement);
988 
989   void StartKeepRunning();
990   // Implementation of KeepRunning() and KeepRunningBatch().
991   // is_batch must be true unless n is 1.
992   inline bool KeepRunningInternal(IterationCount n, bool is_batch);
993   void FinishKeepRunning();
994 
995   const std::string name_;
996   const int thread_index_;
997   const int threads_;
998 
999   internal::ThreadTimer* const timer_;
1000   internal::ThreadManager* const manager_;
1001   internal::PerfCountersMeasurement* const perf_counters_measurement_;
1002 
1003   friend class internal::BenchmarkInstance;
1004 };
1005 
KeepRunning()1006 inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunning() {
1007   return KeepRunningInternal(1, /*is_batch=*/false);
1008 }
1009 
KeepRunningBatch(IterationCount n)1010 inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningBatch(IterationCount n) {
1011   return KeepRunningInternal(n, /*is_batch=*/true);
1012 }
1013 
KeepRunningInternal(IterationCount n,bool is_batch)1014 inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningInternal(IterationCount n,
1015                                                                bool is_batch) {
1016   // total_iterations_ is set to 0 by the constructor, and always set to a
1017   // nonzero value by StartKepRunning().
1018   assert(n > 0);
1019   // n must be 1 unless is_batch is true.
1020   assert(is_batch || n == 1);
1021   if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n, true)) {
1022     total_iterations_ -= n;
1023     return true;
1024   }
1025   if (!started_) {
1026     StartKeepRunning();
1027     if (!skipped() && total_iterations_ >= n) {
1028       total_iterations_ -= n;
1029       return true;
1030     }
1031   }
1032   // For non-batch runs, total_iterations_ must be 0 by now.
1033   if (is_batch && total_iterations_ != 0) {
1034     batch_leftover_ = n - total_iterations_;
1035     total_iterations_ = 0;
1036     return true;
1037   }
1038   FinishKeepRunning();
1039   return false;
1040 }
1041 
1042 struct State::StateIterator {
1043   struct BENCHMARK_UNUSED Value {};
1044   typedef std::forward_iterator_tag iterator_category;
1045   typedef Value value_type;
1046   typedef Value reference;
1047   typedef Value pointer;
1048   typedef std::ptrdiff_t difference_type;
1049 
1050  private:
1051   friend class State;
1052   BENCHMARK_ALWAYS_INLINE
StateIteratorStateIterator1053   StateIterator() : cached_(0), parent_() {}
1054 
1055   BENCHMARK_ALWAYS_INLINE
StateIteratorStateIterator1056   explicit StateIterator(State* st)
1057       : cached_(st->skipped() ? 0 : st->max_iterations), parent_(st) {}
1058 
1059  public:
1060   BENCHMARK_ALWAYS_INLINE
1061   Value operator*() const { return Value(); }
1062 
1063   BENCHMARK_ALWAYS_INLINE
1064   StateIterator& operator++() {
1065     assert(cached_ > 0);
1066     --cached_;
1067     return *this;
1068   }
1069 
1070   BENCHMARK_ALWAYS_INLINE
1071   bool operator!=(StateIterator const&) const {
1072     if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
1073     parent_->FinishKeepRunning();
1074     return false;
1075   }
1076 
1077  private:
1078   IterationCount cached_;
1079   State* const parent_;
1080 };
1081 
begin()1082 inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
1083   return StateIterator(this);
1084 }
end()1085 inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
1086   StartKeepRunning();
1087   return StateIterator();
1088 }
1089 
1090 namespace internal {
1091 
1092 typedef void(Function)(State&);
1093 
1094 // ------------------------------------------------------
1095 // Benchmark registration object.  The BENCHMARK() macro expands
1096 // into an internal::Benchmark* object.  Various methods can
1097 // be called on this object to change the properties of the benchmark.
1098 // Each method returns "this" so that multiple method calls can
1099 // chained into one expression.
1100 class BENCHMARK_EXPORT Benchmark {
1101  public:
1102   virtual ~Benchmark();
1103 
1104   // Note: the following methods all return "this" so that multiple
1105   // method calls can be chained together in one expression.
1106 
1107   // Specify the name of the benchmark
1108   Benchmark* Name(const std::string& name);
1109 
1110   // Run this benchmark once with "x" as the extra argument passed
1111   // to the function.
1112   // REQUIRES: The function passed to the constructor must accept an arg1.
1113   Benchmark* Arg(int64_t x);
1114 
1115   // Run this benchmark with the given time unit for the generated output report
1116   Benchmark* Unit(TimeUnit unit);
1117 
1118   // Run this benchmark once for a number of values picked from the
1119   // range [start..limit].  (start and limit are always picked.)
1120   // REQUIRES: The function passed to the constructor must accept an arg1.
1121   Benchmark* Range(int64_t start, int64_t limit);
1122 
1123   // Run this benchmark once for all values in the range [start..limit] with
1124   // specific step
1125   // REQUIRES: The function passed to the constructor must accept an arg1.
1126   Benchmark* DenseRange(int64_t start, int64_t limit, int step = 1);
1127 
1128   // Run this benchmark once with "args" as the extra arguments passed
1129   // to the function.
1130   // REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
1131   Benchmark* Args(const std::vector<int64_t>& args);
1132 
1133   // Equivalent to Args({x, y})
1134   // NOTE: This is a legacy C++03 interface provided for compatibility only.
1135   //   New code should use 'Args'.
ArgPair(int64_t x,int64_t y)1136   Benchmark* ArgPair(int64_t x, int64_t y) {
1137     std::vector<int64_t> args;
1138     args.push_back(x);
1139     args.push_back(y);
1140     return Args(args);
1141   }
1142 
1143   // Run this benchmark once for a number of values picked from the
1144   // ranges [start..limit].  (starts and limits are always picked.)
1145   // REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
1146   Benchmark* Ranges(const std::vector<std::pair<int64_t, int64_t> >& ranges);
1147 
1148   // Run this benchmark once for each combination of values in the (cartesian)
1149   // product of the supplied argument lists.
1150   // REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
1151   Benchmark* ArgsProduct(const std::vector<std::vector<int64_t> >& arglists);
1152 
1153   // Equivalent to ArgNames({name})
1154   Benchmark* ArgName(const std::string& name);
1155 
1156   // Set the argument names to display in the benchmark name. If not called,
1157   // only argument values will be shown.
1158   Benchmark* ArgNames(const std::vector<std::string>& names);
1159 
1160   // Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}).
1161   // NOTE: This is a legacy C++03 interface provided for compatibility only.
1162   //   New code should use 'Ranges'.
RangePair(int64_t lo1,int64_t hi1,int64_t lo2,int64_t hi2)1163   Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
1164     std::vector<std::pair<int64_t, int64_t> > ranges;
1165     ranges.push_back(std::make_pair(lo1, hi1));
1166     ranges.push_back(std::make_pair(lo2, hi2));
1167     return Ranges(ranges);
1168   }
1169 
1170   // Have "setup" and/or "teardown" invoked once for every benchmark run.
1171   // If the benchmark is multi-threaded (will run in k threads concurrently),
1172   // the setup callback will be be invoked exactly once (not k times) before
1173   // each run with k threads. Time allowing (e.g. for a short benchmark), there
1174   // may be multiple such runs per benchmark, each run with its own
1175   // "setup"/"teardown".
1176   //
1177   // If the benchmark uses different size groups of threads (e.g. via
1178   // ThreadRange), the above will be true for each size group.
1179   //
1180   // The callback will be passed a State object, which includes the number
1181   // of threads, thread-index, benchmark arguments, etc.
1182   //
1183   // The callback must not be NULL or self-deleting.
1184   Benchmark* Setup(void (*setup)(const benchmark::State&));
1185   Benchmark* Teardown(void (*teardown)(const benchmark::State&));
1186 
1187   // Pass this benchmark object to *func, which can customize
1188   // the benchmark by calling various methods like Arg, Args,
1189   // Threads, etc.
1190   Benchmark* Apply(void (*func)(Benchmark* benchmark));
1191 
1192   // Set the range multiplier for non-dense range. If not called, the range
1193   // multiplier kRangeMultiplier will be used.
1194   Benchmark* RangeMultiplier(int multiplier);
1195 
1196   // Set the minimum amount of time to use when running this benchmark. This
1197   // option overrides the `benchmark_min_time` flag.
1198   // REQUIRES: `t > 0` and `Iterations` has not been called on this benchmark.
1199   Benchmark* MinTime(double t);
1200 
1201   // Set the minimum amount of time to run the benchmark before taking runtimes
1202   // of this benchmark into account. This
1203   // option overrides the `benchmark_min_warmup_time` flag.
1204   // REQUIRES: `t >= 0` and `Iterations` has not been called on this benchmark.
1205   Benchmark* MinWarmUpTime(double t);
1206 
1207   // Specify the amount of iterations that should be run by this benchmark.
1208   // This option overrides the `benchmark_min_time` flag.
1209   // REQUIRES: 'n > 0' and `MinTime` has not been called on this benchmark.
1210   //
1211   // NOTE: This function should only be used when *exact* iteration control is
1212   //   needed and never to control or limit how long a benchmark runs, where
1213   // `--benchmark_min_time=<N>s` or `MinTime(...)` should be used instead.
1214   Benchmark* Iterations(IterationCount n);
1215 
1216   // Specify the amount of times to repeat this benchmark. This option overrides
1217   // the `benchmark_repetitions` flag.
1218   // REQUIRES: `n > 0`
1219   Benchmark* Repetitions(int n);
1220 
1221   // Specify if each repetition of the benchmark should be reported separately
1222   // or if only the final statistics should be reported. If the benchmark
1223   // is not repeated then the single result is always reported.
1224   // Applies to *ALL* reporters (display and file).
1225   Benchmark* ReportAggregatesOnly(bool value = true);
1226 
1227   // Same as ReportAggregatesOnly(), but applies to display reporter only.
1228   Benchmark* DisplayAggregatesOnly(bool value = true);
1229 
1230   // By default, the CPU time is measured only for the main thread, which may
1231   // be unrepresentative if the benchmark uses threads internally. If called,
1232   // the total CPU time spent by all the threads will be measured instead.
1233   // By default, only the main thread CPU time will be measured.
1234   Benchmark* MeasureProcessCPUTime();
1235 
1236   // If a particular benchmark should use the Wall clock instead of the CPU time
1237   // (be it either the CPU time of the main thread only (default), or the
1238   // total CPU usage of the benchmark), call this method. If called, the elapsed
1239   // (wall) time will be used to control how many iterations are run, and in the
1240   // printing of items/second or MB/seconds values.
1241   // If not called, the CPU time used by the benchmark will be used.
1242   Benchmark* UseRealTime();
1243 
1244   // If a benchmark must measure time manually (e.g. if GPU execution time is
1245   // being
1246   // measured), call this method. If called, each benchmark iteration should
1247   // call
1248   // SetIterationTime(seconds) to report the measured time, which will be used
1249   // to control how many iterations are run, and in the printing of items/second
1250   // or MB/second values.
1251   Benchmark* UseManualTime();
1252 
1253   // Set the asymptotic computational complexity for the benchmark. If called
1254   // the asymptotic computational complexity will be shown on the output.
1255   Benchmark* Complexity(BigO complexity = benchmark::oAuto);
1256 
1257   // Set the asymptotic computational complexity for the benchmark. If called
1258   // the asymptotic computational complexity will be shown on the output.
1259   Benchmark* Complexity(BigOFunc* complexity);
1260 
1261   // Add this statistics to be computed over all the values of benchmark run
1262   Benchmark* ComputeStatistics(const std::string& name,
1263                                StatisticsFunc* statistics,
1264                                StatisticUnit unit = kTime);
1265 
1266   // Support for running multiple copies of the same benchmark concurrently
1267   // in multiple threads.  This may be useful when measuring the scaling
1268   // of some piece of code.
1269 
1270   // Run one instance of this benchmark concurrently in t threads.
1271   Benchmark* Threads(int t);
1272 
1273   // Pick a set of values T from [min_threads,max_threads].
1274   // min_threads and max_threads are always included in T.  Run this
1275   // benchmark once for each value in T.  The benchmark run for a
1276   // particular value t consists of t threads running the benchmark
1277   // function concurrently.  For example, consider:
1278   //    BENCHMARK(Foo)->ThreadRange(1,16);
1279   // This will run the following benchmarks:
1280   //    Foo in 1 thread
1281   //    Foo in 2 threads
1282   //    Foo in 4 threads
1283   //    Foo in 8 threads
1284   //    Foo in 16 threads
1285   Benchmark* ThreadRange(int min_threads, int max_threads);
1286 
1287   // For each value n in the range, run this benchmark once using n threads.
1288   // min_threads and max_threads are always included in the range.
1289   // stride specifies the increment. E.g. DenseThreadRange(1, 8, 3) starts
1290   // a benchmark with 1, 4, 7 and 8 threads.
1291   Benchmark* DenseThreadRange(int min_threads, int max_threads, int stride = 1);
1292 
1293   // Equivalent to ThreadRange(NumCPUs(), NumCPUs())
1294   Benchmark* ThreadPerCpu();
1295 
1296   virtual void Run(State& state) = 0;
1297 
1298   TimeUnit GetTimeUnit() const;
1299 
1300  protected:
1301   explicit Benchmark(const std::string& name);
1302   void SetName(const std::string& name);
1303 
1304  public:
1305   const char* GetName() const;
1306   int ArgsCnt() const;
1307   const char* GetArgName(int arg) const;
1308 
1309  private:
1310   friend class BenchmarkFamilies;
1311   friend class BenchmarkInstance;
1312 
1313   std::string name_;
1314   AggregationReportMode aggregation_report_mode_;
1315   std::vector<std::string> arg_names_;       // Args for all benchmark runs
1316   std::vector<std::vector<int64_t> > args_;  // Args for all benchmark runs
1317 
1318   TimeUnit time_unit_;
1319   bool use_default_time_unit_;
1320 
1321   int range_multiplier_;
1322   double min_time_;
1323   double min_warmup_time_;
1324   IterationCount iterations_;
1325   int repetitions_;
1326   bool measure_process_cpu_time_;
1327   bool use_real_time_;
1328   bool use_manual_time_;
1329   BigO complexity_;
1330   BigOFunc* complexity_lambda_;
1331   std::vector<Statistics> statistics_;
1332   std::vector<int> thread_counts_;
1333 
1334   typedef void (*callback_function)(const benchmark::State&);
1335   callback_function setup_;
1336   callback_function teardown_;
1337 
1338   Benchmark(Benchmark const&)
1339 #if defined(BENCHMARK_HAS_CXX11)
1340       = delete
1341 #endif
1342       ;
1343 
1344   Benchmark& operator=(Benchmark const&)
1345 #if defined(BENCHMARK_HAS_CXX11)
1346       = delete
1347 #endif
1348       ;
1349 };
1350 
1351 }  // namespace internal
1352 
1353 // Create and register a benchmark with the specified 'name' that invokes
1354 // the specified functor 'fn'.
1355 //
1356 // RETURNS: A pointer to the registered benchmark.
1357 internal::Benchmark* RegisterBenchmark(const std::string& name,
1358                                        internal::Function* fn);
1359 
1360 #if defined(BENCHMARK_HAS_CXX11)
1361 template <class Lambda>
1362 internal::Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn);
1363 #endif
1364 
1365 // Remove all registered benchmarks. All pointers to previously registered
1366 // benchmarks are invalidated.
1367 BENCHMARK_EXPORT void ClearRegisteredBenchmarks();
1368 
1369 namespace internal {
1370 // The class used to hold all Benchmarks created from static function.
1371 // (ie those created using the BENCHMARK(...) macros.
1372 class BENCHMARK_EXPORT FunctionBenchmark : public Benchmark {
1373  public:
FunctionBenchmark(const std::string & name,Function * func)1374   FunctionBenchmark(const std::string& name, Function* func)
1375       : Benchmark(name), func_(func) {}
1376 
1377   void Run(State& st) BENCHMARK_OVERRIDE;
1378 
1379  private:
1380   Function* func_;
1381 };
1382 
1383 #ifdef BENCHMARK_HAS_CXX11
1384 template <class Lambda>
1385 class LambdaBenchmark : public Benchmark {
1386  public:
Run(State & st)1387   void Run(State& st) BENCHMARK_OVERRIDE { lambda_(st); }
1388 
1389  private:
1390   template <class OLambda>
LambdaBenchmark(const std::string & name,OLambda && lam)1391   LambdaBenchmark(const std::string& name, OLambda&& lam)
1392       : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1393 
1394   LambdaBenchmark(LambdaBenchmark const&) = delete;
1395 
1396   template <class Lam>  // NOLINTNEXTLINE(readability-redundant-declaration)
1397   friend Benchmark* ::benchmark::RegisterBenchmark(const std::string&, Lam&&);
1398 
1399   Lambda lambda_;
1400 };
1401 #endif
1402 }  // namespace internal
1403 
RegisterBenchmark(const std::string & name,internal::Function * fn)1404 inline internal::Benchmark* RegisterBenchmark(const std::string& name,
1405                                               internal::Function* fn) {
1406   // FIXME: this should be a `std::make_unique<>()` but we don't have C++14.
1407   // codechecker_intentional [cplusplus.NewDeleteLeaks]
1408   return internal::RegisterBenchmarkInternal(
1409       ::new internal::FunctionBenchmark(name, fn));
1410 }
1411 
1412 #ifdef BENCHMARK_HAS_CXX11
1413 template <class Lambda>
RegisterBenchmark(const std::string & name,Lambda && fn)1414 internal::Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn) {
1415   using BenchType =
1416       internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1417   // FIXME: this should be a `std::make_unique<>()` but we don't have C++14.
1418   // codechecker_intentional [cplusplus.NewDeleteLeaks]
1419   return internal::RegisterBenchmarkInternal(
1420       ::new BenchType(name, std::forward<Lambda>(fn)));
1421 }
1422 #endif
1423 
1424 #if defined(BENCHMARK_HAS_CXX11) && \
1425     (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1426 template <class Lambda, class... Args>
RegisterBenchmark(const std::string & name,Lambda && fn,Args &&...args)1427 internal::Benchmark* RegisterBenchmark(const std::string& name, Lambda&& fn,
1428                                        Args&&... args) {
1429   return benchmark::RegisterBenchmark(
1430       name, [=](benchmark::State& st) { fn(st, args...); });
1431 }
1432 #else
1433 #define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1434 #endif
1435 
1436 // The base class for all fixture tests.
1437 class Fixture : public internal::Benchmark {
1438  public:
Fixture()1439   Fixture() : internal::Benchmark("") {}
1440 
Run(State & st)1441   void Run(State& st) BENCHMARK_OVERRIDE {
1442     this->SetUp(st);
1443     this->BenchmarkCase(st);
1444     this->TearDown(st);
1445   }
1446 
1447   // These will be deprecated ...
SetUp(const State &)1448   virtual void SetUp(const State&) {}
TearDown(const State &)1449   virtual void TearDown(const State&) {}
1450   // ... In favor of these.
SetUp(State & st)1451   virtual void SetUp(State& st) { SetUp(const_cast<const State&>(st)); }
TearDown(State & st)1452   virtual void TearDown(State& st) { TearDown(const_cast<const State&>(st)); }
1453 
1454  protected:
1455   virtual void BenchmarkCase(State&) = 0;
1456 };
1457 }  // namespace benchmark
1458 
1459 // ------------------------------------------------------
1460 // Macro to register benchmarks
1461 
1462 // Check that __COUNTER__ is defined and that __COUNTER__ increases by 1
1463 // every time it is expanded. X + 1 == X + 0 is used in case X is defined to be
1464 // empty. If X is empty the expression becomes (+1 == +0).
1465 #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1466 #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1467 #else
1468 #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1469 #endif
1470 
1471 // Helpers for generating unique variable names
1472 #ifdef BENCHMARK_HAS_CXX11
1473 #define BENCHMARK_PRIVATE_NAME(...)                                      \
1474   BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
1475                            __VA_ARGS__)
1476 #else
1477 #define BENCHMARK_PRIVATE_NAME(n) \
1478   BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1479 #endif  // BENCHMARK_HAS_CXX11
1480 
1481 #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1482 #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1483 // Helper for concatenation with macro name expansion
1484 #define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
1485   BaseClass##_##Method##_Benchmark
1486 
1487 #define BENCHMARK_PRIVATE_DECLARE(n)                                 \
1488   static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1489       BENCHMARK_UNUSED
1490 
1491 #ifdef BENCHMARK_HAS_CXX11
1492 #define BENCHMARK(...)                                               \
1493   BENCHMARK_PRIVATE_DECLARE(_benchmark_) =                           \
1494       (::benchmark::internal::RegisterBenchmarkInternal(             \
1495           new ::benchmark::internal::FunctionBenchmark(#__VA_ARGS__, \
1496                                                        __VA_ARGS__)))
1497 #else
1498 #define BENCHMARK(n)                                     \
1499   BENCHMARK_PRIVATE_DECLARE(n) =                         \
1500       (::benchmark::internal::RegisterBenchmarkInternal( \
1501           new ::benchmark::internal::FunctionBenchmark(#n, n)))
1502 #endif  // BENCHMARK_HAS_CXX11
1503 
1504 // Old-style macros
1505 #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1506 #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1507 #define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1508 #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1509 #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1510   BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1511 
1512 #ifdef BENCHMARK_HAS_CXX11
1513 
1514 // Register a benchmark which invokes the function specified by `func`
1515 // with the additional arguments specified by `...`.
1516 //
1517 // For example:
1518 //
1519 // template <class ...ExtraArgs>`
1520 // void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
1521 //  [...]
1522 //}
1523 // /* Registers a benchmark named "BM_takes_args/int_string_test` */
1524 // BENCHMARK_CAPTURE(BM_takes_args, int_string_test, 42, std::string("abc"));
1525 #define BENCHMARK_CAPTURE(func, test_case_name, ...)     \
1526   BENCHMARK_PRIVATE_DECLARE(_benchmark_) =               \
1527       (::benchmark::internal::RegisterBenchmarkInternal( \
1528           new ::benchmark::internal::FunctionBenchmark(  \
1529               #func "/" #test_case_name,                 \
1530               [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1531 
1532 #endif  // BENCHMARK_HAS_CXX11
1533 
1534 // This will register a benchmark for a templatized function.  For example:
1535 //
1536 // template<int arg>
1537 // void BM_Foo(int iters);
1538 //
1539 // BENCHMARK_TEMPLATE(BM_Foo, 1);
1540 //
1541 // will register BM_Foo<1> as a benchmark.
1542 #define BENCHMARK_TEMPLATE1(n, a)                        \
1543   BENCHMARK_PRIVATE_DECLARE(n) =                         \
1544       (::benchmark::internal::RegisterBenchmarkInternal( \
1545           new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1546 
1547 #define BENCHMARK_TEMPLATE2(n, a, b)                                         \
1548   BENCHMARK_PRIVATE_DECLARE(n) =                                             \
1549       (::benchmark::internal::RegisterBenchmarkInternal(                     \
1550           new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1551                                                        n<a, b>)))
1552 
1553 #ifdef BENCHMARK_HAS_CXX11
1554 #define BENCHMARK_TEMPLATE(n, ...)                       \
1555   BENCHMARK_PRIVATE_DECLARE(n) =                         \
1556       (::benchmark::internal::RegisterBenchmarkInternal( \
1557           new ::benchmark::internal::FunctionBenchmark(  \
1558               #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1559 #else
1560 #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1561 #endif
1562 
1563 #ifdef BENCHMARK_HAS_CXX11
1564 // This will register a benchmark for a templatized function,
1565 // with the additional arguments specified by `...`.
1566 //
1567 // For example:
1568 //
1569 // template <typename T, class ...ExtraArgs>`
1570 // void BM_takes_args(benchmark::State& state, ExtraArgs&&... extra_args) {
1571 //  [...]
1572 //}
1573 // /* Registers a benchmark named "BM_takes_args<void>/int_string_test` */
1574 // BENCHMARK_TEMPLATE1_CAPTURE(BM_takes_args, void, int_string_test, 42,
1575 //                             std::string("abc"));
1576 #define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
1577   BENCHMARK_CAPTURE(func<a>, test_case_name, __VA_ARGS__)
1578 
1579 #define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
1580   BENCHMARK_PRIVATE_DECLARE(func) =                                  \
1581       (::benchmark::internal::RegisterBenchmarkInternal(             \
1582           new ::benchmark::internal::FunctionBenchmark(              \
1583               #func "<" #a "," #b ">"                                \
1584                     "/" #test_case_name,                             \
1585               [](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
1586 #endif  // BENCHMARK_HAS_CXX11
1587 
1588 #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method)          \
1589   class BaseClass##_##Method##_Benchmark : public BaseClass {   \
1590    public:                                                      \
1591     BaseClass##_##Method##_Benchmark() {                        \
1592       this->SetName(#BaseClass "/" #Method);                    \
1593     }                                                           \
1594                                                                 \
1595    protected:                                                   \
1596     void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1597   };
1598 
1599 #define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1600   class BaseClass##_##Method##_Benchmark : public BaseClass<a> {    \
1601    public:                                                          \
1602     BaseClass##_##Method##_Benchmark() {                            \
1603       this->SetName(#BaseClass "<" #a ">/" #Method);                \
1604     }                                                               \
1605                                                                     \
1606    protected:                                                       \
1607     void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE;     \
1608   };
1609 
1610 #define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1611   class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> {    \
1612    public:                                                             \
1613     BaseClass##_##Method##_Benchmark() {                               \
1614       this->SetName(#BaseClass "<" #a "," #b ">/" #Method);            \
1615     }                                                                  \
1616                                                                        \
1617    protected:                                                          \
1618     void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE;        \
1619   };
1620 
1621 #ifdef BENCHMARK_HAS_CXX11
1622 #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...)       \
1623   class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1624    public:                                                                 \
1625     BaseClass##_##Method##_Benchmark() {                                   \
1626       this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method);             \
1627     }                                                                      \
1628                                                                            \
1629    protected:                                                              \
1630     void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE;            \
1631   };
1632 #else
1633 #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1634   BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1635 #endif
1636 
1637 #define BENCHMARK_DEFINE_F(BaseClass, Method)    \
1638   BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1639   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1640 
1641 #define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)    \
1642   BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1643   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1644 
1645 #define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b)    \
1646   BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1647   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1648 
1649 #ifdef BENCHMARK_HAS_CXX11
1650 #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...)            \
1651   BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1652   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1653 #else
1654 #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1655   BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1656 #endif
1657 
1658 #define BENCHMARK_REGISTER_F(BaseClass, Method) \
1659   BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
1660 
1661 #define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1662   BENCHMARK_PRIVATE_DECLARE(TestName) =        \
1663       (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1664 
1665 // This macro will define and register a benchmark within a fixture class.
1666 #define BENCHMARK_F(BaseClass, Method)           \
1667   BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1668   BENCHMARK_REGISTER_F(BaseClass, Method);       \
1669   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1670 
1671 #define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)           \
1672   BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1673   BENCHMARK_REGISTER_F(BaseClass, Method);                    \
1674   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1675 
1676 #define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b)           \
1677   BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1678   BENCHMARK_REGISTER_F(BaseClass, Method);                       \
1679   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1680 
1681 #ifdef BENCHMARK_HAS_CXX11
1682 #define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...)                   \
1683   BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1684   BENCHMARK_REGISTER_F(BaseClass, Method);                             \
1685   void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1686 #else
1687 #define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1688   BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1689 #endif
1690 
1691 // Helper macro to create a main routine in a test that runs the benchmarks
1692 // Note the workaround for Hexagon simulator passing argc != 0, argv = NULL.
1693 #define BENCHMARK_MAIN()                                                \
1694   int main(int argc, char** argv) {                                     \
1695     char arg0_default[] = "benchmark";                                  \
1696     char* args_default = arg0_default;                                  \
1697     if (!argv) {                                                        \
1698       argc = 1;                                                         \
1699       argv = &args_default;                                             \
1700     }                                                                   \
1701     ::benchmark::Initialize(&argc, argv);                               \
1702     if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1703     ::benchmark::RunSpecifiedBenchmarks();                              \
1704     ::benchmark::Shutdown();                                            \
1705     return 0;                                                           \
1706   }                                                                     \
1707   int main(int, char**)
1708 
1709 // ------------------------------------------------------
1710 // Benchmark Reporters
1711 
1712 namespace benchmark {
1713 
1714 struct BENCHMARK_EXPORT CPUInfo {
1715   struct CacheInfo {
1716     std::string type;
1717     int level;
1718     int size;
1719     int num_sharing;
1720   };
1721 
1722   enum Scaling { UNKNOWN, ENABLED, DISABLED };
1723 
1724   int num_cpus;
1725   Scaling scaling;
1726   double cycles_per_second;
1727   std::vector<CacheInfo> caches;
1728   std::vector<double> load_avg;
1729 
1730   static const CPUInfo& Get();
1731 
1732  private:
1733   CPUInfo();
1734   BENCHMARK_DISALLOW_COPY_AND_ASSIGN(CPUInfo);
1735 };
1736 
1737 // Adding Struct for System Information
1738 struct BENCHMARK_EXPORT SystemInfo {
1739   std::string name;
1740   static const SystemInfo& Get();
1741 
1742  private:
1743   SystemInfo();
1744   BENCHMARK_DISALLOW_COPY_AND_ASSIGN(SystemInfo);
1745 };
1746 
1747 // BenchmarkName contains the components of the Benchmark's name
1748 // which allows individual fields to be modified or cleared before
1749 // building the final name using 'str()'.
1750 struct BENCHMARK_EXPORT BenchmarkName {
1751   std::string function_name;
1752   std::string args;
1753   std::string min_time;
1754   std::string min_warmup_time;
1755   std::string iterations;
1756   std::string repetitions;
1757   std::string time_type;
1758   std::string threads;
1759 
1760   // Return the full name of the benchmark with each non-empty
1761   // field separated by a '/'
1762   std::string str() const;
1763 };
1764 
1765 // Interface for custom benchmark result printers.
1766 // By default, benchmark reports are printed to stdout. However an application
1767 // can control the destination of the reports by calling
1768 // RunSpecifiedBenchmarks and passing it a custom reporter object.
1769 // The reporter object must implement the following interface.
1770 class BENCHMARK_EXPORT BenchmarkReporter {
1771  public:
1772   struct Context {
1773     CPUInfo const& cpu_info;
1774     SystemInfo const& sys_info;
1775     // The number of chars in the longest benchmark name.
1776     size_t name_field_width;
1777     static const char* executable_name;
1778     Context();
1779   };
1780 
1781   struct BENCHMARK_EXPORT Run {
1782     static const int64_t no_repetition_index = -1;
1783     enum RunType { RT_Iteration, RT_Aggregate };
1784 
RunRun1785     Run()
1786         : run_type(RT_Iteration),
1787           aggregate_unit(kTime),
1788           skipped(internal::NotSkipped),
1789           iterations(1),
1790           threads(1),
1791           time_unit(GetDefaultTimeUnit()),
1792           real_accumulated_time(0),
1793           cpu_accumulated_time(0),
1794           max_heapbytes_used(0),
1795           use_real_time_for_initial_big_o(false),
1796           complexity(oNone),
1797           complexity_lambda(),
1798           complexity_n(0),
1799           report_big_o(false),
1800           report_rms(false),
1801           memory_result(NULL),
1802           allocs_per_iter(0.0) {}
1803 
1804     std::string benchmark_name() const;
1805     BenchmarkName run_name;
1806     int64_t family_index;
1807     int64_t per_family_instance_index;
1808     RunType run_type;
1809     std::string aggregate_name;
1810     StatisticUnit aggregate_unit;
1811     std::string report_label;  // Empty if not set by benchmark.
1812     internal::Skipped skipped;
1813     std::string skip_message;
1814 
1815     IterationCount iterations;
1816     int64_t threads;
1817     int64_t repetition_index;
1818     int64_t repetitions;
1819     TimeUnit time_unit;
1820     double real_accumulated_time;
1821     double cpu_accumulated_time;
1822 
1823     // Return a value representing the real time per iteration in the unit
1824     // specified by 'time_unit'.
1825     // NOTE: If 'iterations' is zero the returned value represents the
1826     // accumulated time.
1827     double GetAdjustedRealTime() const;
1828 
1829     // Return a value representing the cpu time per iteration in the unit
1830     // specified by 'time_unit'.
1831     // NOTE: If 'iterations' is zero the returned value represents the
1832     // accumulated time.
1833     double GetAdjustedCPUTime() const;
1834 
1835     // This is set to 0.0 if memory tracing is not enabled.
1836     double max_heapbytes_used;
1837 
1838     // By default Big-O is computed for CPU time, but that is not what you want
1839     // to happen when manual time was requested, which is stored as real time.
1840     bool use_real_time_for_initial_big_o;
1841 
1842     // Keep track of arguments to compute asymptotic complexity
1843     BigO complexity;
1844     BigOFunc* complexity_lambda;
1845     ComplexityN complexity_n;
1846 
1847     // what statistics to compute from the measurements
1848     const std::vector<internal::Statistics>* statistics;
1849 
1850     // Inform print function whether the current run is a complexity report
1851     bool report_big_o;
1852     bool report_rms;
1853 
1854     UserCounters counters;
1855 
1856     // Memory metrics.
1857     const MemoryManager::Result* memory_result;
1858     double allocs_per_iter;
1859   };
1860 
1861   struct PerFamilyRunReports {
PerFamilyRunReportsPerFamilyRunReports1862     PerFamilyRunReports() : num_runs_total(0), num_runs_done(0) {}
1863 
1864     // How many runs will all instances of this benchmark perform?
1865     int num_runs_total;
1866 
1867     // How many runs have happened already?
1868     int num_runs_done;
1869 
1870     // The reports about (non-errneous!) runs of this family.
1871     std::vector<BenchmarkReporter::Run> Runs;
1872   };
1873 
1874   // Construct a BenchmarkReporter with the output stream set to 'std::cout'
1875   // and the error stream set to 'std::cerr'
1876   BenchmarkReporter();
1877 
1878   // Called once for every suite of benchmarks run.
1879   // The parameter "context" contains information that the
1880   // reporter may wish to use when generating its report, for example the
1881   // platform under which the benchmarks are running. The benchmark run is
1882   // never started if this function returns false, allowing the reporter
1883   // to skip runs based on the context information.
1884   virtual bool ReportContext(const Context& context) = 0;
1885 
1886   // Called once for each group of benchmark runs, gives information about
1887   // the configurations of the runs.
ReportRunsConfig(double,bool,IterationCount)1888   virtual void ReportRunsConfig(double /*min_time*/,
1889                                 bool /*has_explicit_iters*/,
1890                                 IterationCount /*iters*/) {}
1891 
1892   // Called once for each group of benchmark runs, gives information about
1893   // cpu-time and heap memory usage during the benchmark run. If the group
1894   // of runs contained more than two entries then 'report' contains additional
1895   // elements representing the mean and standard deviation of those runs.
1896   // Additionally if this group of runs was the last in a family of benchmarks
1897   // 'reports' contains additional entries representing the asymptotic
1898   // complexity and RMS of that benchmark family.
1899   virtual void ReportRuns(const std::vector<Run>& report) = 0;
1900 
1901   // Called once and only once after ever group of benchmarks is run and
1902   // reported.
Finalize()1903   virtual void Finalize() {}
1904 
1905   // REQUIRES: The object referenced by 'out' is valid for the lifetime
1906   // of the reporter.
SetOutputStream(std::ostream * out)1907   void SetOutputStream(std::ostream* out) {
1908     assert(out);
1909     output_stream_ = out;
1910   }
1911 
1912   // REQUIRES: The object referenced by 'err' is valid for the lifetime
1913   // of the reporter.
SetErrorStream(std::ostream * err)1914   void SetErrorStream(std::ostream* err) {
1915     assert(err);
1916     error_stream_ = err;
1917   }
1918 
GetOutputStream()1919   std::ostream& GetOutputStream() const { return *output_stream_; }
1920 
GetErrorStream()1921   std::ostream& GetErrorStream() const { return *error_stream_; }
1922 
1923   virtual ~BenchmarkReporter();
1924 
1925   // Write a human readable string to 'out' representing the specified
1926   // 'context'.
1927   // REQUIRES: 'out' is non-null.
1928   static void PrintBasicContext(std::ostream* out, Context const& context);
1929 
1930  private:
1931   std::ostream* output_stream_;
1932   std::ostream* error_stream_;
1933 };
1934 
1935 // Simple reporter that outputs benchmark data to the console. This is the
1936 // default reporter used by RunSpecifiedBenchmarks().
1937 class BENCHMARK_EXPORT ConsoleReporter : public BenchmarkReporter {
1938  public:
1939   enum OutputOptions {
1940     OO_None = 0,
1941     OO_Color = 1,
1942     OO_Tabular = 2,
1943     OO_ColorTabular = OO_Color | OO_Tabular,
1944     OO_Defaults = OO_ColorTabular
1945   };
1946   explicit ConsoleReporter(OutputOptions opts_ = OO_Defaults)
output_options_(opts_)1947       : output_options_(opts_), name_field_width_(0), printed_header_(false) {}
1948 
1949   bool ReportContext(const Context& context) BENCHMARK_OVERRIDE;
1950   void ReportRuns(const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1951 
1952  protected:
1953   virtual void PrintRunData(const Run& report);
1954   virtual void PrintHeader(const Run& report);
1955 
1956   OutputOptions output_options_;
1957   size_t name_field_width_;
1958   UserCounters prev_counters_;
1959   bool printed_header_;
1960 };
1961 
1962 class BENCHMARK_EXPORT JSONReporter : public BenchmarkReporter {
1963  public:
JSONReporter()1964   JSONReporter() : first_report_(true) {}
1965   bool ReportContext(const Context& context) BENCHMARK_OVERRIDE;
1966   void ReportRuns(const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1967   void Finalize() BENCHMARK_OVERRIDE;
1968 
1969  private:
1970   void PrintRunData(const Run& report);
1971 
1972   bool first_report_;
1973 };
1974 
1975 class BENCHMARK_EXPORT BENCHMARK_DEPRECATED_MSG(
1976     "The CSV Reporter will be removed in a future release") CSVReporter
1977     : public BenchmarkReporter {
1978  public:
CSVReporter()1979   CSVReporter() : printed_header_(false) {}
1980   bool ReportContext(const Context& context) BENCHMARK_OVERRIDE;
1981   void ReportRuns(const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1982 
1983  private:
1984   void PrintRunData(const Run& report);
1985 
1986   bool printed_header_;
1987   std::set<std::string> user_counter_names_;
1988 };
1989 
GetTimeUnitString(TimeUnit unit)1990 inline const char* GetTimeUnitString(TimeUnit unit) {
1991   switch (unit) {
1992     case kSecond:
1993       return "s";
1994     case kMillisecond:
1995       return "ms";
1996     case kMicrosecond:
1997       return "us";
1998     case kNanosecond:
1999       return "ns";
2000   }
2001   BENCHMARK_UNREACHABLE();
2002 }
2003 
GetTimeUnitMultiplier(TimeUnit unit)2004 inline double GetTimeUnitMultiplier(TimeUnit unit) {
2005   switch (unit) {
2006     case kSecond:
2007       return 1;
2008     case kMillisecond:
2009       return 1e3;
2010     case kMicrosecond:
2011       return 1e6;
2012     case kNanosecond:
2013       return 1e9;
2014   }
2015   BENCHMARK_UNREACHABLE();
2016 }
2017 
2018 // Creates a list of integer values for the given range and multiplier.
2019 // This can be used together with ArgsProduct() to allow multiple ranges
2020 // with different multipliers.
2021 // Example:
2022 // ArgsProduct({
2023 //   CreateRange(0, 1024, /*multi=*/32),
2024 //   CreateRange(0, 100, /*multi=*/4),
2025 //   CreateDenseRange(0, 4, /*step=*/1),
2026 // });
2027 BENCHMARK_EXPORT
2028 std::vector<int64_t> CreateRange(int64_t lo, int64_t hi, int multi);
2029 
2030 // Creates a list of integer values for the given range and step.
2031 BENCHMARK_EXPORT
2032 std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit, int step);
2033 
2034 }  // namespace benchmark
2035 
2036 #if defined(_MSC_VER)
2037 #pragma warning(pop)
2038 #endif
2039 
2040 #endif  // BENCHMARK_BENCHMARK_H_
2041