blob: d2c7eef6d4c2cfa9ffaa979dc0ba08d502f6b5ef [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}
49;
50
51void TimingLogger::Dump(std::ostream &os) const {
52 uint64_t largest_time = 0;
53 os << name_ << ": begin\n";
54 for (size_t i = 1; i < times_.size(); ++i) {
55 uint64_t delta_time = times_[i] - times_[i - 1];
56 largest_time = std::max(largest_time, delta_time);
57 }
58 // Compute which type of unit we will use for printing the timings.
59 TimeUnit tu = GetAppropriateTimeUnit(largest_time);
60 uint64_t divisor = GetNsToTimeUnitDivisor(tu);
61 for (size_t i = 1; i < times_.size(); ++i) {
62 uint64_t delta_time = times_[i] - times_[i - 1];
63 if (!precise_ && divisor >= 1000) {
64 // Make the fraction 0.
65 delta_time -= delta_time % (divisor / 1000);
66 }
67 os << name_ << ": " << std::setw(8) << FormatDuration(delta_time, tu) << " "
68 << labels_[i] << "\n";
69 }
70 os << name_ << ": end, " << NsToMs(GetTotalNs()) << " ms\n";
71}
72
73CumulativeLogger::CumulativeLogger(const std::string &name)
74 : name_(name),
75 lock_(("CumulativeLoggerLock" + name).c_str(), kDefaultMutexLevel, true) {
76 Reset();
77}
78
79CumulativeLogger::~CumulativeLogger() { STLDeleteElements(&histograms_); }
80
81void CumulativeLogger::SetName(const std::string &name) { name_ = name; }
82
83void CumulativeLogger::Start() {
84 MutexLock mu(Thread::Current(), lock_);
85 index_ = 0;
86}
87
88void CumulativeLogger::End() {
89 MutexLock mu(Thread::Current(), lock_);
90 iterations_++;
91}
92void CumulativeLogger::Reset() {
93 MutexLock mu(Thread::Current(), lock_);
94 iterations_ = 0;
95 STLDeleteElements(&histograms_);
96}
97
98uint64_t CumulativeLogger::GetTotalNs() const {
99 return GetTotalTime() * kAdjust;
100}
101
102uint64_t CumulativeLogger::GetTotalTime() const {
103 MutexLock mu(Thread::Current(), lock_);
104 uint64_t total = 0;
105 for (size_t i = 0; i < histograms_.size(); ++i) {
106 total += histograms_[i]->Sum();
107 }
108 return total;
109}
110
111void CumulativeLogger::AddLogger(const TimingLogger &logger) {
112 MutexLock mu(Thread::Current(), lock_);
113 DCHECK_EQ(logger.times_.size(), logger.labels_.size());
114 for (size_t i = 1; i < logger.times_.size(); ++i) {
115 const uint64_t delta_time = logger.times_[i] - logger.times_[i - 1];
116 const std::string &label = logger.labels_[i];
117 AddPair(label, delta_time);
118 }
119}
120
121void CumulativeLogger::Dump(std::ostream &os) {
122 MutexLock mu(Thread::Current(), lock_);
123 DumpHistogram(os);
124}
125
126void CumulativeLogger::AddPair(const std::string &label, uint64_t delta_time) {
127
128 // Convert delta time to microseconds so that we don't overflow our counters.
129 delta_time /= kAdjust;
130 if (index_ >= histograms_.size()) {
131 Histogram<uint64_t> *tmp_hist = new Histogram<uint64_t>(label);
132 tmp_hist->AddValue(delta_time);
133 histograms_.push_back(tmp_hist);
134 } else {
135 histograms_[index_]->AddValue(delta_time);
136 DCHECK_EQ(label, histograms_[index_]->Name());
137 }
138 index_++;
139}
140
141void CumulativeLogger::DumpHistogram(std::ostream &os) {
142 os << "Start Dumping histograms for " << iterations_ << " iterations"
143 << " for " << name_ << "\n";
144 for (size_t Idx = 0; Idx < histograms_.size(); Idx++) {
145 Histogram<uint64_t> &hist = *(histograms_[Idx]);
146 hist.CreateHistogram();
147 hist.PrintConfidenceIntervals(os, 0.99);
148 }
149 os << "Done Dumping histograms \n";
150}
151
152} // namespace art