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