blob: c769f64836038c9232b521c0748e7252ae9ad7cc [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;
Ian Rogers45357052013-04-18 20:49:43 -070036CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080037 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070038 lock_name_("CumulativeLoggerLock" + name),
39 lock_(lock_name_.c_str(), kDefaultMutexLevel, true) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080040 Reset();
41}
42
Ian Rogers45357052013-04-18 20:49:43 -070043CumulativeLogger::~CumulativeLogger() {
Mathieu Chartier19b0a912013-11-20 14:07:54 -080044 STLDeleteElements(&histograms_);
Ian Rogers45357052013-04-18 20:49:43 -070045}
Sameer Abu Asala8439542013-02-14 16:06:42 -080046
Ian Rogers45357052013-04-18 20:49:43 -070047void CumulativeLogger::SetName(const std::string& name) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080048 MutexLock mu(Thread::Current(), lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070049 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070050}
Sameer Abu Asala8439542013-02-14 16:06:42 -080051
52void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080053}
54
55void CumulativeLogger::End() {
56 MutexLock mu(Thread::Current(), lock_);
57 iterations_++;
58}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070059
Sameer Abu Asala8439542013-02-14 16:06:42 -080060void CumulativeLogger::Reset() {
61 MutexLock mu(Thread::Current(), lock_);
62 iterations_ = 0;
Mathieu Chartierafe49982014-03-27 10:55:04 -070063 total_time_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080064 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080065}
66
Ian Rogers5fe9af72013-11-14 00:17:20 -080067void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070068 MutexLock mu(Thread::Current(), lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080069 const TimingLogger::SplitTimings& splits = logger.GetSplits();
70 for (auto it = splits.begin(), end = splits.end(); it != end; ++it) {
71 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -070072 uint64_t split_time = split.first;
73 const char* split_name = split.second;
74 AddPair(split_name, split_time);
75 }
76}
77
Mathieu Chartier590fee92013-09-13 13:46:47 -070078size_t CumulativeLogger::GetIterations() const {
79 MutexLock mu(Thread::Current(), lock_);
80 return iterations_;
81}
82
Mathieu Chartierafe49982014-03-27 10:55:04 -070083void CumulativeLogger::Dump(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080084 MutexLock mu(Thread::Current(), lock_);
85 DumpHistogram(os);
86}
87
Mathieu Chartier19b0a912013-11-20 14:07:54 -080088void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080089 // Convert delta time to microseconds so that we don't overflow our counters.
90 delta_time /= kAdjust;
Mathieu Chartierafe49982014-03-27 10:55:04 -070091 total_time_ += delta_time;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080092 Histogram<uint64_t>* histogram;
93 Histogram<uint64_t> dummy(label.c_str());
94 auto it = histograms_.find(&dummy);
95 if (it == histograms_.end()) {
96 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ?
97 kLowMemoryBucketCount : kDefaultBucketCount;
98 histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets);
99 histograms_.insert(histogram);
100 } else {
101 histogram = *it;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800102 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800103 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800104}
105
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800106class CompareHistorgramByTimeSpentDeclining {
107 public:
108 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
109 return a->Sum() > b->Sum();
110 }
111};
112
Mathieu Chartierafe49982014-03-27 10:55:04 -0700113void CumulativeLogger::DumpHistogram(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800114 os << "Start Dumping histograms for " << iterations_ << " iterations"
115 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800116 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
117 sorted_histograms(histograms_.begin(), histograms_.end());
118 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700119 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800120 // We don't expect DumpHistogram to be called often, so it is not performance critical.
121 histogram->CreateHistogram(&cumulative_data);
122 histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800123 }
124 os << "Done Dumping histograms \n";
125}
126
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700127TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700128 : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700129}
130
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700131void TimingLogger::Reset() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 current_split_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 splits_.clear();
134}
135
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700136void TimingLogger::StartSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800137 DCHECK(new_split_label != nullptr) << "Starting split with null label.";
138 TimingLogger::ScopedSplit* explicit_scoped_split =
139 new TimingLogger::ScopedSplit(new_split_label, this);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700140 explicit_scoped_split->explicit_ = true;
141}
142
143void TimingLogger::EndSplit() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800144 CHECK(current_split_ != nullptr) << "Ending a non-existent split.";
145 DCHECK(current_split_->label_ != nullptr);
146 DCHECK(current_split_->explicit_ == true)
147 << "Explicitly ending scoped split: " << current_split_->label_;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700148 delete current_split_;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800149 // TODO: current_split_ = nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700150}
151
152// Ends the current split and starts the one given by the label.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700153void TimingLogger::NewSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800154 if (current_split_ == nullptr) {
155 StartSplit(new_split_label);
156 } else {
157 DCHECK(new_split_label != nullptr) << "New split (" << new_split_label << ") with null label.";
158 current_split_->TailInsertSplit(new_split_label);
159 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700160}
161
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700162uint64_t TimingLogger::GetTotalNs() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700163 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800164 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
165 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700166 total_ns += split.first;
167 }
168 return total_ns;
169}
170
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700171void TimingLogger::Dump(std::ostream &os) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700172 uint64_t longest_split = 0;
173 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800174 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
175 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700176 uint64_t split_time = split.first;
177 longest_split = std::max(longest_split, split_time);
178 total_ns += split_time;
179 }
180 // Compute which type of unit we will use for printing the timings.
181 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
182 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
183 // Print formatted splits.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800184 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
185 const TimingLogger::SplitTiming& split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700186 uint64_t split_time = split.first;
187 if (!precise_ && divisor >= 1000) {
188 // Make the fractional part 0.
189 split_time -= split_time % (divisor / 1000);
190 }
191 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
192 << split.second << "\n";
193 }
194 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
195}
196
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700197
198TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
199 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
200 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
201 timing_logger_ = timing_logger;
202 label_ = label;
203 running_ns_ = 0;
204 explicit_ = false;
205
206 // Stash away the current split and pause it.
207 enclosing_split_ = timing_logger->current_split_;
208 if (enclosing_split_ != NULL) {
209 enclosing_split_->Pause();
210 }
211
212 timing_logger_->current_split_ = this;
213
214 ATRACE_BEGIN(label_);
215
216 start_ns_ = NanoTime();
217 if (timing_logger_->verbose_) {
218 LOG(INFO) << "Begin: " << label_;
219 }
220}
221
222TimingLogger::ScopedSplit::~ScopedSplit() {
223 uint64_t current_time = NanoTime();
224 uint64_t split_time = current_time - start_ns_;
225 running_ns_ += split_time;
226 ATRACE_END();
227
228 if (timing_logger_->verbose_) {
229 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
230 }
231
Ian Rogers5fe9af72013-11-14 00:17:20 -0800232 // If one or more enclosed explicitly started splits are not terminated we can
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700233 // either fail or "unwind" the stack of splits in the timing logger to 'this'
234 // (by deleting the intervening scoped splits). This implements the latter.
235 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
236 while ((current != NULL) && (current != this)) {
237 delete current;
238 current = timing_logger_->current_split_;
239 }
240
241 CHECK(current != NULL) << "Missing scoped split (" << this->label_
242 << ") in timing logger (" << timing_logger_->name_ << ").";
243 CHECK(timing_logger_->current_split_ == this);
244
245 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
246
247 timing_logger_->current_split_ = enclosing_split_;
248 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700249 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700250 }
251}
252
253
254void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
255 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
256 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
257 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
258 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
259 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
260 // be different from what we had before.
261
262 uint64_t current_time = NanoTime();
263 uint64_t split_time = current_time - start_ns_;
264 ATRACE_END();
265 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
266
267 if (timing_logger_->verbose_) {
268 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
269 << "Begin: " << label;
270 }
271
272 label_ = label;
273 start_ns_ = current_time;
274 running_ns_ = 0;
275
276 ATRACE_BEGIN(label);
277}
278
279void TimingLogger::ScopedSplit::Pause() {
280 uint64_t current_time = NanoTime();
281 uint64_t split_time = current_time - start_ns_;
282 running_ns_ += split_time;
283 ATRACE_END();
284}
285
286
Anwar Ghuloum46543222013-08-12 09:28:42 -0700287void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700288 uint64_t current_time = NanoTime();
289
290 start_ns_ = current_time;
291 ATRACE_BEGIN(label_);
292}
293
Sameer Abu Asala8439542013-02-14 16:06:42 -0800294} // namespace art