Statistics
| Branch: | Revision:

streamers / streaming.c @ 7b86e7d9

History | View | Annotate | Download (15.2 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <sys/time.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <stdint.h>
11
#include <stdbool.h>
12
#include <math.h>
13
#include <assert.h>
14

    
15
#include <net_helper.h>
16
#include <chunk.h> 
17
#include <chunkbuffer.h> 
18
#include <trade_msg_la.h>
19
#include <trade_msg_ha.h>
20
#include <peerset.h>
21
#include <peer.h>
22
#include <chunkidset.h>
23
#include <limits.h>
24
#include <trade_sig_ha.h>
25

    
26
#include "streaming.h"
27
#include "output.h"
28
#include "input.h"
29
#include "dbg.h"
30
#include "chunk_signaling.h"
31
#include "chunklock.h"
32
#include "topology.h"
33
#include "measures.h"
34
#include "scheduling.h"
35

    
36
#include "scheduler_la.h"
37

    
38
struct chunk_attributes {
39
  uint64_t deadline;
40
  uint16_t deadline_increment;
41
  uint16_t hopcount;
42
} __attribute__((packed));
43

    
44
extern bool chunk_log;
45

    
46
struct chunk_buffer *cb;
47
static struct input_desc *input;
48
static int cb_size;
49
static int transid=0;
50

    
51
static int offer_per_tick = 1;        //N_p parameter of POLITO
52

    
53
int _needs(struct chunkID_set *cset, int cb_size, int cid);
54

    
55
uint64_t gettimeofday_in_us(void)
56
{
57
  struct timeval what_time; //to store the epoch time
58

    
59
  gettimeofday(&what_time, NULL);
60
  return what_time.tv_sec * 1000000ULL + what_time.tv_usec;
61
}
62

    
63
void cb_print()
64
{
65
#ifdef DEBUG
66
  struct chunk *chunks;
67
  int num_chunks, i, id;
68
  chunks = cb_get_chunks(cb, &num_chunks);
69

    
70
  dprintf("\tchbuf :");
71
  i = 0;
72
  if(num_chunks) {
73
    id = chunks[0].id;
74
    dprintf(" %d-> ",id);
75
    while (i < num_chunks) {
76
      if (id == chunks[i].id) {
77
        dprintf("%d",id % 10);
78
        i++;
79
      } else if (chunk_islocked(id)) {
80
        dprintf("*");
81
      } else {
82
        dprintf(".");
83
      }
84
      id++;
85
    }
86
  }
87
  dprintf("\n");
88
#endif
89
}
90

    
91
void stream_init(int size, struct nodeID *myID)
92
{
93
  static char conf[32];
94

    
95
  cb_size = size;
96

    
97
  sprintf(conf, "size=%d", cb_size);
98
  cb = cb_init(conf);
99
  chunkDeliveryInit(myID);
100
  chunkSignalingInit(myID);
101
  init_measures();
102
}
103

    
104
int source_init(const char *fname, struct nodeID *myID, bool loop, int *fds, int fds_size)
105
{
106
  input = input_open(fname, loop ? INPUT_LOOP : 0, fds, fds_size);
107
  if (input == NULL) {
108
    return -1;
109
  }
110

    
111
  stream_init(1, myID);
112
  return 0;
113
}
114

    
115
void chunk_attributes_fill(struct chunk* c)
116
{
117
  struct chunk_attributes * ca;
118

    
119
  assert(!c->attributes && c->attributes_size == 0);
120

    
121
  c->attributes_size = sizeof(struct chunk_attributes);
122
  c->attributes = ca = malloc(c->attributes_size);
123

    
124
  ca->deadline = c->timestamp;
125
  ca->deadline_increment = 2;
126
  ca->hopcount = 0;
127
}
128

    
129
int chunk_get_hopcount(struct chunk* c) {
130
  struct chunk_attributes * ca;
131

    
132
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
133
    fprintf(stderr,"Warning, chunk %d with strange attributes block. Size:%d expected:%d\n", c->id, c->attributes ? c->attributes_size : 0, sizeof(struct chunk_attributes));
134
    return -1;
135
  }
136

    
137
  ca = (struct chunk_attributes *) c->attributes;
138
  return ca->hopcount;
139
}
140

    
141
void chunk_attributes_update_received(struct chunk* c)
142
{
143
  struct chunk_attributes * ca;
144

    
145
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
146
    fprintf(stderr,"Warning, received chunk %d with strange attributes block. Size:%d expected:%d\n", c->id, c->attributes ? c->attributes_size : 0, sizeof(struct chunk_attributes));
147
    return;
148
  }
