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