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 | #include <stdio.h> |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 18 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 19 | #include "timing_logger.h" |
| 20 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame^] | 21 | #include "base/histogram-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 23 | #include "base/stl_util.h" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 24 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
Andreas Gampe | d490129 | 2017-05-30 18:41:34 -0700 | [diff] [blame] | 26 | #include "gc/heap.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
| 28 | #include "thread-current-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 29 | |
| 30 | #include <cmath> |
| 31 | #include <iomanip> |
| 32 | |
| 33 | namespace art { |
| 34 | |
Mark Mendell | 45c1165 | 2013-12-11 12:27:35 -0800 | [diff] [blame] | 35 | constexpr size_t CumulativeLogger::kLowMemoryBucketCount; |
| 36 | constexpr size_t CumulativeLogger::kDefaultBucketCount; |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 37 | constexpr size_t TimingLogger::kIndexNotFound; |
| 38 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 39 | CumulativeLogger::CumulativeLogger(const std::string& name) |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 40 | : name_(name), |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 41 | lock_name_("CumulativeLoggerLock" + name), |
| 42 | lock_(lock_name_.c_str(), kDefaultMutexLevel, true) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 43 | Reset(); |
| 44 | } |
| 45 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 46 | CumulativeLogger::~CumulativeLogger() { |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 47 | STLDeleteElements(&histograms_); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 48 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 49 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 50 | void CumulativeLogger::SetName(const std::string& name) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 51 | MutexLock mu(Thread::Current(), lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | name_.assign(name); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 53 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 54 | |
| 55 | void CumulativeLogger::Start() { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void CumulativeLogger::End() { |
| 59 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 60 | ++iterations_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 61 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 62 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 63 | void CumulativeLogger::Reset() { |
| 64 | MutexLock mu(Thread::Current(), lock_); |
| 65 | iterations_ = 0; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 66 | total_time_ = 0; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 67 | STLDeleteElements(&histograms_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 70 | void CumulativeLogger::AddLogger(const TimingLogger &logger) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 71 | MutexLock mu(Thread::Current(), lock_); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 72 | TimingLogger::TimingData timing_data(logger.CalculateTimingData()); |
| 73 | const std::vector<TimingLogger::Timing>& timings = logger.GetTimings(); |
| 74 | for (size_t i = 0; i < timings.size(); ++i) { |
| 75 | if (timings[i].IsStartTiming()) { |
| 76 | AddPair(timings[i].GetName(), timing_data.GetExclusiveTime(i)); |
| 77 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 78 | } |
Mathieu Chartier | 4aeec17 | 2014-03-27 16:09:46 -0700 | [diff] [blame] | 79 | ++iterations_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 82 | size_t CumulativeLogger::GetIterations() const { |
| 83 | MutexLock mu(Thread::Current(), lock_); |
| 84 | return iterations_; |
| 85 | } |
| 86 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 87 | void CumulativeLogger::Dump(std::ostream &os) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 88 | MutexLock mu(Thread::Current(), lock_); |
| 89 | DumpHistogram(os); |
| 90 | } |
| 91 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 92 | void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 93 | // Convert delta time to microseconds so that we don't overflow our counters. |
| 94 | delta_time /= kAdjust; |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 95 | total_time_ += delta_time; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 96 | Histogram<uint64_t>* histogram; |
| 97 | Histogram<uint64_t> dummy(label.c_str()); |
| 98 | auto it = histograms_.find(&dummy); |
| 99 | if (it == histograms_.end()) { |
| 100 | const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ? |
| 101 | kLowMemoryBucketCount : kDefaultBucketCount; |
| 102 | histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets); |
| 103 | histograms_.insert(histogram); |
| 104 | } else { |
| 105 | histogram = *it; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 106 | } |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 107 | histogram->AddValue(delta_time); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 110 | class CompareHistorgramByTimeSpentDeclining { |
| 111 | public: |
| 112 | bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const { |
| 113 | return a->Sum() > b->Sum(); |
| 114 | } |
| 115 | }; |
| 116 | |
Mathieu Chartier | afe4998 | 2014-03-27 10:55:04 -0700 | [diff] [blame] | 117 | void CumulativeLogger::DumpHistogram(std::ostream &os) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 118 | os << "Start Dumping histograms for " << iterations_ << " iterations" |
| 119 | << " for " << name_ << "\n"; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 120 | std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining> |
| 121 | sorted_histograms(histograms_.begin(), histograms_.end()); |
| 122 | for (Histogram<uint64_t>* histogram : sorted_histograms) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 123 | Histogram<uint64_t>::CumulativeData cumulative_data; |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 124 | // We don't expect DumpHistogram to be called often, so it is not performance critical. |
| 125 | histogram->CreateHistogram(&cumulative_data); |
| 126 | histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 127 | } |
Christopher Ferris | a190645 | 2015-10-30 14:21:16 -0700 | [diff] [blame] | 128 | os << "Done Dumping histograms\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 131 | TimingLogger::TimingLogger(const char* name, bool precise, bool verbose) |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 132 | : name_(name), precise_(precise), verbose_(verbose) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 135 | void TimingLogger::Reset() { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 136 | timings_.clear(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 139 | void TimingLogger::StartTiming(const char* label) { |
| 140 | DCHECK(label != nullptr); |
| 141 | timings_.push_back(Timing(NanoTime(), label)); |
| 142 | ATRACE_BEGIN(label); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 145 | void TimingLogger::EndTiming() { |
| 146 | timings_.push_back(Timing(NanoTime(), nullptr)); |
| 147 | ATRACE_END(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 150 | uint64_t TimingLogger::GetTotalNs() const { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 151 | if (timings_.size() < 2) { |
| 152 | return 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 153 | } |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 154 | return timings_.back().GetTime() - timings_.front().GetTime(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 157 | size_t TimingLogger::FindTimingIndex(const char* name, size_t start_idx) const { |
| 158 | DCHECK_LT(start_idx, timings_.size()); |
| 159 | for (size_t i = start_idx; i < timings_.size(); ++i) { |
| 160 | if (timings_[i].IsStartTiming() && strcmp(timings_[i].GetName(), name) == 0) { |
| 161 | return i; |
| 162 | } |
| 163 | } |
| 164 | return kIndexNotFound; |
| 165 | } |
| 166 | |
| 167 | TimingLogger::TimingData TimingLogger::CalculateTimingData() const { |
| 168 | TimingLogger::TimingData ret; |
| 169 | ret.data_.resize(timings_.size()); |
| 170 | std::vector<size_t> open_stack; |
| 171 | for (size_t i = 0; i < timings_.size(); ++i) { |
| 172 | if (timings_[i].IsEndTiming()) { |
| 173 | CHECK(!open_stack.empty()) << "No starting split for ending split at index " << i; |
| 174 | size_t open_idx = open_stack.back(); |
| 175 | uint64_t time = timings_[i].GetTime() - timings_[open_idx].GetTime(); |
| 176 | ret.data_[open_idx].exclusive_time += time; |
| 177 | DCHECK_EQ(ret.data_[open_idx].total_time, 0U); |
| 178 | ret.data_[open_idx].total_time += time; |
| 179 | // Each open split has exactly one end. |
| 180 | open_stack.pop_back(); |
| 181 | // If there is a parent node, subtract from the exclusive time. |
| 182 | if (!open_stack.empty()) { |
| 183 | // Note this may go negative, but will work due to 2s complement when we add the value |
| 184 | // total time value later. |
| 185 | ret.data_[open_stack.back()].exclusive_time -= time; |
| 186 | } |
| 187 | } else { |
| 188 | open_stack.push_back(i); |
| 189 | } |
| 190 | } |
| 191 | CHECK(open_stack.empty()) << "Missing ending for timing " |
| 192 | << timings_[open_stack.back()].GetName() << " at index " << open_stack.back(); |
| 193 | return ret; // No need to fear, C++11 move semantics are here. |
| 194 | } |
| 195 | |
| 196 | void TimingLogger::Dump(std::ostream &os, const char* indent_string) const { |
| 197 | static constexpr size_t kFractionalDigits = 3; |
| 198 | TimingLogger::TimingData timing_data(CalculateTimingData()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 199 | uint64_t longest_split = 0; |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < timings_.size(); ++i) { |
| 201 | longest_split = std::max(longest_split, timing_data.GetTotalTime(i)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 202 | } |
| 203 | // Compute which type of unit we will use for printing the timings. |
| 204 | TimeUnit tu = GetAppropriateTimeUnit(longest_split); |
| 205 | uint64_t divisor = GetNsToTimeUnitDivisor(tu); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 206 | uint64_t mod_fraction = divisor >= 1000 ? divisor / 1000 : 1; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 207 | // Print formatted splits. |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 208 | size_t tab_count = 1; |
| 209 | os << name_ << " [Exclusive time] [Total time]\n"; |
| 210 | for (size_t i = 0; i < timings_.size(); ++i) { |
| 211 | if (timings_[i].IsStartTiming()) { |
| 212 | uint64_t exclusive_time = timing_data.GetExclusiveTime(i); |
| 213 | uint64_t total_time = timing_data.GetTotalTime(i); |
| 214 | if (!precise_) { |
| 215 | // Make the fractional part 0. |
| 216 | exclusive_time -= exclusive_time % mod_fraction; |
| 217 | total_time -= total_time % mod_fraction; |
| 218 | } |
| 219 | for (size_t j = 0; j < tab_count; ++j) { |
| 220 | os << indent_string; |
| 221 | } |
| 222 | os << FormatDuration(exclusive_time, tu, kFractionalDigits); |
| 223 | // If they are the same, just print one value to prevent spam. |
| 224 | if (exclusive_time != total_time) { |
| 225 | os << "/" << FormatDuration(total_time, tu, kFractionalDigits); |
| 226 | } |
| 227 | os << " " << timings_[i].GetName() << "\n"; |
| 228 | ++tab_count; |
| 229 | } else { |
| 230 | --tab_count; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 231 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 232 | } |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 233 | os << name_ << ": end, " << PrettyDuration(GetTotalNs()) << "\n"; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 236 | void TimingLogger::Verify() { |
| 237 | size_t counts[2] = { 0 }; |
| 238 | for (size_t i = 0; i < timings_.size(); ++i) { |
| 239 | if (i > 0) { |
| 240 | CHECK_LE(timings_[i - 1].GetTime(), timings_[i].GetTime()); |
| 241 | } |
| 242 | ++counts[timings_[i].IsStartTiming() ? 0 : 1]; |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 243 | } |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 244 | CHECK_EQ(counts[0], counts[1]) << "Number of StartTiming and EndTiming doesn't match"; |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 247 | TimingLogger::~TimingLogger() { |
| 248 | if (kIsDebugBuild) { |
| 249 | Verify(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 250 | } |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 253 | } // namespace art |