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> |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class CumulativeLogger; |
| 30 | |
| 31 | class TimingLogger { |
| 32 | public: |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 33 | explicit TimingLogger(const std::string& name, bool precise); |
| 34 | void AddSplit(const std::string& label); |
| 35 | void Dump(std::ostream& os) const; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 36 | void Reset(); |
| 37 | uint64_t GetTotalNs() const; |
| 38 | |
| 39 | protected: |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 40 | const std::string name_; |
| 41 | const bool precise_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 42 | std::vector<uint64_t> times_; |
| 43 | std::vector<std::string> labels_; |
| 44 | |
| 45 | friend class CumulativeLogger; |
| 46 | }; |
| 47 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 48 | namespace base { |
| 49 | class NewTimingLogger; |
| 50 | } // namespace base |
| 51 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 52 | class CumulativeLogger { |
| 53 | |
| 54 | public: |
| 55 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 56 | explicit CumulativeLogger(const std::string& name); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 57 | void prepare_stats(); |
| 58 | ~CumulativeLogger(); |
| 59 | void Start(); |
| 60 | void End(); |
| 61 | void Reset(); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 62 | void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 63 | uint64_t GetTotalNs() const; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 64 | // Allow the name to be modified, particularly when the cumulative logger is a field within a |
| 65 | // parent class that is unable to determine the "name" of a sub-class. |
| 66 | void SetName(const std::string& name); |
| 67 | void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | void AddNewLogger(const base::NewTimingLogger& logger) LOCKS_EXCLUDED(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | |
| 72 | void AddPair(const std::string &label, uint64_t delta_time) |
| 73 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 74 | void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 75 | uint64_t GetTotalTime() const; |
| 76 | static const uint64_t kAdjust = 1000; |
| 77 | std::vector<Histogram<uint64_t> *> histograms_ GUARDED_BY(lock_); |
| 78 | std::string name_; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 79 | const std::string lock_name_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 80 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 81 | size_t index_ GUARDED_BY(lock_); |
| 82 | size_t iterations_ GUARDED_BY(lock_); |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(CumulativeLogger); |
| 85 | }; |
| 86 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | namespace base { |
| 88 | |
| 89 | // A replacement to timing logger that know when a split starts for the purposes of logging. |
| 90 | // TODO: replace uses of TimingLogger with base::NewTimingLogger. |
| 91 | class NewTimingLogger { |
| 92 | public: |
| 93 | explicit NewTimingLogger(const char* name, bool precise, bool verbose); |
| 94 | |
| 95 | // Clears current splits and labels. |
| 96 | void Reset(); |
| 97 | |
| 98 | // Starts a split, a split shouldn't be in progress. |
| 99 | void StartSplit(const char* new_split_label); |
| 100 | |
| 101 | // Ends the current split and starts the one given by the label. |
| 102 | void NewSplit(const char* new_split_label); |
| 103 | |
| 104 | // Ends the current split and records the end time. |
| 105 | void EndSplit(); |
| 106 | |
| 107 | uint64_t GetTotalNs() const; |
| 108 | |
| 109 | void Dump(std::ostream& os) const; |
| 110 | |
| 111 | const std::vector<std::pair<uint64_t, const char*> >& GetSplits() const { |
| 112 | return splits_; |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | // The name of the timing logger. |
| 117 | const std::string name_; |
| 118 | |
| 119 | // Do we want to print the exactly recorded split (true) or round down to the time unit being |
| 120 | // used (false). |
| 121 | const bool precise_; |
| 122 | |
| 123 | // Verbose logging. |
| 124 | const bool verbose_; |
| 125 | |
| 126 | // The name of the current split. |
| 127 | const char* current_split_; |
| 128 | |
| 129 | // The nanosecond time the current split started on. |
| 130 | uint64_t current_split_start_ns_; |
| 131 | |
| 132 | // Splits are nanosecond times and split names. |
| 133 | std::vector<std::pair<uint64_t, const char*> > splits_; |
| 134 | |
| 135 | private: |
| 136 | DISALLOW_COPY_AND_ASSIGN(NewTimingLogger); |
| 137 | }; |
| 138 | |
| 139 | } // namespace base |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 140 | } // namespace art |
| 141 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame^] | 142 | #endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_ |