Statistics
| Branch: | Revision:

streamers / streaming.c @ 74a5d4ae

History | View | Annotate | Download (13.8 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
int generated_chunk(suseconds_t *delta)
273
{
274
  int res;
275
  struct chunk c;
276

    
277
  *delta = input_get(input, &c);
278
  if (*delta < 0) {
279
    fprintf(stderr, "Error in input!\n");
280
    exit(-1);
281
  }
282
  if (c.data == NULL) {
283
    return 0;
284
  }
285
  dprintf("Generated chunk %d of %d bytes\n",c.id, c.size);
286
  chunk_attributes_fill(&c);
287
  res = cb_add_chunk(cb, &c);
288
  if (res < 0) {
289
    free(c.data);
290
    free(c.attributes);
291
  }
292

    
293
  return 1;
294
}
295

    
296
/**
297
 *example function to filter chunks based on whether a given peer needs them.
298
 *
299
 * Looks at buffermap information received about the given peer.
300
 */
301
int needs(struct nodeID *n, int cid){
302
  struct peer * p = nodeid_to_peer(n, 0);
303
  if (!p) return 1; // if we don't know this peer, but we assume it needs the chunk (aggressive behaviour!)
304

    
305
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
306
  if (! p->bmap) {
307
    //dprintf("no bmap\n");
308
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
309
  }
310
  return _needs(p->bmap, p->cb_size, cid);
311
}
312

    
313
int _needs(struct chunkID_set *cset, int cb_size, int cid){
314
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
315
    int missing, min;
316
    //@TODO: add some bmap_timestamp based logic
317

    
318
    if (chunkID_set_size(cset) == 0) {
319
      //dprintf("bmap empty\n");
320
      return 1;        // if the bmap seems empty, it needs the chunk
321
    }
322
    missing = cb_size - chunkID_set_size(cset);
323
    missing = missing < 0 ? 0 : missing;
324
    min = chunkID_set_get_earliest(cset);
325
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
326
    return (cid >= min - missing);
327
  }
328

    
329
  //dprintf("has it\n");
330
  return 0;
331
}
332

    
333
double peerWeightReceivedfrom(struct nodeID **n){
334
  struct peer * p = nodeid_to_peer(*n, 0);
335
  if (!p) return 0;
336
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
337
}
338

    
339
double peerWeightUniform(struct nodeID **n){
340
  return 1;
341
}
342

    
343
double peerWeightRtt(struct nodeID **n){
344
#ifdef MONL
345
  double rtt = get_rtt(*n);
346
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
347
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
348
#else
349
  return 1;
350
#endif
351
}
352

    
353
//ordering function for ELp peer selection, chunk ID based
354
//can't be used as weight
355
double peerScoreELpID(struct nodeID **n){
356
  struct chunkID_set *bmap;
357
  int latest;
358
  struct peer * p = nodeid_to_peer(*n, 0);
359
  if (!p) return 0;
360

    
361
  bmap = p->bmap;
362
  if (!bmap) return 0;
363
  latest = chunkID_set_get_latest(bmap);
364
  if (latest == INT_MIN) return 0;
365

    
366
  return -latest;
367
}
368

    
369
double getChunkTimestamp(int *cid){
370
  struct chunk *c = cb_get_chunk(cb, *cid);
371
  if (!c) return 0;
372

    
373
  return (double) c->timestamp;
374
}
375

    
376
void send_accepted_chunks(struct peer *to, struct chunkID_set *cset_acc, int max_deliver, int trans_id){
377
  int i, d, cset_acc_size, res;
378

    
379
  cset_acc_size = chunkID_set_size(cset_acc);
380
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
381
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
382
    struct chunk *c;
383
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
384
    c = cb_get_chunk(cb, chunkid);
385
    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
386
      chunk_attributes_update_sending(c);
387
      res = sendChunk(to->id, c);
388
      if (res >= 0) {
389
        chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
390
        d++;
391
        reg_chunk_send(c->id);
392
      } else {
393
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
394
      }
395
    }
396
  }
397
}
398

    
399
int offer_peer_count()
400
{
401
  return offer_per_tick;
402
}
403

    
404
int offer_max_deliver(struct nodeID *n)
405
{
406
#ifdef MONL
407
  switch (get_hopcount(n)) {
408
    case 0: return 5;
409
    case 1: return 2;
410
    default: return 1;
411
  }
412
#else
413
  return 1;
414
#endif
415
}
416

    
417
void send_offer()
418
{
419
  struct chunk *buff;
420
  int size, res, i, n;
421
  struct peer *neighbours;
422
  struct peerset *pset;
423

    
424
  pset = get_peers();
425
  n = peerset_size(pset);
426
  neighbours = peerset_get_peers(pset);
427
  dprintf("Send Offer: %d neighbours\n", n);
428
  if (n == 0) return;
429
  buff = cb_get_chunks(cb, &size);
430
  if (size == 0) return;
431

    
432
  {
433
    size_t selectedpeers_len = offer_peer_count();
434
    int chunkids[size];
435
    struct nodeID *nodeids[n];
436
    struct nodeID *selectedpeers[selectedpeers_len];
437

    
438
    //reduce load a little bit if there are losses on the path from this guy
439
    double average_lossrate = get_average_lossrate_pset(pset);
440
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
441
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
442
      return;
443
    }
444

    
445
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
446
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
447
    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
448

    
449
    for (i=0; i<selectedpeers_len ; i++){
450
      int max_deliver = offer_max_deliver(selectedpeers[i]);
451
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
452
      dprintf("\t sending offer(%d) to %s\n", transid, node_addr(selectedpeers[i]));
453
      res = offerChunks(selectedpeers[i], my_bmap, max_deliver, transid++);
454
      chunkID_set_free(my_bmap);
455
    }
456
  }
457
}
458

    
459

    
460
void send_chunk()
461
{
462
  struct chunk *buff;
463
  int size, res, i, n;
464
  struct peer *neighbours;
465
  struct peerset *pset;
466

    
467
  pset = get_peers();
468
  n = peerset_size(pset);
469
  neighbours = peerset_get_peers(pset);
470
  dprintf("Send Chunk: %d neighbours\n", n);
471
  if (n == 0) return;
472
  buff = cb_get_chunks(cb, &size);
473
  dprintf("\t %d chunks in buffer...\n", size);
474
  if (size == 0) return;
475

    
476
  /************ STUPID DUMB SCHEDULING ****************/
477
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
478
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
479
  /************ /STUPID DUMB SCHEDULING ****************/
480

    
481
  /************ USE SCHEDULER ****************/
482
  {
483
    size_t selectedpairs_len = 1;
484
    int chunkids[size];
485
    struct nodeID *nodeids[n];
486
    struct PeerChunk selectedpairs[1];
487
  
488
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
489
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i)->id;
490
    schedSelectPeerFirst(SCHED_WEIGHTED, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, needs, peerWeightRtt, getChunkTimestamp);
491
  /************ /USE SCHEDULER ****************/
492

    
493
    for (i=0; i<selectedpairs_len ; i++){
494
      struct peer *p = nodeid_to_peer(selectedpairs[i].peer, 0);
495
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
496
      dprintf("\t sending chunk[%d] to ", c->id);
497
      dprintf("%s\n", node_addr(p->id));
498

    
499
      send_bmap(p);
500
      chunk_attributes_update_sending(c);
501
      res = sendChunk(p->id, c);
502
      dprintf("\tResult: %d\n", res);
503
      if (res>=0) {
504
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
505
        reg_chunk_send(c->id);
506
      } else {
507
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
508
      }
509
    }
510
  }
511
}