-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathserver.cc
More file actions
285 lines (252 loc) · 7.86 KB
/
Copy pathserver.cc
File metadata and controls
285 lines (252 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2010 Miroslav Lichvar <mlichvar@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sysheaders.h"
#include "network.h"
bool load_config(const char *file, Network *network, unsigned int nodes,
unsigned int node_clocks) {
Generator_generator generator;
FILE *f;
const char *ws = " \t\n\r";
char line[1000], *var, *arg, *end;
unsigned int node, node2, clock;
f = fopen(file, "r");
if (!f)
return false;
while (fgets(line, sizeof (line), f)) {
end = line + strlen(line);
var = line + strspn(line, ws);
arg = line + strcspn(line, "=");
*arg++ = '\0';
if (var >= end || *var == '#')
continue;
if (arg >= end)
return false;
while (end > line && (end[-1] == '\r' || end[-1] == '\n' || end[-1] == '\t' || end[-1] == ' '))
*--end = '\0';
arg += strspn(arg, ws);
if (strncmp(var, "node", 4))
return false;
var += 4;
node = atoi(var) - 1;
if (node >= nodes)
continue;
var += strcspn(var, "_") + 1;
if (var >= end)
return false;
if (strncmp(var, "offset", 6) == 0) {
var += 6;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->get_clock(clock)->set_time(atof(arg));
} else if (strncmp(var, "start", 5) == 0)
network->get_node(node)->set_start_time(atof(arg));
else if (strncmp(var, "freq", 4) == 0) {
var += 4;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
if (arg[0] == '(')
network->get_node(node)->get_clock(clock)->
set_freq_generator(generator.generate(arg));
else
network->get_node(node)->get_clock(clock)->set_freq(atof(arg));
} else if (strncmp(var, "step", 4) == 0) {
var += 4;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->get_clock(clock)->
set_step_generator(generator.generate(arg));
} else if (strncmp(var, "shift_pll", 9) == 0) {
var += 9;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->get_clock(clock)->
set_ntp_shift_pll(atoi(arg));
} else if (strncmp(var, "fll_mode2", 9) == 0) {
var += 9;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->get_clock(clock)->
set_ntp_flag(atoi(arg), CLOCK_NTP_FLL_MODE2);
} else if (strncmp(var, "pll_clamp", 9) == 0) {
var += 9;
clock = isdigit(*var) ? atoi(var) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->get_clock(clock)->
set_ntp_flag(atoi(arg), CLOCK_NTP_PLL_CLAMP);
} else if (strncmp(var, "delay_correction", 16) == 0) {
var += 16;
node2 = atoi(var) - 1;
if (node2 >= nodes)
continue;
network->set_link_correction_generator(node, node2, generator.generate(arg));
} else if (strncmp(var, "delay", 5) == 0) {
var += 5;
node2 = atoi(var) - 1;
if (node2 >= nodes)
continue;
network->set_link_delay_generator(node, node2, generator.generate(arg));
} else if (strncmp(var, "refclock_base", 13) == 0) {
if (strncmp(arg, "node", 4) != 0)
return false;
node2 = atoi(arg + 4) - 1;
if (node2 >= nodes)
return false;
network->get_node(node)->set_refclock_base(network->get_node(node2)->get_clock(0));
} else if (strncmp(var, "refclock_target", 15) == 0) {
clock = isdigit(*arg) ? atoi(arg) - 1 : 0;
if (clock >= node_clocks)
continue;
network->get_node(node)->set_refclock_target(network->get_node(node)->get_clock(clock));
} else if (strncmp(var, "refclock", 8) == 0)
network->get_node(node)->get_refclock()->set_offset_generator(generator.generate(arg));
else
return false;
}
fclose(f);
return true;
}
void run_generator(char *expr, int num) {
Generator_generator gen_generator;
Generator *generator;
generator = gen_generator.generate(expr);
while (num--)
printf("%.9e\n", generator->generate(NULL));
delete generator;
}
int main(int argc, char **argv) {
int nodes, subnets = 1, node_clocks = 1, help = 0, verbosity = 2, generate_only = 0, rate = 1;
double limit = 10000.0, reset = 0.0;
const char *offset_log = NULL, *freq_log = NULL, *rawfreq_log = NULL,
*packet_log = NULL, *config, *socket = "clknetsim.sock", *env, *executable = NULL;
struct timeval tv;
int r, opt;
Network *network;
while ((opt = getopt(argc, argv, "l:r:R:n:c:o:f:Gg:p:s:v:e:h")) != -1) {
switch (opt) {
case 'l':
limit = atof(optarg);
break;
case 'r':
reset = atof(optarg);
break;
case 'R':
rate = atoi(optarg);
break;
case 'n':
subnets = atoi(optarg);
break;
case 'c':
node_clocks = atoi(optarg);
break;
case 'o':
offset_log = optarg;
break;
case 'f':
freq_log = optarg;
break;
case 'g':
rawfreq_log = optarg;
break;
case 'p':
packet_log = optarg;
break;
case 's':
socket = optarg;
break;
case 'G':
generate_only = 1;
break;
case 'v':
verbosity = atoi(optarg);
break;
case 'e':
executable = optarg;
break;
case 'h':
default:
help = 1;
}
}
if (optind + 2 != argc || help) {
printf("usage: clknetsim [options] config nodes\n");
printf(" or: clknetsim -G expr num\n");
printf(" -l secs set time limit to secs (default 10000)\n");
printf(" -r secs reset clock stats after secs (default 0)\n");
printf(" -R rate set freq/log/stats update rate (default 1 per second)\n");
printf(" -n subnets set number of subnetworks (default 1)\n");
printf(" -c clocks set number of clocks per node (default 1)\n");
printf(" -o file log time offsets to file\n");
printf(" -f file log frequency offsets to file\n");
printf(" -g file log raw (uncorrected) frequency offsets to file\n");
printf(" -p file log packet delays to file\n");
printf(" -s socket set server socket name (default clknetsim.sock)\n");
printf(" -v level set verbosity level (default 2)\n");
printf(" -e file execute file on every freq/log/stats update\n");
printf(" -G print num numbers generated by expr\n");
printf(" -h print usage\n");
return 1;
}
config = argv[optind];
nodes = atoi(argv[optind + 1]);
env = getenv("CLKNETSIM_RANDOM_SEED");
if (env) {
srandom(atoi(env));
} else {
gettimeofday(&tv, NULL);
srandom(tv.tv_sec ^ tv.tv_usec);
}
if (generate_only) {
run_generator(argv[optind], nodes);
return 0;
}
network = new Network(socket, executable, nodes, subnets, node_clocks, rate);
if (offset_log)
network->open_offset_log(offset_log);
if (freq_log)
network->open_freq_log(freq_log);
if (rawfreq_log)
network->open_rawfreq_log(rawfreq_log);
if (packet_log)
network->open_packet_log(packet_log);
if (!load_config(config, network, nodes, node_clocks)) {
fprintf(stderr, "Couldn't parse config %s\n", config);
return 1;
}
if (!network->prepare_clients())
return 1;
fprintf(stderr, "Running simulation...");
if (reset && reset < limit) {
r = network->run(reset);
network->reset_clock_stats();
} else
r = true;
if (r)
r = network->run(limit);
if (r) {
fprintf(stderr, "done\n\n");
network->print_stats(verbosity);
} else
fprintf(stderr, "failed\n");
delete network;
return !r;
}