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#ifndef NET_QUIC_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_
6#define NET_QUIC_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_
7
8#include "base/logging.h"
9#include "net/quic/quic_bandwidth.h"
10#include "net/quic/quic_time.h"
11
12namespace net {
13
14namespace test {
15class QuicSustainedBandwidthRecorderPeer;
16}  // namespace test
17
18// This class keeps track of a sustained bandwidth estimate to ultimately send
19// to the client in a server config update message. A sustained bandwidth
20// estimate is only marked as valid if the QuicSustainedBandwidthRecorder has
21// been given uninterrupted reliable estimates over a certain period of time.
22class NET_EXPORT_PRIVATE QuicSustainedBandwidthRecorder {
23 public:
24  QuicSustainedBandwidthRecorder();
25
26  // As long as |in_recovery| is consistently false, multiple calls to this
27  // method over a 3 * srtt period results in storage of a valid sustained
28  // bandwidth estimate.
29  // |time_now| is used as a max bandwidth timestamp if needed.
30  void RecordEstimate(bool in_recovery,
31                      bool in_slow_start,
32                      QuicBandwidth bandwidth,
33                      QuicTime estimate_time,
34                      QuicWallTime wall_time,
35                      QuicTime::Delta srtt);
36
37  bool HasEstimate() const {
38    return has_estimate_;
39  }
40
41  QuicBandwidth BandwidthEstimate() const {
42    DCHECK(has_estimate_);
43    return bandwidth_estimate_;
44  }
45
46  QuicBandwidth MaxBandwidthEstimate() const {
47    DCHECK(has_estimate_);
48    return max_bandwidth_estimate_;
49  }
50
51  int64 MaxBandwidthTimestamp() const {
52    DCHECK(has_estimate_);
53    return max_bandwidth_timestamp_;
54  }
55
56  bool EstimateRecordedDuringSlowStart() const {
57    DCHECK(has_estimate_);
58    return bandwidth_estimate_recorded_during_slow_start_;
59  }
60
61 private:
62  friend class test::QuicSustainedBandwidthRecorderPeer;
63
64  // True if we have been able to calculate sustained bandwidth, over at least
65  // one recording period (3 * rtt).
66  bool has_estimate_;
67
68  // True if the last call to RecordEstimate had a reliable estimate.
69  bool is_recording_;
70
71  // True if the current sustained bandwidth estimate was generated while in
72  // slow start.
73  bool bandwidth_estimate_recorded_during_slow_start_;
74
75  // The latest sustained bandwidth estimate.
76  QuicBandwidth bandwidth_estimate_;
77
78  // The maximum sustained bandwidth seen over the lifetime of the connection.
79  QuicBandwidth max_bandwidth_estimate_;
80
81  // Timestamp indicating when the max_bandwidth_estimate_ was seen.
82  int64 max_bandwidth_timestamp_;
83
84  // Timestamp marking the beginning of the latest recording period.
85  QuicTime start_time_;
86
87  DISALLOW_COPY_AND_ASSIGN(QuicSustainedBandwidthRecorder);
88};
89
90}  // namespace net
91
92#endif  // NET_QUIC_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_
93