blob: dae82011171df0a5fc2fbf2a7a15733b67e7c285 [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() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070042 STLDeleteValues(&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;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070060 STLDeleteValues(&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;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070070 for (CumulativeLogger::HistogramsIterator it = histograms_.begin(), end = histograms_.end();
71 it != end; ++it) {
72 total += it->second->Sum();
Sameer Abu Asala8439542013-02-14 16:06:42 -080073 }
74 return total;
75}
76
Ian Rogers5fe9af72013-11-14 00:17:20 -080077void CumulativeLogger::AddLogger(const TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070078 MutexLock mu(Thread::Current(), lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080079 const TimingLogger::SplitTimings& splits = logger.GetSplits();
80 for (auto it = splits.begin(), end = splits.end(); it != end; ++it) {
81 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -070082 uint64_t split_time = split.first;
83 const char* split_name = split.second;
84 AddPair(split_name, split_time);
85 }
86}
87
Mathieu Chartier590fee92013-09-13 13:46:47 -070088size_t CumulativeLogger::GetIterations() const {
89 MutexLock mu(Thread::Current(), lock_);
90 return iterations_;
91}
92
Sameer Abu Asala8439542013-02-14 16:06:42 -080093void CumulativeLogger::Dump(std::ostream &os) {
94 MutexLock mu(Thread::Current(), lock_);
95 DumpHistogram(os);
96}
97
98void CumulativeLogger::AddPair(const std::string &label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080099 // Convert delta time to microseconds so that we don't overflow our counters.
100 delta_time /= kAdjust;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700101
102 if (histograms_.find(label) == histograms_.end()) {
Ian Rogers5fe9af72013-11-14 00:17:20 -0800103 // TODO: Should this be a defined constant so we we know out of which orifice 16 and 100 were
104 // picked?
Mathieu Chartiere0a53e92013-08-05 10:17:40 -0700105 const size_t max_buckets = Runtime::Current()->GetHeap()->IsLowMemoryMode() ? 16 : 100;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700106 // TODO: Should this be a defined constant so we know 50 of WTF?
107 histograms_[label] = new Histogram<uint64_t>(label.c_str(), 50, max_buckets);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800108 }
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700109 histograms_[label]->AddValue(delta_time);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800110}
111
112void CumulativeLogger::DumpHistogram(std::ostream &os) {
113 os << "Start Dumping histograms for " << iterations_ << " iterations"
114 << " for " << name_ << "\n";
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700115 for (CumulativeLogger::HistogramsIterator it = histograms_.begin(), end = histograms_.end();
116 it != end; ++it) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700117 Histogram<uint64_t>::CumulativeData cumulative_data;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700118 it->second->CreateHistogram(cumulative_data);
119 it->second->PrintConfidenceIntervals(os, 0.99, cumulative_data);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700120 // Reset cumulative values to save memory. We don't expect DumpHistogram to be called often, so
121 // it is not performance critical.
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;
Ian Rogers5fe9af72013-11-14 00:17:20 -0800163 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
164 TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700165 total_ns += split.first;
166 }
167 return total_ns;
168}
169
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700170void TimingLogger::Dump(std::ostream &os) const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700171 uint64_t longest_split = 0;
172 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 uint64_t split_time = split.first;
176 longest_split = std::max(longest_split, split_time);
177 total_ns += split_time;
178 }
179 // Compute which type of unit we will use for printing the timings.
180 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
181 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
182 // Print formatted splits.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800183 for (auto it = splits_.begin(), end = splits_.end(); it != end; ++it) {
184 const TimingLogger::SplitTiming& split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700185 uint64_t split_time = split.first;
186 if (!precise_ && divisor >= 1000) {
187 // Make the fractional part 0.
188 split_time -= split_time % (divisor / 1000);
189 }
190 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
191 << split.second << "\n";
192 }
193 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
194}
195
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700196
197TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
198 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
199 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
200 timing_logger_ = timing_logger;
201 label_ = label;
202 running_ns_ = 0;
203 explicit_ = false;
204
205 // Stash away the current split and pause it.
206 enclosing_split_ = timing_logger->current_split_;
207 if (enclosing_split_ != NULL) {
208 enclosing_split_->Pause();
209 }
210
211 timing_logger_->current_split_ = this;
212
213 ATRACE_BEGIN(label_);
214
215 start_ns_ = NanoTime();
216 if (timing_logger_->verbose_) {
217 LOG(INFO) << "Begin: " << label_;
218 }
219}
220
221TimingLogger::ScopedSplit::~ScopedSplit() {
222 uint64_t current_time = NanoTime();
223 uint64_t split_time = current_time - start_ns_;
224 running_ns_ += split_time;
225 ATRACE_END();
226
227 if (timing_logger_->verbose_) {
228 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
229 }
230
Ian Rogers5fe9af72013-11-14 00:17:20 -0800231 // If one or more enclosed explicitly started splits are not terminated we can
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700232 // either fail or "unwind" the stack of splits in the timing logger to 'this'
233 // (by deleting the intervening scoped splits). This implements the latter.
234 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
235 while ((current != NULL) && (current != this)) {
236 delete current;
237 current = timing_logger_->current_split_;
238 }
239
240 CHECK(current != NULL) << "Missing scoped split (" << this->label_
241 << ") in timing logger (" << timing_logger_->name_ << ").";
242 CHECK(timing_logger_->current_split_ == this);
243
244 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
245
246 timing_logger_->current_split_ = enclosing_split_;
247 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700248 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700249 }
250}
251
252
253void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
254 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
255 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
256 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
257 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
258 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
259 // be different from what we had before.
260
261 uint64_t current_time = NanoTime();
262 uint64_t split_time = current_time - start_ns_;
263 ATRACE_END();
264 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
265
266 if (timing_logger_->verbose_) {
267 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
268 << "Begin: " << label;
269 }
270
271 label_ = label;
272 start_ns_ = current_time;
273 running_ns_ = 0;
274
275 ATRACE_BEGIN(label);
276}
277
278void TimingLogger::ScopedSplit::Pause() {
279 uint64_t current_time = NanoTime();
280 uint64_t split_time = current_time - start_ns_;
281 running_ns_ += split_time;
282 ATRACE_END();
283}
284
285
Anwar Ghuloum46543222013-08-12 09:28:42 -0700286void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700287 uint64_t current_time = NanoTime();
288
289 start_ns_ = current_time;
290 ATRACE_BEGIN(label_);
291}
292
Sameer Abu Asala8439542013-02-14 16:06:42 -0800293} // namespace art