Statistics
| Branch: | Revision:

streamers / streaming.c @ 50f1ec20

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

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

    
28
#include "streaming.h"
29
#include "output.h"
30
#include "input.h"
31
#include "dbg.h"
32
#include "chunk_signaling.h"
33
#include "chunklock.h"
34
#include "topology.h"
35
#include "measures.h"
36
#include "scheduling.h"
37
#include "transaction.h"
38

    
39
#include "scheduler_la.h"
40

    
41
static bool heuristics_distance_maxdeliver = false;
42
static int bcast_after_receive_every = 0;
43
static bool neigh_on_chunk_recv = false;
44

    
45
struct chunk_attributes {
46
  uint64_t deadline;
47
  uint16_t deadline_increment;
48
  uint16_t hopcount;
49
} __attribute__((packed));
50

    
51
extern bool chunk_log;
52

    
53
struct chunk_buffer *cb;
54
static struct input_desc *input;
55
static int cb_size;
56

    
57
static int offer_per_tick = 1;        //N_p parameter of POLITO
58

    
59
int _needs(struct chunkID_set *cset, int cb_size, int cid);
60

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

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

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

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

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

    
101
  cb_size = size;
102

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

    
110
int source_init(const char *fname, struct nodeID *myID, bool loop, int *fds, int fds_size)
111
{
112
  int flags = 0;
113

    
114
  if (memcmp(fname, "udp:", 4) == 0) {
115
    fname += 4;
116
    flags = INPUT_UDP;
117
  }
118
  if (loop) {
119
    flags |= INPUT_LOOP;
120
  }
121
  input = input_open(fname, flags, fds, fds_size);
122
  if (input == NULL) {
123
    return -1;
124
  }
125

    
126
  stream_init(1, myID);
127
  return 0;
128
}
129

    
130
void chunk_attributes_fill(struct chunk* c)
131
{
132
  struct chunk_attributes * ca;
133

    
134
  assert(!c->attributes && c->attributes_size == 0);
135

    
136
  c->attributes_size = sizeof(struct chunk_attributes);
137
  c->attributes = ca = malloc(c->attributes_size);
138

    
139
  ca->deadline = c->timestamp;
140
  ca->deadline_increment = 2;
141
  ca->hopcount = 0;
142
}
143

    
144
int chunk_get_hopcount(struct chunk* c) {
145
  struct chunk_attributes * ca;
146

    
147
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
148
    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));
149
    return -1;
150
  }
151

    
152
  ca = (struct chunk_attributes *) c->attributes;
153
  return ca->hopcount;
154
}
155

    
156
void chunk_attributes_update_received(struct chunk* c)
157
{
158
  struct chunk_attributes * ca;
159

    
160
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
161
    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));
162
    return;
163
  }
164

    
165
  ca = (struct chunk_attributes *) c->attributes;
166
  ca->hopcount++;
167
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
168
}
169

    
170
void chunk_attributes_update_sending(const struct chunk* c)
171
{
172
  struct chunk_attributes * ca;
173

    
174
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
175
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
176
    return;
177
  }
178

    
179
  ca = (struct chunk_attributes *) c->attributes;
180
  ca->deadline += ca->deadline_increment;
181
  dprintf("Sending chunk %d with deadline %lu\n", c->id, ca->deadline);
