Statistics
| Branch: | Revision:

streamers / streaming.c @ 685225b2

History | View | Annotate | Download (14.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 <stdlib.h>
8
#include <stdio.h>
9
#include <stdint.h>
10
#include <stdbool.h>
11
#include <math.h>
12
#include <assert.h>
13

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

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

    
34
#include "scheduler_la.h"
35

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

    
42
struct chunk_buffer *cb;
43
static struct input_desc *input;
44
static int cb_size;
45
static int transid=0;
46

    
47
static int offer_per_tick = 1;        //N_p parameter of POLITO
48

    
49
int _needs(struct chunkID_set *cset, int cb_size, int cid);
50

    
51
void cb_print()
52
{
53
#ifdef DEBUG
54
  struct chunk *chunks;
55
  int num_chunks, i, id;
56
  chunks = cb_get_chunks(cb, &num_chunks);
57

    
58
  dprintf("\tchbuf :");
59
  i = 0;
60
  if(num_chunks) {
61
    id = chunks[0].id;
62
    dprintf(" %d-> ",id);
63
    while (i < num_chunks) {
64
      if (id == chunks[i].id) {
65
        dprintf("%d",id % 10);
66
        i++;
67
      } else if (chunk_islocked(id)) {
68
        dprintf("*");
69
      } else {
70
        dprintf(".");
71
      }
72
      id++;
73
    }
74
  }
75
  dprintf("\n");
76
#endif
77
}
78

    
79
void stream_init(int size, struct nodeID *myID)
80
{
81
  char conf[32];
82

    
83
  cb_size = size;
84

    
85
  sprintf(conf, "size=%d", cb_size);
86
  cb = cb_init(conf);
87
  chunkDeliveryInit(myID);
88
  chunkSignalingInit(myID);
89
  init_measures();
90
}
91

    
92
int source_init(const char *fname, struct nodeID *myID, bool loop)
93
{
94
  input = input_open(fname, loop ? INPUT_LOOP : 0);
95
  if (input == NULL) {
96
    return -1;
97
  }
98

    
99
  stream_init(1, myID);
100
  return 0;
101
}
102

    
103
void chunk_attributes_fill(struct chunk* c)
104
{
105
  struct chunk_attributes * ca;
106

    
107
  assert(!c->attributes && c->attributes_size == 0);
108

    
109
  c->attributes_size = sizeof(struct chunk_attributes);
110
  c->attributes = ca = malloc(c->attributes_size);
111

    
112
  ca->deadline = c->timestamp;
113
  ca->deadline_increment = 2;
114
  ca->hopcount = 0;
115
}
116

    
117
int chunk_get_hopcount(struct chunk* c) {
118
  struct chunk_attributes * ca;
119

    
120
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
121
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
122
    return -1;
123
  }
124

    
125
  ca = (struct chunk_attributes *) c->attributes;
126
  return ca->hopcount;
127
}
128

    
129
void chunk_attributes_update_received(struct chunk* c)
130
{
131
  struct chunk_attributes * ca;
132

    
133
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
134
    fprintf(stderr,"Warning, received chunk %d with strange attributes block\n", c->id);
135
    return;
136
  }
137

    
138
  ca = (struct chunk_attributes *) c->attributes;
139
  ca->hopcount++;
140
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
141
}
142

    
143
void chunk_attributes_update_sending(struct chunk* c)
144
{
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\n", c->id);
149
    return;
150
  }
151

    
152
  ca = (struct chunk_attributes *) c->attributes;
153
  ca->deadline += ca->deadline_increment;
154
  dprintf("Sending chunk %d with deadline %lu\n", c->id, ca->deadline);
155
}
156

    
157
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
158
{
159
  struct chunk *chunks;
160
  int num_chunks, i;
161
  struct chunkID_set *my_bmap = chunkID_set_init("type=1");
162
  chunks = cb_get_chunks(chbuf, &num_chunks);
163

    
164
  for(i=num_chunks-1; i>=0; i--) {
165
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
166
  }
167
  return my_bmap;
168
}
169

    
170
// a simple implementation that request everything that we miss ... up to max deliver
171
struct chunkID_set *get_chunks_to_accept(struct peer *from, const struct chunkID_set *cset_off, int max_deliver, int trans_id){
172
  struct chunkID_set *cset_acc, *my_bmap;
173
  int i, d, cset_off_size;
174
  //double lossrate;
175

    
176
  cset_acc = chunkID_set_init("size=0");
177

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

    
203
  return cset_acc;
204
}
205

    
206
void send_bmap(struct peer *to)
207
{
208
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
209
   sendBufferMap(to->id,NULL, my_bmap, cb_size, 0);
210
  chunkID_set_free(my_bmap);
211
}
212

    
213
double get_average_lossrate_pset(struct peerset *pset)
214
{
215
  int i, n;
216
  struct peer *neighbours;
217

    
218
  n = peerset_size(pset);
219
  neighbours = peerset_get_peers(pset);
220
  {
221
    struct nodeID *nodeids[n];
222
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
223
#ifdef MONL
224
    return get_average_lossrate(nodeids, n);
225
#else
226
    return 0;
227
#endif
228
  }
229
}
230

    
231
void ack_chunk(struct chunk *c, struct peer *p)
232
{
233
  //reduce load a little bit if there are losses on the path from this guy
234
  double average_lossrate = get_average_lossrate_pset(get_peers());
235
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
236
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
237
    return;
238
  }
