Debian Codegraph @

srtcore/window.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18
19 * Redistributions of source code must retain the above
20 copyright notice, this list of conditions and the
21 following disclaimer.
22
23 * Redistributions in binary form must reproduce the
24 above copyright notice, this list of conditions
25 and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 * Neither the name of the University of Illinois
29 nor the names of its contributors may be used to
30 endorse or promote products derived from this
31 software without specific prior written permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45
46 /*****************************************************************************
47 written by
48 Yunhong Gu, last updated 01/22/2011
49 modified by
50 Haivision Systems Inc.
51 *****************************************************************************/
52
53 #include "platform_sys.h"
54
55 #include <cmath>
56 #include <cstring>
57 #include "common.h"
58 #include "window.h"
59 #include <algorithm>
60
61 using namespace std;
62 using namespace srt::sync;
63
64 namespace srt
65 {
66 namespace ACKWindowTools
67 {
68
69 void store(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t ack)
70 {
71 r_aSeq[r_iHead].iACKSeqNo = seq;
72 r_aSeq[r_iHead].iACK = ack;
73 r_aSeq[r_iHead].tsTimeStamp = steady_clock::now();
74
75 r_iHead = (r_iHead + 1) % size;
76
77 // overwrite the oldest ACK since it is not likely to be acknowledged
78 if (r_iHead == r_iTail)
79 r_iTail = (r_iTail + 1) % size;
80 }
81
82 int acknowledge(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t& r_ack, const steady_clock::time_point& currtime)
83 {
84 // Head has not exceeded the physical boundary of the window
85 if (r_iHead >= r_iTail)
86 {
87 for (int i = r_iTail, n = r_iHead; i < n; ++ i)
88 {
89 // Looking for an identical ACK Seq. No.
90 if (seq == r_aSeq[i].iACKSeqNo)
91 {
92 // Return the Data ACK it carried
93 r_ack = r_aSeq[i].iACK;
94
95 // Calculate RTT estimate
96 const int rtt = (int)count_microseconds(currtime - r_aSeq[i].tsTimeStamp);
97
98 if (i + 1 == r_iHead)
99 {
100 r_iTail = r_iHead = 0;
101 r_aSeq[0].iACKSeqNo = SRT_SEQNO_NONE;
102 }
103 else
104 r_iTail = (i + 1) % size;
105
106 return rtt;
107 }
108 }
109
110 // The record about ACK is not found in the buffer, RTT can not be calculated
111 return -1;
112 }
113
114 // Head has exceeded the physical window boundary, so it is behind tail
115 for (int j = r_iTail, n = r_iHead + (int)size; j < n; ++ j)
116 {
117 // Looking for an identical ACK Seq. No.
118 if (seq == r_aSeq[j % size].iACKSeqNo)
119 {
120 // Return the Data ACK it carried
121 j %= size;
122 r_ack = r_aSeq[j].iACK;
123
124 // Calculate RTT estimate
125 const int rtt = (int)count_microseconds(currtime - r_aSeq[j].tsTimeStamp);
126
127 if (j == r_iHead)
128 {
129 r_iTail = r_iHead = 0;
130 r_aSeq[0].iACKSeqNo = -1;
131 }
132 else
133 r_iTail = (j + 1) % size;
134
135 return rtt;
136 }
137 }
138
139 // The record about ACK is not found in the buffer, RTT can not be calculated
140 return -1;
141 }
142
143 } // namespace AckTools
144 } // namespace srt
145
146 ////////////////////////////////////////////////////////////////////////////////
147
148 void srt::CPktTimeWindowTools::initializeWindowArrays(int* r_pktWindow, int* r_probeWindow, int* r_bytesWindow, size_t asize, size_t psize, size_t max_payload_size)
149 {
150 for (size_t i = 0; i < asize; ++ i)
151 r_pktWindow[i] = 1000000; //1 sec -> 1 pkt/sec
152
153 for (size_t k = 0; k < psize; ++ k)
154 r_probeWindow[k] = 1000; //1 msec -> 1000 pkts/sec
155
156 for (size_t i = 0; i < asize; ++ i)
157 r_bytesWindow[i] = int(max_payload_size); //based on 1 pkt/sec set in r_pktWindow[i]
158 }
159
160 int srt::CPktTimeWindowTools::ceilPerMega(double value, double count)
161 {
162 static const double MEGA = 1000.0 * 1000.0;
163 return int(::ceil(MEGA / (value / count)));
164 }
165
166 int srt::CPktTimeWindowTools::getPktRcvSpeed_in(const int* window, int* replica, const int* abytes, size_t asize, size_t hdr_size, int& w_bytesps)
167 {
168 PassFilter<int> filter = GetPeakRange(window, replica, asize);
169
170 unsigned count = 0;
171 int sum = 0;
172
173 w_bytesps = 0;
174 unsigned long bytes = 0;
175 // // (explicit specialization due to problems on MSVC 2013 and 2015)
176 AccumulatePassFilterParallel<unsigned, unsigned long>(window, asize, filter, abytes,
177 (sum), (count), (bytes));
178
179 // calculate speed, or return 0 if not enough valid value
180 if (count <= (asize/2))
181 {
182 w_bytesps = 0;
183 return 0;
184 }
185
186 bytes += (unsigned long)(hdr_size * count); //Add protocol headers to bytes received
187 w_bytesps = ceilPerMega(sum, bytes);
188 return ceilPerMega(sum, count);
189 }
190
191 int srt::CPktTimeWindowTools::getBandwidth_in(const int* window, int* replica, size_t psize)
192 {
193 PassFilter<int> filter = GetPeakRange(window, replica, psize);
194
195 int sum, count;
196 Tie2(sum, count) = AccumulatePassFilter(window, psize, filter);
197 sum += filter.median;
198 count += 1;
199
200 return ceilPerMega(sum, count);
201 }
202
203