Statistics
| Branch: | Revision:

streamers / streaming.c @ 84ff82ba

History | View | Annotate | Download (16.1 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
#include <inttypes.h>
15

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

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

    
36
#include "scheduler_la.h"
37

    
38
static bool heuristics_distance_maxdeliver = true;
39
static int bcast_after_receive_every = 0;
40
static bool neigh_on_chunk_recv = true;
41

    
42
struct chunk_attributes {
43
  uint64_t deadline;
44
  uint16_t deadline_increment;
45
  uint16_t hopcount;
46
} __attribute__((packed));
47

    
48
extern bool chunk_log;
49

    
50
struct chunk_buffer *cb;
51
static struct input_desc *input;
52
static int cb_size;
53
static int transid=0;
54

    
55
static int offer_per_tick = 1;        //N_p parameter of POLITO
56

    
57
int _needs(struct chunkID_set *cset, int cb_size, int cid);
58

    
59
uint64_t gettimeofday_in_us(void)
60
{
61
  struct timeval what_time; //to store the epoch time
62

    
63
  gettimeofday(&what_time, NULL);
64
  return what_time.tv_sec * 1000000ULL + what_time.tv_usec;
65
}
66

    
67
void cb_print()
68
{
69
#ifdef DEBUG
70
  struct chunk *chunks;
71
  int num_chunks, i, id;
72
  chunks = cb_get_chunks(cb, &num_chunks);
73

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

    
95
void stream_init(int size, struct nodeID *myID)
96
{
97
  static char conf[32];
98

    
99
  cb_size = size;
100

    
101
  sprintf(conf, "size=%d", cb_size);
102
  cb = cb_init(conf);
103
  chunkDeliveryInit(myID);
104
  chunkSignalingInit(myID);
105
  init_measures();
106
}
107

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

    
115
  stream_init(1, myID);
116
  return 0;
117
}
118

    
119
void chunk_attributes_fill(struct chunk* c)
120
{
121
  struct chunk_attributes * ca;
122

    
123
  assert(!c->attributes && c->attributes_size == 0);
124

    
125
  c->attributes_size = sizeof(struct chunk_attributes);
126
  c->attributes = ca = malloc(c->attributes_size);
127

    
128
  ca->deadline = c->timestamp;
129
  ca->deadline_increment = 2;
130
  ca->hopcount = 0;
131
}
132

    
133
int chunk_get_hopcount(struct chunk* c) {
134
  struct chunk_attributes * ca;
135

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

    
141
  ca = (struct chunk_attributes *) c->attributes;
142
  return ca->hopcount;
143
}
144

    
145
void chunk_attributes_update_received(struct chunk* c)
146
{
147
  struct chunk_attributes * ca;
148

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

    
154
  ca = (struct chunk_attributes *) c->attributes;
155
  ca->hopcount++;
156
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
157
}
158

    
159
void chunk_attributes_update_sending(struct chunk* c)
160
{
161
  struct chunk_attributes * ca;
162

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

    
168
  ca = (struct chunk_attributes *) c->attributes;
169
  ca->deadline += ca->deadline_increment;
170
  dprintf("Sending chunk %d with deadline %lu\n", c->id, ca->deadline);
171
}
172

    
173
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
174
{
175
  struct chunk *chunks;
176
  int num_chunks, i;
177
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
178
  chunks = cb_get_chunks(chbuf, &num_chunks);
179

    
180
  for(i=num_chunks-1; i>=0; i--) {
181
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
182
  }
183
  return my_bmap;
184
}
185

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

    
192
  cset_acc = chunkID_set_init("size=0");
193

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

    
219
  return cset_acc;
220
}
221

    
222
void send_bmap(struct peer *to)
223
{
224
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
225
   sendBufferMap(to->id,NULL, my_bmap, input ? 0 : cb_size, 0);
226
  chunkID_set_free(my_bmap);
227
}
228

    
229
void bcast_bmap()
230
{
231
  int i, n;
232
  struct peer *neighbours;
233
  struct peerset *pset;
234
  struct chunkID_set *my_bmap;
235

    
236
  pset = get_peers();
237
  n = peerset_size(pset);
238
  neighbours = peerset_get_peers(pset);
239

    
240
  my_bmap = cb_to_bmap(cb);        //cache our bmap for faster processing
241
  for (i = 0; i<n; i++) {
242
    sendBufferMap(neighbours[i].id,NULL, my_bmap, input ? 0 : cb_size, 0);
243
  }
244
  chunkID_set_free(my_bmap);
245
}
246

    
247
double get_average_lossrate_pset(struct peerset *pset)
248
{
249
  int i, n;
250
  struct peer *neighbours;
251

    
252
  n = peerset_size(pset);
253
  neighbours = peerset_get_peers(pset);
254
  {
255
    struct nodeID *nodeids[n];
256
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
257
#ifdef MONL
258
    return get_average_lossrate(nodeids, n);
259
#else
260
    return 0;
261
#endif
262
  }
263
}
264

    
265
void ack_chunk(struct chunk *c, struct peer *p)
266
{
267
  //reduce load a little bit if there are losses on the path from this guy
268
  double average_lossrate = get_average_lossrate_pset(get_peers());
269
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
270
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
271
    return;
272
  }
