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 #include "quiche/quic/core/quic_sustained_bandwidth_recorder.h"
6 
7 #include "quiche/quic/core/quic_bandwidth.h"
8 #include "quiche/quic/core/quic_time.h"
9 #include "quiche/quic/platform/api/quic_logging.h"
10 
11 namespace quic {
12 
QuicSustainedBandwidthRecorder()13 QuicSustainedBandwidthRecorder::QuicSustainedBandwidthRecorder()
14     : has_estimate_(false),
15       is_recording_(false),
16       bandwidth_estimate_recorded_during_slow_start_(false),
17       bandwidth_estimate_(QuicBandwidth::Zero()),
18       max_bandwidth_estimate_(QuicBandwidth::Zero()),
19       max_bandwidth_timestamp_(0),
20       start_time_(QuicTime::Zero()) {}
21 
RecordEstimate(bool in_recovery,bool in_slow_start,QuicBandwidth bandwidth,QuicTime estimate_time,QuicWallTime wall_time,QuicTime::Delta srtt)22 void QuicSustainedBandwidthRecorder::RecordEstimate(
23     bool in_recovery, bool in_slow_start, QuicBandwidth bandwidth,
24     QuicTime estimate_time, QuicWallTime wall_time, QuicTime::Delta srtt) {
25   if (in_recovery) {
26     is_recording_ = false;
27     QUIC_DVLOG(1) << "Stopped recording at: "
28                   << estimate_time.ToDebuggingValue();
29     return;
30   }
31 
32   if (!is_recording_) {
33     // This is the first estimate of a new recording period.
34     start_time_ = estimate_time;
35     is_recording_ = true;
36     QUIC_DVLOG(1) << "Started recording at: " << start_time_.ToDebuggingValue();
37     return;
38   }
39 
40   // If we have been recording for at least 3 * srtt, then record the latest
41   // bandwidth estimate as a valid sustained bandwidth estimate.
42   if (estimate_time - start_time_ >= 3 * srtt) {
43     has_estimate_ = true;
44     bandwidth_estimate_recorded_during_slow_start_ = in_slow_start;
45     bandwidth_estimate_ = bandwidth;
46     QUIC_DVLOG(1) << "New sustained bandwidth estimate (KBytes/s): "
47                   << bandwidth_estimate_.ToKBytesPerSecond();
48   }
49 
50   // Check for an increase in max bandwidth.
51   if (bandwidth > max_bandwidth_estimate_) {
52     max_bandwidth_estimate_ = bandwidth;
53     max_bandwidth_timestamp_ = wall_time.ToUNIXSeconds();
54     QUIC_DVLOG(1) << "New max bandwidth estimate (KBytes/s): "
55                   << max_bandwidth_estimate_.ToKBytesPerSecond();
56   }
57 }
58 
59 }  // namespace quic
60