Home | History | Annotate | Download | only in congestion_control
      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   QuicTime::Delta SmoothedRtt() const;
     45 
     46   int64 initial_rtt_us() const {
     47     return initial_rtt_us_;
     48   }
     49 
     50   // Sets an initial RTT to be used for SmoothedRtt before any RTT updates.
     51   void set_initial_rtt_us(int64 initial_rtt_us) {
     52     initial_rtt_us_ = initial_rtt_us;
     53   }
     54 
     55   QuicTime::Delta latest_rtt() const {
     56     return latest_rtt_;
     57   }
     58 
     59   // Returns the min_rtt for the entire connection.
     60   QuicTime::Delta min_rtt() const {
     61     return min_rtt_;
     62   }
     63 
     64   // Returns the min_rtt since SampleNewRecentMinRtt has been called, or the
     65   // min_rtt for the entire connection if SampleNewMinRtt was never called.
     66   QuicTime::Delta recent_min_rtt() const {
     67     return recent_min_rtt_.rtt;
     68   }
     69 
     70   QuicTime::Delta mean_deviation() const {
     71     return mean_deviation_;
     72   }
     73 
     74   // Sets how old a recent min rtt sample can be.
     75   void set_recent_min_rtt_window(QuicTime::Delta recent_min_rtt_window) {
     76     recent_min_rtt_window_ = recent_min_rtt_window;
     77   }
     78 
     79  private:
     80   friend class test::RttStatsPeer;
     81 
     82   // Used to track a sampled RTT window.
     83   struct RttSample {
     84     RttSample() : rtt(QuicTime::Delta::Zero()), time(QuicTime::Zero()) { }
     85     RttSample(QuicTime::Delta rtt, QuicTime time) : rtt(rtt), time(time) { }
     86 
     87     QuicTime::Delta rtt;
     88     QuicTime time;  // Time the rtt sample was recorded.
     89   };
     90 
     91   // Implements the resampling algorithm and the windowed min rtt algorithm.
     92   void UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now);
     93 
     94   QuicTime::Delta latest_rtt_;
     95   QuicTime::Delta min_rtt_;
     96   QuicTime::Delta smoothed_rtt_;
     97   // Mean RTT deviation during this session.
     98   // Approximation of standard deviation, the error is roughly 1.25 times
     99   // larger than the standard deviation, for a normally distributed signal.
    100   QuicTime::Delta mean_deviation_;
    101   int64 initial_rtt_us_;
    102 
    103   RttSample new_min_rtt_;
    104   uint32 num_min_rtt_samples_remaining_;
    105 
    106   // State variables for Kathleen Nichols MinRTT algorithm.
    107   QuicTime::Delta recent_min_rtt_window_;
    108   RttSample recent_min_rtt_;  // a in the windowed algorithm.
    109   RttSample half_window_rtt_;  // b in the sampled algorithm.
    110   RttSample quarter_window_rtt_;  // c in the sampled algorithm.
    111 
    112   DISALLOW_COPY_AND_ASSIGN(RttStats);
    113 };
    114 
    115 }  // namespace net
    116 
    117 #endif  // NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_
    118