273
  send_bmap(p);        //send explicit ack
274
}
275

    
276
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
277
{
278
  int res;
279
  static struct chunk c;
280
  struct peer *p;
281
  static int bcast_cnt;
282

    
283
  res = decodeChunk(&c, buff + 1, len - 1);
284
  if (res > 0) {
285
    chunk_attributes_update_received(&c);
286
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c));
287
    chunk_unlock(c.id);
288
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
289
    if(chunk_log){fprintf(stderr, "TEO: Received chunk %d from peer: %s at: %"PRIu64" hopcount: %i\n", c.id, node_addr(from), gettimeofday_in_us(), chunk_get_hopcount(&c));}
290
    output_deliver(&c);
291
    res = cb_add_chunk(cb, &c);
292
    cb_print();
293
    if (res < 0) {
294
      dprintf("\tchunk too old, buffer full with newer chunks\n");
295
      if(chunk_log){fprintf(stderr, "TEO: Received chunk: %d too old (buffer full with newer chunks) from peer: %s at: %"PRIu64"\n", c.id, node_addr(from), gettimeofday_in_us());}
296
      free(c.data);
297
      free(c.attributes);
298
    }
299
    p = nodeid_to_peer(from, neigh_on_chunk_recv);
300
    if (p) {        //now we have it almost sure
301
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
302
      ack_chunk(&c,p);        //send explicit ack
303
    }
304
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
305
       bcast_bmap();
306
    }
307
  } else {
308
    fprintf(stderr,"\tError: can't decode chunk!\n");
309
  }
310
}
311

    
312
struct chunk *generated_chunk(suseconds_t *delta)
313
{
314
  struct chunk *c;
315

    
316
  c = malloc(sizeof(struct chunk));
317
  if (!c) {
318
    fprintf(stderr, "Memory allocation error!\n");
319
    return NULL;
320
  }
321

    
322
  *delta = input_get(input, c);
323
  if (*delta < 0) {
324
    fprintf(stderr, "Error in input!\n");
325
    exit(-1);
326
  }
327
  if (c->data == NULL) {
328
    free(c);
329
    return NULL;
330
  }
331
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
332
  chunk_attributes_fill(c);
333
  return c;
334
}
335

    
336
int add_chunk(struct chunk *c)
337
{
338
  int res;
339

    
340
  res = cb_add_chunk(cb, c);
341
  if (res < 0) {
342
    free(c->data);
343
    free(c->attributes);
344
    free(c);
345
    return 0;
346
  }
347
  free(c);
348
  return 1;
349
}
350

    
351
/**
352
 *example function to filter chunks based on whether a given peer needs them.
353
 *
354
 * Looks at buffermap information received about the given peer.
355
 */
356
int needs(struct nodeID *n, int cid){
357
  struct peer * p = nodeid_to_peer(n, 0);
358
  if (!p) return 1; // if we don't know this peer, but we assume it needs the chunk (aggressive behaviour!)
359

    
360
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
361
  if (! p->bmap) {
362
    //dprintf("no bmap\n");
363
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
364
  }
365
  return _needs(p->bmap, p->cb_size, cid);
366
}
367

    
368
int _needs(struct chunkID_set *cset, int cb_size, int cid){
369
  if (cb_size == 0) { //if it declared it does not needs chunks
370
    return 0;
371
  }
372

    
373
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
374
    int missing, min;
375
    //@TODO: add some bmap_timestamp based logic
376

    
377
    if (chunkID_set_size(cset) == 0) {
378
      //dprintf("bmap empty\n");
379
      return 1;        // if the bmap seems empty, it needs the chunk
380
    }
381
    missing = cb_size - chunkID_set_size(cset);
382
    missing = missing < 0 ? 0 : missing;
383
    min = chunkID_set_get_earliest(cset);
384
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
385
    return (cid >= min - missing);
386
  }
387

    
388
  //dprintf("has it\n");
389
  return 0;
