Statistics
| Branch: | Tag: | Revision:

sssimulator / sssim.c @ master

History | View | Annotate | Download (5.94 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 78d8960f Luca Baldesi
float netload = 1;
37 86681e55 luca
static int num_peers = N;   //number of nodes
38
static int num_chunks = K;   //number of chunks
39
static int nn;
40 32c9a7bc Luca Baldesi
float convergence_precision = 0;
41 f850347b Luca Baldesi
uint32_t analysis_window = 0;
42 86681e55 luca
static unsigned int seed = 1;
43
static int buf_size;
44
static int ts = 2;
45 52770a8f Luca Baldesi
static int event_driven = 0;
46 4123e0f7 Luca Baldesi
int per_link_events = 0;
47 86681e55 luca
static int display_neighbourhood;
48
49
static void show_usage(char *argv[])
50
{
51
  printf("Usage: %s [options]\n", argv[0]);
52
53
  printf("Options:\n");
54
  printf("\t-s <seed>:\t\t\tSet the seed for the random numbers generator\n");
55
  printf("\t-c <number of chunks>:\t\tSet the number of chunks\n");
56
  printf("\t-o <neighbourhood size>:\tSet the neighbourhood size (outdegree)\n");
57
  printf("\t-n <number of peers>:\t\tSet the number of peers\n");
58
  printf("\t-t <trace file>\n");
59 ba1583a3 Luca Baldesi
  printf("\t-l <netload>:\t\t\tSet the net load factor, the ratio of packages sent over the number of peers per chunk time (default=1).\n");
60 86681e55 luca
  printf("\t-r <results file>:\t\t(dynamically generated during simulation)\n");
61 80f4c362 Luca Baldesi
  printf("\t-d <delay file>:\t\t(dynamically generated during simulation)\n");
62 86681e55 luca
  printf("\t-p <status file>\n");
63 bf3d3ebd Luca Baldesi
  printf("\t-P:\t\t\t\tPrint the neighbourhoods\n");
64 86681e55 luca
  printf("\t-S <stats file>\n");
65 bf3d3ebd Luca Baldesi
  printf("\t-b <chunks buffer size>:\t(playout delay)\n");
66 86681e55 luca
  printf("\t-T <Server Period>:\t\tSet the deadline postponing amount\n");
67
  printf("\t-g <Graph file name>:\t\tSave the overlay as a DOT file\n");
68 bf3d3ebd Luca Baldesi
  printf("\t-e:\t\t\t\tSwitch to event-driven mode\n");
69 4123e0f7 Luca Baldesi
  printf("\t-E:\t\t\t\tSwitch to event-driven mode and generate per-link events\n");
70 8d46f8f6 Luca Baldesi
  printf("\t-G <Edge Graph file name>:\tImport the overlay from an '.edges' file. Options: [,optimize][,random_source]\n");
71 32c9a7bc Luca Baldesi
  printf("\t-a <precision>:\t\t\tStops if convergence is reached.\n");
72 ba1583a3 Luca Baldesi
  printf("\t-w <analysis window size>:\tDefine the analysis chunk window.\n");
73 86681e55 luca
  exit(0);
74
}
75
76
static int opts_parse(int argc, char *argv[])
77
{
78
  int o;
79
  
80 4123e0f7 Luca Baldesi
  while ((o = getopt(argc, argv, "w:a:d:c:eEo:n:t:r:s:p:S:b:T:g:G:l:hP")) != -1) {
81 86681e55 luca
    switch(o) {
82 32c9a7bc Luca Baldesi
      case 'a':
83
        convergence_precision = atof(optarg);
84
        break;
85 f850347b Luca Baldesi
      case 'w':
86
        analysis_window = atoi(optarg);
87
        break;
88 4123e0f7 Luca Baldesi
      case 'E':
89
        per_link_events = 1;
90 52770a8f Luca Baldesi
      case 'e':
91
        event_driven = 1;
92
        break;
93 86681e55 luca
      case 'n':
94
        num_peers = atoi(optarg);
95
        break;
96
      case 'c':
97
        num_chunks = atoi(optarg);
98
        break;
99
      case 'o':
100
        nn = atoi(optarg);
101
        break;
102 78d8960f Luca Baldesi
      case 'l':
103
        netload = atof(optarg);
104
        break;
105 86681e55 luca
      case 'b':
106
        buf_size = pow(2, ilogb(2 * atoi(optarg) - 1));
107
        break;
108
      case 't':
109
        if (strcmp(optarg, "-") == 0) {
110
          trace = stdout;
111
        } else {
112
          trace = fopen(optarg, "w");
113
        }
114
        break;
115
      case 'r':
116
        resfile = fopen(optarg, "w");
117
        break;
118 80f4c362 Luca Baldesi
      case 'd':
119
        delayfile = fopen(optarg, "w");
120
        break;
121 86681e55 luca
      case 'p':
122
        statusfile = fopen(optarg, "w");
123
        break;
124
      case 'P':
125
        display_neighbourhood = 1;
126
        break;
127
      case 'S':
128
        statsfile = fopen(optarg, "w");
129
        break;
130
      case 's':
131
        seed = atoi(optarg);
132
        if (seed == 0) {
133
          seed = time(NULL);
134
        }
135
        break;
136
      case 'T':
137
        ts = atoi(optarg);
138
        if (ts <= 1) {
139
          fprintf(stderr, "Invalid Server Period %d\n", ts);
140
          exit(-1);
141
        }
142
        break;
143
      case 'g':
144
        graphfile = fopen(optarg, "w");
145
        break;
146 91dc88f2 luca
      case 'G':
147
        strcpy(inputgraph, optarg);;
148
        break;
149 86681e55 luca
      case 'h':
150
        show_usage(argv);
151
      default:
152
        fprintf(stderr, "Error: unknown option %c\n", o);
153
154
        exit(-1);
155
      }
156
  }
157
  if (nn == 0) {
158
    nn = num_peers - 1;
159
  }
160
161
  return optind - 1;
162
}
163
164
165
166
167
///==========================================================
168
///     main
169
///==========================================================
170
171
int main(int argc, char *argv[])
172
{
173 91dc88f2 luca
  int n;
174 86681e55 luca
  struct chunk *ch;
175
  struct peer source;
176
177
  n = opts_parse(argc, argv);
178
  argc -= n;
179
  argv += n;
180
181
  srand(seed);
182 91dc88f2 luca
  if (strlen(inputgraph) > 0)
183
          peers = overlay_from_edgefile(inputgraph, &num_peers, &nn, num_chunks, &buf_size, &PO_DELAY, &source);
184
  else
185
          peers = overlay_from_parameters(&num_peers, &nn, num_chunks, &buf_size, &PO_DELAY, &source);
186
  if (!peers)
187
          return -1;
188 86681e55 luca
 
189
 
190
191
    if (display_neighbourhood) {
192
      for (n = 0; n < num_peers; n++) {
193
        printf("Peer %d Neighbourhood: ", n);
194
        neighbourhood_print(peers[n].neighbour, peers[n].neigh_size);
195
        printf("\n");
196
      }
197
    }
198
    if (graphfile) {
199
      graph_dotprint(graphfile, &source, peers, n);
200
    }
201 80f4c362 Luca Baldesi
    if (delayfile) {
202
        fprintf(delayfile, "Time,Receiver,Chunk,Delay\n");
203
        fflush(delayfile);
204
    }
205 86681e55 luca
    printf("Buf Size: %d\n", buf_size);
206 52770a8f Luca Baldesi
    if(event_driven)
207
                        ch = ed_loop(peers, num_peers, num_chunks, ts);
208
    else
209
                        ch = td_loop(peers, num_peers, num_chunks, ts);
210
211 86681e55 luca
212
    if (!per_chunk_delay_analysis(NULL, ch, num_chunks, num_peers)){
213
      printf("sub-optimal distribution!!!\n");
214
    }
215
    if (trace){
216
      per_chunk_delay_analysis(trace, ch, num_chunks, num_peers);
217
      per_node_delay_analysis(trace,  ch, num_chunks);
218
    }
219
220
    if (statsfile) {
221
      fprintf(statsfile, "Seed: %u\n", seed);
222
      per_chunk_delay_analysis(statsfile, ch, num_chunks, num_peers);
223
      per_node_delay_analysis(statsfile,  ch, num_chunks);
224
    }
225
226 80f4c362 Luca Baldesi
    if (delayfile)
227
            fclose(delayfile);
228
229 86681e55 luca
  return 0;
230
}