2
* Copyright 2011 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_BASE_WAVEFORM_H_
20
#define PAGESPEED_KERNEL_BASE_WAVEFORM_H_
22
#include <utility> // for pair
24
#include "pagespeed/kernel/base/basictypes.h"
25
#include "pagespeed/kernel/base/scoped_ptr.h"
26
#include "pagespeed/kernel/base/string_util.h"
28
namespace net_instaweb {
37
// Displays a waveform of values over time. This can run
38
// continuously, in which case it will only display waveforms for a
39
// bounded number of samples. Or it can be run as a trigger.
41
// However the average, min, and max values will account for all the
42
// values seen by the waveform since it was cleared.
44
// This class is threadsafe.
47
Waveform(ThreadSystem* thread_system, Timer* timer, int capacity,
48
UpDownCounter* metric);
56
// Records a value at the current time using the Timer.
57
void Add(double value);
59
// Records a delta relative to the previous value using the Timer.
60
// This is equivalent to calling Add(previous_value + delta).
61
void AddDelta(double delta);
63
// Write script and function to web page. Note that this function
64
// should be called only once for each HTML page, and should not be
65
// used for each waveform.
66
static void RenderHeader(Writer* writer, MessageHandler* handler);
68
// Renders a waveform into HTML.
69
void Render(const StringPiece& title, const StringPiece& label,
70
Writer* writer, MessageHandler* handler);
73
typedef std::pair<int64, double> TimeValue;
75
TimeValue* GetSample(int index); // Must be called with mutex held.
76
void AddHelper(double value); // Must be called with mutex held.
80
scoped_array<TimeValue> samples_;
83
int64 first_sample_timestamp_us_;
84
double total_since_clear_;
87
double previous_value_;
88
scoped_ptr<AbstractMutex> mutex_; // protects all the above member variables.
90
// Un-owned pointer to a variable to export current waveform values.
92
UpDownCounter* metric_;
94
DISALLOW_COPY_AND_ASSIGN(Waveform);
97
} // namespace net_instaweb
99
#endif // PAGESPEED_KERNEL_BASE_WAVEFORM_H_