182
}
183

    
184
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
185
{
186
  struct chunk *chunks;
187
  int num_chunks, i;
188
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
189
  chunks = cb_get_chunks(chbuf, &num_chunks);
190

    
191
  for(i=num_chunks-1; i>=0; i--) {
192
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
193
  }
194
  return my_bmap;
195
}
196

    
197
// a simple implementation that request everything that we miss ... up to max deliver
198
struct chunkID_set *get_chunks_to_accept(struct nodeID *fromid, const struct chunkID_set *cset_off, int max_deliver, uint16_t trans_id){
199
  struct chunkID_set *cset_acc, *my_bmap;
200
  int i, d, cset_off_size;
201
  //double lossrate;
202
  struct peer *from = nodeid_to_peer(fromid, 0);
203

    
204
  cset_acc = chunkID_set_init("size=0");
205

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

    
231
  return cset_acc;
232
}
233

    
234
void send_bmap(struct nodeID *toid)
235
{
236
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
237
   sendBufferMap(toid,NULL, my_bmap, input ? 0 : cb_size, 0);
238
  chunkID_set_free(my_bmap);
239
}
240

    
241
void bcast_bmap()
242
{
243
  int i, n;
244
  struct peer *neighbours;
245
  struct peerset *pset;
246
  struct chunkID_set *my_bmap;
247

    
248
  pset = get_peers();
249
  n = peerset_size(pset);
250
  neighbours = peerset_get_peers(pset);
251

    
252
  my_bmap = cb_to_bmap(cb);        //cache our bmap for faster processing
253
  for (i = 0; i<n; i++) {
254
    sendBufferMap(neighbours[i].id,NULL, my_bmap, input ? 0 : cb_size, 0);
255
  }
256
  chunkID_set_free(my_bmap);
257
}
258

    
259
void send_ack(struct nodeID *toid, uint16_t trans_id)
260
{
261
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
262
  sendAck(toid, my_bmap,trans_id);
263
  chunkID_set_free(my_bmap);
264
}
265

    
266
double get_average_lossrate_pset(struct peerset *pset)
267
{
268
  int i, n;
269
  struct peer *neighbours;
270

    
271
  n = peerset_size(pset);
272
  neighbours = peerset_get_peers(pset);
273
  {
274
    struct nodeID *nodeids[n];
275
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
276
#ifdef MONL
277
    return get_average_lossrate(nodeids, n);
278
#else
279
    return 0;
280
#endif
281
  }
282
}
283

    
284
void ack_chunk(struct chunk *c, struct nodeID *from, uint16_t trans_id)
285
{
286
  //reduce load a little bit if there are losses on the path from this guy
287
  double average_lossrate = get_average_lossrate_pset(get_peers());
288
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
289
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
290
    return;
291
  }
292
  send_ack(from, trans_id);        //send explicit ack
293
}
294

    
295
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
296
{
297
  int res;
298
  static struct chunk c;
299
  struct peer *p;
300
  static int bcast_cnt;
301
  uint16_t transid;
302

    
303
  res = parseChunkMsg(buff + 1, len - 1, &c, &transid);
304
  if (res > 0) {
305
    chunk_attributes_update_received(&c);
306
    chunk_unlock(c.id);
307
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
308
    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));}
309
    output_deliver(&c);
310
    res = cb_add_chunk(cb, &c);
311
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c), res==E_CB_OLD, res==E_CB_DUPLICATE);
312
    cb_print();
313
    if (res < 0) {
314
      dprintf("\tchunk too old, buffer full with newer chunks\n");
315
      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());}
316
      free(c.data);
317
      free(c.attributes);
318
    }
319
    p = nodeid_to_peer(from, neigh_on_chunk_recv);
320
    if (p) {        //now we have it almost sure
321
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
322
    }
323
    ack_chunk(&c, from, transid);        //send explicit ack
324
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
325
       bcast_bmap();
326
    }
327
  } else {
328
    fprintf(stderr,"\tError: can't decode chunk!\n");
329
  }
