blob: b2e5251e735c984b8fa068cbda7cde5b4fcff446 [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 Gampe8cf9cb32017-07-19 09:28:38 -070021#include "base/histogram-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080022#include "base/logging.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080023#include "base/stl_util.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080024#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
Andreas Gamped4901292017-05-30 18:41:34 -070026#include "gc/heap.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070027#include "runtime.h"
28#include "thread-current-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080029
30#include <cmath>
31#include <iomanip>
32
33namespace art {
34
Mark Mendell45c11652013-12-11 12:27:35 -080035constexpr size_t CumulativeLogger::kLowMemoryBucketCount;
36constexpr size_t CumulativeLogger::kDefaultBucketCount;
Mathieu Chartierf5997b42014-06-20 10:37:54 -070037constexpr size_t TimingLogger::kIndexNotFound;
38
Ian Rogers45357052013-04-18 20:49:43 -070039CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080040 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070041 lock_name_("CumulativeLoggerLock" + name),
42 lock_(lock_name_.c_str(), kDefaultMutexLevel, true) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080043 Reset();
44}
45
Ian Rogers45357052013-04-18 20:49:43 -070046CumulativeLogger::~CumulativeLogger() {
Mathieu Chartier19b0a912013-11-20 14:07:54 -080047 STLDeleteElements(&histograms_);
Ian Rogers45357052013-04-18 20:49:43 -070048}
Sameer Abu Asala8439542013-02-14 16:06:42 -080049
Ian Rogers45357052013-04-18 20:49:43 -070050void CumulativeLogger::SetName(const std::string& name) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080051 MutexLock mu(Thread::Current(), lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070052 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070053}
Sameer Abu Asala8439542013-02-14 16:06:42 -080054
55void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080056}
57
58void CumulativeLogger::End() {
59 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier4aeec172014-03-27 16:09:46 -070060 ++iterations_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080061}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070062
Sameer Abu Asala8439542013-02-14 16:06:42 -080063void CumulativeLogger::Reset() {
64 MutexLock mu(Thread::Current(), lock_);
65 iterations_ = 0;
Mathieu Chartierafe49982014-03-27 10:55:04 -070066 total_time_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080067 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080068}
69
Ian Rogers5fe9af72013-11-14 00:17:20 -080070void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070071 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070072 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 Rogers1d54e732013-05-02 21:10:01 -070078 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -070079 ++iterations_;
Ian Rogers1d54e732013-05-02 21:10:01 -070080}
81
Mathieu Chartier590fee92013-09-13 13:46:47 -070082size_t CumulativeLogger::GetIterations() const {
83 MutexLock mu(Thread::Current(), lock_);
84 return iterations_;
85}
86
Mathieu Chartierafe49982014-03-27 10:55:04 -070087void CumulativeLogger::Dump(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080088 MutexLock mu(Thread::Current(), lock_);
89 DumpHistogram(os);
90}
91
Mathieu Chartier19b0a912013-11-20 14:07:54 -080092void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080093 // Convert delta time to microseconds so that we don't overflow our counters.
94 delta_time /= kAdjust;
Mathieu Chartierafe49982014-03-27 10:55:04 -070095 total_time_ += delta_time;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080096 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 Asala8439542013-02-14 16:06:42 -0800106 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800107 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800108}
109
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800110class 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 Chartierafe49982014-03-27 10:55:04 -0700117void CumulativeLogger::DumpHistogram(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800118 os << "Start Dumping histograms for " << iterations_ << " iterations"
119 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800120 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
121 sorted_histograms(histograms_.begin(), histograms_.end());
122 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700123 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800124 // 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 Asala8439542013-02-14 16:06:42 -0800127 }
Christopher Ferrisa1906452015-10-30 14:21:16 -0700128 os << "Done Dumping histograms\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800129}
130
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700131TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700132 : name_(name), precise_(precise), verbose_(verbose) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700133}
134
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700135void TimingLogger::Reset() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700136 timings_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700137}
138
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700139void TimingLogger::StartTiming(const char* label) {
140 DCHECK(label != nullptr);
141 timings_.push_back(Timing(NanoTime(), label));
142 ATRACE_BEGIN(label);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700143}
144
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700145void TimingLogger::EndTiming() {
146 timings_.push_back(Timing(NanoTime(), nullptr));
147 ATRACE_END();
Ian Rogers1d54e732013-05-02 21:10:01 -0700148}
149
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700150uint64_t TimingLogger::GetTotalNs() const {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700151 if (timings_.size() < 2) {
152 return 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700153 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700154 return timings_.back().GetTime() - timings_.front().GetTime();
Ian Rogers1d54e732013-05-02 21:10:01 -0700155}
156
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700157size_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
167TimingLogger::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
196void TimingLogger::Dump(std::ostream &os, const char* indent_string) const {
197 static constexpr size_t kFractionalDigits = 3;
198 TimingLogger::TimingData timing_data(CalculateTimingData());
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 uint64_t longest_split = 0;
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700200 for (size_t i = 0; i < timings_.size(); ++i) {
201 longest_split = std::max(longest_split, timing_data.GetTotalTime(i));
Ian Rogers1d54e732013-05-02 21:10:01 -0700202 }
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 Chartierf5997b42014-06-20 10:37:54 -0700206 uint64_t mod_fraction = divisor >= 1000 ? divisor / 1000 : 1;
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 // Print formatted splits.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700208 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 Rogers1d54e732013-05-02 21:10:01 -0700231 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700232 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700233 os << name_ << ": end, " << PrettyDuration(GetTotalNs()) << "\n";
Ian Rogers1d54e732013-05-02 21:10:01 -0700234}
235
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700236void 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 Ghuloum4446ab92013-08-09 21:17:25 -0700243 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700244 CHECK_EQ(counts[0], counts[1]) << "Number of StartTiming and EndTiming doesn't match";
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700245}
246
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700247TimingLogger::~TimingLogger() {
248 if (kIsDebugBuild) {
249 Verify();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700250 }
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700251}
252
Sameer Abu Asala8439542013-02-14 16:06:42 -0800253} // namespace art