blob: 74aab0cfd6c01d1c5c6c6a16c1e4fd14a0605843 [file] [log] [blame]
Klaus Schneider9e5122b2019-03-19 17:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2016-2019, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Klaus Schneider
24 */
25
26#include "pipeline-interests-cubic.hpp"
27
28#include <cmath>
29
30namespace ndn {
31namespace chunks {
32
33constexpr double CUBIC_C = 0.4;
34
Davide Pesavento5e3773d2019-08-22 15:35:08 -040035PipelineInterestsCubic::PipelineInterestsCubic(Face& face, RttEstimatorWithStats& rttEstimator,
Davide Pesavento97a33b22019-10-17 22:10:47 -040036 const Options& opts)
37 : PipelineInterestsAdaptive(face, rttEstimator, opts)
Klaus Schneider9e5122b2019-03-19 17:03:25 -070038 , m_lastDecrease(time::steady_clock::now())
39{
Davide Pesavento97a33b22019-10-17 22:10:47 -040040 if (m_options.isVerbose) {
41 printOptions();
42 std::cerr << "\tCubic beta = " << m_options.cubicBeta << "\n"
43 << "\tFast convergence = " << (m_options.enableFastConv ? "yes" : "no") << "\n";
Klaus Schneider9e5122b2019-03-19 17:03:25 -070044 }
45}
46
47void
48PipelineInterestsCubic::increaseWindow()
49{
50 // Slow start phase
51 if (m_cwnd < m_ssthresh) {
52 m_cwnd += 1.0;
53 }
54 // Congestion avoidance phase
55 else {
56 // If wmax is still 0, set it to the current cwnd. Usually unnecessary,
57 // if m_ssthresh is large enough.
58 if (m_wmax < m_options.initCwnd) {
59 m_wmax = m_cwnd;
60 }
61
62 // 1. Time since last congestion event in seconds
63 const double t = (time::steady_clock::now() - m_lastDecrease).count() / 1e9;
64
65 // 2. Time it takes to increase the window to m_wmax = the cwnd right before the last
66 // window decrease.
67 // K = cubic_root(wmax*(1-beta_cubic)/C) (Eq. 2)
Davide Pesavento97a33b22019-10-17 22:10:47 -040068 const double k = std::cbrt(m_wmax * (1 - m_options.cubicBeta) / CUBIC_C);
Klaus Schneider9e5122b2019-03-19 17:03:25 -070069
70 // 3. Target: W_cubic(t) = C*(t-K)^3 + wmax (Eq. 1)
71 const double wCubic = CUBIC_C * std::pow(t - k, 3) + m_wmax;
72
Klaus Schneider4a2e89d2019-10-27 10:04:39 -070073 // 4. Estimate of Reno Increase (Eq. 4)
74 const double rtt = m_rttEstimator.getSmoothedRtt().count() / 1e9;
75 const double wEst = m_wmax * m_options.cubicBeta +
76 (3 * (1 - m_options.cubicBeta) / (1 + m_options.cubicBeta)) * (t / rtt);
Klaus Schneider9e5122b2019-03-19 17:03:25 -070077
78 // Actual adaptation
79 double cubicIncrement = std::max(wCubic, wEst) - m_cwnd;
80 // Cubic increment must be positive
81 // Note: This change is not part of the RFC, but I added it to improve performance.
82 cubicIncrement = std::max(0.0, cubicIncrement);
83
84 m_cwnd += cubicIncrement / m_cwnd;
85 }
86
87 emitSignal(afterCwndChange, time::steady_clock::now() - getStartTime(), m_cwnd);
88}
89
90void
91PipelineInterestsCubic::decreaseWindow()
92{
93 // A flow remembers the last value of wmax,
94 // before it updates wmax for the current congestion event.
95
96 // Current wmax < last_wmax
Davide Pesavento97a33b22019-10-17 22:10:47 -040097 if (m_options.enableFastConv && m_cwnd < m_lastWmax) {
Klaus Schneider9e5122b2019-03-19 17:03:25 -070098 m_lastWmax = m_cwnd;
Davide Pesavento97a33b22019-10-17 22:10:47 -040099 m_wmax = m_cwnd * (1.0 + m_options.cubicBeta) / 2.0;
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700100 }
101 else {
102 // Save old cwnd as wmax
103 m_lastWmax = m_cwnd;
104 m_wmax = m_cwnd;
105 }
106
Davide Pesavento97a33b22019-10-17 22:10:47 -0400107 m_ssthresh = std::max(m_options.initCwnd, m_cwnd * m_options.cubicBeta);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700108 m_cwnd = m_ssthresh;
109 m_lastDecrease = time::steady_clock::now();
110
111 emitSignal(afterCwndChange, time::steady_clock::now() - getStartTime(), m_cwnd);
112}
113
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700114} // namespace chunks
115} // namespace ndn