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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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)
38 case START_PREFERENCE:
39 return "START_PREFERENCE";
41 return "STOP_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 GNUNET_ATS_TEST_Operation *cur_o;
68 struct GNUNET_ATS_TEST_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);
91 load_episode (struct Experiment *e,
93 struct GNUNET_CONFIGURATION_Handle *cfg)
95 struct GNUNET_ATS_TEST_Operation *o;
103 fprintf (stderr, "Parsing episode %u\n",cur->id);
104 GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
109 GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
110 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
111 sec_name, op_name, &op))
113 GNUNET_free (op_name);
116 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
117 /* operations = set_rate, start_send, stop_send, set_preference */
118 if (0 == strcmp (op, "start_send"))
120 o->type = START_SEND;
122 else if (0 == strcmp (op, "stop_send"))
126 else if (0 == strcmp (op, "start_preference"))
128 o->type = START_PREFERENCE;
130 else if (0 == strcmp (op, "stop_preference"))
132 o->type = STOP_PREFERENCE;
136 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
137 op_counter, op, cur->id);
139 GNUNET_free (op_name);
141 GNUNET_free (sec_name);
142 return GNUNET_SYSERR;
144 GNUNET_free (op_name);
147 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
148 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
149 sec_name, op_name, &o->src_id))
151 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
152 op_counter, op, cur->id);
154 GNUNET_free (op_name);
156 GNUNET_free (sec_name);
157 return GNUNET_SYSERR;
159 if (o->src_id > (e->num_masters - 1))
161 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
162 o->src_id, op_counter, op, cur->id);
164 GNUNET_free (op_name);
166 GNUNET_free (sec_name);
167 return GNUNET_SYSERR;
169 GNUNET_free (op_name);
171 /* Get destination */
172 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
173 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
174 sec_name, op_name, &o->dest_id))
176 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
177 op_counter, op, cur->id);
179 GNUNET_free (op_name);
181 GNUNET_free (sec_name);
182 return GNUNET_SYSERR;
184 if (o->dest_id > (e->num_slaves - 1))
186 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
187 o->dest_id, op_counter, op, cur->id);
189 GNUNET_free (op_name);
191 GNUNET_free (sec_name);
192 return GNUNET_SYSERR;
194 GNUNET_free (op_name);
196 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
197 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
198 sec_name, op_name, &type)) &&
199 ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
201 /* Load arguments for set_rate, start_send, set_preference */
202 if (0 == strcmp (type, "constant"))
204 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
206 else if (0 == strcmp (type, "linear"))
208 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
210 else if (0 == strcmp (type, "sinus"))
212 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
214 else if (0 == strcmp (type, "random"))
216 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
220 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
221 op_counter, op, cur->id);
224 GNUNET_free (op_name);
225 GNUNET_free (sec_name);
227 return GNUNET_SYSERR;
229 GNUNET_free (op_name);
232 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
233 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
234 sec_name, op_name, &o->base_rate))
236 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
237 op_counter, op, cur->id);
240 GNUNET_free (op_name);
241 GNUNET_free (sec_name);
243 return GNUNET_SYSERR;
245 GNUNET_free (op_name);
248 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
249 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
250 sec_name, op_name, &o->max_rate))
252 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
253 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
254 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
256 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
257 op_counter, op, cur->id);
259 GNUNET_free (op_name);
262 GNUNET_free (sec_name);
263 return GNUNET_SYSERR;
266 GNUNET_free (op_name);
269 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
270 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
271 sec_name, op_name, &o->period))
273 o->period = cur->duration;
275 GNUNET_free (op_name);
277 if (START_PREFERENCE == o->type)
280 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
281 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
282 sec_name, op_name, &o->frequency))
284 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
285 op_counter, op, cur->id);
287 GNUNET_free (op_name);
290 GNUNET_free (sec_name);
291 return GNUNET_SYSERR;
293 GNUNET_free (op_name);
296 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
297 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
298 sec_name, op_name, &pref))
300 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
301 op_counter, op, cur->id);
303 GNUNET_free (op_name);
305 GNUNET_free_non_null (pref);
307 GNUNET_free (sec_name);
308 return GNUNET_SYSERR;
311 if (0 == strcmp(pref, "bandwidth"))
312 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
313 else if (0 == strcmp(pref, "latency"))
314 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
317 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
318 op_counter, op, cur->id);
320 GNUNET_free (op_name);
322 GNUNET_free_non_null (pref);
324 GNUNET_free (sec_name);
325 return GNUNET_SYSERR;
328 GNUNET_free (op_name);
333 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
334 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
336 if ((o->max_rate - o->base_rate) > o->base_rate)
338 /* This will cause an underflow */
341 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
344 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
345 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
346 op_counter, cur->id, print_op (o->type), o->src_id,
347 o->dest_id, (NULL != type) ? type : "",
348 o->base_rate, o->max_rate,
349 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
351 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
352 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
354 GNUNET_free_non_null (type);
357 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
360 GNUNET_free (sec_name);
367 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
371 struct GNUNET_TIME_Relative e_duration;
373 struct Episode *last;
379 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
380 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
381 sec_name, "duration", &e_duration))
383 GNUNET_free (sec_name);
387 cur = GNUNET_new (struct Episode);
388 cur->duration = e_duration;
391 if (GNUNET_OK != load_episode (e, cur, cfg))
393 GNUNET_free (sec_name);
395 return GNUNET_SYSERR;
398 fprintf (stderr, "Found episode %u with duration %s \n",
400 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
402 /* Update experiment */
404 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
405 /* Put in linked list */
411 GNUNET_free (sec_name);
420 timeout_experiment (void *cls)
422 struct Experiment *e = cls;
423 e->experiment_timeout_task = NULL;
424 fprintf (stderr, "Experiment timeout!\n");
426 if (NULL != e->episode_timeout_task)
428 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
429 e->episode_timeout_task = NULL;
432 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
438 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
440 struct BenchmarkPeer *peer;
441 struct BenchmarkPartner *partner;
443 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
450 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
457 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
459 if (NULL != partner->tg)
461 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
462 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
466 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
467 op->gen_type, op->base_rate, op->max_rate, op->period,
468 GNUNET_TIME_UNIT_FOREVER_REL);
472 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
474 struct BenchmarkPartner *p;
475 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
482 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
486 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
487 op->src_id, op->dest_id);
488 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
495 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
497 struct BenchmarkPeer *peer;
498 struct BenchmarkPartner *partner;
500 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
507 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
514 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
516 if (NULL != partner->pg)
518 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
519 op->src_id, op->dest_id);
520 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
524 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
525 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
530 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
532 struct BenchmarkPartner *p;
533 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
540 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
544 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
545 op->src_id, op->dest_id);
546 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
551 static void enforce_episode (struct Episode *ep)
553 struct GNUNET_ATS_TEST_Operation *cur;
554 for (cur = ep->head; NULL != cur; cur = cur->next)
557 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
558 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
561 enforce_start_send (cur);
564 enforce_stop_send (cur);
566 case START_PREFERENCE:
567 enforce_start_preference (cur);
569 case STOP_PREFERENCE:
570 enforce_stop_preference (cur);
580 timeout_episode (void *cls)
582 struct Experiment *e = cls;
584 e->episode_timeout_task = NULL;
585 if (NULL != e->ep_done_cb)
586 e->ep_done_cb (e->cur);
588 /* Scheduling next */
589 e->cur = e->cur->next;
593 fprintf (stderr, "Last episode done!\n");
594 if (NULL != e->experiment_timeout_task)
596 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
597 e->experiment_timeout_task = NULL;
599 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
603 fprintf (stderr, "Running episode %u with timeout %s\n",
605 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
606 enforce_episode(e->cur);
608 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
609 &timeout_episode, e);
614 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
615 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
616 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
618 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
619 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
620 e->e_done_cb = e_done_cb;
621 e->ep_done_cb = ep_done_cb;
622 e->start_time = GNUNET_TIME_absolute_get();
624 /* Start total time out */
625 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
626 &timeout_experiment, e);
630 fprintf (stderr, "Running episode %u with timeout %s\n",
632 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
633 enforce_episode(e->cur);
634 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
635 &timeout_episode, e);
642 GNUNET_ATS_TEST_experimentation_load (char *filename)
644 struct Experiment *e;
645 struct GNUNET_CONFIGURATION_Handle *cfg;
648 cfg = GNUNET_CONFIGURATION_create();
649 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
651 fprintf (stderr, "Failed to load `%s'\n", filename);
652 GNUNET_CONFIGURATION_destroy (cfg);
656 e = create_experiment ();
658 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
661 fprintf (stderr, "Invalid %s", "name");
666 fprintf (stderr, "Experiment name: `%s'\n", e->name);
668 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
669 "cfg_file", &e->cfg_file))
671 fprintf (stderr, "Invalid %s", "cfg_file");
676 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
678 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
679 "masters", &e->num_masters))
681 fprintf (stderr, "Invalid %s", "masters");
686 fprintf (stderr, "Experiment masters: `%llu'\n",
689 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
690 "slaves", &e->num_slaves))
692 fprintf (stderr, "Invalid %s", "slaves");
697 fprintf (stderr, "Experiment slaves: `%llu'\n",
700 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
701 "log_freq", &e->log_freq))
703 fprintf (stderr, "Invalid %s", "log_freq");
708 fprintf (stderr, "Experiment logging frequency: `%s'\n",
709 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
711 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
712 "max_duration", &e->max_duration))
714 fprintf (stderr, "Invalid %s", "max_duration");
719 fprintf (stderr, "Experiment duration: `%s'\n",
720 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
722 load_episodes (e, cfg);
723 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
725 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
727 GNUNET_CONFIGURATION_destroy (cfg);
732 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
734 if (NULL != e->experiment_timeout_task)
736 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
737 e->experiment_timeout_task = NULL;
739 if (NULL != e->episode_timeout_task)
741 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
742 e->episode_timeout_task = NULL;
747 /* end of file ats-testing-experiment.c*/