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