Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [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 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 17 | |
| 18 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 19 | #include <stdio.h> |
| 20 | #include <cutils/trace.h> |
| 21 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 22 | #include "timing_logger.h" |
| 23 | |
| 24 | #include "base/logging.h" |
Brian Carlstrom | a3d2718 | 2013-11-05 23:22:27 -0800 | [diff] [blame] | 25 | #include "thread-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 26 | #include "base/stl_util.h" |
| 27 | #include "base/histogram-inl.h" |
| 28 | |
| 29 | #include <cmath> |
| 30 | #include <iomanip> |
| 31 | |
| 32 | namespace art { |
| 33 | |
Mark Mendell | 45c1165 | 2013-12-11 12:27:35 -0800 | [diff] [blame] | 34 | constexpr size_t CumulativeLogger::kLowMemoryBucketCount; |
| 35 | constexpr size_t CumulativeLogger::kDefaultBucketCount; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 36 | CumulativeLogger::CumulativeLogger(const std::string& name) |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 37 | : name_(name), |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 38 | lock_name_("CumulativeLoggerLock" + name), |
| 39 | lock_(lock_name_.c_str(), kDefaultMutexLevel, true) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 40 | Reset(); |
| 41 | } |
| 42 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 43 | CumulativeLogger::~CumulativeLogger() { |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 44 | STLDeleteElements(&histograms_); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 45 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 46 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 47 | void CumulativeLogger::SetName(const std::string& name) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 48 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | name_.assign(name); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 50 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 51 | |
| 52 | void CumulativeLogger::Start() { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void CumulativeLogger::End() { |
| 56 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 57 | ++iterations_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 58 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 59 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 60 | void CumulativeLogger::Reset() { |
| 61 | MutexLock mu(Thread::Current(), lock_); |
| 62 | iterations_ = 0; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 63 | total_time_ = 0; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 64 | STLDeleteElements(&histograms_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 67 | void CumulativeLogger::AddLogger(const TimingLogger &logger) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 69 | for (const TimingLogger::SplitTiming& split : logger.GetSplits()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 70 | uint64_t split_time = split.first; |
| 71 | const char* split_name = split.second; |
| 72 | AddPair(split_name, split_time); |
| 73 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 74 | ++iterations_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 77 | size_t CumulativeLogger::GetIterations() const { |
| 78 | MutexLock mu(Thread::Current(), lock_); |
| 79 | return iterations_; |
| 80 | } |
| 81 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 82 | void CumulativeLogger::Dump(std::ostream &os) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 83 | MutexLock mu(Thread::Current(), lock_); |
| 84 | DumpHistogram(os); |
| 85 | } |
| 86 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 87 | void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 88 | // Convert delta time to microseconds so that we don't overflow our counters. |
| 89 | delta_time /= kAdjust; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 90 | total_time_ += delta_time; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 91 | Histogram<uint64_t>* histogram; |
| 92 | Histogram<uint64_t> dummy(label.c_str()); |
| 93 | auto it = histograms_.find(&dummy); |
| 94 | if (it == histograms_.end()) { |
| 95 | const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ? |
| 96 | kLowMemoryBucketCount : kDefaultBucketCount; |
| 97 | histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets); |
| 98 | histograms_.insert(histogram); |
| 99 | } else { |
| 100 | histogram = *it; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 101 | } |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 102 | histogram->AddValue(delta_time); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 105 | class CompareHistorgramByTimeSpentDeclining { |
| 106 | public: |
| 107 | bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const { |
| 108 | return a->Sum() > b->Sum(); |
| 109 | } |
| 110 | }; |
| 111 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 112 | void CumulativeLogger::DumpHistogram(std::ostream &os) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 113 | os << "Start Dumping histograms for " << iterations_ << " iterations" |
| 114 | << " for " << name_ << "\n"; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 115 | std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining> |
| 116 | sorted_histograms(histograms_.begin(), histograms_.end()); |
| 117 | for (Histogram<uint64_t>* histogram : sorted_histograms) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 118 | Histogram<uint64_t>::CumulativeData cumulative_data; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 119 | // We don't expect DumpHistogram to be called often, so it is not performance critical. |
| 120 | histogram->CreateHistogram(&cumulative_data); |
| 121 | histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 122 | } |
| 123 | os << "Done Dumping histograms \n"; |
| 124 | } |
| 125 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 126 | TimingLogger::TimingLogger(const char* name, bool precise, bool verbose) |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 127 | : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 130 | void TimingLogger::Reset() { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 131 | current_split_ = NULL; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 132 | splits_.clear(); |
| 133 | } |
| 134 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 135 | void TimingLogger::StartSplit(const char* new_split_label) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 136 | DCHECK(new_split_label != nullptr) << "Starting split with null label."; |
| 137 | TimingLogger::ScopedSplit* explicit_scoped_split = |
| 138 | new TimingLogger::ScopedSplit(new_split_label, this); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 139 | explicit_scoped_split->explicit_ = true; |
| 140 | } |
| 141 | |
| 142 | void TimingLogger::EndSplit() { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 143 | CHECK(current_split_ != nullptr) << "Ending a non-existent split."; |
| 144 | DCHECK(current_split_->label_ != nullptr); |
| 145 | DCHECK(current_split_->explicit_ == true) |
| 146 | << "Explicitly ending scoped split: " << current_split_->label_; |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 147 | delete current_split_; |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 148 | // TODO: current_split_ = nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Ends the current split and starts the one given by the label. |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 152 | void TimingLogger::NewSplit(const char* new_split_label) { |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 153 | if (current_split_ == nullptr) { |
| 154 | StartSplit(new_split_label); |
| 155 | } else { |
| 156 | DCHECK(new_split_label != nullptr) << "New split (" << new_split_label << ") with null label."; |
| 157 | current_split_->TailInsertSplit(new_split_label); |
| 158 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 161 | uint64_t TimingLogger::GetTotalNs() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 162 | uint64_t total_ns = 0; |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 163 | for (const TimingLogger::SplitTiming& split : splits_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 164 | total_ns += split.first; |
| 165 | } |
| 166 | return total_ns; |
| 167 | } |
| 168 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 169 | void TimingLogger::Dump(std::ostream &os) const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 170 | uint64_t longest_split = 0; |
| 171 | uint64_t total_ns = 0; |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 172 | for (const SplitTiming& split : splits_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 173 | uint64_t split_time = split.first; |
| 174 | longest_split = std::max(longest_split, split_time); |
| 175 | total_ns += split_time; |
| 176 | } |
| 177 | // Compute which type of unit we will use for printing the timings. |
| 178 | TimeUnit tu = GetAppropriateTimeUnit(longest_split); |
| 179 | uint64_t divisor = GetNsToTimeUnitDivisor(tu); |
| 180 | // Print formatted splits. |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 181 | for (const SplitTiming& split : splits_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 182 | uint64_t split_time = split.first; |
| 183 | if (!precise_ && divisor >= 1000) { |
| 184 | // Make the fractional part 0. |
| 185 | split_time -= split_time % (divisor / 1000); |
| 186 | } |
| 187 | os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " " |
| 188 | << split.second << "\n"; |
| 189 | } |
| 190 | os << name_ << ": end, " << NsToMs(total_ns) << " ms\n"; |
| 191 | } |
| 192 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 193 | TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) { |
| 194 | DCHECK(label != NULL) << "New scoped split (" << label << ") with null label."; |
| 195 | CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger."; |
| 196 | timing_logger_ = timing_logger; |
| 197 | label_ = label; |
| 198 | running_ns_ = 0; |
| 199 | explicit_ = false; |
| 200 | |
| 201 | // Stash away the current split and pause it. |
| 202 | enclosing_split_ = timing_logger->current_split_; |
| 203 | if (enclosing_split_ != NULL) { |
| 204 | enclosing_split_->Pause(); |
| 205 | } |
| 206 | |
| 207 | timing_logger_->current_split_ = this; |
| 208 | |
| 209 | ATRACE_BEGIN(label_); |
| 210 | |
| 211 | start_ns_ = NanoTime(); |
| 212 | if (timing_logger_->verbose_) { |
| 213 | LOG(INFO) << "Begin: " << label_; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | TimingLogger::ScopedSplit::~ScopedSplit() { |
| 218 | uint64_t current_time = NanoTime(); |
| 219 | uint64_t split_time = current_time - start_ns_; |
| 220 | running_ns_ += split_time; |
| 221 | ATRACE_END(); |
| 222 | |
| 223 | if (timing_logger_->verbose_) { |
| 224 | LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time); |
| 225 | } |
| 226 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 227 | // If one or more enclosed explicitly started splits are not terminated we can |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 228 | // either fail or "unwind" the stack of splits in the timing logger to 'this' |
| 229 | // (by deleting the intervening scoped splits). This implements the latter. |
| 230 | TimingLogger::ScopedSplit* current = timing_logger_->current_split_; |
| 231 | while ((current != NULL) && (current != this)) { |
| 232 | delete current; |
| 233 | current = timing_logger_->current_split_; |
| 234 | } |
| 235 | |
| 236 | CHECK(current != NULL) << "Missing scoped split (" << this->label_ |
| 237 | << ") in timing logger (" << timing_logger_->name_ << ")."; |
| 238 | CHECK(timing_logger_->current_split_ == this); |
| 239 | |
| 240 | timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_)); |
| 241 | |
| 242 | timing_logger_->current_split_ = enclosing_split_; |
| 243 | if (enclosing_split_ != NULL) { |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 244 | enclosing_split_->Resume(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) { |
| 250 | // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current |
| 251 | // scoped split in place. Basically, it's one way to make explicit and scoped splits compose |
| 252 | // well while maintaining the current semantics of NewSplit. An alternative is to push a new split |
| 253 | // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies |
| 254 | // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would |
| 255 | // be different from what we had before. |
| 256 | |
| 257 | uint64_t current_time = NanoTime(); |
| 258 | uint64_t split_time = current_time - start_ns_; |
| 259 | ATRACE_END(); |
| 260 | timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_)); |
| 261 | |
| 262 | if (timing_logger_->verbose_) { |
| 263 | LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n" |
| 264 | << "Begin: " << label; |
| 265 | } |
| 266 | |
| 267 | label_ = label; |
| 268 | start_ns_ = current_time; |
| 269 | running_ns_ = 0; |
| 270 | |
| 271 | ATRACE_BEGIN(label); |
| 272 | } |
| 273 | |
| 274 | void TimingLogger::ScopedSplit::Pause() { |
| 275 | uint64_t current_time = NanoTime(); |
| 276 | uint64_t split_time = current_time - start_ns_; |
| 277 | running_ns_ += split_time; |
| 278 | ATRACE_END(); |
| 279 | } |
| 280 | |
| 281 | |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 282 | void TimingLogger::ScopedSplit::Resume() { |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 283 | uint64_t current_time = NanoTime(); |
| 284 | |
| 285 | start_ns_ = current_time; |
| 286 | ATRACE_BEGIN(label_); |
| 287 | } |
| 288 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 289 | } // namespace art |