-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmpsock_scheduler_algorithms.c
426 lines (370 loc) · 12.8 KB
/
mpsock_scheduler_algorithms.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
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
#include "mpsock_scheduler_algorithms.h"
#include "mpsock_socket.h"
#include "mpsock_pool.h"
#include "mpsock_connection.h"
size_t dynamic_alpha_algorithm(mpsock_scheduler *scheduler)
{
/* ================== alpha estimate ==================== */
size_t chunk_size = 0;
size_t bandwidth_sample = 0;
double diff = (double)(scheduler->last_read.tv_sec - scheduler->send_start.tv_sec);
diff += (double)(scheduler->last_read.tv_usec - scheduler->send_start.tv_usec)/1000000;
LOG_DEBUG("%ssd#%d difference: %f",RESULT_EVENT,scheduler->connection->sd,diff);
if(USE_ASSERTS) assert(diff >= 0);
if(diff > 0)
{
bandwidth_sample = scheduler->total_number_bytes/diff;
if(USE_ASSERTS) assert(bandwidth_sample > 0);
scheduler->last_bandwidth = bandwidth_sample;
}
if(scheduler->bandwidth > 0 && scheduler->last_bandwidth > 0)
{
//if((1+OPTIMAL_RANGE_DIFF)*scheduler->last_bandwidth < scheduler->bandwidth || scheduler->is_decreased)
if((1+OPTIMAL_RANGE_DIFF)*scheduler->last_bandwidth < bandwidth_sample || scheduler->is_decreased)
{
// if chunk_size is already too big, then it does not make sense to raise alpha
if(scheduler->alpha * scheduler->rtt * scheduler->bandwidth < MAXIMUM_CHUNK_SIZE)
{
// bandwidth rose a lot -> raise alpha
//scheduler->alpha *= ALPHA_RAISE_BIG;
scheduler->alpha += ALPHA_LINEAR_INC;
LOG_DEBUG("%ssd#%d raise alpha to %f",COND_EVENT,scheduler->connection->sd,scheduler->alpha);
}
scheduler->is_decreased = FALSE;
}
else
{
if(!scheduler->is_decreased)
{
scheduler->alpha *= ALPHA_DECREASE;
scheduler->is_decreased = TRUE;
LOG_DEBUG("%ssd#%d decrease alpha to %f",COND_EVENT,scheduler->connection->sd,scheduler->alpha);
}
}
/*
else if(scheduler->last_bandwidth < scheduler->bandwidth)
{
// TODO: verify
// if chunk_size is already too big, then it does not make sense to raise alpha
if(scheduler->alpha * scheduler->rtt * scheduler->bandwidth < MAXIMUM_CHUNK_SIZE)
{
// bandwidth rose a lot -> raise alpha
scheduler->alpha *= ALPHA_RAISE_SMALL;
LOG_INFO("%ssd#%d small raise alpha to %f",COND_EVENT,scheduler->connection->sd,scheduler->alpha);
}
}
else if((1-OPTIMAL_RANGE_DIFF)*scheduler->last_bandwidth > scheduler->bandwidth)
{
// bandwidth smaller now -> fall back to previous alpha
scheduler->alpha *= (1/ALPHA_RAISE_BIG);
LOG_INFO("%ssd#%d lowered alpha to %f",COND_EVENT,scheduler->connection->sd,scheduler->alpha);
}
*/
// TODO: make alpha dynamic
//scheduler->alpha = INITIAL_ALPHA;
LOG_DEBUG("%ssd#%d current alpha = %f",RESULT_EVENT,scheduler->connection->sd,scheduler->alpha);
}
/* ====================================================== */
// set current bandwidth as last used
//scheduler->last_bandwidth = scheduler->bandwidth;
chunk_size = scheduler->alpha * scheduler->rtt * scheduler->bandwidth;
//size_t chunk_size = scheduler->bandwidth;
if(chunk_size == 0)
{
// TODO: maybe better
LOG_DEBUG("%snot enough reads to estimate bandwidth --> just doubling current chunk_size",COND_EVENT);
//chunk_size = scheduler->connection->chunk_size * 2;
chunk_size = scheduler->connection->pool->mpsocket->initial_chunk_size;
}
return chunk_size;
}
size_t time_chunk_algorithm(struct mpsock_scheduler *scheduler)
{
size_t chunk_size = 0;
// initial case
if(scheduler->bandwidth == 0 || scheduler->rtt == 0) return scheduler->connection->pool->mpsocket->initial_chunk_size;
if(USE_ASSERTS) assert(scheduler->bandwidth > 0);
if(USE_ASSERTS) assert(scheduler->rtt > 0);
/* ============= Calculate Time Slice ============ */
double time_span = 0;
double t_max = scheduler->connection->pool->mpsocket->alpha_max*scheduler->rtt;
double t_min = ALPHA_MIN*scheduler->rtt;
size_t remaining_bytes = scheduler->connection->pool->current_file_size - scheduler->connection->pool->next_start_byte;
if(USE_ASSERTS) assert(remaining_bytes >= 0);
// determine beta
size_t beta = -1;
mpsock_connection *current_con;
mpsock_connection *beta_connection = NULL;
lock_for_scheduling(scheduler->connection->pool);
lock_for_next_chunk(scheduler->connection->pool);
for(current_con = scheduler->connection->pool->connection_table; current_con != NULL; current_con = current_con->hh.next)
{
if(current_con == scheduler->connection || current_con->scheduler->bandwidth == 0 || current_con->current_chunk == NULL || !current_con->is_used) continue;
size_t bytes_left = current_con->current_chunk->data_size - current_con->current_chunk->pos_save;
double delta_t = ((double)(bytes_left)) / ((double)(current_con->scheduler->bandwidth));
if(USE_ASSERTS) assert(delta_t >= 0);
if(beta < 0 || beta > delta_t)
{
beta_connection = current_con;
beta = delta_t;
}
}
if(beta < 0)
{
beta_connection = NULL;
beta = 0;
}
size_t beta_bandwidth = 0;
double beta_t_max = 0;
if(beta_connection != NULL)
{
beta_bandwidth = beta_connection->scheduler->bandwidth;
beta_t_max = beta_connection->scheduler->rtt * scheduler->connection->pool->mpsocket->alpha_max;
if(USE_MIN_T_MAX)
{
if(t_max > beta_t_max && beta_t_max > 0)
{
t_max = beta_t_max;
}
else if(t_max > 0)
{
beta_t_max = t_max;
}
}
}
if(USE_ASSERTS) assert(beta_connection != scheduler->connection);
if(USE_ASSERTS) assert(beta >= 0);
unlock_for_next_chunk(scheduler->connection->pool);
unlock_for_scheduling(scheduler->connection->pool);
/* =================================== 4-CASE-ALGORITHM START =========================================== */
/* 1. T_max,i * R_i >= s - x*/
/*
if(t_max * scheduler->bandwidth >= remaining_bytes)
{
if(beta * scheduler->bandwidth >= remaining_bytes || (scheduler->connection->pool->finishing && !FLAG_MAX_ALGO))
{
// download entire remaining file
//time_span = ((double)(remaining_bytes)) / scheduler->bandwidth;
// to ensure complete download and avoid rounding errors
chunk_size = remaining_bytes;
LOG_DEBUG("%ssd#%d 1.1",RESULT_EVENT,scheduler->connection->sd);
}
else if(scheduler->connection->pool->finishing && FLAG_MAX_ALGO)
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
if(time_span < t_min)
{
time_span = t_min;
}
LOG_DEBUG("%ssd#%d 1.3",RESULT_EVENT,scheduler->connection->sd);
}
else
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
scheduler->connection->pool->finishing = TRUE;
LOG_DEBUG("%ssd#%d 1.2: %f == %zu",RESULT_EVENT,scheduler->connection->sd,time_span*scheduler->bandwidth + (time_span - beta)*(double)(beta_bandwidth),remaining_bytes);
}
}
*/
/* T_max,i * R_i < s - x*/
/*
else
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
if(time_span > t_max || (time_span-beta) > beta_t_max)
{
time_span = t_max;
LOG_DEBUG("%ssd#%d 2.2",RESULT_EVENT,scheduler->connection->sd);
}
else if(FLAG_MAX_ALGO)
{
if(time_span < t_min)
{
time_span = t_min;
}
scheduler->connection->pool->finishing = TRUE;
LOG_DEBUG("%ssd#%d 2.3",RESULT_EVENT,scheduler->connection->sd);
}
else
{
scheduler->connection->pool->finishing = TRUE;
LOG_DEBUG("%ssd#%d 2.1: %f == %zu",RESULT_EVENT,scheduler->connection->sd,time_span*scheduler->bandwidth + (time_span - beta)*(double)(beta_bandwidth),remaining_bytes);
}
}
*/
/* =================================== 4-CASE-ALGORITHM END =========================================== */
/* =================================== 3-CASE-ALGORITHM START =========================================== */
if(scheduler->connection->pool->mpsocket->scheduler_version == 0)
{
/* case 1 */
if(t_max * scheduler->bandwidth >= remaining_bytes && (beta * scheduler->bandwidth >= remaining_bytes || scheduler->connection->pool->finishing))
{
// download entire remaining file
//time_span = ((double)(remaining_bytes)) / scheduler->bandwidth;
// to ensure complete download and avoid rounding errors
chunk_size = remaining_bytes;
LOG_INFO("%ssd#%d Scheduler Case 1",RESULT_EVENT,scheduler->connection->sd);
}
/* T_max,i * R_i < s - x*/
else
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
/* case 3 */
if(time_span > t_max || (time_span-beta) > beta_t_max)
{
time_span = t_max;
LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
}
/* case 2 */
else
{
if(time_span < t_min && FLAG_MAX_ALGO)
{
time_span = t_min;
}
scheduler->connection->pool->finishing = TRUE;
LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
}
///* case 2 */
//if(time_span < t_max)
//{
// if(time_span < t_min && FLAG_MAX_ALGO)
// {
// time_span = t_min;
// }
//
// scheduler->connection->pool->finishing = TRUE;
// LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
//
//}
///* case 3 */
//else
//{
// time_span = t_max;
// LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
//}
}
}
else if(scheduler->connection->pool->mpsocket->scheduler_version == 1)
{
/* case 1 */
if(t_max * scheduler->bandwidth >= remaining_bytes && (beta * scheduler->bandwidth >= remaining_bytes || scheduler->connection->pool->finishing))
{
// download entire remaining file
//time_span = ((double)(remaining_bytes)) / scheduler->bandwidth;
// to ensure complete download and avoid rounding errors
chunk_size = remaining_bytes;
LOG_INFO("%ssd#%d Scheduler Case 1",RESULT_EVENT,scheduler->connection->sd);
}
/* T_max,i * R_i < s - x*/
else
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
///* case 3 */
//if(time_span > t_max || (time_span-beta) > beta_t_max)
//{
// time_span = t_max;
// LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
//}
///* case 2 */
//else
//{
// if(time_span < t_min && FLAG_MAX_ALGO)
// {
// time_span = t_min;
// }
//
// scheduler->connection->pool->finishing = TRUE;
// LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
//}
/* case 2 */
if(time_span < t_max)
{
if(time_span < t_min && FLAG_MAX_ALGO)
{
time_span = t_min;
}
scheduler->connection->pool->finishing = TRUE;
LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
}
/* case 3 */
else
{
time_span = t_max;
LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
}
}
}
else if(scheduler->connection->pool->mpsocket->scheduler_version == 2)
{
/* case 1 */
if(t_max * scheduler->bandwidth >= remaining_bytes && (beta * scheduler->bandwidth >= remaining_bytes || (scheduler->connection->pool->finishing && scheduler->bandwidth > 0.9*beta_bandwidth)))
{
// download entire remaining file
//time_span = ((double)(remaining_bytes)) / scheduler->bandwidth;
// to ensure complete download and avoid rounding errors
chunk_size = remaining_bytes;
LOG_INFO("%ssd#%d Scheduler Case 1",RESULT_EVENT,scheduler->connection->sd);
}
/* T_max,i * R_i < s - x*/
else
{
time_span = ((double)(remaining_bytes + beta*(double)(beta_bandwidth)))/((double)(scheduler->bandwidth + beta_bandwidth));
///* case 3 */
//if(time_span > t_max || (time_span-beta) > beta_t_max)
//{
// time_span = t_max;
// LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
//}
///* case 2 */
//else
//{
// if(time_span < t_min && FLAG_MAX_ALGO)
// {
// time_span = t_min;
// }
//
// scheduler->connection->pool->finishing = TRUE;
// LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
//}
/* case 2 */
if(time_span < t_max)
{
if(time_span < t_min && FLAG_MAX_ALGO)
{
time_span = t_min;
}
scheduler->connection->pool->finishing = TRUE;
LOG_INFO("%ssd#%d Scheduler Case 2",RESULT_EVENT,scheduler->connection->sd);
}
/* case 3 */
else
{
time_span = t_max;
LOG_INFO("%ssd#%d Scheduler Case 3",RESULT_EVENT,scheduler->connection->sd);
}
}
}
else
{
perror("undefined scheduler version");
exit(1);
}
/* =================================== 3-CASE-ALGORITHM END =========================================== */
// adjust to t_min
//if(time_span < t_min) time_span = t_min;
/* =============================================== */
// convert to byte size
if(chunk_size == 0)
{
chunk_size = time_span * scheduler->bandwidth;
}
if(chunk_size == 0)
{
chunk_size = scheduler->connection->pool->mpsocket->initial_chunk_size;
}
else
{
}
return chunk_size;
}