Statistics
| Branch: | Tag: | Revision:

sssimulator / sssim.c @ 80f4c362

History | View | Annotate | Download (5.22 KB)

1 86681e55 luca
/*
2
 * This is SSSim: the Simple & Stupid Simulator
3
 *
4
 *  Copyright (c) 2008 Luca Abeni
5
 *  Copyright (c) 2008 Csaba Kiraly
6
 *
7
 *  This is free software; see gpl-3.0.txt
8
 */
9
10
#include <unistd.h>
11
#include <stdlib.h>
12
#include <getopt.h>
13
#include <stdio.h>
14
#include <string.h>
15
#include <time.h>
16
#include <math.h>
17
18
#include "config.h"
19
#include "core.h"
20
#include "graph.h"
21
#include "sched.h"
22
#include "stats.h"
23
#include "td.h"
24 bf3d3ebd Luca Baldesi
#include "ed.h"
25 91dc88f2 luca
#include "overlay.h"
26 86681e55 luca
27
static struct peer *peers;
28
extern FILE *resfile;
29 80f4c362 Luca Baldesi
extern FILE *delayfile;
30 86681e55 luca
extern FILE *statusfile;
31
static FILE *statsfile;
32
static FILE *graphfile;
33 91dc88f2 luca
char inputgraph[200] = {'\0'};;
34 86681e55 luca
35
int PO_DELAY;    // playout delay
36
static int num_peers = N;   //number of nodes
37
static int num_chunks = K;   //number of chunks
38
static int nn;
39
static unsigned int seed = 1;
40
static int buf_size;
41
static int ts = 2;
42 52770a8f Luca Baldesi
static int event_driven = 0;
43 86681e55 luca
static int display_neighbourhood;
44
45
static void show_usage(char *argv[])
46
{
47
  printf("Usage: %s [options]\n", argv[0]);
48
49
  printf("Options:\n");
50
  printf("\t-s <seed>:\t\t\tSet the seed for the random numbers generator\n");
51
  printf("\t-c <number of chunks>:\t\tSet the number of chunks\n");
52
  printf("\t-o <neighbourhood size>:\tSet the neighbourhood size (outdegree)\n");
53
  printf("\t-n <number of peers>:\t\tSet the number of peers\n");
54
  printf("\t-t <trace file>\n");
55
  printf("\t-r <results file>:\t\t(dynamically generated during simulation)\n");
56 80f4c362 Luca Baldesi
  printf("\t-d <delay file>:\t\t(dynamically generated during simulation)\n");
57 86681e55 luca
  printf("\t-p <status file>\n");
58 bf3d3ebd Luca Baldesi
  printf("\t-P:\t\t\t\tPrint the neighbourhoods\n");
59 86681e55 luca
  printf("\t-S <stats file>\n");
60 bf3d3ebd Luca Baldesi
  printf("\t-b <chunks buffer size>:\t(playout delay)\n");
61 86681e55 luca
  printf("\t-T <Server Period>:\t\tSet the deadline postponing amount\n");
62
  printf("\t-g <Graph file name>:\t\tSave the overlay as a DOT file\n");
63 bf3d3ebd Luca Baldesi
  printf("\t-e:\t\t\t\tSwitch to event-driven mode\n");
64 8d46f8f6 Luca Baldesi
  printf("\t-G <Edge Graph file name>:\tImport the overlay from an '.edges' file. Options: [,optimize][,random_source]\n");
65 86681e55 luca
  exit(0);
66
}
67
68
static int opts_parse(int argc, char *argv[])
69
{
70
  int o;
71
  
72 80f4c362 Luca Baldesi
  while ((o = getopt(argc, argv, "d:c:eo:n:t:r:s:p:S:b:T:g:G:hP")) != -1) {
73 86681e55 luca
    switch(o) {
74 52770a8f Luca Baldesi
      case 'e':
75
        event_driven = 1;
76
        break;
77 86681e55 luca
      case 'n':
78
        num_peers = atoi(optarg);
79
        break;
80
      case 'c':
81
        num_chunks = atoi(optarg);
82
        break;
83
      case 'o':
84
        nn = atoi(optarg);
85
        break;
86
      case 'b':
87
        buf_size = pow(2, ilogb(2 * atoi(optarg) - 1));
88
        break;
89
      case 't':
90
        if (strcmp(optarg, "-") == 0) {
91
          trace = stdout;
92
        } else {
93
          trace = fopen(optarg, "w");
94
        }
95
        break;
96
      case 'r':
97
        resfile = fopen(optarg, "w");
98
        break;
99 80f4c362 Luca Baldesi
      case 'd':
100
        delayfile = fopen(optarg, "w");
101
        break;
102 86681e55 luca
      case 'p':
103
        statusfile = fopen(optarg, "w");
104
        break;
105
      case 'P':
106
        display_neighbourhood = 1;
107
        break;
108
      case 'S':
109
        statsfile = fopen(optarg, "w");
110
        break;
111
      case 's':
112
        seed = atoi(optarg);
113
        if (seed == 0) {
114
          seed = time(NULL);
115
        }
116
        break;
117
      case 'T':
118
        ts = atoi(optarg);
119
        if (ts <= 1) {
120
          fprintf(stderr, "Invalid Server Period %d\n", ts);
121
          exit(-1);
122
        }
123
        break;
124
      case 'g':
125
        graphfile = fopen(optarg, "w");
126
        break;
127 91dc88f2 luca
      case 'G':
128
        strcpy(inputgraph, optarg);;
129
        break;
130 86681e55 luca
      case 'h':
131
        show_usage(argv);
132
      default:
133
        fprintf(stderr, "Error: unknown option %c\n", o);
134
135
        exit(-1);
136
      }
137
  }
138
  if (nn == 0) {
139
    nn = num_peers - 1;
140
  }
141
142
  return optind - 1;
143
}
144
145
146
147
148
///==========================================================
149
///     main
150
///==========================================================
151
152
int main(int argc, char *argv[])
153
{
154 91dc88f2 luca
  int n;
155 86681e55 luca
  struct chunk *ch;
156
  struct peer source;
157
158
  n = opts_parse(argc, argv);
159
  argc -= n;
160
  argv += n;
161
162
  srand(seed);
163 91dc88f2 luca
  if (strlen(inputgraph) > 0)
164
          peers = overlay_from_edgefile(inputgraph, &num_peers, &nn, num_chunks, &buf_size, &PO_DELAY, &source);
165
  else
166
          peers = overlay_from_parameters(&num_peers, &nn, num_chunks, &buf_size, &PO_DELAY, &source);
167
  if (!peers)
168
          return -1;
169 86681e55 luca
 
170
 
171
172
    if (display_neighbourhood) {
173
      for (n = 0; n < num_peers; n++) {
174
        printf("Peer %d Neighbourhood: ", n);
175
        neighbourhood_print(peers[n].neighbour, peers[n].neigh_size);
176
        printf("\n");
177
      }
178
    }
179
    if (graphfile) {
180
      graph_dotprint(graphfile, &source, peers, n);
181
    }
182 80f4c362 Luca Baldesi
    if (delayfile) {
183
        fprintf(delayfile, "Time,Receiver,Chunk,Delay\n");
184
        fflush(delayfile);
185
    }
186 86681e55 luca
    printf("Buf Size: %d\n", buf_size);
187 52770a8f Luca Baldesi
    if(event_driven)
188
                        ch = ed_loop(peers, num_peers, num_chunks, ts);
189
    else
190
                        ch = td_loop(peers, num_peers, num_chunks, ts);
191
192 86681e55 luca
193
    if (!per_chunk_delay_analysis(NULL, ch, num_chunks, num_peers)){
194
      printf("sub-optimal distribution!!!\n");
195
    }
196
    if (trace){
197
      per_chunk_delay_analysis(trace, ch, num_chunks, num_peers);
198
      per_node_delay_analysis(trace,  ch, num_chunks);
199
    }
200
201
    if (statsfile) {
202
      fprintf(statsfile, "Seed: %u\n", seed);
203
      per_chunk_delay_analysis(statsfile, ch, num_chunks, num_peers);
204
      per_node_delay_analysis(statsfile,  ch, num_chunks);
205
    }
206
207 80f4c362 Luca Baldesi
    if (delayfile)
208
            fclose(delayfile);
209
210 86681e55 luca
  return 0;
211
}