blob: bd39192a440b5a33785b93874903064b33bbe99d [file] [log] [blame]
Sameer Abu Asala8439542013-02-14 16:06:42 -08001/*
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 Ghuloum6f28d912013-07-24 15:02:53 -070017#include <stdio.h>
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070018
Sameer Abu Asala8439542013-02-14 16:06:42 -080019#include "timing_logger.h"
20
Andreas Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include "base/histogram-inl.h"
Andreas Gampe0bbac302018-11-30 09:44:12 -080024#include "base/mutex.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080025#include "base/stl_util.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070028#include "gc/heap.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070029#include "runtime.h"
30#include "thread-current-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080031
32#include <cmath>
33#include <iomanip>
34
35namespace art {
36
Mark Mendell45c11652013-12-11 12:27:35 -080037constexpr size_t CumulativeLogger::kLowMemoryBucketCount;
38constexpr size_t CumulativeLogger::kDefaultBucketCount;
Mathieu Chartierf5997b42014-06-20 10:37:54 -070039constexpr size_t TimingLogger::kIndexNotFound;
40
Ian Rogers45357052013-04-18 20:49:43 -070041CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080042 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070043 lock_name_("CumulativeLoggerLock" + name),
Andreas Gampe0bbac302018-11-30 09:44:12 -080044 lock_(new Mutex(lock_name_.c_str(), kDefaultMutexLevel, true)) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080045 Reset();
46}
47
Ian Rogers45357052013-04-18 20:49:43 -070048CumulativeLogger::~CumulativeLogger() {
Mathieu Chartier19b0a912013-11-20 14:07:54 -080049 STLDeleteElements(&histograms_);
Ian Rogers45357052013-04-18 20:49:43 -070050}
Sameer Abu Asala8439542013-02-14 16:06:42 -080051
Ian Rogers45357052013-04-18 20:49:43 -070052void CumulativeLogger::SetName(const std::string& name) {
Andreas Gampe0bbac302018-11-30 09:44:12 -080053 MutexLock mu(Thread::Current(), *GetLock());
Ian Rogers1d54e732013-05-02 21:10:01 -070054 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070055}
Sameer Abu Asala8439542013-02-14 16:06:42 -080056
57void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080058}
59
60void CumulativeLogger::End() {
Andreas Gampe0bbac302018-11-30 09:44:12 -080061 MutexLock mu(Thread::Current(), *GetLock());
Mathieu Chartier4aeec172014-03-27 16:09:46 -070062 ++iterations_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080063}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070064
Sameer Abu Asala8439542013-02-14 16:06:42 -080065void CumulativeLogger::Reset() {
Andreas Gampe0bbac302018-11-30 09:44:12 -080066 MutexLock mu(Thread::Current(), *GetLock());
Sameer Abu Asala8439542013-02-14 16:06:42 -080067 iterations_ = 0;
Mathieu Chartierafe49982014-03-27 10:55:04 -070068 total_time_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080069 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080070}
71
Ian Rogers5fe9af72013-11-14 00:17:20 -080072void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Andreas Gampe0bbac302018-11-30 09:44:12 -080073 MutexLock mu(Thread::Current(), *GetLock());
Mathieu Chartierf5997b42014-06-20 10:37:54 -070074 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 Rogers1d54e732013-05-02 21:10:01 -070080 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -070081 ++iterations_;
Ian Rogers1d54e732013-05-02 21:10:01 -070082}
83
Mathieu Chartier590fee92013-09-13 13:46:47 -070084size_t CumulativeLogger::GetIterations() const {
Andreas Gampe0bbac302018-11-30 09:44:12 -080085 MutexLock mu(Thread::Current(), *GetLock());
Mathieu Chartier590fee92013-09-13 13:46:47 -070086 return iterations_;
87}
88
Mathieu Chartierafe49982014-03-27 10:55:04 -070089void CumulativeLogger::Dump(std::ostream &os) const {
Andreas Gampe0bbac302018-11-30 09:44:12 -080090 MutexLock mu(Thread::Current(), *GetLock());
Sameer Abu Asala8439542013-02-14 16:06:42 -080091 DumpHistogram(os);
92}
93
Mathieu Chartier19b0a912013-11-20 14:07:54 -080094void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080095 // Convert delta time to microseconds so that we don't overflow our counters.
96 delta_time /= kAdjust;
Mathieu Chartierafe49982014-03-27 10:55:04 -070097 total_time_ += delta_time;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080098 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 Asala8439542013-02-14 16:06:42 -0800108 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800109 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800110}
111
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800112class 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 Chartierafe49982014-03-27 10:55:04 -0700119void CumulativeLogger::DumpHistogram(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800120 os << "Start Dumping histograms for " << iterations_ << " iterations"
121 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800122 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
123 sorted_histograms(histograms_.begin(), histograms_.end());
124 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700125 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800126 // 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 Asala8439542013-02-14 16:06:42 -0800129 }
Christopher Ferrisa1906452015-10-30 14:21:16 -0700130 os << "Done Dumping histograms\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800131}
132
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100133TimingLogger::TimingLogger(const char* name,
134 bool precise,
135 bool verbose,
136 TimingLogger::TimingKind kind)
137 : name_(name), precise_(precise), verbose_(verbose), kind_(kind) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700138}
139
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700140void TimingLogger::Reset() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700141 timings_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700142}
143
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700144void TimingLogger::StartTiming(const char* label) {
145 DCHECK(label != nullptr);
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100146 timings_.push_back(Timing(kind_, label));
Orion Hodson119733d2019-01-30 15:14:41 +0000147 ATraceBegin(label);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700148}
149
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700150void TimingLogger::EndTiming() {
Nicolas Geoffray3d699222017-09-20 15:15:20 +0100151 timings_.push_back(Timing(kind_, nullptr));
Orion Hodson119733d2019-01-30 15:14:41 +0000152 ATraceEnd();
Ian Rogers1d54e732013-05-02 21:10:01 -0700153}
154
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700155uint64_t TimingLogger::GetTotalNs() const {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700156 if (timings_.size() < 2) {
157 return 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700158 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700159 return timings_.back().GetTime() - timings_.front().GetTime();
Ian Rogers1d54e732013-05-02 21:10:01 -0700160}
161
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700162size_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
172TimingLogger::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
201void TimingLogger::Dump(std::ostream &os, const char* indent_string) const {
202 static constexpr size_t kFractionalDigits = 3;
203 TimingLogger::TimingData timing_data(CalculateTimingData());
Ian Rogers1d54e732013-05-02 21:10:01 -0700204 uint64_t longest_split = 0;
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700205 for (size_t i = 0; i < timings_.size(); ++i) {
206 longest_split = std::max(longest_split, timing_data.GetTotalTime(i));
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 }
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 Chartierf5997b42014-06-20 10:37:54 -0700211 uint64_t mod_fraction = divisor >= 1000 ? divisor / 1000 : 1;
Ian Rogers1d54e732013-05-02 21:10:01 -0700212 // Print formatted splits.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700213 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 Rogers1d54e732013-05-02 21:10:01 -0700236 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700237 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700238 os << name_ << ": end, " << PrettyDuration(GetTotalNs()) << "\n";
Ian Rogers1d54e732013-05-02 21:10:01 -0700239}
240
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700241void 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 Ghuloum4446ab92013-08-09 21:17:25 -0700248 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700249 CHECK_EQ(counts[0], counts[1]) << "Number of StartTiming and EndTiming doesn't match";
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700250}
251
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700252TimingLogger::~TimingLogger() {
253 if (kIsDebugBuild) {
254 Verify();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700255 }
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700256}
257
Sameer Abu Asala8439542013-02-14 16:06:42 -0800258} // namespace art