[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / third-party / benchmark / src / benchmark_api_internal.cc
blob286f986530859492a271f7ef97ae092dae3ffa32
1 #include "benchmark_api_internal.h"
3 #include <cinttypes>
5 #include "string_util.h"
7 namespace benchmark {
8 namespace internal {
10 BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx,
11 int per_family_instance_idx,
12 const std::vector<int64_t>& args,
13 int thread_count)
14 : benchmark_(*benchmark),
15 family_index_(family_idx),
16 per_family_instance_index_(per_family_instance_idx),
17 aggregation_report_mode_(benchmark_.aggregation_report_mode_),
18 args_(args),
19 time_unit_(benchmark_.GetTimeUnit()),
20 measure_process_cpu_time_(benchmark_.measure_process_cpu_time_),
21 use_real_time_(benchmark_.use_real_time_),
22 use_manual_time_(benchmark_.use_manual_time_),
23 complexity_(benchmark_.complexity_),
24 complexity_lambda_(benchmark_.complexity_lambda_),
25 statistics_(benchmark_.statistics_),
26 repetitions_(benchmark_.repetitions_),
27 min_time_(benchmark_.min_time_),
28 min_warmup_time_(benchmark_.min_warmup_time_),
29 iterations_(benchmark_.iterations_),
30 threads_(thread_count) {
31 name_.function_name = benchmark_.name_;
33 size_t arg_i = 0;
34 for (const auto& arg : args) {
35 if (!name_.args.empty()) {
36 name_.args += '/';
39 if (arg_i < benchmark->arg_names_.size()) {
40 const auto& arg_name = benchmark_.arg_names_[arg_i];
41 if (!arg_name.empty()) {
42 name_.args += StrFormat("%s:", arg_name.c_str());
46 name_.args += StrFormat("%" PRId64, arg);
47 ++arg_i;
50 if (!IsZero(benchmark->min_time_)) {
51 name_.min_time = StrFormat("min_time:%0.3f", benchmark_.min_time_);
54 if (!IsZero(benchmark->min_warmup_time_)) {
55 name_.min_warmup_time =
56 StrFormat("min_warmup_time:%0.3f", benchmark_.min_warmup_time_);
59 if (benchmark_.iterations_ != 0) {
60 name_.iterations = StrFormat(
61 "iterations:%lu", static_cast<unsigned long>(benchmark_.iterations_));
64 if (benchmark_.repetitions_ != 0) {
65 name_.repetitions = StrFormat("repeats:%d", benchmark_.repetitions_);
68 if (benchmark_.measure_process_cpu_time_) {
69 name_.time_type = "process_time";
72 if (benchmark_.use_manual_time_) {
73 if (!name_.time_type.empty()) {
74 name_.time_type += '/';
76 name_.time_type += "manual_time";
77 } else if (benchmark_.use_real_time_) {
78 if (!name_.time_type.empty()) {
79 name_.time_type += '/';
81 name_.time_type += "real_time";
84 if (!benchmark_.thread_counts_.empty()) {
85 name_.threads = StrFormat("threads:%d", threads_);
88 setup_ = benchmark_.setup_;
89 teardown_ = benchmark_.teardown_;
92 State BenchmarkInstance::Run(
93 IterationCount iters, int thread_id, internal::ThreadTimer* timer,
94 internal::ThreadManager* manager,
95 internal::PerfCountersMeasurement* perf_counters_measurement) const {
96 State st(name_.function_name, iters, args_, thread_id, threads_, timer,
97 manager, perf_counters_measurement);
98 benchmark_.Run(st);
99 return st;
102 void BenchmarkInstance::Setup() const {
103 if (setup_) {
104 State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
105 nullptr, nullptr, nullptr);
106 setup_(st);
110 void BenchmarkInstance::Teardown() const {
111 if (teardown_) {
112 State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
113 nullptr, nullptr, nullptr);
114 teardown_(st);
117 } // namespace internal
118 } // namespace benchmark