Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / quic / congestion_control / rtt_stats.h
bloba14cfcb3e26eedb18031b55594dad92036576638
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 // Updates the RTT from an incoming ack which is received |send_delta| after
27 // the packet is sent and the peer reports the ack being delayed |ack_delay|.
28 void UpdateRtt(QuicTime::Delta send_delta,
29 QuicTime::Delta ack_delay,
30 QuicTime now);
32 // Causes the smoothed_rtt to be increased to the latest_rtt if the latest_rtt
33 // is larger. The mean deviation is increased to the most recent deviation if
34 // it's larger.
35 void ExpireSmoothedMetrics();
37 // Forces RttStats to sample a new recent min rtt within the next
38 // |num_samples| UpdateRtt calls.
39 void SampleNewRecentMinRtt(uint32 num_samples);
41 // Returns the EWMA smoothed RTT for the connection.
42 // May return Zero if no valid updates have occurred.
43 QuicTime::Delta smoothed_rtt() const {
44 return smoothed_rtt_;
47 int64 initial_rtt_us() const {
48 return initial_rtt_us_;
51 // Sets an initial RTT to be used for SmoothedRtt before any RTT updates.
52 void set_initial_rtt_us(int64 initial_rtt_us) {
53 if (initial_rtt_us <= 0) {
54 LOG(DFATAL) << "Attempt to set initial rtt to <= 0.";
55 return;
57 initial_rtt_us_ = initial_rtt_us;
60 // The most recent rtt measurement.
61 // May return Zero if no valid updates have occurred.
62 QuicTime::Delta latest_rtt() const {
63 return latest_rtt_;
66 // Returns the min_rtt for the entire connection.
67 // May return Zero if no valid updates have occurred.
68 QuicTime::Delta min_rtt() const {
69 return min_rtt_;
72 // Returns the min_rtt since SampleNewRecentMinRtt has been called, or the
73 // min_rtt for the entire connection if SampleNewMinRtt was never called.
74 QuicTime::Delta recent_min_rtt() const {
75 return recent_min_rtt_.rtt;
78 QuicTime::Delta mean_deviation() const {
79 return mean_deviation_;
82 // Sets how old a recent min rtt sample can be.
83 void set_recent_min_rtt_window(QuicTime::Delta recent_min_rtt_window) {
84 recent_min_rtt_window_ = recent_min_rtt_window;
87 private:
88 friend class test::RttStatsPeer;
90 // Used to track a sampled RTT window.
91 struct RttSample {
92 RttSample() : rtt(QuicTime::Delta::Zero()), time(QuicTime::Zero()) { }
93 RttSample(QuicTime::Delta rtt, QuicTime time) : rtt(rtt), time(time) { }
95 QuicTime::Delta rtt;
96 QuicTime time; // Time the rtt sample was recorded.
99 // Implements the resampling algorithm and the windowed min rtt algorithm.
100 void UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now);
102 QuicTime::Delta latest_rtt_;
103 QuicTime::Delta min_rtt_;
104 QuicTime::Delta smoothed_rtt_;
105 // Mean RTT deviation during this session.
106 // Approximation of standard deviation, the error is roughly 1.25 times
107 // larger than the standard deviation, for a normally distributed signal.
108 QuicTime::Delta mean_deviation_;
109 int64 initial_rtt_us_;
111 RttSample new_min_rtt_;
112 uint32 num_min_rtt_samples_remaining_;
114 // State variables for Kathleen Nichols MinRTT algorithm.
115 QuicTime::Delta recent_min_rtt_window_;
116 RttSample recent_min_rtt_; // a in the windowed algorithm.
117 RttSample half_window_rtt_; // b in the sampled algorithm.
118 RttSample quarter_window_rtt_; // c in the sampled algorithm.
120 DISALLOW_COPY_AND_ASSIGN(RttStats);
123 } // namespace net
125 #endif // NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_