blob: 9b558980c59710d5ccaca2147c2bd06c13474505 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_TIMING_LOGGER_H_
18#define ART_RUNTIME_BASE_TIMING_LOGGER_H_
Sameer Abu Asala8439542013-02-14 16:06:42 -080019
20#include "base/histogram.h"
21#include "base/macros.h"
22#include "base/mutex.h"
23
Mathieu Chartier19b0a912013-11-20 14:07:54 -080024#include <set>
Sameer Abu Asala8439542013-02-14 16:06:42 -080025#include <string>
26#include <vector>
27
28namespace art {
Ian Rogers5fe9af72013-11-14 00:17:20 -080029class TimingLogger;
Ian Rogers1d54e732013-05-02 21:10:01 -070030
Sameer Abu Asala8439542013-02-14 16:06:42 -080031class CumulativeLogger {
Sameer Abu Asala8439542013-02-14 16:06:42 -080032 public:
Ian Rogers45357052013-04-18 20:49:43 -070033 explicit CumulativeLogger(const std::string& name);
Sameer Abu Asala8439542013-02-14 16:06:42 -080034 ~CumulativeLogger();
35 void Start();
Mathieu Chartierc528dba2013-11-26 12:00:11 -080036 void End() LOCKS_EXCLUDED(lock_);
37 void Reset() LOCKS_EXCLUDED(lock_);
Mathieu Chartierafe49982014-03-27 10:55:04 -070038 void Dump(std::ostream& os) const LOCKS_EXCLUDED(lock_);
39 uint64_t GetTotalNs() const {
40 return GetTotalTime() * kAdjust;
41 }
Ian Rogers45357052013-04-18 20:49:43 -070042 // Allow the name to be modified, particularly when the cumulative logger is a field within a
43 // parent class that is unable to determine the "name" of a sub-class.
Mathieu Chartierc528dba2013-11-26 12:00:11 -080044 void SetName(const std::string& name) LOCKS_EXCLUDED(lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080045 void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070046 size_t GetIterations() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080047
48 private:
Mathieu Chartier19b0a912013-11-20 14:07:54 -080049 class HistogramComparator {
50 public:
51 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
52 return a->Name() < b->Name();
53 }
54 };
55
56 static constexpr size_t kLowMemoryBucketCount = 16;
57 static constexpr size_t kDefaultBucketCount = 100;
58 static constexpr size_t kInitialBucketSize = 50; // 50 microseconds.
Anwar Ghuloum67f99412013-08-12 14:19:48 -070059
Sameer Abu Asala8439542013-02-14 16:06:42 -080060 void AddPair(const std::string &label, uint64_t delta_time)
61 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartierafe49982014-03-27 10:55:04 -070062 void DumpHistogram(std::ostream &os) const EXCLUSIVE_LOCKS_REQUIRED(lock_);
63 uint64_t GetTotalTime() const {
64 return total_time_;
65 }
Sameer Abu Asala8439542013-02-14 16:06:42 -080066 static const uint64_t kAdjust = 1000;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080067 std::set<Histogram<uint64_t>*, HistogramComparator> histograms_ GUARDED_BY(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080068 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070069 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080070 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Sameer Abu Asala8439542013-02-14 16:06:42 -080071 size_t iterations_ GUARDED_BY(lock_);
Mathieu Chartierafe49982014-03-27 10:55:04 -070072 uint64_t total_time_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080073
74 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
75};
76
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070077// A timing logger that knows when a split starts for the purposes of logging tools, like systrace.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070078class TimingLogger {
Ian Rogers1d54e732013-05-02 21:10:01 -070079 public:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070080 // Splits are nanosecond times and split names.
81 typedef std::pair<uint64_t, const char*> SplitTiming;
82 typedef std::vector<SplitTiming> SplitTimings;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070083
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070084 explicit TimingLogger(const char* name, bool precise, bool verbose);
Ian Rogers5fe9af72013-11-14 00:17:20 -080085 ~TimingLogger() {
86 // TODO: DCHECK(current_split_ == nullptr) << "Forgot to end split: " << current_split_->label_;
87 }
Ian Rogers1d54e732013-05-02 21:10:01 -070088 // Clears current splits and labels.
89 void Reset();
90
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070091 // Starts a split
92 void StartSplit(const char* new_split_label);
Ian Rogers1d54e732013-05-02 21:10:01 -070093
94 // Ends the current split and starts the one given by the label.
95 void NewSplit(const char* new_split_label);
96
97 // Ends the current split and records the end time.
98 void EndSplit();
99
100 uint64_t GetTotalNs() const;
101
102 void Dump(std::ostream& os) const;
103
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700104 // Scoped timing splits that can be nested and composed with the explicit split
105 // starts and ends.
106 class ScopedSplit {
107 public:
108 explicit ScopedSplit(const char* label, TimingLogger* timing_logger);
109
110 ~ScopedSplit();
111
112 friend class TimingLogger;
113
114 private:
115 // Pauses timing of the split, usually due to nesting of another split.
116 void Pause();
117
Anwar Ghuloum46543222013-08-12 09:28:42 -0700118 // Resumes timing of the split, usually because a nested split has ended.
119 void Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700120
121 // Used by new split to swap splits in place in a ScopedSplit instance.
122 void TailInsertSplit(const char* label);
123
124 // The scoped split immediately enclosing this split. Essentially, we get a
125 // stack of nested splits through this field.
126 ScopedSplit* enclosing_split_;
127
128 // Was this created via TimingLogger's StartSplit?
129 bool explicit_;
130
131 // The split's name.
132 const char* label_;
133
134 // The current split's latest start time. (It may have been paused and restarted.)
135 uint64_t start_ns_;
136
137 // The running time, outside of pauses.
138 uint64_t running_ns_;
139
140 // The timing logger holding this split.
141 TimingLogger* timing_logger_;
142
143 DISALLOW_COPY_AND_ASSIGN(ScopedSplit);
144 };
145
146 const SplitTimings& GetSplits() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700147 return splits_;
148 }
149
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700150 friend class ScopedSplit;
Ian Rogers1d54e732013-05-02 21:10:01 -0700151 protected:
152 // The name of the timing logger.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800153 const char* const name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700154
155 // Do we want to print the exactly recorded split (true) or round down to the time unit being
156 // used (false).
157 const bool precise_;
158
159 // Verbose logging.
160 const bool verbose_;
161
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700162 // The current scoped split is also the 'top' of the stack of splits in progress.
163 ScopedSplit* current_split_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700164
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700165 // Splits that have ended.
166 SplitTimings splits_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700167
168 private:
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700169 DISALLOW_COPY_AND_ASSIGN(TimingLogger);
Ian Rogers1d54e732013-05-02 21:10:01 -0700170};
171
Sameer Abu Asala8439542013-02-14 16:06:42 -0800172} // namespace art
173
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700174#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_