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 | |
| 17 | #ifndef ART_SRC_TIMING_LOGGER_H_ |
| 18 | #define ART_SRC_TIMING_LOGGER_H_ |
| 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 | |
| 48 | class CumulativeLogger { |
| 49 | |
| 50 | public: |
| 51 | |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 52 | explicit CumulativeLogger(const std::string& name); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 53 | void prepare_stats(); |
| 54 | ~CumulativeLogger(); |
| 55 | void Start(); |
| 56 | void End(); |
| 57 | void Reset(); |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 58 | void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 59 | uint64_t GetTotalNs() const; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 60 | // Allow the name to be modified, particularly when the cumulative logger is a field within a |
| 61 | // parent class that is unable to determine the "name" of a sub-class. |
| 62 | void SetName(const std::string& name); |
| 63 | void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | |
| 67 | void AddPair(const std::string &label, uint64_t delta_time) |
| 68 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 69 | void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 70 | uint64_t GetTotalTime() const; |
| 71 | static const uint64_t kAdjust = 1000; |
| 72 | std::vector<Histogram<uint64_t> *> histograms_ GUARDED_BY(lock_); |
| 73 | std::string name_; |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 74 | const std::string lock_name_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 75 | mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 76 | size_t index_ GUARDED_BY(lock_); |
| 77 | size_t iterations_ GUARDED_BY(lock_); |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(CumulativeLogger); |
| 80 | }; |
| 81 | |
| 82 | } // namespace art |
| 83 | |
| 84 | #endif // ART_SRC_TIMING_LOGGER_H_ |