239
  send_bmap(p);        //send explicit ack
240
}
241

    
242
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
243
{
244
  int res;
245
  static struct chunk c;
246
  struct peer *p;
247

    
248
  res = decodeChunk(&c, buff + 1, len - 1);
249
  if (res > 0) {
250
    chunk_attributes_update_received(&c);
251
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c));
252
    chunk_unlock(c.id);
253
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
254
    output_deliver(&c);
255
    res = cb_add_chunk(cb, &c);
256
    cb_print();
257
    if (res < 0) {
258
      dprintf("\tchunk too old, buffer full with newer chunks\n");
259
      free(c.data);
260
      free(c.attributes);
261
    }
262
    p = nodeid_to_peer(from,1);
263
    if (p) {        //now we have it almost sure
264
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
265
      ack_chunk(&c,p);        //send explicit ack
266
    }
267
  } else {
268
    fprintf(stderr,"\tError: can't decode chunk!\n");
269
  }
270
}
271

    
272
struct chunk *generated_chunk(suseconds_t *delta)
273
{
274
  struct chunk *c;
275

    
276
  c = malloc(sizeof(struct chunk));
277
  if (!c) {
278
    fprintf(stderr, "Memory allocation error!\n");
279
    return NULL;
280
  }
281

    
282
  *delta = input_get(input, c);
283
  if (*delta < 0) {
284
    fprintf(stderr, "Error in input!\n");
285
    exit(-1);
286
  }
287
  if (c->data == NULL) {
288
    free(c);
289
    return NULL;
290
  }
291
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
292
  chunk_attributes_fill(c);
293
  return c;
294
}
295

    
296
int add_chunk(struct chunk *c)
297
{
298
  int res;
299

    
300
  res = cb_add_chunk(cb, c);
301
  if (res < 0) {
302
    free(c->data);
303
    free(c->attributes);
304
    free(c);
305
    return 0;
306
  }
307
  free(c);
308
  return 1;
309
}
310

    
311
/**
312
 *example function to filter chunks based on whether a given peer needs them.
313
 *
314
 * Looks at buffermap information received about the given peer.
315
 */
316
int needs(struct nodeID *n, int cid){
317
  struct peer * p = nodeid_to_peer(n, 0);
318
  if (!p) return 1; // if we don't know this peer, but we assume it needs the chunk (aggressive behaviour!)
319

    
320
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
321
  if (! p->bmap) {
322
    //dprintf("no bmap\n");
323
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
324
  }
325
  return _needs(p->bmap, p->cb_size, cid);
326
}
327

    
328
int _needs(struct chunkID_set *cset, int cb_size, int cid){
329
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
330
    int missing, min;
331
    //@TODO: add some bmap_timestamp based logic
332

    
333
    if (chunkID_set_size(cset) == 0) {
334
      //dprintf("bmap empty\n");
335
      return 1;        // if the bmap seems empty, it needs the chunk
336
    }
337
    missing = cb_size - chunkID_set_size(cset);
338
    missing = missing < 0 ? 0 : missing;
339
    min = chunkID_set_get_earliest(cset);
340
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
341
    return (cid >= min - missing);
342
  }
343

    
344
  //dprintf("has it\n");
345
  return 0;