149

    
150
  ca = (struct chunk_attributes *) c->attributes;
151
  ca->hopcount++;
152
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
153
}
154

    
155
void chunk_attributes_update_sending(struct chunk* c)
156
{
157
  struct chunk_attributes * ca;
158

    
159
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
160
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
161
    return;
162
  }
163

    
164
  ca = (struct chunk_attributes *) c->attributes;
165
  ca->deadline += ca->deadline_increment;
166
  dprintf("Sending chunk %d with deadline %lu\n", c->id, ca->deadline);
167
}
168

    
169
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
170
{
171
  struct chunk *chunks;
172
  int num_chunks, i;
173
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
174
  chunks = cb_get_chunks(chbuf, &num_chunks);
175

    
176
  for(i=num_chunks-1; i>=0; i--) {
177
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
178
  }
179
  return my_bmap;
180
}
181

    
182
// a simple implementation that request everything that we miss ... up to max deliver
183
struct chunkID_set *get_chunks_to_accept(struct peer *from, const struct chunkID_set *cset_off, int max_deliver, int trans_id){
184
  struct chunkID_set *cset_acc, *my_bmap;
185
  int i, d, cset_off_size;
186
  //double lossrate;
187

    
188
  cset_acc = chunkID_set_init("size=0");
189

    
190
  //reduce load a little bit if there are losses on the path from this guy
191
  //lossrate = get_lossrate_receive(from->id);
192
  //lossrate = finite(lossrate) ? lossrate : 0;        //start agressively, assuming 0 loss
193
  //if (rand()/((double)RAND_MAX + 1) >= 10 * lossrate ) {
194
    my_bmap = cb_to_bmap(cb);
195
    cset_off_size = chunkID_set_size(cset_off);
196
    for (i = 0, d = 0; i < cset_off_size && d < max_deliver; i++) {
197
      int chunkid = chunkID_set_get_chunk(cset_off, i);
198
      //dprintf("\tdo I need c%d ? :",chunkid);
199
      if (!chunk_islocked(chunkid) && _needs(my_bmap, cb_size, chunkid)) {
200
        chunkID_set_add_chunk(cset_acc, chunkid);
201
        chunk_lock(chunkid,from);
202
        dtprintf("accepting %d from %s", chunkid, node_addr(from->id));
203
#ifdef MONL
204
        dprintf(", loss:%f rtt:%f", get_lossrate(from->id), get_rtt(from->id));
205
#endif
206
        dprintf("\n");
207
        d++;
208
      }
209
    }
210
    chunkID_set_free(my_bmap);
211
  //} else {
212
  //    dtprintf("accepting -- from %s loss:%f rtt:%f\n", node_addr(from->id), lossrate, get_rtt(from->id));
213
  //}
214

    
215
  return cset_acc;
216
}
217

    
218
void send_bmap(struct peer *to)
219
{
220
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
221
   sendBufferMap(to->id,NULL, my_bmap, input ? 0 : cb_size, 0);
222
  chunkID_set_free(my_bmap);
223
}
224

    
225
double get_average_lossrate_pset(struct peerset *pset)
226
{
227
  int i, n;
228
  struct peer *neighbours;
229

    
230
  n = peerset_size(pset);
231
  neighbours = peerset_get_peers(pset);
232
  {
233
    struct nodeID *nodeids[n];
234
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
235
#ifdef MONL
236
    return get_average_lossrate(nodeids, n);
237
#else
238
    return 0;
239
#endif
240
  }
241
}
242

    
243
void ack_chunk(struct chunk *c, struct peer *p)
244
{
245
  //reduce load a little bit if there are losses on the path from this guy
246
  double average_lossrate = get_average_lossrate_pset(get_peers());
247
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
248
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
249
    return;
250
  }
251
  send_bmap(p);        //send explicit ack
252
}
253

    
254
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
255
{
256
  int res;
257
  static struct chunk c;
258
  struct peer *p;
259

    
260
  res = decodeChunk(&c, buff + 1, len - 1);
261
  if (res > 0) {
262
    chunk_attributes_update_received(&c);
263
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c));
264
    chunk_unlock(c.id);
265
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
266
    if(chunk_log){fprintf(stderr, "TEO: Received chunk %d from peer: %s at: %lld hopcount: %i\n", c.id, node_addr(from), gettimeofday_in_us(), chunk_get_hopcount(&c));}
