1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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  *      http://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 
17 #include <inttypes.h>
18 #include <regex.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <time.h>
23 
24 #include <string>
25 #include <vector>
26 
27 #include <base/stringprintf.h>
28 
29 #include <benchmark/Benchmark.h>
30 
31 #include "utils.h"
32 
33 namespace testing {
34 
NanoTime()35 static uint64_t NanoTime() {
36   struct timespec t;
37   t.tv_sec = t.tv_nsec = 0;
38   clock_gettime(CLOCK_MONOTONIC, &t);
39   return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
40 }
41 
42 bool Benchmark::header_printed_;
43 
List()44 std::vector<Benchmark*>& Benchmark::List() {
45   static std::vector<Benchmark*> list;
46   return list;
47 }
48 
MaxNameColumnWidth()49 int Benchmark::MaxNameColumnWidth() {
50   size_t max = 20;
51   for (auto& benchmark : List()) {
52     max = std::max(max, benchmark->NameColumnWidth());
53   }
54   return static_cast<int>(max);
55 }
56 
RunAll(std::vector<regex_t * > & regs)57 size_t Benchmark::RunAll(std::vector<regex_t*>& regs) {
58   size_t benchmarks_run = 0;
59   header_printed_ = false;
60   for (auto& benchmark : List()) {
61     benchmarks_run += benchmark->RunAllArgs(regs);
62   }
63   return benchmarks_run;
64 }
65 
PrintHeader()66 void Benchmark::PrintHeader() {
67   if (!header_printed_) {
68     printf("%-*s %10s %10s\n", MaxNameColumnWidth(), "", "iterations", "ns/op");
69     header_printed_ = true;
70   }
71 }
72 
73 template <typename T>
ShouldRun(std::vector<regex_t * > & regs,T arg)74 bool BenchmarkT<T>::ShouldRun(std::vector<regex_t*>& regs, T arg) {
75   if (regs.empty()) {
76     return true;
77   }
78 
79   for (const auto& re : regs) {
80     if (regexec(re, GetNameStr(arg).c_str(), 0, NULL, 0) != REG_NOMATCH) {
81       return true;
82     }
83   }
84   return false;
85 }
86 
StopBenchmarkTiming()87 void Benchmark::StopBenchmarkTiming() {
88   if (start_time_ns_ != 0) {
89     total_time_ns_ += NanoTime() - start_time_ns_;
90   }
91   start_time_ns_ = 0;
92 }
93 
StartBenchmarkTiming()94 void Benchmark::StartBenchmarkTiming() {
95   if (start_time_ns_ == 0) {
96     start_time_ns_ = NanoTime();
97   }
98 }
99 
GetNameStr(void *)100 std::string BenchmarkWithoutArg::GetNameStr(void*) {
101   return Name();
102 }
103 
104 template <>
GetNameStr(int arg)105 std::string BenchmarkWithArg<int>::GetNameStr(int arg) {
106   return Name() + "/" + PrettyInt(arg, 2);
107 }
108 
109 template <>
GetNameStr(double arg)110 std::string BenchmarkWithArg<double>::GetNameStr(double arg) {
111   return Name() + "/" + android::base::StringPrintf("%0.6f", arg);
112 }
113 
114 template<typename T>
RunWithArg(T arg)115 void BenchmarkT<T>::RunWithArg(T arg) {
116   int new_iterations = 1;
117   int iterations;
118   while (new_iterations < 1e8) {
119     bytes_processed_ = 0;
120     total_time_ns_ = 0;
121     start_time_ns_ = 0;
122 
123     iterations = new_iterations;
124     RunIterations(iterations, arg);
125     if (total_time_ns_ >= 1e9) {
126       break;
127     }
128 
129     if (total_time_ns_/iterations == 0) {
130       new_iterations = 1e9;
131     } else {
132       new_iterations = 1e9/ (total_time_ns_/iterations);
133     }
134     new_iterations = std::max(iterations + 1,
135                           std::min(new_iterations + new_iterations/2, 100*iterations));
136 
137     new_iterations = Round(new_iterations);
138   }
139 
140   printf("%-*s %10s %10" PRId64, MaxNameColumnWidth(), GetNameStr(arg).c_str(),
141          PrettyInt(iterations, 10).c_str(), total_time_ns_/iterations);
142 
143   if (total_time_ns_ > 0 && bytes_processed_ > 0) {
144     double gib_processed = static_cast<double>(bytes_processed_)/1e9;
145     double seconds = static_cast<double>(total_time_ns_)/1e9;
146     printf(" %8.3f GiB/s", gib_processed/seconds);
147   }
148   printf("\n");
149   fflush(stdout);
150 }
151 
152 template class BenchmarkT<int>;
153 template class BenchmarkT<double>;
154 template class BenchmarkT<void*>;
155 
156 template class BenchmarkWithArg<int>;
157 template class BenchmarkWithArg<double>;
158 
159 }  // namespace testing
160