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 "gnunet_ats_plugin.h"
29 #include "gnunet_ats_service.h"
30 #include "ats-testing.h"
38 print_op (enum OperationType op)
45 case START_PREFERENCE:
46 return "START_PREFERENCE";
48 return "STOP_PREFERENCE";
56 static struct Experiment *
60 e = GNUNET_new (struct Experiment);
65 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);
97 load_episode (struct Experiment *e, struct Episode *cur,
98 struct GNUNET_CONFIGURATION_Handle *cfg)
100 struct GNUNET_ATS_TEST_Operation *o;
107 fprintf (stderr, "Parsing episode %u\n",cur->id);
108 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
113 GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
114 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
115 sec_name, op_name, &op))
117 GNUNET_free (op_name);
120 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
121 /* operations = set_rate, start_send, stop_send, set_preference */
122 if (0 == strcmp (op, "start_send"))
124 o->type = START_SEND;
126 else if (0 == strcmp (op, "stop_send"))
130 else if (0 == strcmp (op, "start_preference"))
132 o->type = START_PREFERENCE;
134 else if (0 == strcmp (op, "stop_preference"))
136 o->type = STOP_PREFERENCE;
140 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
141 op_counter, op, cur->id);
143 GNUNET_free (op_name);
144 GNUNET_free (sec_name);
146 return GNUNET_SYSERR;
148 GNUNET_free (op_name);
151 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
152 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
153 sec_name, op_name, &o->src_id))
155 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
156 op_counter, op, cur->id);
158 GNUNET_free (op_name);
159 GNUNET_free (sec_name);
161 return GNUNET_SYSERR;
163 if (o->src_id > (e->num_masters - 1))
165 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
166 o->src_id, op_counter, op, cur->id);
168 GNUNET_free (op_name);
169 GNUNET_free (sec_name);
171 return GNUNET_SYSERR;
173 GNUNET_free (op_name);
175 /* Get destination */
176 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
177 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
178 sec_name, op_name, &o->dest_id))
180 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
181 op_counter, op, cur->id);
183 GNUNET_free (op_name);
184 GNUNET_free (sec_name);
186 return GNUNET_SYSERR;
188 if (o->dest_id > (e->num_slaves - 1))
190 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
191 o->dest_id, op_counter, op, cur->id);
193 GNUNET_free (op_name);
194 GNUNET_free (sec_name);
196 return GNUNET_SYSERR;
198 GNUNET_free (op_name);
200 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
201 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
202 sec_name, op_name, &type)) &&
203 ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
205 /* Load arguments for set_rate, start_send, set_preference */
206 if (0 == strcmp (type, "constant"))
208 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
210 else if (0 == strcmp (type, "linear"))
212 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
214 else if (0 == strcmp (type, "sinus"))
216 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
218 else if (0 == strcmp (type, "random"))
220 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
224 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
225 op_counter, op, cur->id);
228 GNUNET_free (op_name);
229 GNUNET_free (sec_name);
231 return GNUNET_SYSERR;
233 GNUNET_free (op_name);
236 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
237 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
238 sec_name, op_name, &o->base_rate))
240 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
241 op_counter, op, cur->id);
244 GNUNET_free (op_name);
245 GNUNET_free (sec_name);
247 return GNUNET_SYSERR;
249 GNUNET_free (op_name);
252 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
253 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
254 sec_name, op_name, &o->max_rate))
256 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
257 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
258 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
260 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
261 op_counter, op, cur->id);
263 GNUNET_free (op_name);
265 GNUNET_free (sec_name);
267 return GNUNET_SYSERR;
270 GNUNET_free (op_name);
273 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
274 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
275 sec_name, op_name, &o->period))
277 o->period = cur->duration;
279 GNUNET_free (op_name);
281 if (START_PREFERENCE == o->type)
284 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
285 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
286 sec_name, op_name, &o->frequency))
288 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
289 op_counter, op, cur->id);
291 GNUNET_free (op_name);
293 GNUNET_free (sec_name);
295 return GNUNET_SYSERR;
297 GNUNET_free (op_name);
300 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
301 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
302 sec_name, op_name, &pref))
304 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
305 op_counter, op, cur->id);
307 GNUNET_free (op_name);
309 GNUNET_free (sec_name);
310 GNUNET_free_non_null (pref);
312 return GNUNET_SYSERR;
315 if (0 == strcmp(pref, "bandwidth"))
316 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
317 else if (0 == strcmp(pref, "latency"))
318 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
321 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
322 op_counter, op, cur->id);
324 GNUNET_free (op_name);
327 GNUNET_free (sec_name);
328 GNUNET_free_non_null (pref);
330 return GNUNET_SYSERR;
333 GNUNET_free (op_name);
338 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
339 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
341 if ((o->max_rate - o->base_rate) > o->base_rate)
343 /* This will cause an underflow */
346 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
349 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
350 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
351 op_counter, cur->id, print_op (o->type), o->src_id,
352 o->dest_id, (NULL != type) ? type : "",
353 o->base_rate, o->max_rate,
354 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
356 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
357 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
359 GNUNET_free_non_null (type);
362 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
365 GNUNET_free (sec_name);
371 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
375 struct GNUNET_TIME_Relative e_duration;
377 struct Episode *last;
383 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
384 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
385 sec_name, "duration", &e_duration))
387 GNUNET_free (sec_name);
391 cur = GNUNET_new (struct Episode);
392 cur->duration = e_duration;
395 if (GNUNET_OK != load_episode (e, cur, cfg))
397 GNUNET_free (sec_name);
399 return GNUNET_SYSERR;
402 fprintf (stderr, "Found episode %u with duration %s \n",
404 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
406 /* Update experiment */
408 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
409 /* Put in linked list */
415 GNUNET_free (sec_name);
423 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
425 struct Experiment *e = cls;
426 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
427 fprintf (stderr, "Experiment timeout!\n");
429 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
431 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
432 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
435 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
440 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
443 struct BenchmarkPeer *peer;
444 struct BenchmarkPartner *partner;
446 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
453 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
460 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
462 if (NULL != partner->tg)
464 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
465 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
469 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
470 op->tg_type, op->base_rate, op->max_rate, op->period,
471 GNUNET_TIME_UNIT_FOREVER_REL);
476 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
479 struct BenchmarkPartner *p;
480 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
487 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
491 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
492 op->src_id, op->dest_id);
493 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
501 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
504 struct BenchmarkPeer *peer;
505 struct BenchmarkPartner *partner;
507 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
514 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
521 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
523 if (NULL != partner->pg)
525 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
526 op->src_id, op->dest_id);
527 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
531 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
532 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
538 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
541 struct BenchmarkPartner *p;
542 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
549 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
553 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
554 op->src_id, op->dest_id);
555 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
561 static void enforce_episode (struct Episode *ep)
563 struct GNUNET_ATS_TEST_Operation *cur;
564 for (cur = ep->head; NULL != cur; cur = cur->next)
567 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
568 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
571 enforce_start_send (cur);
574 enforce_stop_send (cur);
576 case START_PREFERENCE:
577 enforce_start_preference (cur);
579 case STOP_PREFERENCE:
580 enforce_stop_preference (cur);
589 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
591 struct Experiment *e = cls;
592 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
593 if (NULL != e->ep_done_cb)
594 e->ep_done_cb (e->cur);
596 /* Scheduling next */
597 e->cur = e->cur->next;
601 fprintf (stderr, "Last episode done!\n");
602 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
604 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
605 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
607 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
611 fprintf (stderr, "Running episode %u with timeout %s\n",
613 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
614 enforce_episode(e->cur);
616 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
617 &timeout_episode, e);
622 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
623 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
624 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
626 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
627 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
628 e->e_done_cb = e_done_cb;
629 e->ep_done_cb = ep_done_cb;
630 e->start_time = GNUNET_TIME_absolute_get();
632 /* Start total time out */
633 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
634 &timeout_experiment, e);
638 fprintf (stderr, "Running episode %u with timeout %s\n",
640 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
641 enforce_episode(e->cur);
642 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
643 &timeout_episode, e);
650 GNUNET_ATS_solvers_experimentation_load (char *filename)
652 struct Experiment *e;
653 struct GNUNET_CONFIGURATION_Handle *cfg;
656 cfg = GNUNET_CONFIGURATION_create();
657 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
659 fprintf (stderr, "Failed to load `%s'\n", filename);
660 GNUNET_CONFIGURATION_destroy (cfg);
664 e = create_experiment ();
666 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
669 fprintf (stderr, "Invalid %s", "name");
674 fprintf (stderr, "Experiment name: `%s'\n", e->name);
676 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
677 "cfg_file", &e->cfg_file))
679 fprintf (stderr, "Invalid %s", "cfg_file");
684 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
686 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
687 "masters", &e->num_masters))
689 fprintf (stderr, "Invalid %s", "masters");
694 fprintf (stderr, "Experiment masters: `%llu'\n",
697 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
698 "slaves", &e->num_slaves))
700 fprintf (stderr, "Invalid %s", "slaves");
705 fprintf (stderr, "Experiment slaves: `%llu'\n",
708 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
709 "log_freq", &e->log_freq))
711 fprintf (stderr, "Invalid %s", "log_freq");
716 fprintf (stderr, "Experiment logging frequency: `%s'\n",
717 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
719 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
720 "max_duration", &e->max_duration))
722 fprintf (stderr, "Invalid %s", "max_duration");
727 fprintf (stderr, "Experiment duration: `%s'\n",
728 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
730 load_episodes (e, cfg);
731 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
733 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
735 GNUNET_CONFIGURATION_destroy (cfg);
740 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
742 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
744 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
745 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
747 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
749 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
750 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
759 struct GNUNET_ATS_TESTING_SolverHandle
762 struct GNUNET_ATS_PluginEnvironment env;
766 enum GNUNET_ATS_Solvers
768 GNUNET_ATS_SOLVER_PROPORTIONAL,
769 GNUNET_ATS_SOLVER_MLP,
770 GNUNET_ATS_SOLVER_RIL,
774 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
776 GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
777 GNUNET_free (sh->plugin);
781 struct GNUNET_ATS_TESTING_SolverHandle *
782 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
784 struct GNUNET_ATS_TESTING_SolverHandle *sh;
788 case GNUNET_ATS_SOLVER_PROPORTIONAL:
789 solver_str = "proportional";
791 case GNUNET_ATS_SOLVER_MLP:
794 case GNUNET_ATS_SOLVER_RIL:
803 sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
804 GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
805 //sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
806 if (NULL == sh->solver)
808 fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
815 static struct Experiment *e;
817 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
820 * cmd option -e: experiment file
822 static char *opt_exp_file;
824 static char *opt_solver;
827 * cmd option -l: enable logging
832 * cmd option -p: enable plots
837 * cmd option -v: verbose logs
839 static int opt_verbose;
842 run (void *cls, char * const *args, const char *cfgfile,
843 const struct GNUNET_CONFIGURATION_Handle *cfg)
845 enum GNUNET_ATS_Solvers solver;
847 if (NULL == opt_exp_file)
849 fprintf (stderr, "No experiment given ...\n");
853 if (NULL == opt_solver)
855 fprintf (stderr, "No solver given ...\n");
859 if (0 == strcmp(opt_solver, "mlp"))
861 solver = GNUNET_ATS_SOLVER_MLP;
863 else if (0 == strcmp(opt_solver, "proportional"))
865 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
867 else if (0 == strcmp(opt_solver, "ril"))
869 solver = GNUNET_ATS_SOLVER_RIL;
873 fprintf (stderr, "No solver given ...");
877 /* load experiment */
878 e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
881 fprintf (stderr, "Failed to load experiment ...\n");
886 sh = GNUNET_ATS_solvers_solver_start (solver);
889 fprintf (stderr, "Failed to start solver ...\n");
902 * Main function of the benchmark
904 * @param argc argument count
905 * @param argv argument values
908 main (int argc, char *argv[])
914 opt_plot = GNUNET_NO;
916 static struct GNUNET_GETOPT_CommandLineOption options[] =
918 { 's', "solver", NULL,
919 gettext_noop ("solver to use"),
920 1, &GNUNET_GETOPT_set_string, &opt_solver},
921 { 'e', "experiment", NULL,
922 gettext_noop ("experiment to use"),
923 1, &GNUNET_GETOPT_set_string, &opt_exp_file},
924 { 'e', "experiment", NULL,
925 gettext_noop ("experiment to use"),
926 1, &GNUNET_GETOPT_set_one, &opt_verbose},
927 GNUNET_GETOPT_OPTION_END
930 GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
934 /* end of file ats-testing-experiment.c*/