267
    output_deliver(&c);
268
    res = cb_add_chunk(cb, &c);
269
    cb_print();
270
    if (res < 0) {
271
      dprintf("\tchunk too old, buffer full with newer chunks\n");
272
      if(chunk_log){fprintf(stderr, "TEO: Received chunk: %d too old (buffer full with newer chunks) from peer: %s at: %lld\n", c.id, node_addr(from), gettimeofday_in_us());}
273
      free(c.data);
274
      free(c.attributes);
275
    }
276
    p = nodeid_to_peer(from,1);
277
    if (p) {        //now we have it almost sure
278
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
279
      ack_chunk(&c,p);        //send explicit ack
280
    }
281
  } else {
282
    fprintf(stderr,"\tError: can't decode chunk!\n");
283
  }
284
}
285

    
286
struct chunk *generated_chunk(suseconds_t *delta)
287
{
288
  struct chunk *c;
289

    
290
  c = malloc(sizeof(struct chunk));
291
  if (!c) {
292
    fprintf(stderr, "Memory allocation error!\n");
293
    return NULL;
294
  }
295

    
296
  *delta = input_get(input, c);
297
  if (*delta < 0) {
298
    fprintf(stderr, "Error in input!\n");
299
    exit(-1);
300
  }
301
  if (c->data == NULL) {
302
    free(c);
303
    return NULL;
304
  }
305
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
306
  chunk_attributes_fill(c);
307
  return c;
308
}
309

    
310
int add_chunk(struct chunk *c)
311
{
312
  int res;
313

    
314
  res = cb_add_chunk(cb, c);
315
  if (res < 0) {
316
    free(c->data);
317
    free(c->attributes);
318
    free(c);
319
    return 0;
320
  }
321
  free(c);
322
  return 1;
323
}
324

    
325
/**
326
 *example function to filter chunks based on whether a given peer needs them.
327
 *
328
 * Looks at buffermap information received about the given peer.
329
 */
330
int needs(struct peer *n, int cid){
331
  struct peer * p = n;
332

    
333
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
334
  if (! p->bmap) {
335
    //dprintf("no bmap\n");
336
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
337
  }
338
  return _needs(p->bmap, p->cb_size, cid);
339
}
340

    
341
int _needs(struct chunkID_set *cset, int cb_size, int cid){
342
  if (cb_size == 0) { //if it declared it does not needs chunks
343
    return 0;
344
  }
345

    
346
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
347
    int missing, min;
348
    //@TODO: add some bmap_timestamp based logic
349

    
350
    if (chunkID_set_size(cset) == 0) {
351
      //dprintf("bmap empty\n");
352
      return 1;        // if the bmap seems empty, it needs the chunk
353
    }
354
    missing = cb_size - chunkID_set_size(cset);
355
    missing = missing < 0 ? 0 : missing;
356
    min = chunkID_set_get_earliest(cset);
357
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
358
    return (cid >= min - missing);
359
  }
360

    
361
  //dprintf("has it\n");
362
  return 0;
363
}
364

    
365
double peerWeightReceivedfrom(struct peer **n){
366
  struct peer * p = *n;
367
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
368
}
369

    
370
double peerWeightUniform(struct nodeID **n){
371
  return 1;
372
}
373

    
374
double peerWeightRtt(struct peer **n){
375
#ifdef MONL
376
  double rtt = get_rtt(*n->id);
377
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
378
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
379
#else
380
  return 1;
381
#endif
382
}
383

    
384
//ordering function for ELp peer selection, chunk ID based
385
//can't be used as weight
386
double peerScoreELpID(struct nodeID **n){
387
  struct chunkID_set *bmap;
388
  int latest;
389
  struct peer * p = nodeid_to_peer(*n, 0);
390
  if (!p) return 0;
391

    
392
  bmap = p->bmap;
393
  if (!bmap) return 0;
394
  latest = chunkID_set_get_latest(bmap);
395
  if (latest == INT_MIN) return 0;
396

    
397
  return -latest;
398
}
399

    
400
double getChunkTimestamp(int *cid){
401
  struct chunk *c = cb_get_chunk(cb, *cid);
402
  if (!c) return 0;
403

    
404
  return (double) c->timestamp;
405
}
406

    
407
void send_accepted_chunks(struct peer *to, struct chunkID_set *cset_acc, int max_deliver, int trans_id){
408
  int i, d, cset_acc_size, res;
409

    
410
  cset_acc_size = chunkID_set_size(cset_acc);
411
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
412
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
413
    struct chunk *c;
414
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
415
    c = cb_get_chunk(cb, chunkid);
416
    if (c && needs(to, chunkid) ) {        // we should have the chunk, and he should not have it. Although the "accept" should have been an answer to our "offer", we do some verification
417
      chunk_attributes_update_sending(c);
418
      res = sendChunk(to->id, c);
419
      if (res >= 0) {
420
        chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
421
        d++;
422
        reg_chunk_send(c->id);
423
        if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %lld Result: %d\n", c->id, node_addr(to->id), gettimeofday_in_us(), res);}
424
      } else {
425
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
426
      }
427
    }
428
  }
