blob: f1f78557aae145311a2e2c16964023b454f5eafb [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 {
Ian Rogers5fe9af72013-11-14 00:17:20 -080029class TimingLogger;
Ian Rogers1d54e732013-05-02 21:10:01 -070030
Sameer Abu Asala8439542013-02-14 16:06:42 -080031class CumulativeLogger {
Sameer Abu Asala8439542013-02-14 16:06:42 -080032 public:
Ian Rogers45357052013-04-18 20:49:43 -070033 explicit CumulativeLogger(const std::string& name);
Sameer Abu Asala8439542013-02-14 16:06:42 -080034 void prepare_stats();
35 ~CumulativeLogger();
36 void Start();
37 void End();
38 void Reset();
Ian Rogers45357052013-04-18 20:49:43 -070039 void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080040 uint64_t GetTotalNs() const;
Ian Rogers45357052013-04-18 20:49:43 -070041 // Allow the name to be modified, particularly when the cumulative logger is a field within a
42 // parent class that is unable to determine the "name" of a sub-class.
43 void SetName(const std::string& name);
Ian Rogers5fe9af72013-11-14 00:17:20 -080044 void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -070045 size_t GetIterations() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080046
47 private:
Anwar Ghuloum67f99412013-08-12 14:19:48 -070048 typedef std::map<std::string, Histogram<uint64_t> *> Histograms;
49 typedef std::map<std::string, Histogram<uint64_t> *>::const_iterator HistogramsIterator;
50
Sameer Abu Asala8439542013-02-14 16:06:42 -080051 void AddPair(const std::string &label, uint64_t delta_time)
52 EXCLUSIVE_LOCKS_REQUIRED(lock_);
53 void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_);
54 uint64_t GetTotalTime() const;
55 static const uint64_t kAdjust = 1000;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070056 Histograms histograms_ GUARDED_BY(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080057 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070058 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080059 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Sameer Abu Asala8439542013-02-14 16:06:42 -080060 size_t iterations_ GUARDED_BY(lock_);
61
62 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
63};
64
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070065// A timing logger that knows when a split starts for the purposes of logging tools, like systrace.
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070066class TimingLogger {
Ian Rogers1d54e732013-05-02 21:10:01 -070067 public:
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070068 // Splits are nanosecond times and split names.
69 typedef std::pair<uint64_t, const char*> SplitTiming;
70 typedef std::vector<SplitTiming> SplitTimings;
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070071
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070072 explicit TimingLogger(const char* name, bool precise, bool verbose);
Ian Rogers5fe9af72013-11-14 00:17:20 -080073 ~TimingLogger() {
74 // TODO: DCHECK(current_split_ == nullptr) << "Forgot to end split: " << current_split_->label_;
75 }
Ian Rogers1d54e732013-05-02 21:10:01 -070076 // Clears current splits and labels.
77 void Reset();
78
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070079 // Starts a split
80 void StartSplit(const char* new_split_label);
Ian Rogers1d54e732013-05-02 21:10:01 -070081
82 // Ends the current split and starts the one given by the label.
83 void NewSplit(const char* new_split_label);
84
85 // Ends the current split and records the end time.
86 void EndSplit();
87
88 uint64_t GetTotalNs() const;
89
90 void Dump(std::ostream& os) const;
91
Anwar Ghuloum4446ab92013-08-09 21:17:25 -070092 // Scoped timing splits that can be nested and composed with the explicit split
93 // starts and ends.
94 class ScopedSplit {
95 public:
96 explicit ScopedSplit(const char* label, TimingLogger* timing_logger);
97
98 ~ScopedSplit();
99
100 friend class TimingLogger;
101
102 private:
103 // Pauses timing of the split, usually due to nesting of another split.
104 void Pause();
105
Anwar Ghuloum46543222013-08-12 09:28:42 -0700106 // Resumes timing of the split, usually because a nested split has ended.
107 void Resume();
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700108
109 // Used by new split to swap splits in place in a ScopedSplit instance.
110 void TailInsertSplit(const char* label);
111
112 // The scoped split immediately enclosing this split. Essentially, we get a
113 // stack of nested splits through this field.
114 ScopedSplit* enclosing_split_;
115
116 // Was this created via TimingLogger's StartSplit?
117 bool explicit_;
118
119 // The split's name.
120 const char* label_;
121
122 // The current split's latest start time. (It may have been paused and restarted.)
123 uint64_t start_ns_;
124
125 // The running time, outside of pauses.
126 uint64_t running_ns_;
127
128 // The timing logger holding this split.
129 TimingLogger* timing_logger_;
130
131 DISALLOW_COPY_AND_ASSIGN(ScopedSplit);
132 };
133
134 const SplitTimings& GetSplits() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700135 return splits_;
136 }
137
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700138 friend class ScopedSplit;
Ian Rogers1d54e732013-05-02 21:10:01 -0700139 protected:
140 // The name of the timing logger.
Ian Rogers5fe9af72013-11-14 00:17:20 -0800141 const char* const name_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700142
143 // Do we want to print the exactly recorded split (true) or round down to the time unit being
144 // used (false).
145 const bool precise_;
146
147 // Verbose logging.
148 const bool verbose_;
149
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700150 // The current scoped split is also the 'top' of the stack of splits in progress.
151 ScopedSplit* current_split_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700152
Anwar Ghuloum4446ab92013-08-09 21:17:25 -0700153 // Splits that have ended.
154 SplitTimings splits_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700155
156 private:
Anwar Ghuloum6f28d912013-07-24 15:02:53 -0700157 DISALLOW_COPY_AND_ASSIGN(TimingLogger);
Ian Rogers1d54e732013-05-02 21:10:01 -0700158};
159
Sameer Abu Asala8439542013-02-14 16:06:42 -0800160} // namespace art
161
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700162#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_