2 This file is part of GNUnet.
3 Copyright (C) 2010-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file ats-tests/ats-testing-experiment.c
17 * @brief ats benchmark: controlled experiment execution
18 * @author Christian Grothoff
19 * @author Matthias Wachs
22 #include "gnunet_util_lib.h"
23 #include "ats-testing.h"
26 print_op (enum OperationType op)
33 case START_PREFERENCE:
34 return "START_PREFERENCE";
36 return "STOP_PREFERENCE";
44 static struct Experiment *
48 e = GNUNET_new (struct Experiment);
53 e->total_duration = GNUNET_TIME_UNIT_ZERO;
58 free_experiment (struct Experiment *e)
62 struct GNUNET_ATS_TEST_Operation *cur_o;
63 struct GNUNET_ATS_TEST_Operation *next_o;
66 for (cur = next; NULL != cur; cur = next)
71 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
79 GNUNET_free_non_null (e->name);
80 GNUNET_free_non_null (e->cfg_file);
86 load_episode (struct Experiment *e,
88 struct GNUNET_CONFIGURATION_Handle *cfg)
90 struct GNUNET_ATS_TEST_Operation *o;
98 fprintf (stderr, "Parsing episode %u\n",cur->id);
99 GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
104 GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
105 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
106 sec_name, op_name, &op))
108 GNUNET_free (op_name);
111 o = GNUNET_new (struct GNUNET_ATS_TEST_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, "start_preference"))
123 o->type = START_PREFERENCE;
125 else if (0 == strcmp (op, "stop_preference"))
127 o->type = STOP_PREFERENCE;
131 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
132 op_counter, op, cur->id);
134 GNUNET_free (op_name);
136 GNUNET_free (sec_name);
137 return GNUNET_SYSERR;
139 GNUNET_free (op_name);
142 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
143 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
144 sec_name, op_name, &o->src_id))
146 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
147 op_counter, op, cur->id);
149 GNUNET_free (op_name);
151 GNUNET_free (sec_name);
152 return GNUNET_SYSERR;
154 if (o->src_id > (e->num_masters - 1))
156 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
157 o->src_id, op_counter, op, cur->id);
159 GNUNET_free (op_name);
161 GNUNET_free (sec_name);
162 return GNUNET_SYSERR;
164 GNUNET_free (op_name);
166 /* Get destination */
167 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
168 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
169 sec_name, op_name, &o->dest_id))
171 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
172 op_counter, op, cur->id);
174 GNUNET_free (op_name);
176 GNUNET_free (sec_name);
177 return GNUNET_SYSERR;
179 if (o->dest_id > (e->num_slaves - 1))
181 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
182 o->dest_id, op_counter, op, cur->id);
184 GNUNET_free (op_name);
186 GNUNET_free (sec_name);
187 return GNUNET_SYSERR;
189 GNUNET_free (op_name);
191 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
192 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
193 sec_name, op_name, &type)) &&
194 (STOP_SEND != o->type) &&
195 (STOP_PREFERENCE != o->type) )
197 /* Load arguments for set_rate, start_send, set_preference */
198 if (0 == strcmp (type, "constant"))
200 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
202 else if (0 == strcmp (type, "linear"))
204 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
206 else if (0 == strcmp (type, "sinus"))
208 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
210 else if (0 == strcmp (type, "random"))
212 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
216 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
217 op_counter, op, cur->id);
220 GNUNET_free (op_name);
221 GNUNET_free (sec_name);
223 return GNUNET_SYSERR;
225 GNUNET_free (op_name);
228 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
229 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
230 sec_name, op_name, &o->base_rate))
232 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
233 op_counter, op, cur->id);
236 GNUNET_free (op_name);
237 GNUNET_free (sec_name);
239 return GNUNET_SYSERR;
241 GNUNET_free (op_name);
244 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
245 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
246 sec_name, op_name, &o->max_rate))
248 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
249 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
250 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
252 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
253 op_counter, op, cur->id);
255 GNUNET_free (op_name);
258 GNUNET_free (sec_name);
259 return GNUNET_SYSERR;
262 GNUNET_free (op_name);
265 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
266 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
267 sec_name, op_name, &o->period))
269 o->period = cur->duration;
271 GNUNET_free (op_name);
273 if (START_PREFERENCE == o->type)
276 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
277 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
278 sec_name, op_name, &o->frequency))
280 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
281 op_counter, op, cur->id);
283 GNUNET_free (op_name);
286 GNUNET_free (sec_name);
287 return GNUNET_SYSERR;
289 GNUNET_free (op_name);
292 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
293 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
294 sec_name, op_name, &pref))
296 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
297 op_counter, op, cur->id);
299 GNUNET_free (op_name);
301 GNUNET_free_non_null (pref);
303 GNUNET_free (sec_name);
304 return GNUNET_SYSERR;
307 if (0 == strcmp(pref, "bandwidth"))
308 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
309 else if (0 == strcmp(pref, "latency"))
310 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
313 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
314 op_counter, op, cur->id);
316 GNUNET_free (op_name);
318 GNUNET_free_non_null (pref);
320 GNUNET_free (sec_name);
321 return GNUNET_SYSERR;
324 GNUNET_free (op_name);
329 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
330 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
332 if ((o->max_rate - o->base_rate) > o->base_rate)
334 /* This will cause an underflow */
337 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
340 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
341 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
342 op_counter, cur->id, print_op (o->type), o->src_id,
343 o->dest_id, (NULL != type) ? type : "",
344 o->base_rate, o->max_rate,
345 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
347 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
348 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
350 GNUNET_free_non_null (type);
353 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
356 GNUNET_free (sec_name);
363 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
367 struct GNUNET_TIME_Relative e_duration;
369 struct Episode *last;
375 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
376 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
377 sec_name, "duration", &e_duration))
379 GNUNET_free (sec_name);
383 cur = GNUNET_new (struct Episode);
384 cur->duration = e_duration;
387 if (GNUNET_OK != load_episode (e, cur, cfg))
389 GNUNET_free (sec_name);
391 return GNUNET_SYSERR;
394 fprintf (stderr, "Found episode %u with duration %s \n",
396 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
398 /* Update experiment */
400 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
401 /* Put in linked list */
407 GNUNET_free (sec_name);
416 timeout_experiment (void *cls)
418 struct Experiment *e = cls;
419 e->experiment_timeout_task = NULL;
420 fprintf (stderr, "Experiment timeout!\n");
422 if (NULL != e->episode_timeout_task)
424 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
425 e->episode_timeout_task = NULL;
428 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
434 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
436 struct BenchmarkPeer *peer;
437 struct BenchmarkPartner *partner;
439 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
446 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
453 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
455 if (NULL != partner->tg)
457 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
458 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
462 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
463 op->gen_type, op->base_rate, op->max_rate, op->period,
464 GNUNET_TIME_UNIT_FOREVER_REL);
468 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
470 struct BenchmarkPartner *p;
471 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
478 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
482 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
483 op->src_id, op->dest_id);
484 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
491 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
493 struct BenchmarkPeer *peer;
494 struct BenchmarkPartner *partner;
496 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
503 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
510 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
512 if (NULL != partner->pg)
514 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
515 op->src_id, op->dest_id);
516 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
520 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
521 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
526 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
528 struct BenchmarkPartner *p;
529 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
536 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
540 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
541 op->src_id, op->dest_id);
542 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
547 static void enforce_episode (struct Episode *ep)
549 struct GNUNET_ATS_TEST_Operation *cur;
550 for (cur = ep->head; NULL != cur; cur = cur->next)
553 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
554 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
557 enforce_start_send (cur);
560 enforce_stop_send (cur);
562 case START_PREFERENCE:
563 enforce_start_preference (cur);
565 case STOP_PREFERENCE:
566 enforce_stop_preference (cur);
576 timeout_episode (void *cls)
578 struct Experiment *e = cls;
580 e->episode_timeout_task = NULL;
581 if (NULL != e->ep_done_cb)
582 e->ep_done_cb (e->cur);
584 /* Scheduling next */
585 e->cur = e->cur->next;
589 fprintf (stderr, "Last episode done!\n");
590 if (NULL != e->experiment_timeout_task)
592 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
593 e->experiment_timeout_task = NULL;
595 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
599 fprintf (stderr, "Running episode %u with timeout %s\n",
601 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
602 enforce_episode(e->cur);
604 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
605 &timeout_episode, e);
610 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
611 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
612 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
614 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
615 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
616 e->e_done_cb = e_done_cb;
617 e->ep_done_cb = ep_done_cb;
618 e->start_time = GNUNET_TIME_absolute_get();
620 /* Start total time out */
621 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
622 &timeout_experiment, e);
626 fprintf (stderr, "Running episode %u with timeout %s\n",
628 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
629 enforce_episode(e->cur);
630 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
631 &timeout_episode, e);
638 GNUNET_ATS_TEST_experimentation_load (const char *filename)
640 struct Experiment *e;
641 struct GNUNET_CONFIGURATION_Handle *cfg;
644 cfg = GNUNET_CONFIGURATION_create();
645 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
647 fprintf (stderr, "Failed to load `%s'\n", filename);
648 GNUNET_CONFIGURATION_destroy (cfg);
652 e = create_experiment ();
654 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
657 fprintf (stderr, "Invalid %s", "name");
662 fprintf (stderr, "Experiment name: `%s'\n", e->name);
664 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
665 "cfg_file", &e->cfg_file))
667 fprintf (stderr, "Invalid %s", "cfg_file");
672 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
674 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
675 "masters", &e->num_masters))
677 fprintf (stderr, "Invalid %s", "masters");
682 fprintf (stderr, "Experiment masters: `%llu'\n",
685 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
686 "slaves", &e->num_slaves))
688 fprintf (stderr, "Invalid %s", "slaves");
693 fprintf (stderr, "Experiment slaves: `%llu'\n",
696 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
697 "log_freq", &e->log_freq))
699 fprintf (stderr, "Invalid %s", "log_freq");
704 fprintf (stderr, "Experiment logging frequency: `%s'\n",
705 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
707 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
708 "max_duration", &e->max_duration))
710 fprintf (stderr, "Invalid %s", "max_duration");
715 fprintf (stderr, "Experiment duration: `%s'\n",
716 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
718 load_episodes (e, cfg);
719 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
721 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
723 GNUNET_CONFIGURATION_destroy (cfg);
728 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
730 if (NULL != e->experiment_timeout_task)
732 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
733 e->experiment_timeout_task = NULL;
735 if (NULL != e->episode_timeout_task)
737 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
738 e->episode_timeout_task = NULL;
743 /* end of file ats-testing-experiment.c*/