Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #ifndef ART_SRC_TIMING_LOGGER_H_ |
| 18 | #define ART_SRC_TIMING_LOGGER_H_ |
| 19 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 21 | #include "utils.h" |
| 22 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 23 | #include <cmath> |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 24 | #include <stdint.h> |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 30 | class CumulativeLogger; |
| 31 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 32 | class TimingLogger { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 33 | public: |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 34 | explicit TimingLogger(const std::string& name, bool precise = false) |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 35 | : name_(name), precise_(precise) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 36 | AddSplit(""); |
| 37 | } |
| 38 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 39 | void Reset() { |
| 40 | times_.clear(); |
| 41 | labels_.clear(); |
| 42 | AddSplit(""); |
| 43 | } |
| 44 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 45 | void AddSplit(const std::string& label) { |
| 46 | times_.push_back(NanoTime()); |
| 47 | labels_.push_back(label); |
| 48 | } |
| 49 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 50 | void Dump() const { |
| 51 | Dump(LOG(INFO)); |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 54 | void Dump(std::ostream& os) const { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 55 | uint64_t largest_time = 0; |
Elliott Hughes | 4cb0568 | 2012-05-24 19:05:53 -0700 | [diff] [blame] | 56 | os << name_ << ": begin\n"; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 57 | for (size_t i = 1; i < times_.size(); ++i) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 58 | uint64_t delta_time = times_[i] - times_[i - 1]; |
| 59 | largest_time = std::max(largest_time, delta_time); |
| 60 | } |
| 61 | // Compute which type of unit we will use for printing the timings. |
| 62 | TimeUnit tu = GetAppropriateTimeUnit(largest_time); |
| 63 | uint64_t divisor = GetNsToTimeUnitDivisor(tu); |
| 64 | for (size_t i = 1; i < times_.size(); ++i) { |
| 65 | uint64_t delta_time = times_[i] - times_[i - 1]; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 66 | if (!precise_ && divisor >= 1000) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 67 | // Make the fraction 0. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 68 | delta_time -= delta_time % (divisor / 1000); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 69 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 70 | os << name_ << ": " << std::setw(8) << FormatDuration(delta_time, tu) << " " << labels_[i] |
| 71 | << "\n"; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 72 | } |
Elliott Hughes | 4cb0568 | 2012-05-24 19:05:53 -0700 | [diff] [blame] | 73 | os << name_ << ": end, " << NsToMs(GetTotalNs()) << " ms\n"; |
Elliott Hughes | 73e66f7 | 2012-05-09 09:34:45 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | uint64_t GetTotalNs() const { |
Elliott Hughes | 601a123 | 2012-02-02 17:47:38 -0800 | [diff] [blame] | 77 | return times_.back() - times_.front(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 80 | protected: |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 81 | std::string name_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 82 | bool precise_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 83 | std::vector<uint64_t> times_; |
| 84 | std::vector<std::string> labels_; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 85 | |
| 86 | friend class CumulativeLogger; |
| 87 | }; |
| 88 | |
| 89 | class CumulativeLogger { |
| 90 | public: |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 91 | explicit CumulativeLogger(const std::string& name = "", bool precise = false) |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 92 | : name_(name), |
Ian Rogers | 5f5a2c0 | 2012-09-17 10:52:08 -0700 | [diff] [blame] | 93 | precise_(precise) { |
| 94 | Reset(); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 97 | void SetName(const std::string& name) { |
| 98 | name_ = name; |
| 99 | } |
| 100 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 101 | void Start() { |
| 102 | index_ = 0; |
| 103 | last_split_ = NanoTime(); |
| 104 | } |
| 105 | |
| 106 | void End() { |
| 107 | iterations_++; |
| 108 | } |
| 109 | |
| 110 | void AddSplit(const std::string& label) { |
| 111 | uint64_t cur_time = NanoTime(); |
| 112 | AddPair(label, cur_time - last_split_); |
| 113 | last_split_ = cur_time; |
| 114 | } |
| 115 | |
| 116 | void Reset() { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 117 | times_.clear(); |
| 118 | labels_.clear(); |
| 119 | times_squared_.clear(); |
Ian Rogers | 5f5a2c0 | 2012-09-17 10:52:08 -0700 | [diff] [blame] | 120 | iterations_ = 0; |
| 121 | total_time_squared_ = 0; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void AddPair(const std::string& label, uint64_t delta_time) { |
| 125 | // Convert delta time to microseconds so that we don't overflow our counters. |
| 126 | delta_time /= kAdjust; |
| 127 | if (index_ >= times_.size()) { |
| 128 | times_.push_back(delta_time); |
| 129 | times_squared_.push_back(delta_time * delta_time); |
| 130 | labels_.push_back(label); |
| 131 | } else { |
| 132 | times_[index_] += delta_time; |
| 133 | times_squared_[index_] += delta_time * delta_time; |
| 134 | DCHECK_EQ(labels_[index_], label); |
| 135 | } |
| 136 | index_++; |
| 137 | } |
| 138 | |
| 139 | void AddLogger(const TimingLogger& logger) { |
| 140 | DCHECK_EQ(logger.times_.size(), logger.labels_.size()); |
| 141 | uint64_t total_time = 0; |
| 142 | for (size_t i = 1; i < logger.times_.size(); ++i) { |
| 143 | const uint64_t delta_time = logger.times_[i] - logger.times_[i - 1]; |
| 144 | const std::string& label = logger.labels_[i]; |
| 145 | AddPair(label, delta_time); |
| 146 | total_time += delta_time; |
| 147 | } |
| 148 | total_time /= kAdjust; |
| 149 | total_time_squared_ += total_time * total_time; |
| 150 | } |
| 151 | |
| 152 | void Dump() const { |
| 153 | Dump(LOG(INFO)); |
| 154 | } |
| 155 | |
| 156 | void Dump(std::ostream& os) const { |
| 157 | os << name_ << ": iterations " << iterations_ << " begin\n"; |
| 158 | //Find which unit we will use for the timing logger. |
| 159 | uint64_t largest_mean = 0; |
| 160 | for (size_t i = 0; i < times_.size(); ++i) { |
| 161 | // Convert back to nanoseconds from microseconds. |
| 162 | uint64_t mean = times_[i] / iterations_; |
| 163 | largest_mean = std::max(largest_mean, mean); |
| 164 | } |
| 165 | // Convert largest mean back to ns |
| 166 | TimeUnit tu = GetAppropriateTimeUnit(largest_mean * kAdjust); |
| 167 | uint64_t divisor = GetNsToTimeUnitDivisor(tu); |
| 168 | for (size_t i = 0; i < times_.size(); ++i) { |
| 169 | uint64_t mean_x2 = times_squared_[i] / iterations_; |
| 170 | uint64_t mean = times_[i] / iterations_; |
| 171 | uint64_t variance = mean_x2 - (mean * mean); |
| 172 | uint64_t std_dev = static_cast<uint64_t>(std::sqrt(static_cast<double>(variance))); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 173 | if (!precise_ && divisor >= 1000) { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 174 | // Make the fraction 0. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 175 | mean -= mean % (divisor / 1000); |
| 176 | std_dev -= std_dev % (divisor / 1000); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 177 | } |
| 178 | os << name_ << ": " << std::setw(8) |
| 179 | << FormatDuration(mean * kAdjust, tu) << " std_dev " |
| 180 | << FormatDuration(std_dev * kAdjust, tu) << " " << labels_[i] << "\n"; |
| 181 | } |
| 182 | uint64_t total_mean_x2 = total_time_squared_; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 183 | uint64_t mean_total_ns = GetTotalTime(); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 184 | if (iterations_ != 0) { |
| 185 | total_mean_x2 /= iterations_; |
| 186 | mean_total_ns /= iterations_; |
| 187 | } |
| 188 | uint64_t total_variance = total_mean_x2 - (mean_total_ns * mean_total_ns); |
| 189 | uint64_t total_std_dev = static_cast<uint64_t>( |
| 190 | std::sqrt(static_cast<double>(total_variance))); |
| 191 | os << name_ << ": end, mean " << PrettyDuration(mean_total_ns * kAdjust) |
| 192 | << " std_dev " << PrettyDuration(total_std_dev * kAdjust) << "\n"; |
| 193 | } |
| 194 | |
| 195 | uint64_t GetTotalNs() const { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 196 | return GetTotalTime() * kAdjust; |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | |
| 201 | uint64_t GetTotalTime() const { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 202 | uint64_t total = 0; |
| 203 | for (size_t i = 0; i < times_.size(); ++i) { |
| 204 | total += times_[i]; |
| 205 | } |
| 206 | return total; |
| 207 | } |
| 208 | |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 209 | static const uint64_t kAdjust = 1000; |
| 210 | std::string name_; |
| 211 | bool precise_; |
| 212 | uint64_t total_time_squared_; |
| 213 | std::vector<uint64_t> times_; |
| 214 | std::vector<uint64_t> times_squared_; |
| 215 | std::vector<std::string> labels_; |
| 216 | size_t index_; |
| 217 | size_t iterations_; |
| 218 | uint64_t last_split_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | } // namespace art |
| 222 | |
| 223 | #endif // ART_SRC_TIMING_LOGGER_H_ |