blob: c8dee6d346fbf2dad09867c257a61ab0410d725e [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
Ian Rogers45357052013-04-18 20:49:43 -070034CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080035 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070036 lock_name_("CumulativeLoggerLock" + name),
37 lock_(lock_name_.c_str(), kDefaultMutexLevel, true) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080038 Reset();
39}
40
Ian Rogers45357052013-04-18 20:49:43 -070041CumulativeLogger::~CumulativeLogger() {
Mathieu Chartier19b0a912013-11-20 14:07:54 -080042 STLDeleteElements(&histograms_);
Ian Rogers45357052013-04-18 20:49:43 -070043}
Sameer Abu Asala8439542013-02-14 16:06:42 -080044
Ian Rogers45357052013-04-18 20:49:43 -070045void CumulativeLogger::SetName(const std::string& name) {
Ian Rogers1d54e732013-05-02 21:10:01 -070046 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070047}
Sameer Abu Asala8439542013-02-14 16:06:42 -080048
49void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080050}
51
52void CumulativeLogger::End() {
53 MutexLock mu(Thread::Current(), lock_);
54 iterations_++;
55}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070056
Sameer Abu Asala8439542013-02-14 16:06:42 -080057void CumulativeLogger::Reset() {
58 MutexLock mu(Thread::Current(), lock_);
59 iterations_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080060 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080061}
62
63uint64_t CumulativeLogger::GetTotalNs() const {
64 return GetTotalTime() * kAdjust;
65}
66
67uint64_t CumulativeLogger::GetTotalTime() const {
68 MutexLock mu(Thread::Current(), lock_);
69 uint64_t total = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080070 for (Histogram<uint64_t>* histogram : histograms_) {
71 total += histogram->Sum();
Sameer Abu Asala8439542013-02-14 16:06:42 -080072 }
73 return total;
74}
75
Ian Rogers5fe9af72013-11-14 00:17:20 -080076void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070077 MutexLock mu(Thread::Current(), lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080078 const TimingLogger::SplitTimings& splits = logger.GetSplits();
79 for (auto it = splits.begin(), end = splits.end(); it != end; ++it) {
80 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -070081 uint64_t split_time = split.first;
82 const char* split_name = split.second;
83 AddPair(split_name, split_time);
84 }
85}
86
Mathieu Chartier590fee92013-09-13 13:46:47 -070087size_t CumulativeLogger::GetIterations() const {
88 MutexLock mu(Thread::Current(), lock_);
89 return iterations_;
90}
91
Sameer Abu Asala8439542013-02-14 16:06:42 -080092void CumulativeLogger::Dump(std::ostream &os) {
93 MutexLock mu(Thread::Current(), lock_);
94 DumpHistogram(os);
95}
96
Mathieu Chartier19b0a912013-11-20 14:07:54 -080097void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080098 // Convert delta time to microseconds so that we don't overflow our counters.
99 delta_time /= kAdjust;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700100
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800101 Histogram<uint64_t>* histogram;
102 Histogram<uint64_t> dummy(label.c_str());
103 auto it = histograms_.find(&dummy);
104 if (it == histograms_.end()) {
105 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ?
106 kLowMemoryBucketCount : kDefaultBucketCount;
107 histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets);
108 histograms_.insert(histogram);
109 } else {
110 histogram = *it;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800111 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800112 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800113}
114
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800115class CompareHistorgramByTimeSpentDeclining {
116 public:
117 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
118 return a->Sum() > b->Sum();
119 }
120};
121
Sameer Abu Asala8439542013-02-14 16:06:42 -0800122void CumulativeLogger::DumpHistogram(std::ostream &os) {
123 os << "Start Dumping histograms for " << iterations_ << " iterations"
124 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800125 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
126 sorted_histograms(histograms_.begin(), histograms_.end());
127 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700128 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800129 // We don't expect DumpHistogram to be called often, so it is not performance critical.
130 histogram->CreateHistogram(&cumulative_data);
131 histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800132 }
133 os << "Done Dumping histograms \n";
134}
135
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700136TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700137 : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700138}
139
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700140void TimingLogger::Reset() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700141 current_split_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700142 splits_.clear();
143}
144
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700145void TimingLogger::StartSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800146 DCHECK(new_split_label != nullptr) << "Starting split with null label.";
147 TimingLogger::ScopedSplit* explicit_scoped_split =
148 new TimingLogger::ScopedSplit(new_split_label, this);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700149 explicit_scoped_split->explicit_ = true;
150}
151
152void TimingLogger::EndSplit() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800153 CHECK(current_split_ != nullptr) << "Ending a non-existent split.";
154 DCHECK(current_split_->label_ != nullptr);
155 DCHECK(current_split_->explicit_ == true)
156 << "Explicitly ending scoped split: " << current_split_->label_;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700157 delete current_split_;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800158 // TODO: current_split_ = nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700159}
160
161// Ends the current split and starts the one given by the label.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700162void TimingLogger::NewSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800163 if (current_split_ == nullptr) {
164 StartSplit(new_split_label);
165 } else {
166 DCHECK(new_split_label != nullptr) << "New split (" << new_split_label << ") with null label.";
167 current_split_->TailInsertSplit(new_split_label);
168 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700169}
170
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700171uint64_t TimingLogger::GetTotalNs() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700172 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800173 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
174 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700175 total_ns += split.first;
176 }
177 return total_ns;
178}
179
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700180void TimingLogger::Dump(std::ostream &os) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700181 uint64_t longest_split = 0;
182 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800183 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
184 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700185 uint64_t split_time = split.first;
186 longest_split = std::max(longest_split, split_time);
187 total_ns += split_time;
188 }
189 // Compute which type of unit we will use for printing the timings.
190 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
191 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
192 // Print formatted splits.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800193 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
194 const TimingLogger::SplitTiming& split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700195 uint64_t split_time = split.first;
196 if (!precise_ && divisor >= 1000) {
197 // Make the fractional part 0.
198 split_time -= split_time % (divisor / 1000);
199 }
200 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
201 << split.second << "\n";
202 }
203 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
204}
205
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700206
207TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
208 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
209 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
210 timing_logger_ = timing_logger;
211 label_ = label;
212 running_ns_ = 0;
213 explicit_ = false;
214
215 // Stash away the current split and pause it.
216 enclosing_split_ = timing_logger->current_split_;
217 if (enclosing_split_ != NULL) {
218 enclosing_split_->Pause();
219 }
220
221 timing_logger_->current_split_ = this;
222
223 ATRACE_BEGIN(label_);
224
225 start_ns_ = NanoTime();
226 if (timing_logger_->verbose_) {
227 LOG(INFO) << "Begin: " << label_;
228 }
229}
230
231TimingLogger::ScopedSplit::~ScopedSplit() {
232 uint64_t current_time = NanoTime();
233 uint64_t split_time = current_time - start_ns_;
234 running_ns_ += split_time;
235 ATRACE_END();
236
237 if (timing_logger_->verbose_) {
238 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
239 }
240
Ian Rogers5fe9af72013-11-14 00:17:20 -0800241 // If one or more enclosed explicitly started splits are not terminated we can
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700242 // either fail or "unwind" the stack of splits in the timing logger to 'this'
243 // (by deleting the intervening scoped splits). This implements the latter.
244 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
245 while ((current != NULL) && (current != this)) {
246 delete current;
247 current = timing_logger_->current_split_;
248 }
249
250 CHECK(current != NULL) << "Missing scoped split (" << this->label_
251 << ") in timing logger (" << timing_logger_->name_ << ").";
252 CHECK(timing_logger_->current_split_ == this);
253
254 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
255
256 timing_logger_->current_split_ = enclosing_split_;
257 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700258 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700259 }
260}
261
262
263void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
264 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
265 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
266 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
267 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
268 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
269 // be different from what we had before.
270
271 uint64_t current_time = NanoTime();
272 uint64_t split_time = current_time - start_ns_;
273 ATRACE_END();
274 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
275
276 if (timing_logger_->verbose_) {
277 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
278 << "Begin: " << label;
279 }
280
281 label_ = label;
282 start_ns_ = current_time;
283 running_ns_ = 0;
284
285 ATRACE_BEGIN(label);
286}
287
288void TimingLogger::ScopedSplit::Pause() {
289 uint64_t current_time = NanoTime();
290 uint64_t split_time = current_time - start_ns_;
291 running_ns_ += split_time;
292 ATRACE_END();
293}
294
295
Anwar Ghuloum46543222013-08-12 09:28:42 -0700296void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700297 uint64_t current_time = NanoTime();
298
299 start_ns_ = current_time;
300 ATRACE_BEGIN(label_);
301}
302
Sameer Abu Asala8439542013-02-14 16:06:42 -0800303} // namespace art