-
Notifications
You must be signed in to change notification settings - Fork 36
/
pcc.h
452 lines (400 loc) · 13.3 KB
/
pcc.h
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <udt/udt.h>
#include <udt/ccc.h>
#include <iostream>
#include <cmath>
#define MAXCOUNT 1000
#define GRANULARITY 0.01
#define MUTATION_TH 300
#define JUMP_RANGE 0.05
#define NUMBER_OF_PROBE 4
#define MAX_COUNTINOUS_GUESS 5
#define MAX_COUNTINOUS_SEND 1
#define MAX_MONITOR_NUMBER 100
//#define DEBUGCC
//#define UTILITY_TRACE
using namespace std;
class BBCC: public CCC
{
public:
double utility_array[MAXCOUNT];
double rate_array[MAXCOUNT];
int array_pointer;
int target_monitor;
int starting_phase;
int make_guess;
int guess_result;
int moving_phase;
int moving_phase_initial;
int find_max;
int random_guess;
int random_guess_result;
int mutation_counter;
double rate_new;
int change_intense;
double change_amount;
int change_direction;
double rate_bucket[NUMBER_OF_PROBE];
int monitor_bucket[NUMBER_OF_PROBE];
double utility_bucket[NUMBER_OF_PROBE];
int recorded_number;
double current_rate;
double previous_rate;
double current_utility;
double previous_utility;
int guess_time;
int continous_guess_count;
int continous_send;
int continous_send_count;
int recording_guess_result;
int baseline;
double utility_baseline;
double start_rate_array[100];
int start_previous_monitor;
double start_previous_utility;
double previous_rtt;
public:
BBCC()
{
m_dPktSndPeriod = 10000;
m_dCWndSize = 100000.0;
setRTO(100000000);
starting_phase=1;
target_monitor = 0;
make_guess = 0;
guess_result = 0;
moving_phase = 0;
moving_phase_initial = 0;
change_direction=0;
change_intense=1;
guess_time = 0;
continous_guess_count = 0;
continous_send = 0;
continous_send_count =0;
previous_utility = 0;
previous_rate = 1;
current_rate = 1;
recording_guess_result = 0;
recorded_number = 0;
baseline = 0;
utility_baseline = 0;
for(int i=0;i<100;i++)
start_rate_array[i] = 0;
start_previous_monitor = -1;
start_previous_utility = -10000;
previous_rtt = 0;
}
public:
virtual void onLoss(const int32_t*, const int&) {
}
virtual void onTimeout(){
}
/* int findmax(double arr[]){
int tmp=0;
for(int i=0;i<1000;i++){
if(arr[i]>arr[tmp]&&stale[i]<20)
{
tmp=i;
}
}
return tmp;
}*/
virtual void onMonitorStart(int current_monitor){
if(starting_phase){
start_rate_array[current_monitor] = previous_rate*2;
#ifdef DEBUGCC
cerr<<"double rate to "<<start_rate_array[current_monitor]<<endl;
#endif
previous_rate = start_rate_array[current_monitor];
setRate(start_rate_array[current_monitor]);
return;
}
if(make_guess == 1){
#ifdef DEBUGCC
cerr<<"make guess!"<<continous_guess_count<<endl;
#endif
if(guess_time == 0 && continous_guess_count == MAX_COUNTINOUS_GUESS)
{
#ifdef DEBUGCC
cerr<<"skip guess"<<endl;
#endif
continous_guess_count =0;
}
if(guess_time == 0){
recording_guess_result = 1;
continous_guess_count++;
int rand_dir;
for(int i = 0; i < NUMBER_OF_PROBE; i++){
rand_dir = (rand()%2*2-1);
rate_bucket[i] = current_rate + rand_dir*continous_guess_count*GRANULARITY*current_rate;
rate_bucket[++i] = current_rate - rand_dir*continous_guess_count*GRANULARITY*current_rate;
#ifdef DEBUGCC
cerr<<"guess rate"<<rate_bucket[i-1]<<" "<<rate_bucket[i]<<endl;
#endif
}
for(int i = 0; i < NUMBER_OF_PROBE; i++){
monitor_bucket[i] = (current_monitor + i) % MAX_MONITOR_NUMBER;
#ifdef DEBUGCC
cerr<<"guess monitor"<<monitor_bucket[i]<<endl;
#endif
}
}
setRate(rate_bucket[guess_time]);
#ifdef DEBUGCC
cerr<<"setrate as "<<rate_bucket[guess_time]<<endl;
#endif
guess_time++;
//TODO:Here the sender stopped at a particular rate
if(guess_time == NUMBER_OF_PROBE){
#ifdef DEBUGCC
cerr<<"Guess exit!"<<endl;
#endif
make_guess = 0;
guess_time = 0;
}
}
if(continous_send == 1){
#ifdef DEBUGCC
cerr<<"CONTINOUS send"<<endl;
#endif
if(continous_send_count == 1){
setRate(current_rate);
}
if(continous_send_count < MAX_COUNTINOUS_SEND){
continous_send_count++;
#ifdef DEBUGCC
cerr<<"continous send"<<endl;
#endif
}else{
#ifdef DEBUGCC
cerr<<"clear continous send"<<endl;
#endif
continous_send = 0;
continous_send_count = 0;
continous_guess_count = 0;
make_guess = 1;
}
}
}
virtual void onMonitorEnds(int total, int loss __attribute((unused)), double time __attribute((unused)), int current, int endMonitor, double rtt){
double utility;
double t=total;
double l=loss;
//int random_direciton;
if(l<0)
l=0;
//utility = ((t-l)/time-20*l/time);
cerr<<rtt<<endl;
if(rtt==0) {
cerr<<"RTT cannot be 0!!!"<<endl;
}
if(previous_rtt==0)
previous_rtt = rtt;
//utility = ((t-l)/time*(1-1/(1+exp(-100*(l/t-0.05))))-1*l/time);
utility = ((t-l)/time*(1-1/(1+exp(-100*(l/t-0.05))))* (1-1/(1+exp(-10*(1-previous_rtt/rtt)))) -1*l/time)/rtt*1000;
previous_rtt = rtt;
if(endMonitor == 0 && starting_phase)
utility /=2;
#ifdef UTILITY_TRACE
cerr<<current_rate<<'\t'<<(t-l)*12/time/1000<<'\t'<<t<<'\t'<<l<<'\t'<<time<<"\t"<<utility<<"\t"<<m_iRTT<<endl;
#endif
// utility = (t-l)/time*(1-l/t)*(1-l/t)*(1-l/t)*(1-l/t)*(1-l/t);(m_iRTT/1000);(m_iRTT/1000);
#ifdef DEBUGCC
cerr<<"end number"<<endMonitor<<endl;
#endif
if(starting_phase){
if(endMonitor - 1 > start_previous_monitor){
if(start_previous_monitor == -1){
#ifdef DEBUGCC
cerr<<"fall back to guess mode"<<endl;
#endif
starting_phase = 0;
make_guess = 1;
setRate(start_rate_array[0]);
current_rate = start_rate_array[0];
return;
}else{
#ifdef DEBUGCC
cerr<<"exit because of loss"<<endl;
cerr<<"in monitor"<<start_previous_monitor<<endl;
cerr<<"fall back to due to loss"<<start_rate_array[start_previous_monitor]<<endl;
#endif
starting_phase = 0;
make_guess = 1;
setRate(start_rate_array[start_previous_monitor]);
current_rate = start_rate_array[start_previous_monitor];
return;}
}
if (start_previous_utility < utility){
#ifdef DEBUGCC
cerr<<"moving forward"<<endl;
#endif
// do nothing
start_previous_utility = utility;
start_previous_monitor = endMonitor;
return;
} else{
starting_phase = 0;
make_guess = 1;
setRate(start_rate_array[start_previous_monitor]);
current_rate = start_rate_array[start_previous_monitor];
#ifdef DEBUGCC
cerr<<"fall back to "<<start_rate_array[start_previous_monitor]<<endl;
#endif
previous_rate = current_rate;
return;
}
}
if(recording_guess_result){
/* if(current_rate>(t*12/time/1000+10) && current_rate > 200)
{
current_rate=t*12/time/1000;
make_guess = 1;
moving_phase = 0;
moving_phase_initial = 0;
change_direction=0;
change_intense=1;
guess_time = 0;
continous_guess_count = 0;
continous_send = 0;
continous_send_count =0;
recording_guess_result = 0;
recorded_number = 0;
setRate(current_rate);
cerr<<"trigger"<<endl;
return;
}*/
for(int i = 0; i<NUMBER_OF_PROBE; i++){
if(endMonitor == monitor_bucket[i]){
recorded_number++;
utility_bucket[i]=utility;
}
}
//TODO to let the sender go back to the current sending rate, one way is to
//let the decision maker stop for another monitor period,which might not be a good option, let's try this first
if(recorded_number == NUMBER_OF_PROBE){
recorded_number = 0;
double decision = 0;
for(int i=0; i < NUMBER_OF_PROBE; i++){
// TODO: I don't use this because I am afraid of the calculation precision problem
// decision += (utility_bucket[i]>utility_bucket[i+1])?(rate_bucket[i]-rate_bucket[i+1]):(rate_bucket[i+1]-rate_bucket[i]);
//cerr<<"utility"<<utility_bucket[i]<<" "<<utility_bucket[i+1]<<endl;
//cerr<<"rate"<<rate_bucket[i]<<" "<<rate_bucket[i+1]<<endl;
if(((utility_bucket[i]>utility_bucket[i+1])&&(rate_bucket[i]>rate_bucket[i+1]))||((utility_bucket[i]<utility_bucket[i+1])&&(rate_bucket[i]<rate_bucket[i+1])))
decision +=1;
else
decision -=1;
i++;
}
if(decision == 0){
make_guess = 1;
recording_guess_result = 0;
#ifdef DEBUGCC
cerr<<"no decision"<<endl;
#endif
}else{
change_direction = decision>0?1:-1;
#ifdef DEBUGCC
cerr<<"change to the direction of"<<change_direction<<endl;
#endif
recording_guess_result = 0;
target_monitor = (current+1)%MAX_MONITOR_NUMBER;
moving_phase_initial = 1;
change_intense = 1;
change_amount = (continous_guess_count/2+1)*change_intense*change_direction * GRANULARITY * current_rate;
previous_utility = 0;
continous_guess_count--; continous_guess_count=0;
if(continous_guess_count < 0)
continous_guess_count = 0;
previous_rate = current_rate;
current_rate = current_rate + change_amount;
target_monitor = (current+1)%MAX_MONITOR_NUMBER;
setRate(current_rate);
}
}
}
if(moving_phase_initial && endMonitor == target_monitor){
if(current_rate>(t*12/time/1000+30) && current_rate > 200)
{
current_rate=t*12/time/1000;
make_guess = 1;
moving_phase = 0;
moving_phase_initial = 0;
change_direction=0;
change_intense=1;
guess_time = 0;
continous_guess_count = 0;
continous_send = 0;
continous_send_count =0;
recording_guess_result = 0;
recorded_number = 0;
setRate(current_rate);
#ifdef DEBUGCC
cerr<<"system udp call speed limiting, resyncing rate"<<endl;
#endif
return;
}
#ifdef DEBUGCC
cerr<<"first time moving"<<endl;
#endif
target_monitor = (current+1)%MAX_MONITOR_NUMBER;
previous_rate = current_rate;
previous_utility = utility;
change_intense+=1;
change_amount = change_intense * GRANULARITY * current_rate * change_direction;
current_rate = current_rate + change_amount;
setRate(current_rate);
moving_phase_initial = 0;
moving_phase = 1;
}
if(moving_phase && endMonitor == target_monitor){
if(current_rate>(t*12/time/1000+30) && current_rate > 200)
{
current_rate=t*12/time/1000;
make_guess = 1;
moving_phase = 0;
moving_phase_initial = 0;
change_direction=0;
change_intense=1;
guess_time = 0;
continous_guess_count = 0;
continous_send = 0;
continous_send_count =0;
recording_guess_result = 0;
recorded_number = 0;
setRate(current_rate);
#ifdef DEBUGCC
cerr<<"system udp call speed limiting, resyncing rate"<<endl;
#endif
return;
}
#ifdef DEBUGCC
cerr<<"moving faster"<<endl;
#endif
current_utility = utility;
if(current_utility>previous_utility){
target_monitor = (current+1)%MAX_MONITOR_NUMBER;
change_intense+=1;
previous_utility = current_utility;
previous_rate = current_rate;
change_amount = change_intense * GRANULARITY * current_rate * change_direction;
current_rate = current_rate + change_amount;
setRate(current_rate);
}else{
moving_phase = 0;
make_guess = 1;
//change_intense+=1;
previous_utility = current_utility;
previous_rate = current_rate;
change_amount = change_intense * GRANULARITY * current_rate * change_direction;
current_rate = current_rate -change_amount;
setRate(current_rate);
}
}
}
virtual void onACK(const int& ack)
{int i=ack;i=i;}
void setRate(double mbps)
{
m_dPktSndPeriod = (m_iMSS * 8.0) / mbps;
}
};