429
}
430

    
431
int offer_peer_count()
432
{
433
  return offer_per_tick;
434
}
435

    
436
int offer_max_deliver(struct nodeID *n)
437
{
438
#ifdef MONL
439
  switch (get_hopcount(n)) {
440
    case 0: return 5;
441
    case 1: return 2;
442
    default: return 1;
443
  }
444
#else
445
  return 1;
446
#endif
447
}
448

    
449
void send_offer()
450
{
451
  struct chunk *buff;
452
  int size, res, i, n;
453
  struct peer *neighbours;
454
  struct peerset *pset;
455

    
456
  pset = get_peers();
457
  n = peerset_size(pset);
458
  neighbours = peerset_get_peers(pset);
459
  dprintf("Send Offer: %d neighbours\n", n);
460
  if (n == 0) return;
461
  buff = cb_get_chunks(cb, &size);
462
  if (size == 0) return;
463

    
464
  {
465
    size_t selectedpeers_len = offer_peer_count();
466
    int chunkids[size];
467
    struct nodeID *nodeids[n];
468
    struct nodeID *selectedpeers[selectedpeers_len];
469

    
470
    //reduce load a little bit if there are losses on the path from this guy
471
    double average_lossrate = get_average_lossrate_pset(pset);
472
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
473
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
474
      return;
475
    }
476

    
477
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
478
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
479
    selectPeersForChunks(SCHED_BEST, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, needs, (transid % 2) ? peerWeightReceivedfrom : peerWeightRtt);        //select a peer that needs at least one of our chunks
480

    
481
    for (i=0; i<selectedpeers_len ; i++){
482
      int max_deliver = offer_max_deliver(selectedpeers[i]);
483
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
484
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]), nodeid_to_peer(selectedpeers[i],0)->cb_size);
485
      res = offerChunks(selectedpeers[i], my_bmap, max_deliver, transid++);
486
      chunkID_set_free(my_bmap);
487
    }
488
  }
489
}
490

    
491

    
492
void send_chunk()
493
{
494
  struct chunk *buff;
495
  int size, res, i, n;
496
  struct peer *neighbours;
497
  struct peerset *pset;
498

    
499
  pset = get_peers();
500
  n = peerset_size(pset);
501
  neighbours = peerset_get_peers(pset);
502
  dprintf("Send Chunk: %d neighbours\n", n);
503
  if (n == 0) return;
504
  buff = cb_get_chunks(cb, &size);
505
  dprintf("\t %d chunks in buffer...\n", size);
506
  if (size == 0) return;
507

    
508
  /************ STUPID DUMB SCHEDULING ****************/
509
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
510
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
511
  /************ /STUPID DUMB SCHEDULING ****************/
512

    
513
  /************ USE SCHEDULER ****************/
514
  {
515
    size_t selectedpairs_len = 1;
516
    int chunkids[size];
517
    struct peer *nodeids[n];
518
    struct PeerChunk selectedpairs[1];
519
  
520
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
521
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
522
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
523
  /************ /USE SCHEDULER ****************/
524

    
525
    for (i=0; i<selectedpairs_len ; i++){
526
      struct peer *p = selectedpairs[i].peer;
527
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
528
      dprintf("\t sending chunk[%d] to ", c->id);
529
      dprintf("%s\n", node_addr(p->id));
530

    
531
      send_bmap(p);
532
      if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %lld ", c->id, node_addr(p->id), gettimeofday_in_us());}
533

    
534
      chunk_attributes_update_sending(c);
535
      res = sendChunk(p->id, c);
536
      if(chunk_log){fprintf(stderr, "Result: %d Size: %d bytes\n", res, c->size);}
537
      dprintf("\tResult: %d\n", res);
538
      if (res>=0) {
539
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
540
        reg_chunk_send(c->id);
541
      } else {
542
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
543
      }
544
    }
545
  }
546
}