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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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)
47 case START_PREFERENCE:
48 return "START_PREFERENCE";
51 return "STOP_PREFERENCE";
60 static struct Experiment *
65 e = GNUNET_new (struct Experiment);
70 e->total_duration = GNUNET_TIME_UNIT_ZERO;
76 free_experiment (struct Experiment *e)
80 struct GNUNET_ATS_TEST_Operation *cur_o;
81 struct GNUNET_ATS_TEST_Operation *next_o;
84 for (cur = next; NULL != cur; cur = next)
89 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
97 GNUNET_free_non_null (e->name);
98 GNUNET_free_non_null (e->cfg_file);
104 load_episode (struct Experiment *e,
106 struct GNUNET_CONFIGURATION_Handle *cfg)
108 struct GNUNET_ATS_TEST_Operation *o;
116 fprintf (stderr, "Parsing episode %u\n", cur->id);
117 GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
122 GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
123 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
127 GNUNET_free (op_name);
130 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
131 /* operations = set_rate, start_send, stop_send, set_preference */
132 if (0 == strcmp (op, "start_send"))
134 o->type = START_SEND;
136 else if (0 == strcmp (op, "stop_send"))
140 else if (0 == strcmp (op, "start_preference"))
142 o->type = START_PREFERENCE;
144 else if (0 == strcmp (op, "stop_preference"))
146 o->type = STOP_PREFERENCE;
150 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
151 op_counter, op, cur->id);
153 GNUNET_free (op_name);
154 GNUNET_free (sec_name);
156 return GNUNET_SYSERR;
158 GNUNET_free (op_name);
161 GNUNET_asprintf (&op_name, "op-%u-src", op_counter);
162 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
167 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
168 op_counter, op, cur->id);
170 GNUNET_free (op_name);
171 GNUNET_free (sec_name);
173 return GNUNET_SYSERR;
175 if (o->src_id > (e->num_masters - 1))
177 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
178 o->src_id, op_counter, op, cur->id);
180 GNUNET_free (op_name);
181 GNUNET_free (sec_name);
183 return GNUNET_SYSERR;
185 GNUNET_free (op_name);
187 /* Get destination */
188 GNUNET_asprintf (&op_name, "op-%u-dest", op_counter);
189 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
194 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
195 op_counter, op, cur->id);
197 GNUNET_free (op_name);
198 GNUNET_free (sec_name);
200 return GNUNET_SYSERR;
202 if (o->dest_id > (e->num_slaves - 1))
205 "Invalid destination %llu in operation %u `%s' in episode %u\n",
211 GNUNET_free (op_name);
212 GNUNET_free (sec_name);
214 return GNUNET_SYSERR;
216 GNUNET_free (op_name);
218 GNUNET_asprintf (&op_name, "op-%u-type", op_counter);
219 if ((GNUNET_SYSERR !=
220 GNUNET_CONFIGURATION_get_value_string (cfg,
224 (STOP_SEND != o->type) &&
225 (STOP_PREFERENCE != o->type))
227 /* Load arguments for set_rate, start_send, set_preference */
228 if (0 == strcmp (type, "constant"))
230 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
232 else if (0 == strcmp (type, "linear"))
234 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
236 else if (0 == strcmp (type, "sinus"))
238 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
240 else if (0 == strcmp (type, "random"))
242 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
246 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
247 op_counter, op, cur->id);
250 GNUNET_free (op_name);
251 GNUNET_free (sec_name);
253 return GNUNET_SYSERR;
255 GNUNET_free (op_name);
258 GNUNET_asprintf (&op_name, "op-%u-base-rate", op_counter);
259 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
265 "Missing base rate in operation %u `%s' in episode %u\n",
266 op_counter, op, cur->id);
269 GNUNET_free (op_name);
270 GNUNET_free (sec_name);
272 return GNUNET_SYSERR;
274 GNUNET_free (op_name);
277 GNUNET_asprintf (&op_name, "op-%u-max-rate", op_counter);
279 GNUNET_CONFIGURATION_get_value_number (cfg,
284 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
285 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
286 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
289 "Missing max rate in operation %u `%s' in episode %u\n",
290 op_counter, op, cur->id);
292 GNUNET_free (op_name);
294 GNUNET_free (sec_name);
296 return GNUNET_SYSERR;
299 GNUNET_free (op_name);
302 GNUNET_asprintf (&op_name, "op-%u-period", op_counter);
303 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
308 o->period = cur->duration;
310 GNUNET_free (op_name);
312 if (START_PREFERENCE == o->type)
315 GNUNET_asprintf (&op_name, "op-%u-frequency", op_counter);
316 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
322 "Missing frequency in operation %u `%s' in episode %u\n",
323 op_counter, op, cur->id);
325 GNUNET_free (op_name);
327 GNUNET_free (sec_name);
329 return GNUNET_SYSERR;
331 GNUNET_free (op_name);
334 GNUNET_asprintf (&op_name, "op-%u-pref", op_counter);
335 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
341 "Missing preference in operation %u `%s' in episode %u\n",
342 op_counter, op, cur->id);
344 GNUNET_free (op_name);
346 GNUNET_free (sec_name);
347 GNUNET_free_non_null (pref);
349 return GNUNET_SYSERR;
352 if (0 == strcmp (pref, "bandwidth"))
353 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
354 else if (0 == strcmp (pref, "latency"))
355 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
359 "Invalid preference in operation %u `%s' in episode %u\n",
364 GNUNET_free (op_name);
366 GNUNET_free (sec_name);
367 GNUNET_free_non_null (pref);
369 return GNUNET_SYSERR;
372 GNUNET_free (op_name);
377 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
378 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
380 if ((o->max_rate - o->base_rate) > o->base_rate)
382 /* This will cause an underflow */
386 "Selected max rate and base rate cannot be used for desired traffic form!\n");
389 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
391 "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
392 op_counter, cur->id, print_op (o->type), o->src_id,
393 o->dest_id, (NULL != type) ? type : "",
394 o->base_rate, o->max_rate,
395 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
397 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
398 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
400 GNUNET_free_non_null (type);
403 GNUNET_CONTAINER_DLL_insert (cur->head, cur->tail, o);
406 GNUNET_free (sec_name);
413 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
417 struct GNUNET_TIME_Relative e_duration;
419 struct Episode *last;
425 GNUNET_asprintf (&sec_name, "episode-%u", e_counter);
426 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
431 GNUNET_free (sec_name);
435 cur = GNUNET_new (struct Episode);
436 cur->duration = e_duration;
439 if (GNUNET_OK != load_episode (e, cur, cfg))
441 GNUNET_free (sec_name);
443 return GNUNET_SYSERR;
446 fprintf (stderr, "Found episode %u with duration %s \n",
448 GNUNET_STRINGS_relative_time_to_string (cur->duration,
451 /* Update experiment */
453 e->total_duration = GNUNET_TIME_relative_add (e->total_duration,
455 /* Put in linked list */
461 GNUNET_free (sec_name);
470 timeout_experiment (void *cls)
472 struct Experiment *e = cls;
474 e->experiment_timeout_task = NULL;
475 fprintf (stderr, "Experiment timeout!\n");
477 if (NULL != e->episode_timeout_task)
479 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
480 e->episode_timeout_task = NULL;
483 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration (e->start_time),
489 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
492 struct BenchmarkPeer *peer;
493 struct BenchmarkPartner *partner;
495 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
502 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
509 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
511 if (NULL != partner->tg)
513 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
514 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
518 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
519 op->tg_type, op->base_rate, op->max_rate, op->period,
520 GNUNET_TIME_UNIT_FOREVER_REL);
525 enforce_stop_send (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 traffic between master %llu slave %llu\n",
541 op->src_id, op->dest_id);
542 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
549 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
552 struct BenchmarkPeer *peer;
553 struct BenchmarkPartner *partner;
555 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
562 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
569 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
571 if (NULL != partner->pg)
573 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
574 op->src_id, op->dest_id);
575 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
579 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
580 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
586 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
589 struct BenchmarkPartner *p;
590 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
597 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
601 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
602 op->src_id, op->dest_id);
603 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
610 enforce_episode (struct Episode *ep)
612 struct GNUNET_ATS_TEST_Operation *cur;
614 for (cur = ep->head; NULL != cur; cur = cur->next)
616 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
617 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
621 enforce_start_send (cur);
625 enforce_stop_send (cur);
628 case START_PREFERENCE:
629 enforce_start_preference (cur);
632 case STOP_PREFERENCE:
633 enforce_stop_preference (cur);
644 timeout_episode (void *cls)
646 struct Experiment *e = cls;
648 e->episode_timeout_task = NULL;
649 if (NULL != e->ep_done_cb)
650 e->ep_done_cb (e->cur);
652 /* Scheduling next */
653 e->cur = e->cur->next;
657 fprintf (stderr, "Last episode done!\n");
658 if (NULL != e->experiment_timeout_task)
660 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
661 e->experiment_timeout_task = NULL;
663 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration (e->start_time),
668 fprintf (stderr, "Running episode %u with timeout %s\n",
670 GNUNET_STRINGS_relative_time_to_string (e->cur->duration,
672 enforce_episode (e->cur);
674 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
675 &timeout_episode, e);
680 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
681 GNUNET_ATS_TESTING_EpisodeDoneCallback
683 GNUNET_ATS_TESTING_ExperimentDoneCallback
686 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
687 GNUNET_STRINGS_relative_time_to_string (e->max_duration,
689 e->e_done_cb = e_done_cb;
690 e->ep_done_cb = ep_done_cb;
691 e->start_time = GNUNET_TIME_absolute_get ();
693 /* Start total time out */
694 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
700 fprintf (stderr, "Running episode %u with timeout %s\n",
702 GNUNET_STRINGS_relative_time_to_string (e->cur->duration,
704 enforce_episode (e->cur);
705 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
706 &timeout_episode, e);
711 GNUNET_ATS_solvers_experimentation_load (char *filename)
713 struct Experiment *e;
714 struct GNUNET_CONFIGURATION_Handle *cfg;
718 cfg = GNUNET_CONFIGURATION_create ();
719 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
721 fprintf (stderr, "Failed to load `%s'\n", filename);
722 GNUNET_CONFIGURATION_destroy (cfg);
726 e = create_experiment ();
728 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "experiment",
731 fprintf (stderr, "Invalid %s", "name");
736 fprintf (stderr, "Experiment name: `%s'\n", e->name);
738 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg,
743 fprintf (stderr, "Invalid %s", "cfg_file");
748 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
750 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "experiment",
754 fprintf (stderr, "Invalid %s", "masters");
759 fprintf (stderr, "Experiment masters: `%llu'\n",
762 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "experiment",
766 fprintf (stderr, "Invalid %s", "slaves");
771 fprintf (stderr, "Experiment slaves: `%llu'\n",
774 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg, "experiment",
778 fprintf (stderr, "Invalid %s", "log_freq");
783 fprintf (stderr, "Experiment logging frequency: `%s'\n",
784 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
786 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg, "experiment",
790 fprintf (stderr, "Invalid %s", "max_duration");
795 fprintf (stderr, "Experiment duration: `%s'\n",
796 GNUNET_STRINGS_relative_time_to_string (e->max_duration,
799 load_episodes (e, cfg);
800 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
802 GNUNET_STRINGS_relative_time_to_string (e->total_duration,
805 GNUNET_CONFIGURATION_destroy (cfg);
811 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
813 if (NULL != e->experiment_timeout_task)
815 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
816 e->experiment_timeout_task = NULL;
818 if (NULL != e->episode_timeout_task)
820 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
821 e->episode_timeout_task = NULL;
831 struct GNUNET_ATS_TESTING_SolverHandle
834 struct GNUNET_ATS_PluginEnvironment env;
838 enum GNUNET_ATS_Solvers
840 GNUNET_ATS_SOLVER_PROPORTIONAL,
841 GNUNET_ATS_SOLVER_MLP,
842 GNUNET_ATS_SOLVER_RIL,
846 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
848 GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
849 GNUNET_free (sh->plugin);
854 struct GNUNET_ATS_TESTING_SolverHandle *
855 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
857 struct GNUNET_ATS_TESTING_SolverHandle *sh;
862 case GNUNET_ATS_SOLVER_PROPORTIONAL:
863 solver_str = "proportional";
866 case GNUNET_ATS_SOLVER_MLP:
870 case GNUNET_ATS_SOLVER_RIL:
880 sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
881 GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
882 // sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
883 if (NULL == sh->solver)
885 fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
893 static struct Experiment *e;
895 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
898 * cmd option -e: experiment file
900 static char *opt_exp_file;
902 static char *opt_solver;
905 * cmd option -l: enable logging
910 * cmd option -p: enable plots
915 * cmd option -v: verbose logs
917 static int opt_verbose;
920 run (void *cls, char *const *args, const char *cfgfile,
921 const struct GNUNET_CONFIGURATION_Handle *cfg)
923 enum GNUNET_ATS_Solvers solver;
925 if (NULL == opt_exp_file)
927 fprintf (stderr, "No experiment given ...\n");
931 if (NULL == opt_solver)
933 fprintf (stderr, "No solver given ...\n");
937 if (0 == strcmp (opt_solver, "mlp"))
939 solver = GNUNET_ATS_SOLVER_MLP;
941 else if (0 == strcmp (opt_solver, "proportional"))
943 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
945 else if (0 == strcmp (opt_solver, "ril"))
947 solver = GNUNET_ATS_SOLVER_RIL;
951 fprintf (stderr, "No solver given ...");
955 /* load experiment */
956 e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
959 fprintf (stderr, "Failed to load experiment ...\n");
964 sh = GNUNET_ATS_solvers_solver_start (solver);
967 fprintf (stderr, "Failed to start solver ...\n");
980 * Main function of the benchmark
982 * @param argc argument count
983 * @param argv argument values
986 main (int argc, char *argv[])
991 opt_plot = GNUNET_NO;
993 struct GNUNET_GETOPT_CommandLineOption options[] = {
994 GNUNET_GETOPT_option_string ('s',
997 gettext_noop ("solver to use"),
1000 GNUNET_GETOPT_option_string ('e',
1003 gettext_noop ("experiment to use"),
1006 GNUNET_GETOPT_option_flag ('e',
1008 gettext_noop ("experiment to use"),
1010 GNUNET_GETOPT_OPTION_END
1014 GNUNET_PROGRAM_run (argc,
1025 /* end of file gnunet-solver-eval.c*/