330
}
331

    
332
struct chunk *generated_chunk(suseconds_t *delta)
333
{
334
  struct chunk *c;
335

    
336
  c = malloc(sizeof(struct chunk));
337
  if (!c) {
338
    fprintf(stderr, "Memory allocation error!\n");
339
    return NULL;
340
  }
341

    
342
  *delta = input_get(input, c);
343
  if (*delta < 0) {
344
    fprintf(stderr, "Error in input!\n");
345
    exit(-1);
346
  }
347
  if (c->data == NULL) {
348
    free(c);
349
    return NULL;
350
  }
351
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
352
  chunk_attributes_fill(c);
353
  return c;
354
}
355

    
356
int add_chunk(struct chunk *c)
357
{
358
  int res;
359

    
360
  res = cb_add_chunk(cb, c);
361
  if (res < 0) {
362
    free(c->data);
363
    free(c->attributes);
364
    free(c);
365
    return 0;
366
  }
367
  free(c);
368
  return 1;
369
}
370

    
371
/**
372
 *example function to filter chunks based on whether a given peer needs them.
373
 *
374
 * Looks at buffermap information received about the given peer.
375
 */
376
int needs(struct peer *n, int cid){
377
  struct peer * p = n;
378

    
379
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
380
  if (! p->bmap) {
381
    //dprintf("no bmap\n");
382
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
383
  }
384
  return _needs(p->bmap, p->cb_size, cid);
385
}
386

    
387
int _needs(struct chunkID_set *cset, int cb_size, int cid){
388
  if (cb_size == 0) { //if it declared it does not needs chunks
389
    return 0;
390
  }
391

    
392
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
393
    int missing, min;
394
    //@TODO: add some bmap_timestamp based logic
395

    
396
    if (chunkID_set_size(cset) == 0) {
397
      //dprintf("bmap empty\n");
398
      return 1;        // if the bmap seems empty, it needs the chunk
399
    }
400
    missing = cb_size - chunkID_set_size(cset);
401
    missing = missing < 0 ? 0 : missing;
402
    min = chunkID_set_get_earliest(cset);
403
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
404
    return (cid >= min - missing);
405
  }
406

    
407
  //dprintf("has it\n");
408
  return 0;
409
}
410

    
411
double peerWeightReceivedfrom(struct peer **n){
412
  struct peer * p = *n;
413
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
414
}
415

    
416
double peerWeightUniform(struct peer **n){
417
  return 1;
418
}
419

    
420
double peerWeightRtt(struct peer **n){
421
#ifdef MONL
422
  double rtt = get_rtt(*n->id);
423
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
424
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
425
#else
426
  return 1;
427
#endif
428
}
429

    
430
//ordering function for ELp peer selection, chunk ID based
431
//can't be used as weight
432
double peerScoreELpID(struct nodeID **n){
433
  struct chunkID_set *bmap;
434
  int latest;
435
  struct peer * p = nodeid_to_peer(*n, 0);
436
  if (!p) return 0;
437

    
438
  bmap = p->bmap;
439
  if (!bmap) return 0;
440
  latest = chunkID_set_get_latest(bmap);
441
  if (latest == INT_MIN) return 0;
442

    
443
  return -latest;
444
}
445

    
446
double chunkScoreChunkID(int *cid){
447
  return (double) *cid;
448
}
449

    
450
double getChunkTimestamp(int *cid){
451
  const struct chunk *c = cb_get_chunk(cb, *cid);
452
  if (!c) return 0;
453

    
454
  return (double) c->timestamp;
455
}
456

    
457
void send_accepted_chunks(struct nodeID *toid, struct chunkID_set *cset_acc, int max_deliver, uint16_t trans_id){
458
  int i, d, cset_acc_size, res;
459
  struct peer *to = nodeid_to_peer(toid, 0);
460

    
461
  transaction_reg_accept(trans_id, to->id);
462

    
463
  cset_acc_size = chunkID_set_size(cset_acc);
464
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
465
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
466
    const struct chunk *c;
467
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
468
    c = cb_get_chunk(cb, chunkid);
469
    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
470
      chunk_attributes_update_sending(c);
471
      res = sendChunk(toid, c, trans_id);
472
      if (res >= 0) {
473
        if(to) chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
474
        d++;
475
        reg_chunk_send(c->id);
476
        if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d\n", c->id, node_addr(toid), gettimeofday_in_us(), res);}
477
      } else {
478
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
479
      }
480
    }
