blob: b6a2aaf33bb3c1d5b56a0dbbef88f768ef7b2192 [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
18#define ATRACE_TAG ATRACE_TAG_DALVIK
19#include <stdio.h>
20#include <cutils/trace.h>
21
Sameer Abu Asala8439542013-02-14 16:06:42 -080022#include "timing_logger.h"
23
24#include "base/logging.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080025#include "thread-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080026#include "base/stl_util.h"
27#include "base/histogram-inl.h"
28
29#include <cmath>
30#include <iomanip>
31
32namespace art {
33
Mark Mendell45c11652013-12-11 12:27:35 -080034constexpr size_t CumulativeLogger::kLowMemoryBucketCount;
35constexpr size_t CumulativeLogger::kDefaultBucketCount;
Mathieu Chartierf5997b42014-06-20 10:37:54 -070036constexpr size_t TimingLogger::kIndexNotFound;
37
Ian Rogers45357052013-04-18 20:49:43 -070038CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080039 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070040 lock_name_("CumulativeLoggerLock" + name),
41 lock_(lock_name_.c_str(), kDefaultMutexLevel, true) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080042 Reset();
43}
44
Ian Rogers45357052013-04-18 20:49:43 -070045CumulativeLogger::~CumulativeLogger() {
Mathieu Chartier19b0a912013-11-20 14:07:54 -080046 STLDeleteElements(&histograms_);
Ian Rogers45357052013-04-18 20:49:43 -070047}
Sameer Abu Asala8439542013-02-14 16:06:42 -080048
Ian Rogers45357052013-04-18 20:49:43 -070049void CumulativeLogger::SetName(const std::string& name) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080050 MutexLock mu(Thread::Current(), lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070051 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070052}
Sameer Abu Asala8439542013-02-14 16:06:42 -080053
54void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080055}
56
57void CumulativeLogger::End() {
58 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier4aeec172014-03-27 16:09:46 -070059 ++iterations_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080060}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070061
Sameer Abu Asala8439542013-02-14 16:06:42 -080062void CumulativeLogger::Reset() {
63 MutexLock mu(Thread::Current(), lock_);
64 iterations_ = 0;
Mathieu Chartierafe49982014-03-27 10:55:04 -070065 total_time_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080066 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080067}
68
Ian Rogers5fe9af72013-11-14 00:17:20 -080069void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070070 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070071 TimingLogger::TimingData timing_data(logger.CalculateTimingData());
72 const std::vector<TimingLogger::Timing>& timings = logger.GetTimings();
73 for (size_t i = 0; i < timings.size(); ++i) {
74 if (timings[i].IsStartTiming()) {
75 AddPair(timings[i].GetName(), timing_data.GetExclusiveTime(i));
76 }
Ian Rogers1d54e732013-05-02 21:10:01 -070077 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -070078 ++iterations_;
Ian Rogers1d54e732013-05-02 21:10:01 -070079}
80
Mathieu Chartier590fee92013-09-13 13:46:47 -070081size_t CumulativeLogger::GetIterations() const {
82 MutexLock mu(Thread::Current(), lock_);
83 return iterations_;
84}
85
Mathieu Chartierafe49982014-03-27 10:55:04 -070086void CumulativeLogger::Dump(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080087 MutexLock mu(Thread::Current(), lock_);
88 DumpHistogram(os);
89}
90
Mathieu Chartier19b0a912013-11-20 14:07:54 -080091void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080092 // Convert delta time to microseconds so that we don't overflow our counters.
93 delta_time /= kAdjust;
Mathieu Chartierafe49982014-03-27 10:55:04 -070094 total_time_ += delta_time;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080095 Histogram<uint64_t>* histogram;
96 Histogram<uint64_t> dummy(label.c_str());
97 auto it = histograms_.find(&dummy);
98 if (it == histograms_.end()) {
99 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ?
100 kLowMemoryBucketCount : kDefaultBucketCount;
101 histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets);
102 histograms_.insert(histogram);
103 } else {
104 histogram = *it;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800105 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800106 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800107}
108
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800109class CompareHistorgramByTimeSpentDeclining {
110 public:
111 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
112 return a->Sum() > b->Sum();
113 }
114};
115
Mathieu Chartierafe49982014-03-27 10:55:04 -0700116void CumulativeLogger::DumpHistogram(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800117 os << "Start Dumping histograms for " << iterations_ << " iterations"
118 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800119 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
120 sorted_histograms(histograms_.begin(), histograms_.end());
121 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700122 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800123 // We don't expect DumpHistogram to be called often, so it is not performance critical.
124 histogram->CreateHistogram(&cumulative_data);
125 histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800126 }
127 os << "Done Dumping histograms \n";
128}
129
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700130TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700131 : name_(name), precise_(precise), verbose_(verbose) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700132}
133
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700134void TimingLogger::Reset() {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700135 timings_.clear();
Ian Rogers1d54e732013-05-02 21:10:01 -0700136}
137
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700138void TimingLogger::StartTiming(const char* label) {
139 DCHECK(label != nullptr);
140 timings_.push_back(Timing(NanoTime(), label));
141 ATRACE_BEGIN(label);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700142}
143
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700144void TimingLogger::EndTiming() {
145 timings_.push_back(Timing(NanoTime(), nullptr));
146 ATRACE_END();
Ian Rogers1d54e732013-05-02 21:10:01 -0700147}
148
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700149uint64_t TimingLogger::GetTotalNs() const {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700150 if (timings_.size() < 2) {
151 return 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700152 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700153 return timings_.back().GetTime() - timings_.front().GetTime();
Ian Rogers1d54e732013-05-02 21:10:01 -0700154}
155
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700156size_t TimingLogger::FindTimingIndex(const char* name, size_t start_idx) const {
157 DCHECK_LT(start_idx, timings_.size());
158 for (size_t i = start_idx; i < timings_.size(); ++i) {
159 if (timings_[i].IsStartTiming() && strcmp(timings_[i].GetName(), name) == 0) {
160 return i;
161 }
162 }
163 return kIndexNotFound;
164}
165
166TimingLogger::TimingData TimingLogger::CalculateTimingData() const {
167 TimingLogger::TimingData ret;
168 ret.data_.resize(timings_.size());
169 std::vector<size_t> open_stack;
170 for (size_t i = 0; i < timings_.size(); ++i) {
171 if (timings_[i].IsEndTiming()) {
172 CHECK(!open_stack.empty()) << "No starting split for ending split at index " << i;
173 size_t open_idx = open_stack.back();
174 uint64_t time = timings_[i].GetTime() - timings_[open_idx].GetTime();
175 ret.data_[open_idx].exclusive_time += time;
176 DCHECK_EQ(ret.data_[open_idx].total_time, 0U);
177 ret.data_[open_idx].total_time += time;
178 // Each open split has exactly one end.
179 open_stack.pop_back();
180 // If there is a parent node, subtract from the exclusive time.
181 if (!open_stack.empty()) {
182 // Note this may go negative, but will work due to 2s complement when we add the value
183 // total time value later.
184 ret.data_[open_stack.back()].exclusive_time -= time;
185 }
186 } else {
187 open_stack.push_back(i);
188 }
189 }
190 CHECK(open_stack.empty()) << "Missing ending for timing "
191 << timings_[open_stack.back()].GetName() << " at index " << open_stack.back();
192 return ret; // No need to fear, C++11 move semantics are here.
193}
194
195void TimingLogger::Dump(std::ostream &os, const char* indent_string) const {
196 static constexpr size_t kFractionalDigits = 3;
197 TimingLogger::TimingData timing_data(CalculateTimingData());
Ian Rogers1d54e732013-05-02 21:10:01 -0700198 uint64_t longest_split = 0;
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700199 for (size_t i = 0; i < timings_.size(); ++i) {
200 longest_split = std::max(longest_split, timing_data.GetTotalTime(i));
Ian Rogers1d54e732013-05-02 21:10:01 -0700201 }
202 // Compute which type of unit we will use for printing the timings.
203 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
204 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700205 uint64_t mod_fraction = divisor >= 1000 ? divisor / 1000 : 1;
Ian Rogers1d54e732013-05-02 21:10:01 -0700206 // Print formatted splits.
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700207 size_t tab_count = 1;
208 os << name_ << " [Exclusive time] [Total time]\n";
209 for (size_t i = 0; i < timings_.size(); ++i) {
210 if (timings_[i].IsStartTiming()) {
211 uint64_t exclusive_time = timing_data.GetExclusiveTime(i);
212 uint64_t total_time = timing_data.GetTotalTime(i);
213 if (!precise_) {
214 // Make the fractional part 0.
215 exclusive_time -= exclusive_time % mod_fraction;
216 total_time -= total_time % mod_fraction;
217 }
218 for (size_t j = 0; j < tab_count; ++j) {
219 os << indent_string;
220 }
221 os << FormatDuration(exclusive_time, tu, kFractionalDigits);
222 // If they are the same, just print one value to prevent spam.
223 if (exclusive_time != total_time) {
224 os << "/" << FormatDuration(total_time, tu, kFractionalDigits);
225 }
226 os << " " << timings_[i].GetName() << "\n";
227 ++tab_count;
228 } else {
229 --tab_count;
Ian Rogers1d54e732013-05-02 21:10:01 -0700230 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700231 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700232 os << name_ << ": end, " << PrettyDuration(GetTotalNs()) << "\n";
Ian Rogers1d54e732013-05-02 21:10:01 -0700233}
234
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700235void TimingLogger::Verify() {
236 size_t counts[2] = { 0 };
237 for (size_t i = 0; i < timings_.size(); ++i) {
238 if (i > 0) {
239 CHECK_LE(timings_[i - 1].GetTime(), timings_[i].GetTime());
240 }
241 ++counts[timings_[i].IsStartTiming() ? 0 : 1];
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700242 }
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700243 CHECK_EQ(counts[0], counts[1]) << "Number of StartTiming and EndTiming doesn't match";
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700244}
245
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700246TimingLogger::~TimingLogger() {
247 if (kIsDebugBuild) {
248 Verify();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700249 }
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700250}
251
Sameer Abu Asala8439542013-02-14 16:06:42 -0800252} // namespace art