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