Repository navigation
Expand file tree
/
Copy pathAdaptiveWindow.cs
More file actions
134 lines (130 loc) · 6.86 KB
/
Copy pathAdaptiveWindow.cs
File metadata and controls
134 lines (130 loc) · 6.86 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace ModemAPI
{
/// <summary>
/// The window of an SRTP sender, that follows the path instead of always being full. More packets in flight than the path
/// holds (its delivery rate times its round trip) don't arrive faster: they wait in queues on the way (of the modems, of
/// the address machines, of the network), and long queues cost round trip, memory, garbage collection and, once full, losses.
/// So the window is twice what the path holds, as BBR does: the highest delivery rate of the last rounds times the smallest
/// round trip measured lately, doubled, plus room for the acknowledgements, that come for every 16 packets. The sender's own
/// choice: nothing changes on the wire, and the receiver doesn't depend on it.
/// <para>
/// A round ends when a packet sent at its start is acknowledged. The rate of a round is the packets acknowledged in it per
/// its duration. While the window is what limits the sender, doubling it doubles the rate of the next round, so it grows
/// fast until the path is full. Every 10 s the window drops to the minimum for two rounds to measure the round trip without
/// a queue again (the route may change, and a queue that never drains would look like the round trip itself).
/// </para>
/// <para>
/// Small packets get a cushion of <see cref="CushionBytes"/> on top: every packet costs the modems and the address machines
/// some work of its own, and a fuller queue lets them read and write many packets at once, so small packets keep getting
/// faster with more of them in flight. The cushion is never more than <see cref="CushionTimeTicks"/> of the delivery rate,
/// so on a slow line it is a packet or two and doesn't make a queue there.
/// </para>
/// </summary>
internal sealed class AdaptiveWindow
{
/// <summary>
/// Not smaller: a receiver acknowledges every 16 delivered packets (<see cref="SRTPClient.AckEveryPackets"/>), fewer
/// in flight would be acknowledged only by its delay timer.
/// </summary>
internal const int MinimumWindow = 32;
internal const int CushionBytes = 512 * 1024;
internal static readonly long CushionTimeTicks = TimeSpan.FromMilliseconds(5).Ticks;
// how many times what the path holds may be in flight
private const double Gain = 2;
// the delivery rate is the highest of this many last rounds: one slow round doesn't shrink the window
private const int RateRounds = 10;
internal static readonly long ProbeIntervalTicks = TimeSpan.FromSeconds(10).Ticks;
private long MinRoundTrip = long.MaxValue;
private long MinRoundTripMeasuredAt = 0;
private long RoundEnd = 0;
private long RoundStartedAt = -1;
private long RoundAcknowledged = 0;
private readonly double[] Rates = new double[RateRounds]; // packets per tick
private int RateIndex = 0;
// 0: normal, 1: draining the queue before a probe, 2: measuring the round trip without a queue
private int ProbePhase = 0;
private int Current = MinimumWindow;
// a rough running average of the sent packets: senders may race on it, that doesn't matter
private int AveragePacketBytes = 1024;
/// <summary>
/// The window now: how many packets may be in flight, not more than the given maximum.
/// </summary>
internal int Get(int maximum)
{
return Math.Min(Volatile.Read(ref Current), maximum);
}
/// <summary>
/// Takes the size of a sent packet into the average one, that turns the cushion into packets.
/// </summary>
internal void PacketSent(int bytes)
{
int average = Volatile.Read(ref AveragePacketBytes);
Volatile.Write(ref AveragePacketBytes, average + (bytes - average) / 8);
}
/// <summary>
/// Takes an acknowledgement. Called under one lock (the acknowledgement lock of the client).
/// </summary>
/// <param name="acknowledgedId">The highest sequential number it acknowledged</param>
/// <param name="acknowledgedCount">How many packets it acknowledged</param>
/// <param name="nextId">The number the next sent packet will get</param>
/// <param name="maximum">The largest window allowed (MaxUnackedPackets)</param>
/// <param name="nowTicks">Now, in TimeSpan ticks of any monotonic clock</param>
internal void Sample(long acknowledgedId, int acknowledgedCount, long nextId, int maximum, long nowTicks)
{
if (RoundStartedAt < 0)
{
RoundStartedAt = nowTicks;
}
RoundAcknowledged += acknowledgedCount;
if (acknowledgedId < RoundEnd)
{
return;
}
// a round is complete: the packet sent at its start is acknowledged, so the round lasted its whole round trip,
// with the wait for the acknowledgement of a batch (the round trip of the single fastest packet is shorter than that)
long duration = nowTicks - RoundStartedAt;
if (duration > 0 && ProbePhase != 1)
{
if (duration <= MinRoundTrip)
{
MinRoundTrip = duration;
MinRoundTripMeasuredAt = nowTicks;
}
if (ProbePhase == 0)
{
Rates[RateIndex] = (double)RoundAcknowledged / duration;
RateIndex = (RateIndex + 1) % RateRounds;
}
}
switch (ProbePhase)
{
case 1:
// the queue of the old window is gone; the next round measures the round trip of the small window
ProbePhase = 2;
MinRoundTrip = long.MaxValue;
break;
case 2:
ProbePhase = 0;
break;
default:
if (nowTicks - MinRoundTripMeasuredAt > ProbeIntervalTicks)
{
ProbePhase = 1;
}
break;
}
RoundStartedAt = nowTicks;
RoundAcknowledged = 0;
RoundEnd = nextId;
Volatile.Write(ref Current, ProbePhase == 0 ? Window(maximum) : MinimumWindow);
}
private int Window(int maximum)
{
double rate = Rates.Max();
double cushion = Math.Min((double)CushionBytes / Math.Max(1, Volatile.Read(ref AveragePacketBytes)), rate * CushionTimeTicks);
// room for two acknowledgements of 16 packets each on top of what the path holds
double window = Gain * rate * MinRoundTrip + cushion + 2 * 16;
return (int)Math.Clamp(window, MinimumWindow, Math.Max(maximum, MinimumWindow));
}
}
}