blob: 501d2d7fd2019f59dd0305e6f972d3849c01b2c2 [file] [log] [blame]
Sameer Abu Asala8439542013-02-14 16:06:42 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_TIMING_LOGGER_H_
18#define ART_RUNTIME_BASE_TIMING_LOGGER_H_
Sameer Abu Asala8439542013-02-14 16:06:42 -080019
20#include "base/histogram.h"
21#include "base/macros.h"
22#include "base/mutex.h"
23
24#include <string>
25#include <vector>
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070026#include <map>
Sameer Abu Asala8439542013-02-14 16:06:42 -080027
28namespace art {
29
Ian Rogers1d54e732013-05-02 21:10:01 -070030namespace base {
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070031 class TimingLogger;
Ian Rogers1d54e732013-05-02 21:10:01 -070032} // namespace base
33
Sameer Abu Asala8439542013-02-14 16:06:42 -080034class CumulativeLogger {
Sameer Abu Asala8439542013-02-14 16:06:42 -080035 public:
Ian Rogers45357052013-04-18 20:49:43 -070036 explicit CumulativeLogger(const std::string& name);
Sameer Abu Asala8439542013-02-14 16:06:42 -080037 void prepare_stats();
38 ~CumulativeLogger();
39 void Start();
40 void End();
41 void Reset();
Ian Rogers45357052013-04-18 20:49:43 -070042 void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080043 uint64_t GetTotalNs() const;
Ian Rogers45357052013-04-18 20:49:43 -070044 // 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 Ghuloum6f28d912013-07-24 15:02:53 -070047 void AddLogger(const base::TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070048 size_t GetIterations() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080049
50 private:
Anwar Ghuloum67f99412013-08-12 14:19:48 -070051 typedef std::map<std::string, Histogram<uint64_t> *> Histograms;
52 typedef std::map<std::string, Histogram<uint64_t> *>::const_iterator HistogramsIterator;
53
Sameer Abu Asala8439542013-02-14 16:06:42 -080054 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 Ghuloum4446ab92013-08-09 21:17:25 -070059 Histograms histograms_ GUARDED_BY(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080060 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070061 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080062 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Sameer Abu Asala8439542013-02-14 16:06:42 -080063 size_t iterations_ GUARDED_BY(lock_);
64
65 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
66};
67
Ian Rogers1d54e732013-05-02 21:10:01 -070068namespace base {
69
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070070
71// A timing logger that knows when a split starts for the purposes of logging tools, like systrace.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070072class TimingLogger {
Ian Rogers1d54e732013-05-02 21:10:01 -070073 public:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070074 // Splits are nanosecond times and split names.
75 typedef std::pair<uint64_t, const char*> SplitTiming;
76 typedef std::vector<SplitTiming> SplitTimings;
Anwar Ghuloum67f99412013-08-12 14:19:48 -070077 typedef std::vector<SplitTiming>::const_iterator SplitTimingsIterator;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070078
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070079 explicit TimingLogger(const char* name, bool precise, bool verbose);
Ian Rogers1d54e732013-05-02 21:10:01 -070080
81 // Clears current splits and labels.
82 void Reset();
83
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070084 // Starts a split
85 void StartSplit(const char* new_split_label);
Ian Rogers1d54e732013-05-02 21:10:01 -070086
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 Ghuloum4446ab92013-08-09 21:17:25 -070097 // 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 Ghuloum46543222013-08-12 09:28:42 -0700111 // Resumes timing of the split, usually because a nested split has ended.
112 void Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700113
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 Rogers1d54e732013-05-02 21:10:01 -0700140 return splits_;
141 }
142
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700143 friend class ScopedSplit;
Ian Rogers1d54e732013-05-02 21:10:01 -0700144 protected:
145 // The name of the timing logger.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700146 const char* name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700147
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 Ghuloum4446ab92013-08-09 21:17:25 -0700155 // The current scoped split is also the 'top' of the stack of splits in progress.
156 ScopedSplit* current_split_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700157
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700158 // Splits that have ended.
159 SplitTimings splits_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700160
161 private:
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700162 DISALLOW_COPY_AND_ASSIGN(TimingLogger);
Ian Rogers1d54e732013-05-02 21:10:01 -0700163};
164
165} // namespace base
Sameer Abu Asala8439542013-02-14 16:06:42 -0800166} // namespace art
167
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700168#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_