blob: b0bcf107ae661dc1804e81eba8fab4687799b986 [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_);
Ian Rogers45357052013-04-18 20:49:43 -070038 void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080039 uint64_t GetTotalNs() const;
Ian Rogers45357052013-04-18 20:49:43 -070040 // Allow the name to be modified, particularly when the cumulative logger is a field within a
41 // parent class that is unable to determine the "name" of a sub-class.
Mathieu Chartierc528dba2013-11-26 12:00:11 -080042 void SetName(const std::string& name) LOCKS_EXCLUDED(lock_);
Ian Rogers5fe9af72013-11-14 00:17:20 -080043 void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070044 size_t GetIterations() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080045
46 private:
Mathieu Chartier19b0a912013-11-20 14:07:54 -080047 class HistogramComparator {
48 public:
49 bool operator()(const Histogram<uint64_t>* a, const Histogram<uint64_t>* b) const {
50 return a->Name() < b->Name();
51 }
52 };
53
54 static constexpr size_t kLowMemoryBucketCount = 16;
55 static constexpr size_t kDefaultBucketCount = 100;
56 static constexpr size_t kInitialBucketSize = 50; // 50 microseconds.
Anwar Ghuloum67f99412013-08-12 14:19:48 -070057
Sameer Abu Asala8439542013-02-14 16:06:42 -080058 void AddPair(const std::string &label, uint64_t delta_time)
59 EXCLUSIVE_LOCKS_REQUIRED(lock_);
60 void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_);
61 uint64_t GetTotalTime() const;
62 static const uint64_t kAdjust = 1000;
Mathieu Chartier19b0a912013-11-20 14:07:54 -080063 std::set<Histogram<uint64_t>*, HistogramComparator> histograms_ GUARDED_BY(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080064 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070065 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080066 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Sameer Abu Asala8439542013-02-14 16:06:42 -080067 size_t iterations_ GUARDED_BY(lock_);
68
69 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
70};
71
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070072// A timing logger that knows when a split starts for the purposes of logging tools, like systrace.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070073class TimingLogger {
Ian Rogers1d54e732013-05-02 21:10:01 -070074 public:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070075 // Splits are nanosecond times and split names.
76 typedef std::pair<uint64_t, const char*> SplitTiming;
77 typedef std::vector<SplitTiming> SplitTimings;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070078
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070079 explicit TimingLogger(const char* name, bool precise, bool verbose);
Ian Rogers5fe9af72013-11-14 00:17:20 -080080 ~TimingLogger() {
81 // TODO: DCHECK(current_split_ == nullptr) << "Forgot to end split: " << current_split_->label_;
82 }
Ian Rogers1d54e732013-05-02 21:10:01 -070083 // Clears current splits and labels.
84 void Reset();
85
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070086 // Starts a split
87 void StartSplit(const char* new_split_label);
Ian Rogers1d54e732013-05-02 21:10:01 -070088
89 // Ends the current split and starts the one given by the label.
90 void NewSplit(const char* new_split_label);
91
92 // Ends the current split and records the end time.
93 void EndSplit();
94
95 uint64_t GetTotalNs() const;
96
97 void Dump(std::ostream& os) const;
98
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070099 // Scoped timing splits that can be nested and composed with the explicit split
100 // starts and ends.
101 class ScopedSplit {
102 public:
103 explicit ScopedSplit(const char* label, TimingLogger* timing_logger);
104
105 ~ScopedSplit();
106
107 friend class TimingLogger;
108
109 private:
110 // Pauses timing of the split, usually due to nesting of another split.
111 void Pause();
112
Anwar Ghuloum46543222013-08-12 09:28:42 -0700113 // Resumes timing of the split, usually because a nested split has ended.
114 void Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700115
116 // Used by new split to swap splits in place in a ScopedSplit instance.
117 void TailInsertSplit(const char* label);
118
119 // The scoped split immediately enclosing this split. Essentially, we get a
120 // stack of nested splits through this field.
121 ScopedSplit* enclosing_split_;
122
123 // Was this created via TimingLogger's StartSplit?
124 bool explicit_;
125
126 // The split's name.
127 const char* label_;
128
129 // The current split's latest start time. (It may have been paused and restarted.)
130 uint64_t start_ns_;
131
132 // The running time, outside of pauses.
133 uint64_t running_ns_;
134
135 // The timing logger holding this split.
136 TimingLogger* timing_logger_;
137
138 DISALLOW_COPY_AND_ASSIGN(ScopedSplit);
139 };
140
141 const SplitTimings& GetSplits() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700142 return splits_;
143 }
144
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700145 friend class ScopedSplit;
Ian Rogers1d54e732013-05-02 21:10:01 -0700146 protected:
147 // The name of the timing logger.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800148 const char* const name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700149
150 // Do we want to print the exactly recorded split (true) or round down to the time unit being
151 // used (false).
152 const bool precise_;
153
154 // Verbose logging.
155 const bool verbose_;
156
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700157 // The current scoped split is also the 'top' of the stack of splits in progress.
158 ScopedSplit* current_split_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700159
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700160 // Splits that have ended.
161 SplitTimings splits_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700162
163 private:
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700164 DISALLOW_COPY_AND_ASSIGN(TimingLogger);
Ian Rogers1d54e732013-05-02 21:10:01 -0700165};
166
Sameer Abu Asala8439542013-02-14 16:06:42 -0800167} // namespace art
168
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700169#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_