blob: bf6fd17a49446354b976ea6c4ad8e6a6493a686c [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
17#include "timing_logger.h"
18
19#include "base/logging.h"
20#include "thread.h"
21#include "base/stl_util.h"
22#include "base/histogram-inl.h"
23
24#include <cmath>
25#include <iomanip>
26
27namespace art {
28
29void TimingLogger::Reset() {
30 times_.clear();
31 labels_.clear();
32 AddSplit("");
33}
34
35TimingLogger::TimingLogger(const std::string &name, bool precise)
36 : name_(name),
37 precise_(precise) {
38 AddSplit("");
39}
40
41void TimingLogger::AddSplit(const std::string &label) {
42 times_.push_back(NanoTime());
43 labels_.push_back(label);
44}
45
46uint64_t TimingLogger::GetTotalNs() const {
47 return times_.back() - times_.front();
48}
Sameer Abu Asala8439542013-02-14 16:06:42 -080049
50void TimingLogger::Dump(std::ostream &os) const {
51 uint64_t largest_time = 0;
52 os << name_ << ": begin\n";
53 for (size_t i = 1; i < times_.size(); ++i) {
54 uint64_t delta_time = times_[i] - times_[i - 1];
55 largest_time = std::max(largest_time, delta_time);
56 }
57 // Compute which type of unit we will use for printing the timings.
58 TimeUnit tu = GetAppropriateTimeUnit(largest_time);
59 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
60 for (size_t i = 1; i < times_.size(); ++i) {
61 uint64_t delta_time = times_[i] - times_[i - 1];
62 if (!precise_ && divisor >= 1000) {
63 // Make the fraction 0.
64 delta_time -= delta_time % (divisor / 1000);
65 }
66 os << name_ << ": " << std::setw(8) << FormatDuration(delta_time, tu) << " "
67 << labels_[i] << "\n";
68 }
69 os << name_ << ": end, " << NsToMs(GetTotalNs()) << " ms\n";
70}
71
Ian Rogers45357052013-04-18 20:49:43 -070072CumulativeLogger::CumulativeLogger(const std::string& name)
Sameer Abu Asala8439542013-02-14 16:06:42 -080073 : name_(name),
Ian Rogers45357052013-04-18 20:49:43 -070074 lock_name_("CumulativeLoggerLock" + name),
75 lock_(lock_name_.c_str(), kDefaultMutexLevel, true) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080076 Reset();
77}
78
Ian Rogers45357052013-04-18 20:49:43 -070079CumulativeLogger::~CumulativeLogger() {
80 STLDeleteElements(&histograms_);
81}
Sameer Abu Asala8439542013-02-14 16:06:42 -080082
Ian Rogers45357052013-04-18 20:49:43 -070083void CumulativeLogger::SetName(const std::string& name) {
Ian Rogers1d54e732013-05-02 21:10:01 -070084 name_.assign(name);
Ian Rogers45357052013-04-18 20:49:43 -070085}
Sameer Abu Asala8439542013-02-14 16:06:42 -080086
87void CumulativeLogger::Start() {
88 MutexLock mu(Thread::Current(), lock_);
89 index_ = 0;
90}
91
92void CumulativeLogger::End() {
93 MutexLock mu(Thread::Current(), lock_);
94 iterations_++;
95}
96void CumulativeLogger::Reset() {
97 MutexLock mu(Thread::Current(), lock_);
98 iterations_ = 0;
99 STLDeleteElements(&histograms_);
100}
101
102uint64_t CumulativeLogger::GetTotalNs() const {
103 return GetTotalTime() * kAdjust;
104}
105
106uint64_t CumulativeLogger::GetTotalTime() const {
107 MutexLock mu(Thread::Current(), lock_);
108 uint64_t total = 0;
109 for (size_t i = 0; i < histograms_.size(); ++i) {
110 total += histograms_[i]->Sum();
111 }
112 return total;
113}
114
115void CumulativeLogger::AddLogger(const TimingLogger &logger) {
116 MutexLock mu(Thread::Current(), lock_);
117 DCHECK_EQ(logger.times_.size(), logger.labels_.size());
118 for (size_t i = 1; i < logger.times_.size(); ++i) {
119 const uint64_t delta_time = logger.times_[i] - logger.times_[i - 1];
120 const std::string &label = logger.labels_[i];
121 AddPair(label, delta_time);
122 }
123}
124
Ian Rogers1d54e732013-05-02 21:10:01 -0700125void CumulativeLogger::AddNewLogger(const base::NewTimingLogger &logger) {
126 MutexLock mu(Thread::Current(), lock_);
127 const std::vector<std::pair<uint64_t, const char*> >& splits = logger.GetSplits();
128 typedef std::vector<std::pair<uint64_t, const char*> >::const_iterator It;
129 if (kIsDebugBuild && splits.size() != histograms_.size()) {
130 LOG(ERROR) << "Mismatch in splits.";
131 typedef std::vector<Histogram<uint64_t> *>::const_iterator It2;
132 It it = splits.begin();
133 It2 it2 = histograms_.begin();
134 while ((it != splits.end()) && (it2 != histograms_.end())) {
135 if (it != splits.end()) {
136 LOG(ERROR) << "\tsplit: " << it->second;
137 ++it;
138 }
139 if (it2 != histograms_.end()) {
140 LOG(ERROR) << "\tpreviously record: " << (*it2)->Name();
141 ++it2;
142 }
143 }
144 }
145 for (It it = splits.begin(), end = splits.end(); it != end; ++it) {
146 std::pair<uint64_t, const char*> split = *it;
147 uint64_t split_time = split.first;
148 const char* split_name = split.second;
149 AddPair(split_name, split_time);
150 }
151}
152
Sameer Abu Asala8439542013-02-14 16:06:42 -0800153void CumulativeLogger::Dump(std::ostream &os) {
154 MutexLock mu(Thread::Current(), lock_);
155 DumpHistogram(os);
156}
157
158void CumulativeLogger::AddPair(const std::string &label, uint64_t delta_time) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800159 // Convert delta time to microseconds so that we don't overflow our counters.
160 delta_time /= kAdjust;
161 if (index_ >= histograms_.size()) {
162 Histogram<uint64_t> *tmp_hist = new Histogram<uint64_t>(label);
163 tmp_hist->AddValue(delta_time);
164 histograms_.push_back(tmp_hist);
165 } else {
166 histograms_[index_]->AddValue(delta_time);
167 DCHECK_EQ(label, histograms_[index_]->Name());
168 }
169 index_++;
170}
171
172void CumulativeLogger::DumpHistogram(std::ostream &os) {
173 os << "Start Dumping histograms for " << iterations_ << " iterations"
174 << " for " << name_ << "\n";
175 for (size_t Idx = 0; Idx < histograms_.size(); Idx++) {
176 Histogram<uint64_t> &hist = *(histograms_[Idx]);
177 hist.CreateHistogram();
178 hist.PrintConfidenceIntervals(os, 0.99);
179 }
180 os << "Done Dumping histograms \n";
181}
182
Ian Rogers1d54e732013-05-02 21:10:01 -0700183
184namespace base {
185
186NewTimingLogger::NewTimingLogger(const char* name, bool precise, bool verbose)
187 : name_(name), precise_(precise), verbose_(verbose),
188 current_split_(NULL), current_split_start_ns_(0) {
189}
190
191void NewTimingLogger::Reset() {
192 current_split_ = NULL;
193 current_split_start_ns_ = 0;
194 splits_.clear();
195}
196
197void NewTimingLogger::StartSplit(const char* new_split_label) {
198 DCHECK(current_split_ == NULL);
199 if (verbose_) {
200 LOG(INFO) << "Begin: " << new_split_label;
201 }
202 current_split_ = new_split_label;
203 current_split_start_ns_ = NanoTime();
204}
205
206// Ends the current split and starts the one given by the label.
207void NewTimingLogger::NewSplit(const char* new_split_label) {
208 DCHECK(current_split_ != NULL);
209 uint64_t current_time = NanoTime();
210 uint64_t split_time = current_time - current_split_start_ns_;
211 splits_.push_back(std::pair<uint64_t, const char*>(split_time, current_split_));
212 if (verbose_) {
213 LOG(INFO) << "End: " << current_split_ << " " << PrettyDuration(split_time) << "\n"
214 << "Begin: " << new_split_label;
215 }
216 current_split_ = new_split_label;
217 current_split_start_ns_ = current_time;
218}
219
220void NewTimingLogger::EndSplit() {
221 DCHECK(current_split_ != NULL);
222 uint64_t current_time = NanoTime();
223 uint64_t split_time = current_time - current_split_start_ns_;
224 if (verbose_) {
225 LOG(INFO) << "End: " << current_split_ << " " << PrettyDuration(split_time);
226 }
227 splits_.push_back(std::pair<uint64_t, const char*>(split_time, current_split_));
228}
229
230uint64_t NewTimingLogger::GetTotalNs() const {
231 uint64_t total_ns = 0;
232 typedef std::vector<std::pair<uint64_t, const char*> >::const_iterator It;
233 for (It it = splits_.begin(), end = splits_.end(); it != end; ++it) {
234 std::pair<uint64_t, const char*> split = *it;
235 total_ns += split.first;
236 }
237 return total_ns;
238}
239
240void NewTimingLogger::Dump(std::ostream &os) const {
241 uint64_t longest_split = 0;
242 uint64_t total_ns = 0;
243 typedef std::vector<std::pair<uint64_t, const char*> >::const_iterator It;
244 for (It it = splits_.begin(), end = splits_.end(); it != end; ++it) {
245 std::pair<uint64_t, const char*> split = *it;
246 uint64_t split_time = split.first;
247 longest_split = std::max(longest_split, split_time);
248 total_ns += split_time;
249 }
250 // Compute which type of unit we will use for printing the timings.
251 TimeUnit tu = GetAppropriateTimeUnit(longest_split);
252 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
253 // Print formatted splits.
254 for (It it = splits_.begin(), end = splits_.end(); it != end; ++it) {
255 std::pair<uint64_t, const char*> split = *it;
256 uint64_t split_time = split.first;
257 if (!precise_ && divisor >= 1000) {
258 // Make the fractional part 0.
259 split_time -= split_time % (divisor / 1000);
260 }
261 os << name_ << ": " << std::setw(8) << FormatDuration(split_time, tu) << " "
262 << split.second << "\n";
263 }
264 os << name_ << ": end, " << NsToMs(total_ns) << " ms\n";
265}
266
267} // namespace base
Sameer Abu Asala8439542013-02-14 16:06:42 -0800268} // namespace art