346
}
347

    
348
double peerWeightReceivedfrom(struct nodeID **n){
349
  struct peer * p = nodeid_to_peer(*n, 0);
350
  if (!p) return 0;
351
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
352
}
353

    
354
double peerWeightUniform(struct nodeID **n){
355
  return 1;
356
}
357

    
358
double peerWeightRtt(struct nodeID **n){
359
#ifdef MONL
360
  double rtt = get_rtt(*n);
361
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
362
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
363
#else
364
  return 1;
365
#endif
366
}
367

    
368
//ordering function for ELp peer selection, chunk ID based
369
//can't be used as weight
370
double peerScoreELpID(struct nodeID **n){
371
  struct chunkID_set *bmap;
372
  int latest;
373
  struct peer * p = nodeid_to_peer(*n, 0);
374
  if (!p) return 0;
375

    
376
  bmap = p->bmap;
377
  if (!bmap) return 0;
378
  latest = chunkID_set_get_latest(bmap);
379
  if (latest == INT_MIN) return 0;
380

    
381
  return -latest;
382
}
383

    
384
double getChunkTimestamp(int *cid){
385
  struct chunk *c = cb_get_chunk(cb, *cid);
386
  if (!c) return 0;
387

    
388
  return (double) c->timestamp;
389
}
390

    
391
void send_accepted_chunks(struct peer *to, struct chunkID_set *cset_acc, int max_deliver, int trans_id){
392
  int i, d, cset_acc_size, res;
393

    
394
  cset_acc_size = chunkID_set_size(cset_acc);
395
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
396
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
397
    struct chunk *c;
398
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
399
    c = cb_get_chunk(cb, chunkid);
400
    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
401
      chunk_attributes_update_sending(c);
402
      res = sendChunk(to->id, c);
403
      if (res >= 0) {
404
        chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
405
        d++;
406
        reg_chunk_send(c->id);
407
      } else {
408
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
409
      }
410
    }
411
  }
412
}
413

    
414
int offer_peer_count()
415
{
416
  return offer_per_tick;
417
}
418

    
419
int offer_max_deliver(struct nodeID *n)
420
{
421
#ifdef MONL
422
  switch (get_hopcount(n)) {
423
    case 0: return 5;
424
    case 1: return 2;
425
    default: return 1;
426
  }
427
#else
428
  return 1;
429
#endif
430
}
431

    
432
void send_offer()
433
{
434
  struct chunk *buff;
435
  int size, res, i, n;
436
  struct peer *neighbours;
437
  struct peerset *pset;
438

    
439
  pset = get_peers();
440
  n = peerset_size(pset);
441
  neighbours = peerset_get_peers(pset);
442
  dprintf("Send Offer: %d neighbours\n", n);
443
  if (n == 0) return;
444
  buff = cb_get_chunks(cb, &size);
445
  if (size == 0) return;
446

    
447
  {
448
    size_t selectedpeers_len = offer_peer_count();
449
    int chunkids[size];
450
    struct nodeID *nodeids[n];
451
    struct nodeID *selectedpeers[selectedpeers_len];
452

    
453
    //reduce load a little bit if there are losses on the path from this guy
454
    double average_lossrate = get_average_lossrate_pset(pset);
455
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
456
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
457
      return;
458
    }
459

    
460
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
461
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
462
    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
463

    
464
    for (i=0; i<selectedpeers_len ; i++){
465
      int max_deliver = offer_max_deliver(selectedpeers[i]);
466
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
467
      dprintf("\t sending offer(%d) to %s\n", transid, node_addr(selectedpeers[i]));
468
      res = offerChunks(selectedpeers[i], my_bmap, max_deliver, transid++);
469
      chunkID_set_free(my_bmap);
470
    }
471
  }
472
}
473

    
474

    
475
void send_chunk()
476
{
477
  struct chunk *buff;
478
  int size, res, i, n;
479
  struct peer *neighbours;
480
  struct peerset *pset;
481

    
482
  pset = get_peers();
483
  n = peerset_size(pset);
484
  neighbours = peerset_get_peers(pset);
485
  dprintf("Send Chunk: %d neighbours\n", n);
486
  if (n == 0) return;
487
  buff = cb_get_chunks(cb, &size);
488
  dprintf("\t %d chunks in buffer...\n", size);
489
  if (size == 0) return;
490

    
491
  /************ STUPID DUMB SCHEDULING ****************/
492
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
493
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
494
  /************ /STUPID DUMB SCHEDULING ****************/
495

    
496
  /************ USE SCHEDULER ****************/
497
  {
498
    size_t selectedpairs_len = 1;
499
    int chunkids[size];
500
    struct nodeID *nodeids[n];
501
    struct PeerChunk selectedpairs[1];
502
  
503
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
504
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
505
    schedSelectPeerFirst(SCHED_WEIGHTED, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, needs, peerWeightRtt, getChunkTimestamp);
506
  /************ /USE SCHEDULER ****************/
507

    
508
    for (i=0; i<selectedpairs_len ; i++){
509
      struct peer *p = nodeid_to_peer(selectedpairs[i].peer, 0);
510
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
511
      dprintf("\t sending chunk[%d] to ", c->id);
512
      dprintf("%s\n", node_addr(p->id));
513

    
514
      send_bmap(p);
515
      chunk_attributes_update_sending(c);
516
      res = sendChunk(p->id, c);
517
      dprintf("\tResult: %d\n", res);
518
      if (res>=0) {
519
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
520
        reg_chunk_send(c->id);
521
      } else {
522
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
523
      }
524
    }
525
  }
526
}