blob: 0f00a046e5d60382070996b12e57f8cfb9c17543 [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>
26
27namespace art {
28
29class CumulativeLogger;
30
31class TimingLogger {
32 public:
Ian Rogers45357052013-04-18 20:49:43 -070033 explicit TimingLogger(const std::string& name, bool precise);
34 void AddSplit(const std::string& label);
35 void Dump(std::ostream& os) const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080036 void Reset();
37 uint64_t GetTotalNs() const;
38
39 protected:
Ian Rogers45357052013-04-18 20:49:43 -070040 const std::string name_;
41 const bool precise_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080042 std::vector<uint64_t> times_;
43 std::vector<std::string> labels_;
44
45 friend class CumulativeLogger;
46};
47
Ian Rogers1d54e732013-05-02 21:10:01 -070048namespace base {
49 class NewTimingLogger;
50} // namespace base
51
Sameer Abu Asala8439542013-02-14 16:06:42 -080052class CumulativeLogger {
Sameer Abu Asala8439542013-02-14 16:06:42 -080053 public:
Ian Rogers45357052013-04-18 20:49:43 -070054 explicit CumulativeLogger(const std::string& name);
Sameer Abu Asala8439542013-02-14 16:06:42 -080055 void prepare_stats();
56 ~CumulativeLogger();
57 void Start();
58 void End();
59 void Reset();
Ian Rogers45357052013-04-18 20:49:43 -070060 void Dump(std::ostream& os) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080061 uint64_t GetTotalNs() const;
Ian Rogers45357052013-04-18 20:49:43 -070062 // Allow the name to be modified, particularly when the cumulative logger is a field within a
63 // parent class that is unable to determine the "name" of a sub-class.
64 void SetName(const std::string& name);
65 void AddLogger(const TimingLogger& logger) LOCKS_EXCLUDED(lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070066 void AddNewLogger(const base::NewTimingLogger& logger) LOCKS_EXCLUDED(lock_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080067
68 private:
Sameer Abu Asala8439542013-02-14 16:06:42 -080069 void AddPair(const std::string &label, uint64_t delta_time)
70 EXCLUSIVE_LOCKS_REQUIRED(lock_);
71 void DumpHistogram(std::ostream &os) EXCLUSIVE_LOCKS_REQUIRED(lock_);
72 uint64_t GetTotalTime() const;
73 static const uint64_t kAdjust = 1000;
74 std::vector<Histogram<uint64_t> *> histograms_ GUARDED_BY(lock_);
75 std::string name_;
Ian Rogers45357052013-04-18 20:49:43 -070076 const std::string lock_name_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080077 mutable Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
78 size_t index_ GUARDED_BY(lock_);
79 size_t iterations_ GUARDED_BY(lock_);
80
81 DISALLOW_COPY_AND_ASSIGN(CumulativeLogger);
82};
83
Ian Rogers1d54e732013-05-02 21:10:01 -070084namespace base {
85
86// A replacement to timing logger that know when a split starts for the purposes of logging.
87// TODO: replace uses of TimingLogger with base::NewTimingLogger.
88class NewTimingLogger {
89 public:
90 explicit NewTimingLogger(const char* name, bool precise, bool verbose);
91
92 // Clears current splits and labels.
93 void Reset();
94
95 // Starts a split, a split shouldn't be in progress.
96 void StartSplit(const char* new_split_label);
97
98 // Ends the current split and starts the one given by the label.
99 void NewSplit(const char* new_split_label);
100
101 // Ends the current split and records the end time.
102 void EndSplit();
103
104 uint64_t GetTotalNs() const;
105
106 void Dump(std::ostream& os) const;
107
108 const std::vector<std::pair<uint64_t, const char*> >& GetSplits() const {
109 return splits_;
110 }
111
112 protected:
113 // The name of the timing logger.
114 const std::string name_;
115
116 // Do we want to print the exactly recorded split (true) or round down to the time unit being
117 // used (false).
118 const bool precise_;
119
120 // Verbose logging.
121 const bool verbose_;
122
123 // The name of the current split.
124 const char* current_split_;
125
126 // The nanosecond time the current split started on.
127 uint64_t current_split_start_ns_;
128
129 // Splits are nanosecond times and split names.
130 std::vector<std::pair<uint64_t, const char*> > splits_;
131
132 private:
133 DISALLOW_COPY_AND_ASSIGN(NewTimingLogger);
134};
135
136} // namespace base
Sameer Abu Asala8439542013-02-14 16:06:42 -0800137} // namespace art
138
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700139#endif // ART_RUNTIME_BASE_TIMING_LOGGER_H_