| OLD | NEW |
| (Empty) |
| 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. | |
| 6 | |
| 7 #ifndef NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_ | |
| 8 #define NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_ | |
| 9 | |
| 10 #include <algorithm> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "net/quic/quic_protocol.h" | |
| 14 #include "net/quic/quic_time.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace test { | |
| 19 class RttStatsPeer; | |
| 20 } // namespace test | |
| 21 | |
| 22 class NET_EXPORT_PRIVATE RttStats { | |
| 23 public: | |
| 24 RttStats(); | |
| 25 | |
| 26 // Returns true if any RTT measurements have been made. | |
| 27 bool HasUpdates() const; | |
| 28 | |
| 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); | |
| 34 | |
| 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(); | |
| 39 | |
| 40 // Forces RttStats to sample a new recent min rtt within the next | |
| 41 // |num_samples| UpdateRtt calls. | |
| 42 void SampleNewRecentMinRtt(uint32 num_samples); | |
| 43 | |
| 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_; | |
| 48 } | |
| 49 | |
| 50 int64 initial_rtt_us() const { | |
| 51 return initial_rtt_us_; | |
| 52 } | |
| 53 | |
| 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; | |
| 59 } | |
| 60 initial_rtt_us_ = initial_rtt_us; | |
| 61 } | |
| 62 | |
| 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_; | |
| 67 } | |
| 68 | |
| 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_; | |
| 73 } | |
| 74 | |
| 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; | |
| 79 } | |
| 80 | |
| 81 QuicTime::Delta mean_deviation() const { | |
| 82 return mean_deviation_; | |
| 83 } | |
| 84 | |
| 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; | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 friend class test::RttStatsPeer; | |
| 92 | |
| 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) { } | |
| 97 | |
| 98 QuicTime::Delta rtt; | |
| 99 QuicTime time; // Time the rtt sample was recorded. | |
| 100 }; | |
| 101 | |
| 102 // Implements the resampling algorithm and the windowed min rtt algorithm. | |
| 103 void UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now); | |
| 104 | |
| 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_; | |
| 113 | |
| 114 RttSample new_min_rtt_; | |
| 115 uint32 num_min_rtt_samples_remaining_; | |
| 116 | |
| 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. | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(RttStats); | |
| 124 }; | |
| 125 | |
| 126 } // namespace net | |
| 127 | |
| 128 #endif // NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_ | |
| OLD | NEW |