481
  }
482
}
483

    
484
int offer_peer_count()
485
{
486
  return offer_per_tick;
487
}
488

    
489
int offer_max_deliver(struct nodeID *n)
490
{
491

    
492
  if (!heuristics_distance_maxdeliver) return 1;
493

    
494
#ifdef MONL
495
  switch (get_hopcount(n)) {
496
    case 0: return 5;
497
    case 1: return 2;
498
    default: return 1;
499
  }
500
#else
501
  return 1;
502
#endif
503
}
504

    
505
void send_offer()
506
{
507
  struct chunk *buff;
508
  int size, res, i, n;
509
  struct peer *neighbours;
510
  struct peerset *pset;
511

    
512
  pset = get_peers();
513
  n = peerset_size(pset);
514
  neighbours = peerset_get_peers(pset);
515
  dprintf("Send Offer: %d neighbours\n", n);
516
  if (n == 0) return;
517
  buff = cb_get_chunks(cb, &size);
518
  if (size == 0) return;
519

    
520
  {
521
    size_t selectedpeers_len = offer_peer_count();
522
    int chunkids[size];
523
    struct peer *nodeids[n];
524
    struct peer *selectedpeers[selectedpeers_len];
525

    
526
    //reduce load a little bit if there are losses on the path from this guy
527
    double average_lossrate = get_average_lossrate_pset(pset);
528
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
529
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
530
      return;
531
    }
532

    
533
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
534
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
535
    selectPeersForChunks(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, SCHED_NEEDS, SCHED_PEER);
536

    
537
    for (i=0; i<selectedpeers_len ; i++){
538
      int transid = transaction_create(selectedpeers[i]);
539
      int max_deliver = offer_max_deliver(selectedpeers[i]->id);
540
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
541
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]->id), selectedpeers[i]->cb_size);
542
      res = offerChunks(selectedpeers[i]->id, my_bmap, max_deliver, transid++);
543
      chunkID_set_free(my_bmap);
544
    }
545
  }
546
}
547

    
548

    
549
void send_chunk()
550
{
551
  struct chunk *buff;
552
  int size, res, i, n;
553
  struct peer *neighbours;
554
  struct peerset *pset;
555

    
556
  pset = get_peers();
557
  n = peerset_size(pset);
558
  neighbours = peerset_get_peers(pset);
559
  dprintf("Send Chunk: %d neighbours\n", n);
560
  if (n == 0) return;
561
  buff = cb_get_chunks(cb, &size);
562
  dprintf("\t %d chunks in buffer...\n", size);
563
  if (size == 0) return;
564

    
565
  /************ STUPID DUMB SCHEDULING ****************/
566
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
567
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
568
  /************ /STUPID DUMB SCHEDULING ****************/
569

    
570
  /************ USE SCHEDULER ****************/
571
  {
572
    size_t selectedpairs_len = 1;
573
    int chunkids[size];
574
    struct peer *nodeids[n];
575
    struct PeerChunk selectedpairs[1];
576
  
577
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
578
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
579
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
580
  /************ /USE SCHEDULER ****************/
581

    
582
    for (i=0; i<selectedpairs_len ; i++){
583
      struct peer *p = selectedpairs[i].peer;
584
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
585
      dprintf("\t sending chunk[%d] to ", c->id);
586
      dprintf("%s\n", node_addr(p->id));
587

    
588
      send_bmap(p->id);
589

    
590
      chunk_attributes_update_sending(c);
591
      res = sendChunk(p->id, c, 0);        //we do not use transactions in pure push
592
      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);}
593
      dprintf("\tResult: %d\n", res);
594
      if (res>=0) {
595
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
596
        reg_chunk_send(c->id);
597
      } else {
598
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
599
      }
600
    }
601
  }
602
}