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