summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/quic/core/quic_sustained_bandwidth_recorder.h
blob: f1fdfebe1f17e254f543b45a2007325e605ed884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef QUICHE_QUIC_CORE_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_
#define QUICHE_QUIC_CORE_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_

#include <cstdint>

#include "quic/core/quic_bandwidth.h"
#include "quic/core/quic_time.h"
#include "quic/platform/api/quic_export.h"
#include "quic/platform/api/quic_logging.h"

namespace quic {

namespace test {
class QuicSustainedBandwidthRecorderPeer;
}  // namespace test

// This class keeps track of a sustained bandwidth estimate to ultimately send
// to the client in a server config update message. A sustained bandwidth
// estimate is only marked as valid if the QuicSustainedBandwidthRecorder has
// been given uninterrupted reliable estimates over a certain period of time.
class QUIC_EXPORT_PRIVATE QuicSustainedBandwidthRecorder {
 public:
  QuicSustainedBandwidthRecorder();
  QuicSustainedBandwidthRecorder(const QuicSustainedBandwidthRecorder&) =
      delete;
  QuicSustainedBandwidthRecorder& operator=(
      const QuicSustainedBandwidthRecorder&) = delete;

  // As long as |in_recovery| is consistently false, multiple calls to this
  // method over a 3 * srtt period results in storage of a valid sustained
  // bandwidth estimate.
  // |time_now| is used as a max bandwidth timestamp if needed.
  void RecordEstimate(bool in_recovery,
                      bool in_slow_start,
                      QuicBandwidth bandwidth,
                      QuicTime estimate_time,
                      QuicWallTime wall_time,
                      QuicTime::Delta srtt);

  bool HasEstimate() const { return has_estimate_; }

  QuicBandwidth BandwidthEstimate() const {
    QUICHE_DCHECK(has_estimate_);
    return bandwidth_estimate_;
  }

  QuicBandwidth MaxBandwidthEstimate() const {
    QUICHE_DCHECK(has_estimate_);
    return max_bandwidth_estimate_;
  }

  int64_t MaxBandwidthTimestamp() const {
    QUICHE_DCHECK(has_estimate_);
    return max_bandwidth_timestamp_;
  }

  bool EstimateRecordedDuringSlowStart() const {
    QUICHE_DCHECK(has_estimate_);
    return bandwidth_estimate_recorded_during_slow_start_;
  }

 private:
  friend class test::QuicSustainedBandwidthRecorderPeer;

  // True if we have been able to calculate sustained bandwidth, over at least
  // one recording period (3 * rtt).
  bool has_estimate_;

  // True if the last call to RecordEstimate had a reliable estimate.
  bool is_recording_;

  // True if the current sustained bandwidth estimate was generated while in
  // slow start.
  bool bandwidth_estimate_recorded_during_slow_start_;

  // The latest sustained bandwidth estimate.
  QuicBandwidth bandwidth_estimate_;

  // The maximum sustained bandwidth seen over the lifetime of the connection.
  QuicBandwidth max_bandwidth_estimate_;

  // Timestamp indicating when the max_bandwidth_estimate_ was seen.
  int64_t max_bandwidth_timestamp_;

  // Timestamp marking the beginning of the latest recording period.
  QuicTime start_time_;
};

}  // namespace quic

#endif  // QUICHE_QUIC_CORE_QUIC_SUSTAINED_BANDWIDTH_RECORDER_H_