390
}
391

    
392
double peerWeightReceivedfrom(struct nodeID **n){
393
  struct peer * p = nodeid_to_peer(*n, 0);
394
  if (!p) return 0;
395
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
396
}
397

    
398
double peerWeightUniform(struct nodeID **n){
399
  return 1;
400
}
401

    
402
double peerWeightRtt(struct nodeID **n){
403
#ifdef MONL
404
  double rtt = get_rtt(*n);
405
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
406
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
407
#else
408
  return 1;
409
#endif
410
}
411

    
412
//ordering function for ELp peer selection, chunk ID based
413
//can't be used as weight
414
double peerScoreELpID(struct nodeID **n){
415
  struct chunkID_set *bmap;
416
  int latest;
417
  struct peer * p = nodeid_to_peer(*n, 0);
418
  if (!p) return 0;
419

    
420
  bmap = p->bmap;
421
  if (!bmap) return 0;
422
  latest = chunkID_set_get_latest(bmap);
423
  if (latest == INT_MIN) return 0;
424

    
425
  return -latest;
426
}
427

    
428
double getChunkTimestamp(int *cid){
429
  struct chunk *c = cb_get_chunk(cb, *cid);
430
  if (!c) return 0;
431

    
432
  return (double) c->timestamp;
433
}
434

    
435
void send_accepted_chunks(struct peer *to, struct chunkID_set *cset_acc, int max_deliver, int trans_id){
436
  int i, d, cset_acc_size, res;
437

    
438
  cset_acc_size = chunkID_set_size(cset_acc);
439
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
440
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
441
    struct chunk *c;
442
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
443
    c = cb_get_chunk(cb, chunkid);
444
    if (c && needs(to->id, 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
445
      chunk_attributes_update_sending(c);
446
      res = sendChunk(to->id, c);
447
      if (res >= 0) {
448
        chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
449
        d++;
450
        reg_chunk_send(c->id);
451
        if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d\n", c->id, node_addr(to->id), gettimeofday_in_us(), res);}
452
      } else {
453
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
454
      }
455
    }
456
  }
457
}
458

    
459
int offer_peer_count()
460
{
461
  return offer_per_tick;
462
}
463

    
464
int offer_max_deliver(struct nodeID *n)
465
{
466

    
467
  if (!heuristics_distance_maxdeliver) return 1;
468

    
469
#ifdef MONL
470
  switch (get_hopcount(n)) {
471
    case 0: return 5;
472
    case 1: return 2;
473
    default: return 1;
474
  }
475
#else
476
  return 1;
477
#endif
478
}
479

    
480
void send_offer()
481
{
482
  struct chunk *buff;
483
  int size, res, i, n;
484
  struct peer *neighbours;
485
  struct peerset *pset;
486

    
487
  pset = get_peers();
488
  n = peerset_size(pset);
489
  neighbours = peerset_get_peers(pset);
490
  dprintf("Send Offer: %d neighbours\n", n);
491
  if (n == 0) return;
492
  buff = cb_get_chunks(cb, &size);
493
  if (size == 0) return;
494

    
495
  {
496
    size_t selectedpeers_len = offer_peer_count();
497
    int chunkids[size];
498
    struct nodeID *nodeids[n];
499
    struct nodeID *selectedpeers[selectedpeers_len];
500

    
501
    //reduce load a little bit if there are losses on the path from this guy
502
    double average_lossrate = get_average_lossrate_pset(pset);
503
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
504
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
505
      return;
506
    }
507

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

    
512
    for (i=0; i<selectedpeers_len ; i++){
513
      int max_deliver = offer_max_deliver(selectedpeers[i]);
514
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
515
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]), nodeid_to_peer(selectedpeers[i],0)->cb_size);
516
      res = offerChunks(selectedpeers[i], my_bmap, max_deliver, transid++);
517
      chunkID_set_free(my_bmap);
518
    }
519
  }
520
}
521

    
522

    
523
void send_chunk()
524
{
525
  struct chunk *buff;
526
  int size, res, i, n;
527
  struct peer *neighbours;
528
  struct peerset *pset;
529

    
530
  pset = get_peers();
531
  n = peerset_size(pset);
532
  neighbours = peerset_get_peers(pset);
533
  dprintf("Send Chunk: %d neighbours\n", n);
534
  if (n == 0) return;
535
  buff = cb_get_chunks(cb, &size);
536
  dprintf("\t %d chunks in buffer...\n", size);
537
  if (size == 0) return;
538

    
539
  /************ STUPID DUMB SCHEDULING ****************/
540
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
541
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
542
  /************ /STUPID DUMB SCHEDULING ****************/
543

    
544
  /************ USE SCHEDULER ****************/
545
  {
546
    size_t selectedpairs_len = 1;
547
    int chunkids[size];
548
    struct nodeID *nodeids[n];
549
    struct PeerChunk selectedpairs[1];
550
  
551
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
552
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
553
    schedSelectPeerFirst(SCHED_WEIGHTED, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, needs, peerWeightRtt, getChunkTimestamp);
554
  /************ /USE SCHEDULER ****************/
555

    
556
    for (i=0; i<selectedpairs_len ; i++){
557
      struct peer *p = nodeid_to_peer(selectedpairs[i].peer, 0);
558
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
559
      dprintf("\t sending chunk[%d] to ", c->id);
560
      dprintf("%s\n", node_addr(p->id));
561

    
562
      send_bmap(p);
563

    
564
      chunk_attributes_update_sending(c);
565
      res = sendChunk(p->id, c);
566
      if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d Size: %d bytes\n", c->id, node_addr(p->id), gettimeofday_in_us(), res, c->size);}
567
      dprintf("\tResult: %d\n", res);
568
      if (res>=0) {
569
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
570
        reg_chunk_send(c->id);
571
      } else {
572
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
573
      }
574
    }
575
  }
576
}