Statistics
| Branch: | Revision:

grapes / som / Scheduler / sched_random.c @ 8ab58ec7

History | View | Annotate | Download (1.07 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include "scheduler_ha.h"
8
#include <stdlib.h>
9
#include <string.h>
10

    
11

    
12
#define MAX(A,B)    ((A)>(B) ? (A) : (B))
13
#define MIN(A,B)    ((A)<(B) ? (A) : (B))
14

    
15
/*
16
 * A simple RBp/RBc scheduler
17
*/
18
void schedSelectRandom(struct peer **peers, int peers_len, struct chunk **chunks, int chunks_len,         //in
19
                     struct PeerChunk *selected, int *selected_len)        //out, inout
20
{
21

    
22
  int s;
23

    
24
  //compose list of selected pairs
25
  *selected_len = MIN(*selected_len, peers_len * chunks_len);
26
  s=0;
27
  while (s != *selected_len)  {
28
    int i;
29
    int already_selected = 0;
30

    
31
    selected[s].peer = peers[(int)(peers_len * (rand() / (RAND_MAX + 1.0)))];
32
    selected[s].chunk = chunks[(int)(chunks_len * (rand() / (RAND_MAX + 1.0)))];
33

    
34
    //don't be so blind to include the same pair twice
35
    for (i=0; i<s; i++){
36
      if (selected[i].peer == selected[s].peer && selected[i].chunk == selected[s].chunk) {
37
        already_selected = 1;
38
        break;
39
      }
40
    }
41
    if (already_selected) continue;
42

    
43
    s++;
44
  }
45
}