Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / congestion_control / rtt_stats.h
blob3d256d9ddfa10d468fb33930c6c15e1242a0ff8b
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // A convenience class to store rtt samples and calculate smoothed rtt.
7 #ifndef NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_
8 #define NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_
10 #include <algorithm>
12 #include "base/basictypes.h"
13 #include "net/quic/quic_protocol.h"
14 #include "net/quic/quic_time.h"
16 namespace net {
18 namespace test {
19 class RttStatsPeer;
20 } // namespace test
22 class NET_EXPORT_PRIVATE RttStats {
23 public:
24 RttStats();
26 // Returns true if any RTT measurements have been made.
27 bool HasUpdates() const;
29 // Updates the RTT from an incoming ack which is received |send_delta| after
30 // the packet is sent and the peer reports the ack being delayed |ack_delay|.
31 void UpdateRtt(QuicTime::Delta send_delta,
32 QuicTime::Delta ack_delay,
33 QuicTime now);
35 // Causes the smoothed_rtt to be increased to the latest_rtt if the latest_rtt
36 // is larger. The mean deviation is increased to the most recent deviation if
37 // it's larger.
38 void ExpireSmoothedMetrics();
40 // Forces RttStats to sample a new recent min rtt within the next
41 // |num_samples| UpdateRtt calls.
42 void SampleNewRecentMinRtt(uint32 num_samples);
44 // Returns the EWMA smoothed RTT for the connection.
45 // May return Zero if no valid updates have occurred.
46 QuicTime::Delta smoothed_rtt() const {
47 return smoothed_rtt_;
50 int64 initial_rtt_us() const {
51 return initial_rtt_us_;
54 // Sets an initial RTT to be used for SmoothedRtt before any RTT updates.
55 void set_initial_rtt_us(int64 initial_rtt_us) {
56 if (initial_rtt_us <= 0) {
57 LOG(DFATAL) << "Attempt to set initial rtt to <= 0.";
58 return;
60 initial_rtt_us_ = initial_rtt_us;
63 // The most recent rtt measurement.
64 // May return Zero if no valid updates have occurred.
65 QuicTime::Delta latest_rtt() const {
66 return latest_rtt_;
69 // Returns the min_rtt for the entire connection.
70 // May return Zero if no valid updates have occurred.
71 QuicTime::Delta min_rtt() const {
72 return min_rtt_;
75 // Returns the min_rtt since SampleNewRecentMinRtt has been called, or the
76 // min_rtt for the entire connection if SampleNewMinRtt was never called.
77 QuicTime::Delta recent_min_rtt() const {
78 return recent_min_rtt_.rtt;
81 QuicTime::Delta mean_deviation() const {
82 return mean_deviation_;
85 // Sets how old a recent min rtt sample can be.
86 void set_recent_min_rtt_window(QuicTime::Delta recent_min_rtt_window) {
87 recent_min_rtt_window_ = recent_min_rtt_window;
90 private:
91 friend class test::RttStatsPeer;
93 // Used to track a sampled RTT window.
94 struct RttSample {
95 RttSample() : rtt(QuicTime::Delta::Zero()), time(QuicTime::Zero()) { }
96 RttSample(QuicTime::Delta rtt, QuicTime time) : rtt(rtt), time(time) { }
98 QuicTime::Delta rtt;
99 QuicTime time; // Time the rtt sample was recorded.
102 // Implements the resampling algorithm and the windowed min rtt algorithm.
103 void UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now);
105 QuicTime::Delta latest_rtt_;
106 QuicTime::Delta min_rtt_;
107 QuicTime::Delta smoothed_rtt_;
108 // Mean RTT deviation during this session.
109 // Approximation of standard deviation, the error is roughly 1.25 times
110 // larger than the standard deviation, for a normally distributed signal.
111 QuicTime::Delta mean_deviation_;
112 int64 initial_rtt_us_;
114 RttSample new_min_rtt_;
115 uint32 num_min_rtt_samples_remaining_;
117 // State variables for Kathleen Nichols MinRTT algorithm.
118 QuicTime::Delta recent_min_rtt_window_;
119 RttSample recent_min_rtt_; // a in the windowed algorithm.
120 RttSample half_window_rtt_; // b in the sampled algorithm.
121 RttSample quarter_window_rtt_; // c in the sampled algorithm.
123 DISALLOW_COPY_AND_ASSIGN(RttStats);
126 } // namespace net
128 #endif // NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_