blob: 076bc119c1ea888692dd4dc2fbb7b4534753f513 [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) {
Ian Rogers1d54e732013-05-02 21:10:01 -070048 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070049}
Sameer Abu Asala8439542013-02-14 16:06:42 -080050
51void CumulativeLogger::Start() {
Sameer Abu Asala8439542013-02-14 16:06:42 -080052}
53
54void CumulativeLogger::End() {
55 MutexLock mu(Thread::Current(), lock_);
56 iterations_++;
57}
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070058
Sameer Abu Asala8439542013-02-14 16:06:42 -080059void CumulativeLogger::Reset() {
60 MutexLock mu(Thread::Current(), lock_);
61 iterations_ = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080062 STLDeleteElements(&histograms_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080063}
64
65uint64_t CumulativeLogger::GetTotalNs() const {
66 return GetTotalTime() * kAdjust;
67}
68
69uint64_t CumulativeLogger::GetTotalTime() const {
70 MutexLock mu(Thread::Current(), lock_);
71 uint64_t total = 0;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080072 for (Histogram<uint64_t>* histogram : histograms_) {
73 total += histogram->Sum();
Sameer Abu Asala8439542013-02-14 16:06:42 -080074 }
75 return total;
76}
77
Ian Rogers5fe9af72013-11-14 00:17:20 -080078void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070079 MutexLock mu(Thread::Current(), lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080080 const TimingLogger::SplitTimings& splits = logger.GetSplits();
81 for (auto it = splits.begin(), end = splits.end(); it != end; ++it) {
82 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -070083 uint64_t split_time = split.first;
84 const char* split_name = split.second;
85 AddPair(split_name, split_time);
86 }
87}
88
Mathieu Chartier590fee92013-09-13 13:46:47 -070089size_t CumulativeLogger::GetIterations() const {
90 MutexLock mu(Thread::Current(), lock_);
91 return iterations_;
92}
93
Sameer Abu Asala8439542013-02-14 16:06:42 -080094void CumulativeLogger::Dump(std::ostream &os) {
95 MutexLock mu(Thread::Current(), lock_);
96 DumpHistogram(os);
97}
98
Mathieu Chartier19b0a912013-11-20 14:07:54 -080099void CumulativeLogger::AddPair(const std::string& label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800100 // Convert delta time to microseconds so that we don't overflow our counters.
101 delta_time /= kAdjust;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700102
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800103 Histogram<uint64_t>* histogram;
104 Histogram<uint64_t> dummy(label.c_str());
105 auto it = histograms_.find(&dummy);
106 if (it == histograms_.end()) {
107 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ?
108 kLowMemoryBucketCount : kDefaultBucketCount;
109 histogram = new Histogram<uint64_t>(label.c_str(), kInitialBucketSize, max_buckets);
110 histograms_.insert(histogram);
111 } else {
112 histogram = *it;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800113 }
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800114 histogram->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800115}
116
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800117class CompareHistorgramByTimeSpentDeclining {
118 public:
119 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
120 return a->Sum() > b->Sum();
121 }
122};
123
Sameer Abu Asala8439542013-02-14 16:06:42 -0800124void CumulativeLogger::DumpHistogram(std::ostream &os) {
125 os << "Start Dumping histograms for " << iterations_ << " iterations"
126 << " for " << name_ << "\n";
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800127 std::set<Histogram<uint64_t>*, CompareHistorgramByTimeSpentDeclining>
128 sorted_histograms(histograms_.begin(), histograms_.end());
129 for (Histogram<uint64_t>* histogram : sorted_histograms) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700130 Histogram<uint64_t>::CumulativeData cumulative_data;
Mathieu Chartier19b0a912013-11-20 14:07:54 -0800131 // We don't expect DumpHistogram to be called often, so it is not performance critical.
132 histogram->CreateHistogram(&cumulative_data);
133 histogram->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800134 }
135 os << "Done Dumping histograms \n";
136}
137
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700138TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700139 : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700140}
141
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700142void TimingLogger::Reset() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700143 current_split_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700144 splits_.clear();
145}
146
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700147void TimingLogger::StartSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800148 DCHECK(new_split_label != nullptr) << "Starting split with null label.";
149 TimingLogger::ScopedSplit* explicit_scoped_split =
150 new TimingLogger::ScopedSplit(new_split_label, this);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700151 explicit_scoped_split->explicit_ = true;
152}
153
154void TimingLogger::EndSplit() {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800155 CHECK(current_split_ != nullptr) << "Ending a non-existent split.";
156 DCHECK(current_split_->label_ != nullptr);
157 DCHECK(current_split_->explicit_ == true)
158 << "Explicitly ending scoped split: " << current_split_->label_;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700159 delete current_split_;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800160 // TODO: current_split_ = nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700161}
162
163// Ends the current split and starts the one given by the label.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700164void TimingLogger::NewSplit(const char* new_split_label) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800165 if (current_split_ == nullptr) {
166 StartSplit(new_split_label);
167 } else {
168 DCHECK(new_split_label != nullptr) << "New split (" << new_split_label << ") with null label.";
169 current_split_->TailInsertSplit(new_split_label);
170 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700171}
172
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700173uint64_t TimingLogger::GetTotalNs() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700174 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800175 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
176 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 total_ns += split.first;
178 }
179 return total_ns;
180}
181
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700182void TimingLogger::Dump(std::ostream &os) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700183 uint64_t longest_split = 0;
184 uint64_t total_ns = 0;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800185 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
186 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700187 uint64_t split_time = split.first;
188 longest_split = std::max(longest_split, split_time);
189 total_ns += split_time;
190 }
191 // Compute which type of unit we will use for printing the timings.
192 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
193 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
194 // Print formatted splits.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800195 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
196 const TimingLogger::SplitTiming& split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700197 uint64_t split_time = split.first;
198 if (!precise_ && divisor >= 1000) {
199 // Make the fractional part 0.
200 split_time -= split_time % (divisor / 1000);
201 }
202 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
203 << split.second << "\n";
204 }
205 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
206}
207
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700208
209TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
210 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
211 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
212 timing_logger_ = timing_logger;
213 label_ = label;
214 running_ns_ = 0;
215 explicit_ = false;
216
217 // Stash away the current split and pause it.
218 enclosing_split_ = timing_logger->current_split_;
219 if (enclosing_split_ != NULL) {
220 enclosing_split_->Pause();
221 }
222
223 timing_logger_->current_split_ = this;
224
225 ATRACE_BEGIN(label_);
226
227 start_ns_ = NanoTime();
228 if (timing_logger_->verbose_) {
229 LOG(INFO) << "Begin: " << label_;
230 }
231}
232
233TimingLogger::ScopedSplit::~ScopedSplit() {
234 uint64_t current_time = NanoTime();
235 uint64_t split_time = current_time - start_ns_;
236 running_ns_ += split_time;
237 ATRACE_END();
238
239 if (timing_logger_->verbose_) {
240 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
241 }
242
Ian Rogers5fe9af72013-11-14 00:17:20 -0800243 // If one or more enclosed explicitly started splits are not terminated we can
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700244 // either fail or "unwind" the stack of splits in the timing logger to 'this'
245 // (by deleting the intervening scoped splits). This implements the latter.
246 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
247 while ((current != NULL) && (current != this)) {
248 delete current;
249 current = timing_logger_->current_split_;
250 }
251
252 CHECK(current != NULL) << "Missing scoped split (" << this->label_
253 << ") in timing logger (" << timing_logger_->name_ << ").";
254 CHECK(timing_logger_->current_split_ == this);
255
256 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
257
258 timing_logger_->current_split_ = enclosing_split_;
259 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700260 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700261 }
262}
263
264
265void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
266 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
267 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
268 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
269 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
270 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
271 // be different from what we had before.
272
273 uint64_t current_time = NanoTime();
274 uint64_t split_time = current_time - start_ns_;
275 ATRACE_END();
276 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
277
278 if (timing_logger_->verbose_) {
279 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
280 << "Begin: " << label;
281 }
282
283 label_ = label;
284 start_ns_ = current_time;
285 running_ns_ = 0;
286
287 ATRACE_BEGIN(label);
288}
289
290void TimingLogger::ScopedSplit::Pause() {
291 uint64_t current_time = NanoTime();
292 uint64_t split_time = current_time - start_ns_;
293 running_ns_ += split_time;
294 ATRACE_END();
295}
296
297
Anwar Ghuloum46543222013-08-12 09:28:42 -0700298void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700299 uint64_t current_time = NanoTime();
300
301 start_ns_ = current_time;
302 ATRACE_BEGIN(label_);
303}
304
Sameer Abu Asala8439542013-02-14 16:06:42 -0800305} // namespace art