Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1093)

Side by Side Diff: statsreport/metrics.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « statsreport/metrics.h ('k') | statsreport/metrics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2006-2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15 //
16 // Implements metrics and metrics collections
17 #include "omaha/statsreport/metrics.h"
18 #include "omaha/base/synchronized.h"
19
20 namespace stats_report {
21 // Make sure global stats collection is placed in zeroed storage so as to avoid
22 // initialization order snafus.
23 MetricCollectionBase g_global_metric_storage = { 0, 0 };
24 MetricCollection &g_global_metrics =
25 *static_cast<MetricCollection*>(&g_global_metric_storage);
26
27 #pragma warning(push)
28 // C4640: construction of local static object is not thread-safe.
29 // C4073: initializers put in library initialization area.
30 #pragma warning(disable : 4640 4073)
31
32 // Serialize all metric manipulation and access under this lock.
33 //
34 // Initializes g_lock before other global objects of user defined types.
35 // It assumes the program is single threaded while executing CRT startup and
36 // exit code.
37 #pragma init_seg(lib)
38 omaha::LLock g_lock;
39 #pragma warning(pop)
40
41 class MetricBase::ObjectLock {
42 public:
43 ObjectLock(const MetricBase *metric) : metric_(metric) {
44 metric_->Lock();
45 }
46
47 ~ObjectLock() {
48 metric_->Unlock();
49 }
50
51 private:
52 MetricBase const *const metric_;
53 DISALLOW_EVIL_CONSTRUCTORS(MetricBase::ObjectLock);
54 };
55
56 void MetricBase::Lock() const {
57 g_lock.Lock();
58 }
59
60 void MetricBase::Unlock() const {
61 g_lock.Unlock();
62 }
63
64 MetricBase::MetricBase(const char *name,
65 MetricType type,
66 MetricCollectionBase *coll)
67 : name_(name), type_(type), next_(coll->first_), coll_(coll) {
68 DCHECK_NE(static_cast<MetricCollectionBase*>(NULL), coll_);
69 DCHECK_EQ(false, coll_->initialized_);
70 coll->first_ = this;
71 }
72
73 MetricBase::MetricBase(const char *name, MetricType type)
74 : name_(name), type_(type), next_(NULL), coll_(NULL) {
75 }
76
77 MetricBase::~MetricBase() {
78 if (coll_) {
79 DCHECK_EQ(this, coll_->first_);
80 DCHECK(!coll_->initialized_)
81 << "Metric destructor called without call to Uninitialize().";
82
83 coll_->first_ = next_;
84 } else {
85 DCHECK(NULL == next_);
86 }
87 }
88
89 void IntegerMetricBase::Set(int64 value) {
90 ObjectLock lock(this);
91 value_ = value;
92 }
93
94 int64 IntegerMetricBase::value() const {
95 ObjectLock lock(this);
96 int64 ret = value_;
97 return ret;
98 }
99
100 void IntegerMetricBase::Increment() {
101 ObjectLock lock(this);
102 ++value_;
103 }
104
105 void IntegerMetricBase::Decrement() {
106 ObjectLock lock(this);
107 --value_;
108 }
109
110 void IntegerMetricBase::Add(int64 value){
111 ObjectLock lock(this);
112 value_ += value;
113 }
114
115 void IntegerMetricBase::Subtract(int64 value) {
116 ObjectLock lock(this);
117 if (value_ < value)
118 value_ = 0;
119 else
120 value_ -= value;
121 }
122
123 int64 CountMetric::Reset() {
124 ObjectLock lock(this);
125 int64 ret = value_;
126 value_ = 0;
127 return ret;
128 }
129
130 TimingMetric::TimingData TimingMetric::Reset() {
131 ObjectLock lock(this);
132 TimingData ret = data_;
133 Clear();
134 return ret;
135 }
136
137 uint32 TimingMetric::count() const {
138 ObjectLock lock(this);
139 uint32 ret = data_.count;
140 return ret;
141 }
142
143 int64 TimingMetric::sum() const {
144 ObjectLock lock(this);
145 int64 ret = data_.sum;
146 return ret;
147 }
148
149 int64 TimingMetric::minimum() const {
150 ObjectLock lock(this);
151 int64 ret = data_.minimum;
152 return ret;
153 }
154
155 int64 TimingMetric::maximum() const {
156 ObjectLock lock(this);
157 int64 ret = data_.maximum;
158 return ret;
159 }
160
161 int64 TimingMetric::average() const {
162 ObjectLock lock(this);
163
164 int64 ret = 0;
165 if (0 == data_.count) {
166 DCHECK_EQ(0, data_.sum);
167 } else {
168 ret = data_.sum / data_.count;
169 }
170 return ret;
171 }
172
173 void TimingMetric::AddSample(int64 time_ms) {
174 ObjectLock lock(this);
175 if (0 == data_.count) {
176 data_.minimum = time_ms;
177 data_.maximum = time_ms;
178 } else {
179 if (data_.minimum > time_ms)
180 data_.minimum = time_ms;
181 if (data_.maximum < time_ms)
182 data_.maximum = time_ms;
183 }
184 data_.count++;
185 data_.sum += time_ms;
186 }
187
188 void TimingMetric::AddSamples(int64 count, int64 total_time_ms) {
189 if (0 == count)
190 return;
191
192 int64 time_ms = total_time_ms / count;
193
194 ObjectLock lock(this);
195 if (0 == data_.count) {
196 data_.minimum = time_ms;
197 data_.maximum = time_ms;
198 } else {
199 if (data_.minimum > time_ms)
200 data_.minimum = time_ms;
201 if (data_.maximum < time_ms)
202 data_.maximum = time_ms;
203 }
204
205 // TODO(omaha): truncation from 64 to 32 may occur here.
206 DCHECK_LE(count, kuint32max);
207 data_.count += static_cast<uint32>(count);
208 data_.sum += total_time_ms;
209 }
210
211 void TimingMetric::Clear() {
212 memset(&data_, 0, sizeof(data_));
213 }
214
215 void BoolMetric::Set(bool value) {
216 ObjectLock lock(this);
217 value_ = value ? kBoolTrue : kBoolFalse;
218 }
219
220 BoolMetric::TristateBoolValue BoolMetric::Reset() {
221 ObjectLock lock(this);
222 TristateBoolValue ret = value_;
223 value_ = kBoolUnset;
224 return ret;
225 }
226
227 void MetricCollection::Initialize() {
228 DCHECK(!initialized());
229 initialized_ = true;
230 }
231
232 void MetricCollection::Uninitialize() {
233 DCHECK(initialized());
234 initialized_ = false;
235 }
236
237
238 } // namespace stats_report
OLDNEW
« no previous file with comments | « statsreport/metrics.h ('k') | statsreport/metrics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698