2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file ats-tests/ats-testing-experiment.c
22 * @brief ats benchmark: controlled experiment execution
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "ats-testing.h"
31 print_op (enum OperationType op)
41 return "SET_PREFERENCE";
49 static struct Experiment *
53 e = GNUNET_new (struct Experiment);
58 e->total_duration = GNUNET_TIME_UNIT_ZERO;
63 free_experiment (struct Experiment *e)
67 struct Operation *cur_o;
68 struct Operation *next_o;
71 for (cur = next; NULL != cur; cur = next)
76 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
84 GNUNET_free_non_null (e->name);
85 GNUNET_free_non_null (e->cfg_file);
90 load_episode (struct Experiment *e, struct Episode *cur,
91 struct GNUNET_CONFIGURATION_Handle *cfg)
99 fprintf (stderr, "Parsing episode %u\n",cur->id);
100 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
105 GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
106 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
107 sec_name, op_name, &op))
111 o = GNUNET_new (struct Operation);
112 /* operations = set_rate, start_send, stop_send, set_preference */
113 if (0 == strcmp (op, "start_send"))
115 o->type = START_SEND;
117 else if (0 == strcmp (op, "stop_send"))
121 else if (0 == strcmp (op, "set_rate"))
125 else if (0 == strcmp (op, "set_preference"))
127 o->type = SET_PREFERENCE;
131 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
132 op_counter, op, cur->id);
134 GNUNET_free (op_name);
135 return GNUNET_SYSERR;
137 GNUNET_free (op_name);
140 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
141 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
142 sec_name, op_name, &o->src_id))
144 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
145 op_counter, op, cur->id);
147 GNUNET_free (op_name);
148 return GNUNET_SYSERR;
150 if (o->src_id > e->num_masters)
152 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
153 o->src_id, op_counter, op, cur->id);
155 GNUNET_free (op_name);
156 return GNUNET_SYSERR;
158 GNUNET_free (op_name);
160 /* Get destination */
161 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
162 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
163 sec_name, op_name, &o->dest_id))
165 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
166 op_counter, op, cur->id);
168 GNUNET_free (op_name);
169 return GNUNET_SYSERR;
171 if (o->dest_id > e->num_slaves)
173 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
174 o->dest_id, op_counter, op, cur->id);
176 GNUNET_free (op_name);
177 return GNUNET_SYSERR;
179 GNUNET_free (op_name);
181 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
182 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
183 sec_name, op_name, &type))
188 if (STOP_SEND != o->type)
190 /* Load arguments for set_rate, start_send, set_preference */
191 if (0 == strcmp (type, "constant"))
193 o->tg_type = GNUNET_ATS_TEST_TG_CONSTANT;
195 else if (0 == strcmp (type, "linear"))
197 o->tg_type = GNUNET_ATS_TEST_TG_LINEAR;
199 else if (0 == strcmp (type, "sinus"))
201 o->tg_type = GNUNET_ATS_TEST_TG_SINUS;
203 else if (0 == strcmp (type, "random"))
205 o->tg_type = GNUNET_ATS_TEST_TG_RANDOM;
209 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
210 op_counter, op, cur->id);
212 GNUNET_free (op_name);
213 return GNUNET_SYSERR;
215 GNUNET_free (op_name);
218 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
219 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
220 sec_name, op_name, &o->base_rate))
222 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
223 op_counter, op, cur->id);
225 GNUNET_free (op_name);
226 return GNUNET_SYSERR;
228 GNUNET_free (op_name);
231 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
232 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
233 sec_name, op_name, &o->max_rate))
235 if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
236 (GNUNET_ATS_TEST_TG_RANDOM == o->tg_type) ||
237 (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
239 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
240 op_counter, op, cur->id);
242 return GNUNET_SYSERR;
245 GNUNET_free (op_name);
249 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
250 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
251 sec_name, op_name, &o->period))
253 o->period = cur->duration;
255 GNUNET_free (op_name);
260 if ((GNUNET_ATS_TEST_TG_LINEAR == o->tg_type) ||
261 (GNUNET_ATS_TEST_TG_SINUS == o->tg_type))
263 if ((o->max_rate - o->base_rate) > o->base_rate)
265 /* This will cause an underflow */
268 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
271 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
272 op_counter, cur->id, print_op (o->type), o->src_id,
273 o->dest_id, type, o->base_rate, o->max_rate,
274 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
276 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
279 GNUNET_free (sec_name);
285 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
289 struct GNUNET_TIME_Relative e_duration;
291 struct Episode *last;
297 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
298 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
299 sec_name, "duration", &e_duration))
301 GNUNET_free (sec_name);
305 cur = GNUNET_new (struct Episode);
306 cur->duration = e_duration;
309 if (GNUNET_OK != load_episode (e, cur, cfg))
311 GNUNET_free (sec_name);
313 return GNUNET_SYSERR;
316 fprintf (stderr, "Found episode %u with duration %s \n",
318 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
320 /* Update experiment */
322 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
323 /* Put in linked list */
329 GNUNET_free (sec_name);
337 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
339 struct Experiment *e = cls;
340 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
341 fprintf (stderr, "Experiment timeout!\n");
343 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
345 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
346 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
349 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
354 enforce_start_send (struct Operation *op)
360 enforce_stop_send (struct Operation *op)
366 enforce_set_rate (struct Operation *op)
372 enforce_set_preference (struct Operation *op)
377 static void enforce_episode (struct Episode *ep)
379 struct Operation *cur;
380 for (cur = ep->head; NULL != cur; cur = cur->next)
383 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
384 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
387 enforce_start_send (cur);
390 enforce_stop_send (cur);
393 enforce_set_rate (cur);
396 enforce_set_preference (cur);
405 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
407 struct Experiment *e = cls;
408 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
409 if (NULL != e->ep_done_cb)
410 e->ep_done_cb (e->cur);
412 /* Scheduling next */
413 e->cur = e->cur->next;
417 fprintf (stderr, "Last episode done!\n");
418 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
420 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
421 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
423 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
427 fprintf (stderr, "Running episode %u with timeout %s\n",
429 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
430 enforce_episode(e->cur);
432 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
433 &timeout_episode, e);
438 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
439 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
440 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
442 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
443 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
444 e->e_done_cb = e_done_cb;
445 e->ep_done_cb = ep_done_cb;
446 e->start_time = GNUNET_TIME_absolute_get();
448 /* Start total time out */
449 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
450 &timeout_experiment, e);
454 fprintf (stderr, "Running episode %u with timeout %s\n",
456 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
457 enforce_episode(e->cur);
458 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
459 &timeout_episode, e);
466 GNUNET_ATS_TEST_experimentation_load (char *filename)
468 struct Experiment *e;
469 struct GNUNET_CONFIGURATION_Handle *cfg;
472 cfg = GNUNET_CONFIGURATION_create();
473 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
475 fprintf (stderr, "Failed to load `%s'\n", filename);
476 GNUNET_CONFIGURATION_destroy (cfg);
480 e = create_experiment ();
482 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
485 fprintf (stderr, "Invalid %s", "name");
490 fprintf (stderr, "Experiment name: `%s'\n", e->name);
492 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
493 "cfg_file", &e->cfg_file))
495 fprintf (stderr, "Invalid %s", "cfg_file");
500 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
502 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
503 "masters", &e->num_masters))
505 fprintf (stderr, "Invalid %s", "masters");
510 fprintf (stderr, "Experiment masters: `%llu'\n",
513 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
514 "slaves", &e->num_slaves))
516 fprintf (stderr, "Invalid %s", "slaves");
521 fprintf (stderr, "Experiment slaves: `%llu'\n",
524 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
525 "max_duration", &e->max_duration))
527 fprintf (stderr, "Invalid %s", "max_duration");
532 fprintf (stderr, "Experiment duration: `%s'\n",
533 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
535 load_episodes (e, cfg);
536 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
538 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
540 GNUNET_CONFIGURATION_destroy (cfg);
545 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
547 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
549 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
550 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
555 /* end of file ats-testing-experiment.c*/