MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / net / quic / congestion_control / rtt_stats.h
blobb7ec6b94dcfbb05a171aade4857210c852be9a04
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 QuicTime::Delta SmoothedRtt() const;
46 // Returns the min_rtt for the entire connection if a min has been measured.
47 // This returns an initial non-zero RTT estimate if no measurements have yet
48 // been made.
49 QuicTime::Delta MinRtt() const;
51 int64 initial_rtt_us() const {
52 return initial_rtt_us_;
55 // Sets an initial RTT to be used for SmoothedRtt before any RTT updates.
56 void set_initial_rtt_us(int64 initial_rtt_us) {
57 initial_rtt_us_ = initial_rtt_us;
60 QuicTime::Delta latest_rtt() const {
61 return latest_rtt_;
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;
70 QuicTime::Delta mean_deviation() const {
71 return mean_deviation_;
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;
79 private:
80 friend class test::RttStatsPeer;
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) { }
87 QuicTime::Delta rtt;
88 QuicTime time; // Time the rtt sample was recorded.
91 // Implements the resampling algorithm and the windowed min rtt algorithm.
92 void UpdateRecentMinRtt(QuicTime::Delta rtt_sample, QuicTime now);
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_;
103 RttSample new_min_rtt_;
104 uint32 num_min_rtt_samples_remaining_;
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.
112 DISALLOW_COPY_AND_ASSIGN(RttStats);
115 } // namespace net
117 #endif // NET_QUIC_CONGESTION_CONTROL_RTT_STATS_H_