blob: a1550028e99632565ebc3d8e687c32a48186a1d2 [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_);
Mathieu Chartier4aeec172014-03-27 16:09:46 -070057 ++iterations_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080058}
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_);
Mathieu Chartier4aeec172014-03-27 16:09:46 -070069 for (const TimingLogger::SplitTiming& split : logger.GetSplits()) {
Ian Rogers1d54e732013-05-02 21:10:01 -070070 uint64_t split_time = split.first;
71 const char* split_name = split.second;
72 AddPair(split_name, split_time);
73 }
Mathieu Chartier4aeec172014-03-27 16:09:46 -070074 ++iterations_;
Ian Rogers1d54e732013-05-02 21:10:01 -070075}
76
Mathieu Chartier590fee92013-09-13 13:46:47 -070077size_t CumulativeLogger::GetIterations() const {
78 MutexLock mu(Thread::Current(), lock_);
79 return iterations_;
80}
81
Mathieu Chartierafe49982014-03-27 10:55:04 -070082void CumulativeLogger::Dump(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080083 MutexLock mu(Thread::Current(), lock_);
84 DumpHistogram(os);
85}
86
Mathieu Chartier19b0a912013-11-20 14:07:54 -080087void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080088 // Convert delta time to microseconds so that we don't overflow our counters.
89 delta_time /= kAdjust;
Mathieu Chartierafe49982014-03-27 10:55:04 -070090 total_time_ += delta_time;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080091 Histogram<uint64_t>* histogram;
92 Histogram<uint64_t> dummy(label.c_str());
93 auto it = histograms_.find(&dummy);
94 if (it == histograms_.end()) {
95 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ?
96 kLowMemoryBucketCount : kDefaultBucketCount;
97 histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets);
98 histograms_.insert(histogram);
99 } else {
100 histogram = *it;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800101 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800102 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800103}
104
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800105class CompareHistorgramByTimeSpentDeclining {
106 public:
107 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
108 return a->Sum() > b->Sum();
109 }
110};
111
Mathieu Chartierafe49982014-03-27 10:55:04 -0700112void CumulativeLogger::DumpHistogram(std::ostream &os) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800113 os << "Start Dumping histograms for " << iterations_ << " iterations"
114 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800115 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
116 sorted_histograms(histograms_.begin(), histograms_.end());
117 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700118 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800119 // We don't expect DumpHistogram to be called often, so it is not performance critical.
120 histogram->CreateHistogram(&cumulative_data);
121 histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800122 }
123 os << "Done Dumping histograms \n";
124}
125
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700126TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700127 : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700128}
129
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700130void TimingLogger::Reset() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700131 current_split_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 splits_.clear();
133}
134
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700135void TimingLogger::StartSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800136 DCHECK(new_split_label != nullptr) << "Starting split with null label.";
137 TimingLogger::ScopedSplit* explicit_scoped_split =
138 new TimingLogger::ScopedSplit(new_split_label, this);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700139 explicit_scoped_split->explicit_ = true;
140}
141
142void TimingLogger::EndSplit() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800143 CHECK(current_split_ != nullptr) << "Ending a non-existent split.";
144 DCHECK(current_split_->label_ != nullptr);
145 DCHECK(current_split_->explicit_ == true)
146 << "Explicitly ending scoped split: " << current_split_->label_;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700147 delete current_split_;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800148 // TODO: current_split_ = nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700149}
150
151// Ends the current split and starts the one given by the label.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700152void TimingLogger::NewSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800153 if (current_split_ == nullptr) {
154 StartSplit(new_split_label);
155 } else {
156 DCHECK(new_split_label != nullptr) << "New split (" << new_split_label << ") with null label.";
157 current_split_->TailInsertSplit(new_split_label);
158 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700159}
160
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700161uint64_t TimingLogger::GetTotalNs() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700162 uint64_t total_ns = 0;
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700163 for (const TimingLogger::SplitTiming& split : splits_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700164 total_ns += split.first;
165 }
166 return total_ns;
167}
168
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700169void TimingLogger::Dump(std::ostream &os) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700170 uint64_t longest_split = 0;
171 uint64_t total_ns = 0;
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700172 for (const SplitTiming& split : splits_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700173 uint64_t split_time = split.first;
174 longest_split = std::max(longest_split, split_time);
175 total_ns += split_time;
176 }
177 // Compute which type of unit we will use for printing the timings.
178 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
179 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
180 // Print formatted splits.
Mathieu Chartier4aeec172014-03-27 16:09:46 -0700181 for (const SplitTiming& split : splits_) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700182 uint64_t split_time = split.first;
183 if (!precise_ && divisor >= 1000) {
184 // Make the fractional part 0.
185 split_time -= split_time % (divisor / 1000);
186 }
187 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
188 << split.second << "\n";
189 }
190 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
191}
192
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700193TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
194 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
195 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
196 timing_logger_ = timing_logger;
197 label_ = label;
198 running_ns_ = 0;
199 explicit_ = false;
200
201 // Stash away the current split and pause it.
202 enclosing_split_ = timing_logger->current_split_;
203 if (enclosing_split_ != NULL) {
204 enclosing_split_->Pause();
205 }
206
207 timing_logger_->current_split_ = this;
208
209 ATRACE_BEGIN(label_);
210
211 start_ns_ = NanoTime();
212 if (timing_logger_->verbose_) {
213 LOG(INFO) << "Begin: " << label_;
214 }
215}
216
217TimingLogger::ScopedSplit::~ScopedSplit() {
218 uint64_t current_time = NanoTime();
219 uint64_t split_time = current_time - start_ns_;
220 running_ns_ += split_time;
221 ATRACE_END();
222
223 if (timing_logger_->verbose_) {
224 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
225 }
226
Ian Rogers5fe9af72013-11-14 00:17:20 -0800227 // If one or more enclosed explicitly started splits are not terminated we can
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700228 // either fail or "unwind" the stack of splits in the timing logger to 'this'
229 // (by deleting the intervening scoped splits). This implements the latter.
230 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
231 while ((current != NULL) && (current != this)) {
232 delete current;
233 current = timing_logger_->current_split_;
234 }
235
236 CHECK(current != NULL) << "Missing scoped split (" << this->label_
237 << ") in timing logger (" << timing_logger_->name_ << ").";
238 CHECK(timing_logger_->current_split_ == this);
239
240 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
241
242 timing_logger_->current_split_ = enclosing_split_;
243 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700244 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700245 }
246}
247
248
249void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
250 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
251 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
252 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
253 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
254 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
255 // be different from what we had before.
256
257 uint64_t current_time = NanoTime();
258 uint64_t split_time = current_time - start_ns_;
259 ATRACE_END();
260 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
261
262 if (timing_logger_->verbose_) {
263 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
264 << "Begin: " << label;
265 }
266
267 label_ = label;
268 start_ns_ = current_time;
269 running_ns_ = 0;
270
271 ATRACE_BEGIN(label);
272}
273
274void TimingLogger::ScopedSplit::Pause() {
275 uint64_t current_time = NanoTime();
276 uint64_t split_time = current_time - start_ns_;
277 running_ns_ += split_time;
278 ATRACE_END();
279}
280
281
Anwar Ghuloum46543222013-08-12 09:28:42 -0700282void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700283 uint64_t current_time = NanoTime();
284
285 start_ns_ = current_time;
286 ATRACE_BEGIN(label_);
287}
288
Sameer Abu Asala8439542013-02-14 16:06:42 -0800289} // namespace art