blob: 5eaf41194f26d03668783eca241d007df00d76c7 [file] [log] [blame]
Klaus Schneider9e5122b2019-03-19 17:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Klaus Schneider9e5122b2019-03-19 17:03:25 -07004 * 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050030namespace ndn::chunks {
Klaus Schneider9e5122b2019-03-19 17:03:25 -070031
32constexpr double CUBIC_C = 0.4;
33
Davide Pesavento5e3773d2019-08-22 15:35:08 -040034PipelineInterestsCubic::PipelineInterestsCubic(Face& face, RttEstimatorWithStats& rttEstimator,
Davide Pesavento97a33b22019-10-17 22:10:47 -040035 const Options& opts)
36 : PipelineInterestsAdaptive(face, rttEstimator, opts)
Klaus Schneider9e5122b2019-03-19 17:03:25 -070037 , m_lastDecrease(time::steady_clock::now())
38{
Davide Pesavento97a33b22019-10-17 22:10:47 -040039 if (m_options.isVerbose) {
40 printOptions();
41 std::cerr << "\tCubic beta = " << m_options.cubicBeta << "\n"
42 << "\tFast convergence = " << (m_options.enableFastConv ? "yes" : "no") << "\n";
Klaus Schneider9e5122b2019-03-19 17:03:25 -070043 }
44}
45
46void
47PipelineInterestsCubic::increaseWindow()
48{
49 // Slow start phase
50 if (m_cwnd < m_ssthresh) {
51 m_cwnd += 1.0;
52 }
53 // Congestion avoidance phase
54 else {
55 // If wmax is still 0, set it to the current cwnd. Usually unnecessary,
56 // if m_ssthresh is large enough.
57 if (m_wmax < m_options.initCwnd) {
58 m_wmax = m_cwnd;
59 }
60
61 // 1. Time since last congestion event in seconds
62 const double t = (time::steady_clock::now() - m_lastDecrease).count() / 1e9;
63
64 // 2. Time it takes to increase the window to m_wmax = the cwnd right before the last
65 // window decrease.
66 // K = cubic_root(wmax*(1-beta_cubic)/C) (Eq. 2)
Davide Pesavento97a33b22019-10-17 22:10:47 -040067 const double k = std::cbrt(m_wmax * (1 - m_options.cubicBeta) / CUBIC_C);
Klaus Schneider9e5122b2019-03-19 17:03:25 -070068
69 // 3. Target: W_cubic(t) = C*(t-K)^3 + wmax (Eq. 1)
70 const double wCubic = CUBIC_C * std::pow(t - k, 3) + m_wmax;
71
Klaus Schneider4a2e89d2019-10-27 10:04:39 -070072 // 4. Estimate of Reno Increase (Eq. 4)
73 const double rtt = m_rttEstimator.getSmoothedRtt().count() / 1e9;
74 const double wEst = m_wmax * m_options.cubicBeta +
75 (3 * (1 - m_options.cubicBeta) / (1 + m_options.cubicBeta)) * (t / rtt);
Klaus Schneider9e5122b2019-03-19 17:03:25 -070076
77 // Actual adaptation
78 double cubicIncrement = std::max(wCubic, wEst) - m_cwnd;
79 // Cubic increment must be positive
80 // Note: This change is not part of the RFC, but I added it to improve performance.
81 cubicIncrement = std::max(0.0, cubicIncrement);
82
83 m_cwnd += cubicIncrement / m_cwnd;
84 }
85
86 emitSignal(afterCwndChange, time::steady_clock::now() - getStartTime(), m_cwnd);
87}
88
89void
90PipelineInterestsCubic::decreaseWindow()
91{
92 // A flow remembers the last value of wmax,
93 // before it updates wmax for the current congestion event.
94
95 // Current wmax < last_wmax
Davide Pesavento97a33b22019-10-17 22:10:47 -040096 if (m_options.enableFastConv && m_cwnd < m_lastWmax) {
Klaus Schneider9e5122b2019-03-19 17:03:25 -070097 m_lastWmax = m_cwnd;
Davide Pesavento97a33b22019-10-17 22:10:47 -040098 m_wmax = m_cwnd * (1.0 + m_options.cubicBeta) / 2.0;
Klaus Schneider9e5122b2019-03-19 17:03:25 -070099 }
100 else {
101 // Save old cwnd as wmax
102 m_lastWmax = m_cwnd;
103 m_wmax = m_cwnd;
104 }
105
Davide Pesavento97a33b22019-10-17 22:10:47 -0400106 m_ssthresh = std::max(m_options.initCwnd, m_cwnd * m_options.cubicBeta);
Klaus Schneider9e5122b2019-03-19 17:03:25 -0700107 m_cwnd = m_ssthresh;
108 m_lastDecrease = time::steady_clock::now();
109
110 emitSignal(afterCwndChange, time::steady_clock::now() - getStartTime(), m_cwnd);
111}
112
Davide Pesaventob3570c62022-02-19 19:19:00 -0500113} // namespace ndn::chunks