Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
statistics.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
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 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_STATISTICS_H_
20 #define PAGESPEED_KERNEL_BASE_STATISTICS_H_
21 
22 #include <map>
23 
24 #include "base/logging.h"
30 #include "pagespeed/kernel/base/thread_annotations.h"
31 
32 namespace net_instaweb {
33 
34 class MessageHandler;
35 class Statistics;
36 class StatisticsLogger;
37 class Writer;
38 
43 class Variable {
44  public:
45  virtual ~Variable();
46 
47  virtual int64 Get() const = 0;
50  virtual StringPiece GetName() const = 0;
51 
53  int64 Add(int64 non_negative_delta) {
54  DCHECK_LE(0, non_negative_delta);
55  return AddHelper(non_negative_delta);
56  }
57 
58  virtual void Clear() = 0;
59 
60  protected:
62  virtual int64 AddHelper(int64 delta) = 0;
63 };
64 
74  public:
75  virtual ~UpDownCounter();
76 
77  virtual int64 Get() const = 0;
80  virtual StringPiece GetName() const = 0;
81 
90  virtual int64 SetReturningPreviousValue(int64 value);
91 
92  virtual void Set(int64 value) = 0;
93  void Clear() { Set(0); }
94  int64 Add(int64 delta) { return AddHelper(delta); }
95 
96  protected:
98  virtual int64 AddHelper(int64 delta) = 0;
99 };
100 
113  public:
114  virtual ~MutexedScalar();
115 
118  int64 Get() const;
119  void Set(int64 value);
120  int64 SetReturningPreviousValue(int64 value);
121  int64 AddHelper(int64 delta);
122 
123  protected:
124  friend class StatisticsLogger;
125 
126  virtual AbstractMutex* mutex() const = 0;
127 
129  virtual int64 GetLockHeld() const = 0;
130  virtual int64 SetReturningPreviousValueLockHeld(int64 value) = 0;
131 
134  void SetLockHeld(int64 value);
135  int64 AddLockHeld(int64 delta);
136 };
137 
138 class Histogram {
139  public:
140  virtual ~Histogram();
142  virtual void Add(double value) = 0;
144  virtual void Clear() = 0;
146  bool Empty() {
147  ScopedMutex hold(lock());
148  return CountInternal() == 0;
149  }
161  virtual void Render(int index, Writer* writer, MessageHandler* handler);
162 
164  virtual int NumBuckets() = 0;
166  virtual void EnableNegativeBuckets() = 0;
168  virtual void SetMinValue(double value) = 0;
172  virtual void SetMaxValue(double value) = 0;
173 
176  virtual void SetSuggestedNumBuckets(int i) = 0;
177 
179  double Average() {
180  ScopedMutex hold(lock());
181  return AverageInternal();
182  }
186  double Percentile(const double perc) {
187  ScopedMutex hold(lock());
188  return PercentileInternal(perc);
189  }
190  double StandardDeviation() {
191  ScopedMutex hold(lock());
192  return StandardDeviationInternal();
193  }
194  double Count() {
195  ScopedMutex hold(lock());
196  return CountInternal();
197  }
198  double Maximum() {
199  ScopedMutex hold(lock());
200  return MaximumInternal();
201  }
202  double Minimum() {
203  ScopedMutex hold(lock());
204  return MinimumInternal();
205  }
206  double Median() {
207  return Percentile(50);
208  }
209 
216  GoogleString HtmlTableRow(const GoogleString& title, int index);
217 
221  virtual double BucketStart(int index) = 0;
223  virtual double BucketLimit(int index) {
224  return BucketStart(index + 1);
225  }
227  virtual double BucketCount(int index) = 0;
228 
229  protected:
230  Histogram() {}
231 
233  virtual double AverageInternal() = 0;
234  virtual double PercentileInternal(const double perc) = 0;
235  virtual double StandardDeviationInternal() = 0;
236  virtual double CountInternal() = 0;
237  virtual double MaximumInternal() = 0;
238  virtual double MinimumInternal() = 0;
239 
240  virtual AbstractMutex* lock() = 0;
241 
248  void WriteRawHistogramData(Writer* writer, MessageHandler* handler);
249 
250  private:
251 
252 };
253 
255 class CountHistogram : public Histogram {
256  public:
258  explicit CountHistogram(AbstractMutex* mutex);
259  virtual ~CountHistogram();
260  virtual void Add(double value) {
261  ScopedMutex hold(lock());
262  ++count_;
263  }
264  virtual void Clear() {
265  ScopedMutex hold(lock());
266  count_ = 0;
267  }
268  virtual int NumBuckets() { return 0; }
269  virtual void EnableNegativeBuckets() { }
270  virtual void SetMinValue(double value) { }
271  virtual void SetMaxValue(double value) { }
272  virtual void SetSuggestedNumBuckets(int i) { }
273  virtual GoogleString GetName() const { return ""; }
274 
275  protected:
276  virtual AbstractMutex* lock() LOCK_RETURNED(mutex_) { return mutex_.get(); }
277  virtual double AverageInternal() { return 0.0; }
278  virtual double PercentileInternal(const double perc) { return 0.0; }
279  virtual double StandardDeviationInternal() { return 0.0; }
280  virtual double CountInternal() EXCLUSIVE_LOCKS_REQUIRED(lock()) {
281  return count_;
282  }
283  virtual double MaximumInternal() { return 0.0; }
284  virtual double MinimumInternal() { return 0.0; }
285  virtual double BucketStart(int index) { return 0.0; }
286  virtual double BucketCount(int index) { return 0.0; }
287 
288  private:
290  int count_ GUARDED_BY(mutex_);
291 
292 
293 };
294 
299  public:
301  enum Levels { TENSEC, MINUTE, HOUR, START };
302  virtual ~TimedVariable();
304  virtual void IncBy(int64 delta) = 0;
307  virtual int64 Get(int level) = 0;
309  virtual void Clear() = 0;
310 };
311 
314  public:
315  FakeTimedVariable(StringPiece name, Statistics* stats);
316  virtual ~FakeTimedVariable();
318  virtual void IncBy(int64 delta) {
319  var_->Add(delta);
320  }
323  virtual int64 Get(int level) {
327  if (level == START) {
328  return var_->Get();
329  }
330  return 0;
331  }
333  virtual void Clear() {
334  return var_->Clear();
335  }
336 
337  protected:
338  Variable* var_;
339 };
340 
342 class Statistics {
343  public:
345  static const char kDefaultGroup[];
346 
347  Statistics() {}
348  virtual ~Statistics();
349 
353  virtual UpDownCounter* AddUpDownCounter(const StringPiece& name) = 0;
354 
358  virtual UpDownCounter* AddGlobalUpDownCounter(const StringPiece& name);
359 
361  virtual UpDownCounter* FindUpDownCounter(const StringPiece& name) const = 0;
362 
364  UpDownCounter* GetUpDownCounter(const StringPiece& name) const {
365  UpDownCounter* var = FindUpDownCounter(name);
366  CHECK(var != NULL) << "UpDownCounter not found: " << name;
367  return var;
368  }
369 
373  virtual Variable* AddVariable(const StringPiece& name) = 0;
374 
376  virtual Variable* FindVariable(const StringPiece& name) const = 0;
377 
379  Variable* GetVariable(const StringPiece& name) const {
380  Variable* var = FindVariable(name);
381  CHECK(var != NULL) << "Variable not found: " << name;
382  return var;
383  }
384 
385 
389  virtual Histogram* AddHistogram(const StringPiece& name) = 0;
391  virtual Histogram* FindHistogram(const StringPiece& name) const = 0;
393  Histogram* GetHistogram(const StringPiece& name) const {
394  Histogram* hist = FindHistogram(name);
395  CHECK(hist != NULL) << "Histogram not found: " << name;
396  return hist;
397  }
398 
404  const StringPiece& name, const StringPiece& group) = 0;
407  const StringPiece& name) const = 0;
410  const StringPiece& name) const {
411  TimedVariable* stat = FindTimedVariable(name);
412  CHECK(stat != NULL) << "TimedVariable not found: " << name;
413  return stat;
414  }
416  virtual const StringVector& HistogramNames() = 0;
418  virtual const std::map<GoogleString, StringVector>& TimedVariableMap() = 0;
420  virtual void Dump(Writer* writer, MessageHandler* handler) = 0;
422  virtual void DumpJson(Writer* writer, MessageHandler* message_handler) = 0;
423  virtual void RenderTimedVariables(Writer* writer,
424  MessageHandler* handler);
426  virtual void RenderHistograms(Writer* writer, MessageHandler* handler);
429  virtual void Clear() = 0;
430 
435  virtual StatisticsLogger* console_logger() { return NULL; }
436 
442  int64 LookupValue(StringPiece stat_name);
443 
444  private:
445 
446 };
447 
448 }
449 
450 #endif
int64 LookupValue(StringPiece stat_name)
virtual StringPiece GetName() const =0
virtual void SetMinValue(double value)
Set the minimum value allowed in histogram.
Definition: statistics.h:270
virtual int64 AddHelper(int64 delta)=0
This is virtual so that subclasses can add platform-specific atomicity.
virtual void IncBy(int64 delta)
Update the stat value. delta is in milliseconds.
Definition: statistics.h:318
Variable * GetVariable(const StringPiece &name) const
Find a variable from a name, aborting if not found.
Definition: statistics.h:379
double Average()
Returns average of the values added.
Definition: statistics.h:179
virtual Histogram * FindHistogram(const StringPiece &name) const =0
Find a histogram from a name, returning NULL if not found.
virtual void Dump(Writer *writer, MessageHandler *handler)=0
Dump the variable-values to a writer.
virtual UpDownCounter * AddUpDownCounter(const StringPiece &name)=0
virtual void Clear()=0
Throw away all data.
virtual double BucketStart(int index)=0
virtual Variable * FindVariable(const StringPiece &name) const =0
Find a variable from a name, returning NULL if not found.
virtual UpDownCounter * FindUpDownCounter(const StringPiece &name) const =0
Find a variable from a name, returning NULL if not found.
virtual void Clear()=0
Definition: statistics.h:43
static const char kDefaultGroup[]
Default group for use with AddTimedVariable.
Definition: statistics.h:345
virtual TimedVariable * FindTimedVariable(const StringPiece &name) const =0
Find a TimedVariable from a name, returning NULL if not found.
CountHistogram(AbstractMutex *mutex)
Takes ownership of mutex.
virtual void Clear()
Throw away all data.
Definition: statistics.h:264
virtual double AverageInternal()
Note that these *Internal interfaces require the mutex to be held.
Definition: statistics.h:277
virtual void SetSuggestedNumBuckets(int i)=0
virtual int64 Get(int level)
Definition: statistics.h:323
virtual int64 SetReturningPreviousValue(int64 value)
Histogram * GetHistogram(const StringPiece &name) const
Find a histogram from a name, aborting if not found.
Definition: statistics.h:393
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
GoogleString HtmlTableRow(const GoogleString &title, int index)
void WriteRawHistogramData(Writer *writer, MessageHandler *handler)
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
virtual void IncBy(int64 delta)=0
Update the stat value. delta is in milliseconds.
virtual double BucketCount(int index)
Value of a bucket.
Definition: statistics.h:286
virtual void DumpJson(Writer *writer, MessageHandler *message_handler)=0
Dump the variable-values in JSON format to a writer.
virtual UpDownCounter * AddGlobalUpDownCounter(const StringPiece &name)
TimedVariable implementation that only updates a basic UpDownCounter.
Definition: statistics.h:313
bool Empty()
True if the histogram is empty.
Definition: statistics.h:146
virtual void Clear()=0
Throw away all data.
virtual void Render(int index, Writer *writer, MessageHandler *handler)
virtual Variable * AddVariable(const StringPiece &name)=0
double Percentile(const double perc)
Definition: statistics.h:186
Definition: scoped_ptr.h:30
UpDownCounter * GetUpDownCounter(const StringPiece &name) const
Find a variable from a name, aborting if not found.
Definition: statistics.h:364
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
#define ScopedMutex(x)
Definition: abstract_mutex.h:69
Levels
The intervals for which we keep stats.
Definition: statistics.h:301
virtual double BucketLimit(int index)
Upper bound of a bucket.
Definition: statistics.h:223
virtual const StringVector & HistogramNames()=0
Return the names of all the histograms for render.
virtual void Add(double value)
Record a value in its bucket.
Definition: statistics.h:260
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
void SetLockHeld(int64 value)
Trivial implementation. But Count() returns a meaningful value.
Definition: statistics.h:255
virtual StatisticsLogger * console_logger()
Definition: statistics.h:435
virtual void Add(double value)=0
Record a value in its bucket.
Interface for writing bytes to an output stream.
Definition: writer.h:29
virtual void EnableNegativeBuckets()
Allow histogram have negative values.
Definition: statistics.h:269
virtual int NumBuckets()=0
Returns number of buckets the histogram actually has.
virtual void SetMaxValue(double value)=0
virtual void Clear()
Throw away all data.
Definition: statistics.h:333
TimedVariable * GetTimedVariable(const StringPiece &name) const
Find a TimedVariable from a name, aborting if not found.
Definition: statistics.h:409
virtual double BucketCount(int index)=0
Value of a bucket.
virtual void SetMaxValue(double value)
Definition: statistics.h:271
Definition: statistics.h:138
Definition: statistics.h:112
virtual void EnableNegativeBuckets()=0
Allow histogram have negative values.
Definition: statistics.h:73
virtual void RenderHistograms(Writer *writer, MessageHandler *handler)
Write all the histograms in this Statistic object to a writer.
Definition: message_handler.h:39
virtual StringPiece GetName() const =0
virtual int NumBuckets()
Returns number of buckets the histogram actually has.
Definition: statistics.h:268
virtual double BucketStart(int index)
Definition: statistics.h:285
Definition: statistics.h:298
virtual Histogram * AddHistogram(const StringPiece &name)=0
virtual int64 Get(int level)=0
virtual void SetSuggestedNumBuckets(int i)
Definition: statistics.h:272
virtual double AverageInternal()=0
Note that these *Internal interfaces require the mutex to be held.
virtual void SetMinValue(double value)=0
Set the minimum value allowed in histogram.
int64 Add(int64 non_negative_delta)
Adds 'delta' to the variable's value, returning the result.
Definition: statistics.h:53
virtual const std::map< GoogleString, StringVector > & TimedVariableMap()=0
Return the map of groupnames and names of all timedvariables for render.
virtual int64 GetLockHeld() const =0
Get/Setters that may only be called if you already hold the mutex.
virtual TimedVariable * AddTimedVariable(const StringPiece &name, const StringPiece &group)=0
virtual int64 AddHelper(int64 delta)=0
This is virtual so that subclasses can add platform-specific atomicity.
Definition: statistics_logger.h:42