Statistics
| Branch: | Revision:

streamers / streaming.c @ 8e750be6

History | View | Annotate | Download (16 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
static bool heuristics_distance_maxdeliver = false;
39
static int bcast_after_receive_every = 0;
40
static bool neigh_on_chunk_recv = false;
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:%d\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:%d\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 nodeID *fromid, const struct chunkID_set *cset_off, int max_deliver, uint16_t trans_id){
188
  struct chunkID_set *cset_acc, *my_bmap;
189
  int i, d, cset_off_size;
190
  //double lossrate;
191
  struct peer *from = nodeid_to_peer(fromid, 0);
192

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

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

    
220
  return cset_acc;
221
}
222

    
223
void send_bmap(struct nodeID *toid)
224
{
225
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
226
   sendBufferMap(toid,NULL, my_bmap, input ? 0 : cb_size, 0);
227
  chunkID_set_free(my_bmap);
228
}
229

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

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

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

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

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

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

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

    
284
  res = decodeChunk(&c, buff + 1, len - 1);
285
  if (res > 0) {
286
    chunk_attributes_update_received(&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: %lld 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
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c), res==E_CB_OLD, res==E_CB_DUPLICATE);
293
    cb_print();
294
    if (res < 0) {
295
      dprintf("\tchunk too old, buffer full with newer chunks\n");
296
      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());}
297
      free(c.data);
298
      free(c.attributes);
299
    }
300
    p = nodeid_to_peer(from, neigh_on_chunk_recv);
301
    if (p) {        //now we have it almost sure
302
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
303
    }
304
    ack_chunk(&c,from);        //send explicit ack
305
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
306
       bcast_bmap();
307
    }
308
  } else {
309
    fprintf(stderr,"\tError: can't decode chunk!\n");
310
  }
311
}
312

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

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

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

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

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

    
352
/**
353
 *example function to filter chunks based on whether a given peer needs them.
354
 *
355
 * Looks at buffermap information received about the given peer.
356
 */
357
int needs(struct peer *n, int cid){
358
  struct peer * p = n;
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 peer **n){
393
  struct peer * p = *n;
394
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
395
}
396

    
397
double peerWeightUniform(struct peer **n){
398
  return 1;
399
}
400

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

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

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

    
424
  return -latest;
425
}
426

    
427
double chunkScoreChunkID(int *cid){
428
  return (double) *cid;
429
}
430

    
431
double getChunkTimestamp(int *cid){
432
  struct chunk *c = cb_get_chunk(cb, *cid);
433
  if (!c) return 0;
434

    
435
  return (double) c->timestamp;
436
}
437

    
438
void send_accepted_chunks(struct nodeID *toid, struct chunkID_set *cset_acc, int max_deliver, uint16_t trans_id){
439
  int i, d, cset_acc_size, res;
440
  struct peer *to = nodeid_to_peer(toid, 0);
441

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

    
463
int offer_peer_count()
464
{
465
  return offer_per_tick;
466
}
467

    
468
int offer_max_deliver(struct nodeID *n)
469
{
470

    
471
  if (!heuristics_distance_maxdeliver) return 1;
472

    
473
#ifdef MONL
474
  switch (get_hopcount(n)) {
475
    case 0: return 5;
476
    case 1: return 2;
477
    default: return 1;
478
  }
479
#else
480
  return 1;
481
#endif
482
}
483

    
484
void send_offer()
485
{
486
  struct chunk *buff;
487
  int size, res, i, n;
488
  struct peer *neighbours;
489
  struct peerset *pset;
490

    
491
  pset = get_peers();
492
  n = peerset_size(pset);
493
  neighbours = peerset_get_peers(pset);
494
  dprintf("Send Offer: %d neighbours\n", n);
495
  if (n == 0) return;
496
  buff = cb_get_chunks(cb, &size);
497
  if (size == 0) return;
498

    
499
  {
500
    size_t selectedpeers_len = offer_peer_count();
501
    int chunkids[size];
502
    struct peer *nodeids[n];
503
    struct peer *selectedpeers[selectedpeers_len];
504

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

    
512
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
513
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
514
    selectPeersForChunks(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, SCHED_NEEDS, SCHED_PEER);
515

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

    
526

    
527
void send_chunk()
528
{
529
  struct chunk *buff;
530
  int size, res, i, n;
531
  struct peer *neighbours;
532
  struct peerset *pset;
533

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

    
543
  /************ STUPID DUMB SCHEDULING ****************/
544
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
545
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
546
  /************ /STUPID DUMB SCHEDULING ****************/
547

    
548
  /************ USE SCHEDULER ****************/
549
  {
550
    size_t selectedpairs_len = 1;
551
    int chunkids[size];
552
    struct peer *nodeids[n];
553
    struct PeerChunk selectedpairs[1];
554
  
555
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
556
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
557
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
558
  /************ /USE SCHEDULER ****************/
559

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

    
566
      send_bmap(p->id);
567

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