2 This file is part of GNUnet.
3 Copyright (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)
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);
323 GNUNET_free_non_null (pref);
325 GNUNET_free (sec_name);
326 return GNUNET_SYSERR;
329 GNUNET_free (op_name);
334 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
335 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
337 if ((o->max_rate - o->base_rate) > o->base_rate)
339 /* This will cause an underflow */
342 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
345 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
346 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
347 op_counter, cur->id, print_op (o->type), o->src_id,
348 o->dest_id, (NULL != type) ? type : "",
349 o->base_rate, o->max_rate,
350 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
352 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
353 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
355 GNUNET_free_non_null (type);
358 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
361 GNUNET_free (sec_name);
368 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
372 struct GNUNET_TIME_Relative e_duration;
374 struct Episode *last;
380 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
381 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
382 sec_name, "duration", &e_duration))
384 GNUNET_free (sec_name);
388 cur = GNUNET_new (struct Episode);
389 cur->duration = e_duration;
392 if (GNUNET_OK != load_episode (e, cur, cfg))
394 GNUNET_free (sec_name);
396 return GNUNET_SYSERR;
399 fprintf (stderr, "Found episode %u with duration %s \n",
401 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
403 /* Update experiment */
405 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
406 /* Put in linked list */
412 GNUNET_free (sec_name);
420 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
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),
437 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
439 struct BenchmarkPeer *peer;
440 struct BenchmarkPartner *partner;
442 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
449 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
456 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
458 if (NULL != partner->tg)
460 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
461 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
465 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
466 op->gen_type, op->base_rate, op->max_rate, op->period,
467 GNUNET_TIME_UNIT_FOREVER_REL);
471 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
473 struct BenchmarkPartner *p;
474 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
481 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
485 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
486 op->src_id, op->dest_id);
487 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
494 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
496 struct BenchmarkPeer *peer;
497 struct BenchmarkPartner *partner;
499 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
506 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
513 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
515 if (NULL != partner->pg)
517 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
518 op->src_id, op->dest_id);
519 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
523 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
524 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
529 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
531 struct BenchmarkPartner *p;
532 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
539 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
543 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
544 op->src_id, op->dest_id);
545 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
550 static void enforce_episode (struct Episode *ep)
552 struct GNUNET_ATS_TEST_Operation *cur;
553 for (cur = ep->head; NULL != cur; cur = cur->next)
556 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
557 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
560 enforce_start_send (cur);
563 enforce_stop_send (cur);
565 case START_PREFERENCE:
566 enforce_start_preference (cur);
568 case STOP_PREFERENCE:
569 enforce_stop_preference (cur);
578 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
580 struct Experiment *e = cls;
581 e->episode_timeout_task = NULL;
582 if (NULL != e->ep_done_cb)
583 e->ep_done_cb (e->cur);
585 /* Scheduling next */
586 e->cur = e->cur->next;
590 fprintf (stderr, "Last episode done!\n");
591 if (NULL != e->experiment_timeout_task)
593 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
594 e->experiment_timeout_task = NULL;
596 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
600 fprintf (stderr, "Running episode %u with timeout %s\n",
602 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
603 enforce_episode(e->cur);
605 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
606 &timeout_episode, e);
611 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
612 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
613 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
615 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
616 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
617 e->e_done_cb = e_done_cb;
618 e->ep_done_cb = ep_done_cb;
619 e->start_time = GNUNET_TIME_absolute_get();
621 /* Start total time out */
622 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
623 &timeout_experiment, e);
627 fprintf (stderr, "Running episode %u with timeout %s\n",
629 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
630 enforce_episode(e->cur);
631 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
632 &timeout_episode, e);
639 GNUNET_ATS_TEST_experimentation_load (char *filename)
641 struct Experiment *e;
642 struct GNUNET_CONFIGURATION_Handle *cfg;
645 cfg = GNUNET_CONFIGURATION_create();
646 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
648 fprintf (stderr, "Failed to load `%s'\n", filename);
649 GNUNET_CONFIGURATION_destroy (cfg);
653 e = create_experiment ();
655 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
658 fprintf (stderr, "Invalid %s", "name");
663 fprintf (stderr, "Experiment name: `%s'\n", e->name);
665 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
666 "cfg_file", &e->cfg_file))
668 fprintf (stderr, "Invalid %s", "cfg_file");
673 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
675 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
676 "masters", &e->num_masters))
678 fprintf (stderr, "Invalid %s", "masters");
683 fprintf (stderr, "Experiment masters: `%llu'\n",
686 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
687 "slaves", &e->num_slaves))
689 fprintf (stderr, "Invalid %s", "slaves");
694 fprintf (stderr, "Experiment slaves: `%llu'\n",
697 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
698 "log_freq", &e->log_freq))
700 fprintf (stderr, "Invalid %s", "log_freq");
705 fprintf (stderr, "Experiment logging frequency: `%s'\n",
706 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
708 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
709 "max_duration", &e->max_duration))
711 fprintf (stderr, "Invalid %s", "max_duration");
716 fprintf (stderr, "Experiment duration: `%s'\n",
717 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
719 load_episodes (e, cfg);
720 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
722 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
724 GNUNET_CONFIGURATION_destroy (cfg);
729 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
731 if (NULL != e->experiment_timeout_task)
733 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
734 e->experiment_timeout_task = NULL;
736 if (NULL != e->episode_timeout_task)
738 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
739 e->episode_timeout_task = NULL;
744 /* end of file ats-testing-experiment.c*/