blob: 777d3f09aeb4102df9bf3510a72efb339ab2e118 [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:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070036 typedef std::map<std::string, Histogram<uint64_t> *> Histograms;
37 typedef std::map<std::string, Histogram<uint64_t> *>::const_iterator HistogramsIterator;
38
Ian Rogers45357052013-04-18 20:49:43 -070039 explicit CumulativeLogger(const std::string& name);
Sameer Abu Asala8439542013-02-14 16:06:42 -080040 void prepare_stats();
41 ~CumulativeLogger();
42 void Start();
43 void End();
44 void Reset();
Ian Rogers45357052013-04-18 20:49:43 -070045 void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080046 uint64_t GetTotalNs() const;
Ian Rogers45357052013-04-18 20:49:43 -070047 // Allow the name to be modified, particularly when the cumulative logger is a field within a
48 // parent class that is unable to determine the "name" of a sub-class.
49 void SetName(const std::string& name);
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070050 void AddLogger(const base::TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080051
52 private:
Sameer Abu Asala8439542013-02-14 16:06:42 -080053 void AddPair(const std::string &label, uint64_t delta_time)
54 EXCLUSIVE_LOCKS_REQUIRED(lock_);
55 void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_);
56 uint64_t GetTotalTime() const;
57 static const uint64_t kAdjust = 1000;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070058 Histograms histograms_ GUARDED_BY(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080059 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070060 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080061 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Sameer Abu Asala8439542013-02-14 16:06:42 -080062 size_t iterations_ GUARDED_BY(lock_);
63
64 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
65};
66
Ian Rogers1d54e732013-05-02 21:10:01 -070067namespace base {
68
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070069
70// A timing logger that knows when a split starts for the purposes of logging tools, like systrace.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070071class TimingLogger {
Ian Rogers1d54e732013-05-02 21:10:01 -070072 public:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070073 // Splits are nanosecond times and split names.
74 typedef std::pair<uint64_t, const char*> SplitTiming;
75 typedef std::vector<SplitTiming> SplitTimings;
76 typedef std::vector<SplitTiming>::const_iterator SplitsIterator;
77
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070078 explicit TimingLogger(const char* name, bool precise, bool verbose);
Ian Rogers1d54e732013-05-02 21:10:01 -070079
80 // Clears current splits and labels.
81 void Reset();
82
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070083 // Starts a split
84 void StartSplit(const char* new_split_label);
Ian Rogers1d54e732013-05-02 21:10:01 -070085
86 // Ends the current split and starts the one given by the label.
87 void NewSplit(const char* new_split_label);
88
89 // Ends the current split and records the end time.
90 void EndSplit();
91
92 uint64_t GetTotalNs() const;
93
94 void Dump(std::ostream& os) const;
95
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070096 // Scoped timing splits that can be nested and composed with the explicit split
97 // starts and ends.
98 class ScopedSplit {
99 public:
100 explicit ScopedSplit(const char* label, TimingLogger* timing_logger);
101
102 ~ScopedSplit();
103
104 friend class TimingLogger;
105
106 private:
107 // Pauses timing of the split, usually due to nesting of another split.
108 void Pause();
109
Anwar Ghuloum46543222013-08-12 09:28:42 -0700110 // Resumes timing of the split, usually because a nested split has ended.
111 void Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700112
113 // Used by new split to swap splits in place in a ScopedSplit instance.
114 void TailInsertSplit(const char* label);
115
116 // The scoped split immediately enclosing this split. Essentially, we get a
117 // stack of nested splits through this field.
118 ScopedSplit* enclosing_split_;
119
120 // Was this created via TimingLogger's StartSplit?
121 bool explicit_;
122
123 // The split's name.
124 const char* label_;
125
126 // The current split's latest start time. (It may have been paused and restarted.)
127 uint64_t start_ns_;
128
129 // The running time, outside of pauses.
130 uint64_t running_ns_;
131
132 // The timing logger holding this split.
133 TimingLogger* timing_logger_;
134
135 DISALLOW_COPY_AND_ASSIGN(ScopedSplit);
136 };
137
138 const SplitTimings& GetSplits() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 return splits_;
140 }
141
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700142 friend class ScopedSplit;
Ian Rogers1d54e732013-05-02 21:10:01 -0700143 protected:
144 // The name of the timing logger.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700145 const char* name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700146
147 // Do we want to print the exactly recorded split (true) or round down to the time unit being
148 // used (false).
149 const bool precise_;
150
151 // Verbose logging.
152 const bool verbose_;
153
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700154 // The current scoped split is also the 'top' of the stack of splits in progress.
155 ScopedSplit* current_split_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700156
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700157 // Splits that have ended.
158 SplitTimings splits_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700159
160 private:
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700161 DISALLOW_COPY_AND_ASSIGN(TimingLogger);
Ian Rogers1d54e732013-05-02 21:10:01 -0700162};
163
164} // namespace base
Sameer Abu Asala8439542013-02-14 16:06:42 -0800165} // namespace art
166
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700167#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_