Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_TIMING_LOGGER_H_ |
| 18 | #define ART_RUNTIME_BASE_TIMING_LOGGER_H_ |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 19 | |
| 20 | #include "base/histogram.h" |
| 21 | #include "base/macros.h" |
| 22 | #include "base/mutex.h" |
| 23 | |
| 24 | #include <string> |
| 25 | #include <vector> |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 26 | #include <map> |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 30 | namespace base { |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 31 | class TimingLogger; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | } // namespace base |
| 33 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 34 | class CumulativeLogger { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 35 | public: |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 36 | explicit CumulativeLogger(const std::string& name); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 37 | void prepare_stats(); |
| 38 | ~CumulativeLogger(); |
| 39 | void Start(); |
| 40 | void End(); |
| 41 | void Reset(); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 42 | void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 43 | uint64_t GetTotalNs() const; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 44 | // Allow the name to be modified, particularly when the cumulative logger is a field within a |
| 45 | // parent class that is unable to determine the "name" of a sub-class. |
| 46 | void SetName(const std::string& name); |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 47 | void AddLogger(const base::TimingLogger& logger) LOCKS_EXCLUDED(lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame^] | 48 | size_t GetIterations() const; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 49 | |
| 50 | private: |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 51 | typedef std::map<std::string, Histogram<uint64_t> *> Histograms; |
| 52 | typedef std::map<std::string, Histogram<uint64_t> *>::const_iterator HistogramsIterator; |
| 53 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 54 | void AddPair(const std::string &label, uint64_t delta_time) |
| 55 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 56 | void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 57 | uint64_t GetTotalTime() const; |
| 58 | static const uint64_t kAdjust = 1000; |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 59 | Histograms histograms_ GUARDED_BY(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 60 | std::string name_; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 61 | const std::string lock_name_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 62 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 63 | size_t iterations_ GUARDED_BY(lock_); |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(CumulativeLogger); |
| 66 | }; |
| 67 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | namespace base { |
| 69 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 70 | |
| 71 | // A timing logger that knows when a split starts for the purposes of logging tools, like systrace. |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 72 | class TimingLogger { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 73 | public: |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 74 | // Splits are nanosecond times and split names. |
| 75 | typedef std::pair<uint64_t, const char*> SplitTiming; |
| 76 | typedef std::vector<SplitTiming> SplitTimings; |
Anwar Ghuloum | 67f9941 | 2013-08-12 14:19:48 -0700 | [diff] [blame] | 77 | typedef std::vector<SplitTiming>::const_iterator SplitTimingsIterator; |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 78 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 79 | explicit TimingLogger(const char* name, bool precise, bool verbose); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 80 | |
| 81 | // Clears current splits and labels. |
| 82 | void Reset(); |
| 83 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 84 | // Starts a split |
| 85 | void StartSplit(const char* new_split_label); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 86 | |
| 87 | // Ends the current split and starts the one given by the label. |
| 88 | void NewSplit(const char* new_split_label); |
| 89 | |
| 90 | // Ends the current split and records the end time. |
| 91 | void EndSplit(); |
| 92 | |
| 93 | uint64_t GetTotalNs() const; |
| 94 | |
| 95 | void Dump(std::ostream& os) const; |
| 96 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 97 | // Scoped timing splits that can be nested and composed with the explicit split |
| 98 | // starts and ends. |
| 99 | class ScopedSplit { |
| 100 | public: |
| 101 | explicit ScopedSplit(const char* label, TimingLogger* timing_logger); |
| 102 | |
| 103 | ~ScopedSplit(); |
| 104 | |
| 105 | friend class TimingLogger; |
| 106 | |
| 107 | private: |
| 108 | // Pauses timing of the split, usually due to nesting of another split. |
| 109 | void Pause(); |
| 110 | |
Anwar Ghuloum | 4654322 | 2013-08-12 09:28:42 -0700 | [diff] [blame] | 111 | // Resumes timing of the split, usually because a nested split has ended. |
| 112 | void Resume(); |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 113 | |
| 114 | // Used by new split to swap splits in place in a ScopedSplit instance. |
| 115 | void TailInsertSplit(const char* label); |
| 116 | |
| 117 | // The scoped split immediately enclosing this split. Essentially, we get a |
| 118 | // stack of nested splits through this field. |
| 119 | ScopedSplit* enclosing_split_; |
| 120 | |
| 121 | // Was this created via TimingLogger's StartSplit? |
| 122 | bool explicit_; |
| 123 | |
| 124 | // The split's name. |
| 125 | const char* label_; |
| 126 | |
| 127 | // The current split's latest start time. (It may have been paused and restarted.) |
| 128 | uint64_t start_ns_; |
| 129 | |
| 130 | // The running time, outside of pauses. |
| 131 | uint64_t running_ns_; |
| 132 | |
| 133 | // The timing logger holding this split. |
| 134 | TimingLogger* timing_logger_; |
| 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(ScopedSplit); |
| 137 | }; |
| 138 | |
| 139 | const SplitTimings& GetSplits() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 140 | return splits_; |
| 141 | } |
| 142 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 143 | friend class ScopedSplit; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 144 | protected: |
| 145 | // The name of the timing logger. |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 146 | const char* name_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 147 | |
| 148 | // Do we want to print the exactly recorded split (true) or round down to the time unit being |
| 149 | // used (false). |
| 150 | const bool precise_; |
| 151 | |
| 152 | // Verbose logging. |
| 153 | const bool verbose_; |
| 154 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 155 | // The current scoped split is also the 'top' of the stack of splits in progress. |
| 156 | ScopedSplit* current_split_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 157 | |
Anwar Ghuloum | 4446ab9 | 2013-08-09 21:17:25 -0700 | [diff] [blame] | 158 | // Splits that have ended. |
| 159 | SplitTimings splits_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 160 | |
| 161 | private: |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 162 | DISALLOW_COPY_AND_ASSIGN(TimingLogger); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace base |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 166 | } // namespace art |
| 167 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 168 | #endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_ |