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 "gnunet_ats_plugin.h"
29 #include "gnunet_ats_service.h"
30 #include "ats-testing.h"
37 print_op (enum OperationType op)
44 case START_PREFERENCE:
45 return "START_PREFERENCE";
47 return "STOP_PREFERENCE";
55 static struct Experiment *
59 e = GNUNET_new (struct Experiment);
64 e->total_duration = GNUNET_TIME_UNIT_ZERO;
70 free_experiment (struct Experiment *e)
74 struct GNUNET_ATS_TEST_Operation *cur_o;
75 struct GNUNET_ATS_TEST_Operation *next_o;
78 for (cur = next; NULL != cur; cur = next)
83 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
91 GNUNET_free_non_null (e->name);
92 GNUNET_free_non_null (e->cfg_file);
98 load_episode (struct Experiment *e,
100 struct GNUNET_CONFIGURATION_Handle *cfg)
102 struct GNUNET_ATS_TEST_Operation *o;
109 fprintf (stderr, "Parsing episode %u\n",cur->id);
110 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
115 GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
116 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
117 sec_name, op_name, &op))
119 GNUNET_free (op_name);
122 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
123 /* operations = set_rate, start_send, stop_send, set_preference */
124 if (0 == strcmp (op, "start_send"))
126 o->type = START_SEND;
128 else if (0 == strcmp (op, "stop_send"))
132 else if (0 == strcmp (op, "start_preference"))
134 o->type = START_PREFERENCE;
136 else if (0 == strcmp (op, "stop_preference"))
138 o->type = STOP_PREFERENCE;
142 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
143 op_counter, op, cur->id);
145 GNUNET_free (op_name);
146 GNUNET_free (sec_name);
148 return GNUNET_SYSERR;
150 GNUNET_free (op_name);
153 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
154 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
155 sec_name, op_name, &o->src_id))
157 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
158 op_counter, op, cur->id);
160 GNUNET_free (op_name);
161 GNUNET_free (sec_name);
163 return GNUNET_SYSERR;
165 if (o->src_id > (e->num_masters - 1))
167 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
168 o->src_id, op_counter, op, cur->id);
170 GNUNET_free (op_name);
171 GNUNET_free (sec_name);
173 return GNUNET_SYSERR;
175 GNUNET_free (op_name);
177 /* Get destination */
178 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
179 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
180 sec_name, op_name, &o->dest_id))
182 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
183 op_counter, op, cur->id);
185 GNUNET_free (op_name);
186 GNUNET_free (sec_name);
188 return GNUNET_SYSERR;
190 if (o->dest_id > (e->num_slaves - 1))
193 "Invalid destination %llu in operation %u `%s' in episode %u\n",
199 GNUNET_free (op_name);
200 GNUNET_free (sec_name);
202 return GNUNET_SYSERR;
204 GNUNET_free (op_name);
206 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
207 if ( (GNUNET_SYSERR !=
208 GNUNET_CONFIGURATION_get_value_string(cfg,
212 (STOP_SEND != o->type) &&
213 (STOP_PREFERENCE != o->type) )
215 /* Load arguments for set_rate, start_send, set_preference */
216 if (0 == strcmp (type, "constant"))
218 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
220 else if (0 == strcmp (type, "linear"))
222 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
224 else if (0 == strcmp (type, "sinus"))
226 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
228 else if (0 == strcmp (type, "random"))
230 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
234 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
235 op_counter, op, cur->id);
238 GNUNET_free (op_name);
239 GNUNET_free (sec_name);
241 return GNUNET_SYSERR;
243 GNUNET_free (op_name);
246 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
247 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
248 sec_name, op_name, &o->base_rate))
250 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
251 op_counter, op, cur->id);
254 GNUNET_free (op_name);
255 GNUNET_free (sec_name);
257 return GNUNET_SYSERR;
259 GNUNET_free (op_name);
262 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
264 GNUNET_CONFIGURATION_get_value_number (cfg,
269 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
270 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
271 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
273 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
274 op_counter, op, cur->id);
276 GNUNET_free (op_name);
278 GNUNET_free (sec_name);
280 return GNUNET_SYSERR;
283 GNUNET_free (op_name);
286 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
287 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
288 sec_name, op_name, &o->period))
290 o->period = cur->duration;
292 GNUNET_free (op_name);
294 if (START_PREFERENCE == o->type)
297 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
298 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
299 sec_name, op_name, &o->frequency))
301 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
302 op_counter, op, cur->id);
304 GNUNET_free (op_name);
306 GNUNET_free (sec_name);
308 return GNUNET_SYSERR;
310 GNUNET_free (op_name);
313 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
314 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
315 sec_name, op_name, &pref))
317 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
318 op_counter, op, cur->id);
320 GNUNET_free (op_name);
322 GNUNET_free (sec_name);
323 GNUNET_free_non_null (pref);
325 return GNUNET_SYSERR;
328 if (0 == strcmp(pref, "bandwidth"))
329 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
330 else if (0 == strcmp(pref, "latency"))
331 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
335 "Invalid preference in operation %u `%s' in episode %u\n",
340 GNUNET_free (op_name);
342 GNUNET_free (sec_name);
343 GNUNET_free_non_null (pref);
345 return GNUNET_SYSERR;
348 GNUNET_free (op_name);
353 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
354 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
356 if ((o->max_rate - o->base_rate) > o->base_rate)
358 /* This will cause an underflow */
361 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
364 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
365 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
366 op_counter, cur->id, print_op (o->type), o->src_id,
367 o->dest_id, (NULL != type) ? type : "",
368 o->base_rate, o->max_rate,
369 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
371 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
372 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
374 GNUNET_free_non_null (type);
377 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
380 GNUNET_free (sec_name);
386 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
390 struct GNUNET_TIME_Relative e_duration;
392 struct Episode *last;
398 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
399 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
400 sec_name, "duration", &e_duration))
402 GNUNET_free (sec_name);
406 cur = GNUNET_new (struct Episode);
407 cur->duration = e_duration;
410 if (GNUNET_OK != load_episode (e, cur, cfg))
412 GNUNET_free (sec_name);
414 return GNUNET_SYSERR;
417 fprintf (stderr, "Found episode %u with duration %s \n",
419 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
421 /* Update experiment */
423 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
424 /* Put in linked list */
430 GNUNET_free (sec_name);
439 timeout_experiment (void *cls)
441 struct Experiment *e = cls;
442 e->experiment_timeout_task = NULL;
443 fprintf (stderr, "Experiment timeout!\n");
445 if (NULL != e->episode_timeout_task)
447 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
448 e->episode_timeout_task = NULL;
451 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
457 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
460 struct BenchmarkPeer *peer;
461 struct BenchmarkPartner *partner;
463 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
470 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
477 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
479 if (NULL != partner->tg)
481 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
482 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
486 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
487 op->tg_type, op->base_rate, op->max_rate, op->period,
488 GNUNET_TIME_UNIT_FOREVER_REL);
493 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
496 struct BenchmarkPartner *p;
497 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
504 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
508 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
509 op->src_id, op->dest_id);
510 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
518 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
521 struct BenchmarkPeer *peer;
522 struct BenchmarkPartner *partner;
524 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
531 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
538 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
540 if (NULL != partner->pg)
542 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
543 op->src_id, op->dest_id);
544 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
548 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
549 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
555 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
558 struct BenchmarkPartner *p;
559 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
566 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
570 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
571 op->src_id, op->dest_id);
572 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
578 static void enforce_episode (struct Episode *ep)
580 struct GNUNET_ATS_TEST_Operation *cur;
581 for (cur = ep->head; NULL != cur; cur = cur->next)
584 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
585 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
588 enforce_start_send (cur);
591 enforce_stop_send (cur);
593 case START_PREFERENCE:
594 enforce_start_preference (cur);
596 case STOP_PREFERENCE:
597 enforce_stop_preference (cur);
607 timeout_episode (void *cls)
609 struct Experiment *e = cls;
610 e->episode_timeout_task = NULL;
611 if (NULL != e->ep_done_cb)
612 e->ep_done_cb (e->cur);
614 /* Scheduling next */
615 e->cur = e->cur->next;
619 fprintf (stderr, "Last episode done!\n");
620 if (NULL != e->experiment_timeout_task)
622 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
623 e->experiment_timeout_task = NULL;
625 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
629 fprintf (stderr, "Running episode %u with timeout %s\n",
631 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
632 enforce_episode(e->cur);
634 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
635 &timeout_episode, e);
640 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
641 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
642 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
644 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
645 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
646 e->e_done_cb = e_done_cb;
647 e->ep_done_cb = ep_done_cb;
648 e->start_time = GNUNET_TIME_absolute_get();
650 /* Start total time out */
651 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
652 &timeout_experiment, e);
656 fprintf (stderr, "Running episode %u with timeout %s\n",
658 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
659 enforce_episode(e->cur);
660 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
661 &timeout_episode, e);
668 GNUNET_ATS_solvers_experimentation_load (char *filename)
670 struct Experiment *e;
671 struct GNUNET_CONFIGURATION_Handle *cfg;
674 cfg = GNUNET_CONFIGURATION_create();
675 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
677 fprintf (stderr, "Failed to load `%s'\n", filename);
678 GNUNET_CONFIGURATION_destroy (cfg);
682 e = create_experiment ();
684 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
687 fprintf (stderr, "Invalid %s", "name");
692 fprintf (stderr, "Experiment name: `%s'\n", e->name);
694 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
695 "cfg_file", &e->cfg_file))
697 fprintf (stderr, "Invalid %s", "cfg_file");
702 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
704 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
705 "masters", &e->num_masters))
707 fprintf (stderr, "Invalid %s", "masters");
712 fprintf (stderr, "Experiment masters: `%llu'\n",
715 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
716 "slaves", &e->num_slaves))
718 fprintf (stderr, "Invalid %s", "slaves");
723 fprintf (stderr, "Experiment slaves: `%llu'\n",
726 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
727 "log_freq", &e->log_freq))
729 fprintf (stderr, "Invalid %s", "log_freq");
734 fprintf (stderr, "Experiment logging frequency: `%s'\n",
735 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
737 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
738 "max_duration", &e->max_duration))
740 fprintf (stderr, "Invalid %s", "max_duration");
745 fprintf (stderr, "Experiment duration: `%s'\n",
746 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
748 load_episodes (e, cfg);
749 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
751 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
753 GNUNET_CONFIGURATION_destroy (cfg);
758 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
760 if (NULL != e->experiment_timeout_task)
762 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
763 e->experiment_timeout_task = NULL;
765 if (NULL != e->episode_timeout_task)
767 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
768 e->episode_timeout_task = NULL;
777 struct GNUNET_ATS_TESTING_SolverHandle
780 struct GNUNET_ATS_PluginEnvironment env;
784 enum GNUNET_ATS_Solvers
786 GNUNET_ATS_SOLVER_PROPORTIONAL,
787 GNUNET_ATS_SOLVER_MLP,
788 GNUNET_ATS_SOLVER_RIL,
792 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
794 GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
795 GNUNET_free (sh->plugin);
799 struct GNUNET_ATS_TESTING_SolverHandle *
800 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
802 struct GNUNET_ATS_TESTING_SolverHandle *sh;
806 case GNUNET_ATS_SOLVER_PROPORTIONAL:
807 solver_str = "proportional";
809 case GNUNET_ATS_SOLVER_MLP:
812 case GNUNET_ATS_SOLVER_RIL:
821 sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
822 GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
823 //sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
824 if (NULL == sh->solver)
826 fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
833 static struct Experiment *e;
835 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
838 * cmd option -e: experiment file
840 static char *opt_exp_file;
842 static char *opt_solver;
845 * cmd option -l: enable logging
850 * cmd option -p: enable plots
855 * cmd option -v: verbose logs
857 static int opt_verbose;
860 run (void *cls, char * const *args, const char *cfgfile,
861 const struct GNUNET_CONFIGURATION_Handle *cfg)
863 enum GNUNET_ATS_Solvers solver;
865 if (NULL == opt_exp_file)
867 fprintf (stderr, "No experiment given ...\n");
871 if (NULL == opt_solver)
873 fprintf (stderr, "No solver given ...\n");
877 if (0 == strcmp(opt_solver, "mlp"))
879 solver = GNUNET_ATS_SOLVER_MLP;
881 else if (0 == strcmp(opt_solver, "proportional"))
883 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
885 else if (0 == strcmp(opt_solver, "ril"))
887 solver = GNUNET_ATS_SOLVER_RIL;
891 fprintf (stderr, "No solver given ...");
895 /* load experiment */
896 e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
899 fprintf (stderr, "Failed to load experiment ...\n");
904 sh = GNUNET_ATS_solvers_solver_start (solver);
907 fprintf (stderr, "Failed to start solver ...\n");
920 * Main function of the benchmark
922 * @param argc argument count
923 * @param argv argument values
926 main (int argc, char *argv[])
932 opt_plot = GNUNET_NO;
934 static struct GNUNET_GETOPT_CommandLineOption options[] =
936 { 's', "solver", NULL,
937 gettext_noop ("solver to use"),
938 1, &GNUNET_GETOPT_set_string, &opt_solver},
939 { 'e', "experiment", NULL,
940 gettext_noop ("experiment to use"),
941 1, &GNUNET_GETOPT_set_string, &opt_exp_file},
942 { 'e', "experiment", NULL,
943 gettext_noop ("experiment to use"),
944 1, &GNUNET_GETOPT_set_one, &opt_verbose},
945 GNUNET_GETOPT_OPTION_END
949 GNUNET_PROGRAM_run (argc,
958 /* end of file gnunet-solver-eval.c*/