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,
124 sec_name, op_name, &op))
126 GNUNET_free(op_name);
129 o = GNUNET_new(struct GNUNET_ATS_TEST_Operation);
130 /* operations = set_rate, start_send, stop_send, set_preference */
131 if (0 == strcmp(op, "start_send"))
133 o->type = START_SEND;
135 else if (0 == strcmp(op, "stop_send"))
139 else if (0 == strcmp(op, "start_preference"))
141 o->type = START_PREFERENCE;
143 else if (0 == strcmp(op, "stop_preference"))
145 o->type = STOP_PREFERENCE;
149 fprintf(stderr, "Invalid operation %u `%s' in episode %u\n",
150 op_counter, op, cur->id);
152 GNUNET_free(op_name);
153 GNUNET_free(sec_name);
155 return GNUNET_SYSERR;
157 GNUNET_free(op_name);
160 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
161 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg,
162 sec_name, op_name, &o->src_id))
164 fprintf(stderr, "Missing src in operation %u `%s' in episode %u\n",
165 op_counter, op, cur->id);
167 GNUNET_free(op_name);
168 GNUNET_free(sec_name);
170 return GNUNET_SYSERR;
172 if (o->src_id > (e->num_masters - 1))
174 fprintf(stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
175 o->src_id, op_counter, op, cur->id);
177 GNUNET_free(op_name);
178 GNUNET_free(sec_name);
180 return GNUNET_SYSERR;
182 GNUNET_free(op_name);
184 /* Get destination */
185 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
186 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg,
187 sec_name, op_name, &o->dest_id))
189 fprintf(stderr, "Missing src in operation %u `%s' in episode %u\n",
190 op_counter, op, cur->id);
192 GNUNET_free(op_name);
193 GNUNET_free(sec_name);
195 return GNUNET_SYSERR;
197 if (o->dest_id > (e->num_slaves - 1))
200 "Invalid destination %llu in operation %u `%s' in episode %u\n",
206 GNUNET_free(op_name);
207 GNUNET_free(sec_name);
209 return GNUNET_SYSERR;
211 GNUNET_free(op_name);
213 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
214 if ((GNUNET_SYSERR !=
215 GNUNET_CONFIGURATION_get_value_string(cfg,
219 (STOP_SEND != o->type) &&
220 (STOP_PREFERENCE != o->type))
222 /* Load arguments for set_rate, start_send, set_preference */
223 if (0 == strcmp(type, "constant"))
225 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
227 else if (0 == strcmp(type, "linear"))
229 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
231 else if (0 == strcmp(type, "sinus"))
233 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
235 else if (0 == strcmp(type, "random"))
237 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
241 fprintf(stderr, "Invalid type %u `%s' in episode %u\n",
242 op_counter, op, cur->id);
245 GNUNET_free(op_name);
246 GNUNET_free(sec_name);
248 return GNUNET_SYSERR;
250 GNUNET_free(op_name);
253 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
254 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg,
255 sec_name, op_name, &o->base_rate))
257 fprintf(stderr, "Missing base rate in operation %u `%s' in episode %u\n",
258 op_counter, op, cur->id);
261 GNUNET_free(op_name);
262 GNUNET_free(sec_name);
264 return GNUNET_SYSERR;
266 GNUNET_free(op_name);
269 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
271 GNUNET_CONFIGURATION_get_value_number(cfg,
276 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
277 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
278 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
280 fprintf(stderr, "Missing max rate in operation %u `%s' in episode %u\n",
281 op_counter, op, cur->id);
283 GNUNET_free(op_name);
285 GNUNET_free(sec_name);
287 return GNUNET_SYSERR;
290 GNUNET_free(op_name);
293 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
294 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
295 sec_name, op_name, &o->period))
297 o->period = cur->duration;
299 GNUNET_free(op_name);
301 if (START_PREFERENCE == o->type)
304 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
305 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
306 sec_name, op_name, &o->frequency))
308 fprintf(stderr, "Missing frequency in operation %u `%s' in episode %u\n",
309 op_counter, op, cur->id);
311 GNUNET_free(op_name);
313 GNUNET_free(sec_name);
315 return GNUNET_SYSERR;
317 GNUNET_free(op_name);
320 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
321 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
322 sec_name, op_name, &pref))
324 fprintf(stderr, "Missing preference in operation %u `%s' in episode %u\n",
325 op_counter, op, cur->id);
327 GNUNET_free(op_name);
329 GNUNET_free(sec_name);
330 GNUNET_free_non_null(pref);
332 return GNUNET_SYSERR;
335 if (0 == strcmp(pref, "bandwidth"))
336 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
337 else if (0 == strcmp(pref, "latency"))
338 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
342 "Invalid preference in operation %u `%s' in episode %u\n",
347 GNUNET_free(op_name);
349 GNUNET_free(sec_name);
350 GNUNET_free_non_null(pref);
352 return GNUNET_SYSERR;
355 GNUNET_free(op_name);
360 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
361 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
363 if ((o->max_rate - o->base_rate) > o->base_rate)
365 /* This will cause an underflow */
368 fprintf(stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
371 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
372 fprintf(stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
373 op_counter, cur->id, print_op(o->type), o->src_id,
374 o->dest_id, (NULL != type) ? type : "",
375 o->base_rate, o->max_rate,
376 GNUNET_STRINGS_relative_time_to_string(o->period, GNUNET_YES));
378 fprintf(stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
379 op_counter, cur->id, print_op(o->type), o->src_id, o->dest_id);
381 GNUNET_free_non_null(type);
384 GNUNET_CONTAINER_DLL_insert(cur->head, cur->tail, o);
387 GNUNET_free(sec_name);
393 load_episodes(struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
397 struct GNUNET_TIME_Relative e_duration;
399 struct Episode *last;
405 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
406 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
407 sec_name, "duration", &e_duration))
409 GNUNET_free(sec_name);
413 cur = GNUNET_new(struct Episode);
414 cur->duration = e_duration;
417 if (GNUNET_OK != load_episode(e, cur, cfg))
419 GNUNET_free(sec_name);
421 return GNUNET_SYSERR;
424 fprintf(stderr, "Found episode %u with duration %s \n",
426 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
428 /* Update experiment */
430 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
431 /* Put in linked list */
437 GNUNET_free(sec_name);
446 timeout_experiment(void *cls)
448 struct Experiment *e = cls;
450 e->experiment_timeout_task = NULL;
451 fprintf(stderr, "Experiment timeout!\n");
453 if (NULL != e->episode_timeout_task)
455 GNUNET_SCHEDULER_cancel(e->episode_timeout_task);
456 e->episode_timeout_task = NULL;
459 e->e_done_cb(e, GNUNET_TIME_absolute_get_duration(e->start_time),
465 enforce_start_send(struct GNUNET_ATS_TEST_Operation *op)
468 struct BenchmarkPeer *peer;
469 struct BenchmarkPartner *partner;
471 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
478 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
485 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
487 if (NULL != partner->tg)
489 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
490 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
494 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
495 op->tg_type, op->base_rate, op->max_rate, op->period,
496 GNUNET_TIME_UNIT_FOREVER_REL);
501 enforce_stop_send(struct GNUNET_ATS_TEST_Operation *op)
504 struct BenchmarkPartner *p;
505 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
512 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
516 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
517 op->src_id, op->dest_id);
518 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
526 enforce_start_preference(struct GNUNET_ATS_TEST_Operation *op)
529 struct BenchmarkPeer *peer;
530 struct BenchmarkPartner *partner;
532 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
539 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
546 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
548 if (NULL != partner->pg)
550 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
551 op->src_id, op->dest_id);
552 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
556 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
557 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
563 enforce_stop_preference(struct GNUNET_ATS_TEST_Operation *op)
566 struct BenchmarkPartner *p;
567 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
574 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
578 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
579 op->src_id, op->dest_id);
580 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
586 static void enforce_episode(struct Episode *ep)
588 struct GNUNET_ATS_TEST_Operation *cur;
590 for (cur = ep->head; NULL != cur; cur = cur->next)
592 fprintf(stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
593 print_op(cur->type), cur->src_id, cur->dest_id, cur->base_rate);
597 enforce_start_send(cur);
601 enforce_stop_send(cur);
604 case START_PREFERENCE:
605 enforce_start_preference(cur);
608 case STOP_PREFERENCE:
609 enforce_stop_preference(cur);
620 timeout_episode(void *cls)
622 struct Experiment *e = cls;
624 e->episode_timeout_task = NULL;
625 if (NULL != e->ep_done_cb)
626 e->ep_done_cb(e->cur);
628 /* Scheduling next */
629 e->cur = e->cur->next;
633 fprintf(stderr, "Last episode done!\n");
634 if (NULL != e->experiment_timeout_task)
636 GNUNET_SCHEDULER_cancel(e->experiment_timeout_task);
637 e->experiment_timeout_task = NULL;
639 e->e_done_cb(e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
643 fprintf(stderr, "Running episode %u with timeout %s\n",
645 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
646 enforce_episode(e->cur);
648 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed(e->cur->duration,
649 &timeout_episode, e);
654 GNUNET_ATS_solvers_experimentation_run(struct Experiment *e,
655 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
656 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
658 fprintf(stderr, "Running experiment `%s' with timeout %s\n", e->name,
659 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
660 e->e_done_cb = e_done_cb;
661 e->ep_done_cb = ep_done_cb;
662 e->start_time = GNUNET_TIME_absolute_get();
664 /* Start total time out */
665 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed(e->max_duration,
666 &timeout_experiment, e);
670 fprintf(stderr, "Running episode %u with timeout %s\n",
672 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
673 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_load(char *filename)
682 struct Experiment *e;
683 struct GNUNET_CONFIGURATION_Handle *cfg;
687 cfg = GNUNET_CONFIGURATION_create();
688 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load(cfg, filename))
690 fprintf(stderr, "Failed to load `%s'\n", filename);
691 GNUNET_CONFIGURATION_destroy(cfg);
695 e = create_experiment();
697 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
700 fprintf(stderr, "Invalid %s", "name");
705 fprintf(stderr, "Experiment name: `%s'\n", e->name);
707 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename(cfg, "experiment",
708 "cfg_file", &e->cfg_file))
710 fprintf(stderr, "Invalid %s", "cfg_file");
715 fprintf(stderr, "Experiment name: `%s'\n", e->cfg_file);
717 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
718 "masters", &e->num_masters))
720 fprintf(stderr, "Invalid %s", "masters");
725 fprintf(stderr, "Experiment masters: `%llu'\n",
728 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
729 "slaves", &e->num_slaves))
731 fprintf(stderr, "Invalid %s", "slaves");
736 fprintf(stderr, "Experiment slaves: `%llu'\n",
739 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
740 "log_freq", &e->log_freq))
742 fprintf(stderr, "Invalid %s", "log_freq");
747 fprintf(stderr, "Experiment logging frequency: `%s'\n",
748 GNUNET_STRINGS_relative_time_to_string(e->log_freq, GNUNET_YES));
750 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
751 "max_duration", &e->max_duration))
753 fprintf(stderr, "Invalid %s", "max_duration");
758 fprintf(stderr, "Experiment duration: `%s'\n",
759 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
761 load_episodes(e, cfg);
762 fprintf(stderr, "Loaded %u episodes with total duration %s\n",
764 GNUNET_STRINGS_relative_time_to_string(e->total_duration, GNUNET_YES));
766 GNUNET_CONFIGURATION_destroy(cfg);
771 GNUNET_ATS_solvers_experimentation_stop(struct Experiment *e)
773 if (NULL != e->experiment_timeout_task)
775 GNUNET_SCHEDULER_cancel(e->experiment_timeout_task);
776 e->experiment_timeout_task = NULL;
778 if (NULL != e->episode_timeout_task)
780 GNUNET_SCHEDULER_cancel(e->episode_timeout_task);
781 e->episode_timeout_task = NULL;
790 struct GNUNET_ATS_TESTING_SolverHandle {
792 struct GNUNET_ATS_PluginEnvironment env;
796 enum GNUNET_ATS_Solvers {
797 GNUNET_ATS_SOLVER_PROPORTIONAL,
798 GNUNET_ATS_SOLVER_MLP,
799 GNUNET_ATS_SOLVER_RIL,
803 GNUNET_ATS_solvers_solver_stop(struct GNUNET_ATS_TESTING_SolverHandle *sh)
805 GNUNET_PLUGIN_unload(sh->plugin, sh->solver);
806 GNUNET_free(sh->plugin);
810 struct GNUNET_ATS_TESTING_SolverHandle *
811 GNUNET_ATS_solvers_solver_start(enum GNUNET_ATS_Solvers type)
813 struct GNUNET_ATS_TESTING_SolverHandle *sh;
818 case GNUNET_ATS_SOLVER_PROPORTIONAL:
819 solver_str = "proportional";
822 case GNUNET_ATS_SOLVER_MLP:
826 case GNUNET_ATS_SOLVER_RIL:
836 sh = GNUNET_new(struct GNUNET_ATS_TESTING_SolverHandle);
837 GNUNET_asprintf(&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
838 //sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
839 if (NULL == sh->solver)
841 fprintf(stderr, "Failed to load solver `%s'\n", sh->plugin);
848 static struct Experiment *e;
850 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
853 * cmd option -e: experiment file
855 static char *opt_exp_file;
857 static char *opt_solver;
860 * cmd option -l: enable logging
865 * cmd option -p: enable plots
870 * cmd option -v: verbose logs
872 static int opt_verbose;
875 run(void *cls, char * const *args, const char *cfgfile,
876 const struct GNUNET_CONFIGURATION_Handle *cfg)
878 enum GNUNET_ATS_Solvers solver;
880 if (NULL == opt_exp_file)
882 fprintf(stderr, "No experiment given ...\n");
886 if (NULL == opt_solver)
888 fprintf(stderr, "No solver given ...\n");
892 if (0 == strcmp(opt_solver, "mlp"))
894 solver = GNUNET_ATS_SOLVER_MLP;
896 else if (0 == strcmp(opt_solver, "proportional"))
898 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
900 else if (0 == strcmp(opt_solver, "ril"))
902 solver = GNUNET_ATS_SOLVER_RIL;
906 fprintf(stderr, "No solver given ...");
910 /* load experiment */
911 e = GNUNET_ATS_solvers_experimentation_load(opt_exp_file);
914 fprintf(stderr, "Failed to load experiment ...\n");
919 sh = GNUNET_ATS_solvers_solver_start(solver);
922 fprintf(stderr, "Failed to start solver ...\n");
935 * Main function of the benchmark
937 * @param argc argument count
938 * @param argv argument values
941 main(int argc, char *argv[])
946 opt_plot = GNUNET_NO;
948 struct GNUNET_GETOPT_CommandLineOption options[] =
950 GNUNET_GETOPT_option_string('s',
953 gettext_noop("solver to use"),
956 GNUNET_GETOPT_option_string('e',
959 gettext_noop("experiment to use"),
962 GNUNET_GETOPT_option_flag('e',
964 gettext_noop("experiment to use"),
966 GNUNET_GETOPT_OPTION_END
970 GNUNET_PROGRAM_run(argc,
979 /* end of file gnunet-solver-eval.c*/