blob: 45a546f37e1ce8e3438abf86e9322cc86c040d01 [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
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070077void CumulativeLogger::AddLogger(const base::TimingLogger &logger) {
Ian Rogers1d54e732013-05-02 21:10:01 -070078 MutexLock mu(Thread::Current(), lock_);
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070079 const base::TimingLogger::SplitTimings& splits = logger.GetSplits();
Anwar Ghuloum67f99412013-08-12 14:19:48 -070080 for (base::TimingLogger::SplitTimingsIterator it = splits.begin(), end = splits.end();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070081 it != end; ++it) {
82 base::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
99void 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
103 if (histograms_.find(label) == histograms_.end()) {
104 // TODO: Shoud this be a defined constant so we we know out of which orifice 16 and 100 were 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
Ian Rogers1d54e732013-05-02 21:10:01 -0700126
127namespace base {
128
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700129TimingLogger::TimingLogger(const char* name, bool precise, bool verbose)
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700130 : name_(name), precise_(precise), verbose_(verbose), current_split_(NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700131}
132
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700133void TimingLogger::Reset() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700134 current_split_ = NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -0700135 splits_.clear();
136}
137
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700138void TimingLogger::StartSplit(const char* new_split_label) {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700139 DCHECK(new_split_label != NULL) << "Starting split (" << new_split_label << ") with null label.";
140 TimingLogger::ScopedSplit* explicit_scoped_split = new TimingLogger::ScopedSplit(new_split_label, this);
141 explicit_scoped_split->explicit_ = true;
142}
143
144void TimingLogger::EndSplit() {
145 CHECK(current_split_ != NULL) << "Ending a non-existent split.";
146 DCHECK(current_split_->label_ != NULL);
147 DCHECK(current_split_->explicit_ == true) << "Explicitly ending scoped split: " << current_split_->label_;
148
149 delete current_split_;
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) {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700154 CHECK(current_split_ != NULL) << "Inserting a new split (" << new_split_label
155 << ") into a non-existent split.";
156 DCHECK(new_split_label != NULL) << "New split (" << new_split_label << ") with null label.";
Ian Rogers1d54e732013-05-02 21:10:01 -0700157
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700158 current_split_->TailInsertSplit(new_split_label);
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;
Anwar Ghuloum67f99412013-08-12 14:19:48 -0700163 for (base::TimingLogger::SplitTimingsIterator it = splits_.begin(), end = splits_.end();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700164 it != end; ++it) {
165 base::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;
Anwar Ghuloum67f99412013-08-12 14:19:48 -0700174 for (base::TimingLogger::SplitTimingsIterator it = splits_.begin(), end = splits_.end();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700175 it != end; ++it) {
176 base::TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700177 uint64_t split_time = split.first;
178 longest_split = std::max(longest_split, split_time);
179 total_ns += split_time;
180 }
181 // Compute which type of unit we will use for printing the timings.
182 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
183 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
184 // Print formatted splits.
Anwar Ghuloum67f99412013-08-12 14:19:48 -0700185 for (base::TimingLogger::SplitTimingsIterator it = splits_.begin(), end = splits_.end();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700186 it != end; ++it) {
187 base::TimingLogger::SplitTiming split = *it;
Ian Rogers1d54e732013-05-02 21:10:01 -0700188 uint64_t split_time = split.first;
189 if (!precise_ && divisor >= 1000) {
190 // Make the fractional part 0.
191 split_time -= split_time % (divisor / 1000);
192 }
193 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
194 << split.second << "\n";
195 }
196 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
197}
198
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700199
200TimingLogger::ScopedSplit::ScopedSplit(const char* label, TimingLogger* timing_logger) {
201 DCHECK(label != NULL) << "New scoped split (" << label << ") with null label.";
202 CHECK(timing_logger != NULL) << "New scoped split (" << label << ") without TimingLogger.";
203 timing_logger_ = timing_logger;
204 label_ = label;
205 running_ns_ = 0;
206 explicit_ = false;
207
208 // Stash away the current split and pause it.
209 enclosing_split_ = timing_logger->current_split_;
210 if (enclosing_split_ != NULL) {
211 enclosing_split_->Pause();
212 }
213
214 timing_logger_->current_split_ = this;
215
216 ATRACE_BEGIN(label_);
217
218 start_ns_ = NanoTime();
219 if (timing_logger_->verbose_) {
220 LOG(INFO) << "Begin: " << label_;
221 }
222}
223
224TimingLogger::ScopedSplit::~ScopedSplit() {
225 uint64_t current_time = NanoTime();
226 uint64_t split_time = current_time - start_ns_;
227 running_ns_ += split_time;
228 ATRACE_END();
229
230 if (timing_logger_->verbose_) {
231 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time);
232 }
233
234 // If one or more enclosed explcitly started splits are not terminated we can
235 // either fail or "unwind" the stack of splits in the timing logger to 'this'
236 // (by deleting the intervening scoped splits). This implements the latter.
237 TimingLogger::ScopedSplit* current = timing_logger_->current_split_;
238 while ((current != NULL) && (current != this)) {
239 delete current;
240 current = timing_logger_->current_split_;
241 }
242
243 CHECK(current != NULL) << "Missing scoped split (" << this->label_
244 << ") in timing logger (" << timing_logger_->name_ << ").";
245 CHECK(timing_logger_->current_split_ == this);
246
247 timing_logger_->splits_.push_back(SplitTiming(running_ns_, label_));
248
249 timing_logger_->current_split_ = enclosing_split_;
250 if (enclosing_split_ != NULL) {
Anwar Ghuloum46543222013-08-12 09:28:42 -0700251 enclosing_split_->Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700252 }
253}
254
255
256void TimingLogger::ScopedSplit::TailInsertSplit(const char* label) {
257 // Sleight of hand here: Rather than embedding a new scoped split, we're updating the current
258 // scoped split in place. Basically, it's one way to make explicit and scoped splits compose
259 // well while maintaining the current semantics of NewSplit. An alternative is to push a new split
260 // since we unwind the stack of scoped splits in the scoped split destructor. However, this implies
261 // that the current split is not ended by NewSplit (which calls TailInsertSplit), which would
262 // be different from what we had before.
263
264 uint64_t current_time = NanoTime();
265 uint64_t split_time = current_time - start_ns_;
266 ATRACE_END();
267 timing_logger_->splits_.push_back(std::pair<uint64_t, const char*>(split_time, label_));
268
269 if (timing_logger_->verbose_) {
270 LOG(INFO) << "End: " << label_ << " " << PrettyDuration(split_time) << "\n"
271 << "Begin: " << label;
272 }
273
274 label_ = label;
275 start_ns_ = current_time;
276 running_ns_ = 0;
277
278 ATRACE_BEGIN(label);
279}
280
281void TimingLogger::ScopedSplit::Pause() {
282 uint64_t current_time = NanoTime();
283 uint64_t split_time = current_time - start_ns_;
284 running_ns_ += split_time;
285 ATRACE_END();
286}
287
288
Anwar Ghuloum46543222013-08-12 09:28:42 -0700289void TimingLogger::ScopedSplit::Resume() {
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700290 uint64_t current_time = NanoTime();
291
292 start_ns_ = current_time;
293 ATRACE_BEGIN(label_);
294}
295
Ian Rogers1d54e732013-05-02 21:10:01 -0700296} // namespace base
Sameer Abu Asala8439542013-02-14 16:06:42 -0800297} // namespace art