-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcongestion_control.c
302 lines (265 loc) · 8.58 KB
/
congestion_control.c
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include "hercules.h"
#include "congestion_control.h"
#include "utils.h"
#define EPS_MIN 0.01f
#define EPS_MAX 0.05f
#define MSS 1460
struct ccontrol_state *
init_ccontrol_state(u32 max_rate_limit, u32 total_chunks, size_t num_paths, size_t max_paths, size_t total_num_paths)
{
struct ccontrol_state *cc_states = calloc(max_paths, sizeof(struct ccontrol_state));
for(size_t i = 0; i < max_paths; i++) {
struct ccontrol_state *cc_state = &cc_states[i];
cc_state->max_rate_limit = max_rate_limit;
cc_state->num_paths = num_paths;
cc_state->total_num_paths = total_num_paths;
pthread_spin_init(&cc_state->lock, PTHREAD_PROCESS_PRIVATE);
continue_ccontrol(cc_state);
}
return cc_states;
}
void ccontrol_start_monitoring_interval(struct ccontrol_state *cc_state)
{
cc_state->mi_start = get_nsecs();
cc_state->mi_end = cc_state->mi_start + cc_state->pcc_mi_duration * 1e9;
cc_state->mi_seq_start = cc_state->last_seqnr;
cc_state->mi_seq_end = 0;
cc_state->excess_npkts = 0;
atomic_store(&cc_state->mi_tx_npkts, 0);
atomic_store(&cc_state->mi_tx_npkts_monitored, 0);
if(cc_state->mi_nacked.bitmap != NULL) {
bitset__reset(&cc_state->mi_nacked);
}
}
void ccontrol_update_rtt(struct ccontrol_state *cc_state, u64 rtt)
{
cc_state->rtt = rtt / 1e9;
float m = (rand() % 6) / 10.f + 1.7; // m in [1.7, 2.2]
cc_state->pcc_mi_duration = m * cc_state->rtt;
if(cc_state->mi_nacked.bitmap != NULL) {
bitset__destroy(&cc_state->mi_nacked);
}
bitset__create(&cc_state->mi_nacked, ceil(cc_state->max_rate_limit * cc_state->pcc_mi_duration));
if(!cc_state->curr_rate) {
// initial rate should be per-receiver fair
u32 initial_rate = umin32(
(u32)(MSS / cc_state->rtt),
cc_state->max_rate_limit / (cc_state->num_paths * cc_state->total_num_paths)
);
cc_state->curr_rate = initial_rate;
cc_state->prev_rate = initial_rate;
}
// restart current MI
ccontrol_start_monitoring_interval(cc_state);
}
void terminate_ccontrol(struct ccontrol_state *cc_state)
{
cc_state->state = pcc_terminated;
cc_state->curr_rate = 0;
}
void continue_ccontrol(struct ccontrol_state *cc_state)
{
cc_state->prev_rate = cc_state->curr_rate;
cc_state->state = pcc_uninitialized;
cc_state->ignored_first_mi = false;
cc_state->eps = EPS_MIN;
cc_state->sign = 1;
cc_state->rcts_iter = -1;
cc_state->pcc_mi_duration = DBL_MAX;
cc_state->rtt = DBL_MAX;
ccontrol_start_monitoring_interval(cc_state);
}
u32 ccontrol_can_send_npkts(struct ccontrol_state *cc_state, u64 now)
{
if(cc_state->state == pcc_uninitialized) {
cc_state->state = pcc_startup;
cc_state->mi_start = get_nsecs();
cc_state->mi_end = cc_state->mi_start + cc_state->pcc_mi_duration * 1e9;
now = cc_state->mi_start;
}
u64 dt = now - cc_state->mi_start;
dt = umax64(dt, 1);
u32 tx_pps = atomic_load(&cc_state->mi_tx_npkts) * 1000000000. / dt;
if(tx_pps > cc_state->curr_rate) {
return 0;
}
return (cc_state->curr_rate - tx_pps) * cc_state->pcc_mi_duration;
}
void kick_ccontrol(struct ccontrol_state *cc_state)
{
// TODO can / should we get rid of this?
//cc_state->state = pcc_startup;
}
void destroy_ccontrol_state(struct ccontrol_state *cc_states, size_t num_paths)
{
free(cc_states);
}
// XXX: explicitly use symbols from old libc version to allow building on
// ubuntu 19.04 but running on ubuntu 16.04.
__asm__(".symver expf,expf@GLIBC_2.2.5");
static float sigmoid(float x)
{
float alpha = 100; // alpha > 0, to be chosen
return 1.f / (1.f + expf(alpha * x));
}
// PCC utility function
static float pcc_utility(float throughput, float loss)
{
return throughput * (1.f - loss) * sigmoid(loss - 0.05f) - throughput * loss;
}
// Startup state
static u32 pcc_control_startup(struct ccontrol_state *cc_state, float utility, float loss, u32 actual_rate)
{
if(utility > cc_state->prev_utility) {
cc_state->state = pcc_startup;
return 2 * cc_state->prev_rate;
} else {
// Update state: Startup -> Decision
cc_state->state = pcc_decision;
return cc_state->prev_rate * (1 - loss);
//return umin32(actual_rate, cc_state->prev_rate * (1 - loss));
}
}
static inline u32 calculate_rate(double mi_duration, u32 prev_rate, float factor) {
u32 new_rate = prev_rate * factor;
if(factor < 1) {
if((u32) (new_rate * mi_duration) > (u32) (prev_rate * mi_duration) - 10) {
new_rate = (prev_rate * mi_duration - 10) / mi_duration;
}
} else {
if((u32) (new_rate * mi_duration) < (u32) (prev_rate * mi_duration) + 10) {
new_rate = (prev_rate * mi_duration + 10) / mi_duration;
}
}
return new_rate;
}
// Setup randomized controlled trials
static void setup_rcts(struct ccontrol_state *cc_state)
{
float sign;
float eps = cc_state->eps;
int increase_first = rand() % 2;
for(int i = 0; i < RCTS_INTERVALS; i++) {
if((i % 2) == increase_first) {
sign = -1.f;
} else {
sign = 1.f;
}
struct rct trial = {.rate = calculate_rate(cc_state->pcc_mi_duration, cc_state->prev_rate, 1.f + sign * eps), .utility = 0.f};
cc_state->rcts[i] = trial;
}
cc_state->rcts_iter = 0;
cc_state->rate_before_rcts = cc_state->prev_rate;
}
// Check if RCTs are conclusive
static enum rcts_result rcts_decision(struct ccontrol_state *cc_state)
{
float winning_sign = 0.f;
static_assert(RCTS_INTERVALS % 2 == 0, "rcts_decision writes out of bounds if RCTS_INTERVALS is odd.");
for(int i = 0; i < RCTS_INTERVALS; i += 2) {
if(cc_state->rcts[i].utility > cc_state->rcts[i + 1].utility) {
winning_sign += cc_state->rcts[i].rate > cc_state->rcts[i + 1].rate ? 1.f : -1.f;
}
if(cc_state->rcts[i].utility < cc_state->rcts[i + 1].utility) {
winning_sign += cc_state->rcts[i].rate < cc_state->rcts[i + 1].rate ? 1.f : -1.f;
}
}
if(winning_sign > 0.f) {
return increase;
} else if(winning_sign < 0.f) {
return decrease;
} else {
return inconclusive;
}
}
// Decision making state
static u32 pcc_control_decision(struct ccontrol_state *cc_state, float utility, u32 actual_rate)
{
if(cc_state->rcts_iter == -1) {
// Init RCTs
setup_rcts(cc_state);
assert(cc_state->rcts_iter == 0);
cc_state->state = pcc_decision;
return cc_state->rcts[cc_state->rcts_iter].rate;
}
// RCTs in progress
// Collect result
cc_state->rcts[cc_state->rcts_iter].utility = utility;
if(cc_state->rcts_iter + 1 < RCTS_INTERVALS) {
// Move to next trial
cc_state->rcts_iter += 1;
cc_state->state = pcc_decision;
return cc_state->rcts[cc_state->rcts_iter].rate;
}
// RCTs completed
cc_state->rcts_iter = -1;
enum rcts_result decision = rcts_decision(cc_state);
if(decision != inconclusive) {
float trial_eps = cc_state->eps;
cc_state->eps = EPS_MIN; // reset eps for future control_decision calls
// Update state: Decision -> Adjust
if(decision == increase) {
cc_state->sign = 1.f;
}
if(decision == decrease) {
cc_state->sign = -1.f;
}
cc_state->state = pcc_adjust;
return cc_state->rate_before_rcts * (1 + cc_state->sign * trial_eps);
} else {
// Return to prev_rate, update eps
// (Possible optimization: already setup and start new RCTs here for more reactive behavior)
cc_state->eps = fmin(cc_state->eps + EPS_MIN, EPS_MAX);
cc_state->state = pcc_decision;
return cc_state->rate_before_rcts;
}
}
// Rate adjusting state
static u32 pcc_control_adjust(struct ccontrol_state *cc_state, float utility, u32 actual_rate)
{
if(utility > cc_state->prev_utility) {
int n = cc_state->adjust_iter;
float sign = cc_state->sign;
cc_state->adjust_iter += 1;
cc_state->state = pcc_adjust;
return calculate_rate(cc_state->pcc_mi_duration, cc_state->prev_rate, 1.f + sign * n * EPS_MIN);
} else {
// Update state: Adjust -> Decision
cc_state->state = pcc_decision;
cc_state->adjust_iter = 1;
return cc_state->prev_rate;
}
}
u32 pcc_control(struct ccontrol_state *cc_state, float throughput, float loss)
{
if(cc_state->state == pcc_uninitialized || cc_state->state == pcc_terminated) {
return 0;
}
cc_state->prev_rate = cc_state->curr_rate;
float utility = pcc_utility(throughput, loss);
u32 new_rate = cc_state->prev_rate;
enum pcc_state current_pcc_state = cc_state->state;
switch(current_pcc_state) {
case pcc_startup:
new_rate = pcc_control_startup(cc_state, utility, loss, throughput);
break;
case pcc_decision:
new_rate = pcc_control_decision(cc_state, utility, throughput);
break;
case pcc_adjust:
new_rate = pcc_control_adjust(cc_state, utility, throughput);
break;
default:
fprintf(stderr, "Invalid PCC state: %d\n", current_pcc_state);
cc_state->state = pcc_startup;
}
new_rate = umin32(umax32(1000, new_rate), cc_state->max_rate_limit);
cc_state->prev_utility = utility;
cc_state->curr_rate = new_rate;
return new_rate;
}