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;
69 free_experiment (struct Experiment *e)
73 struct GNUNET_ATS_TEST_Operation *cur_o;
74 struct GNUNET_ATS_TEST_Operation *next_o;
77 for (cur = next; NULL != cur; cur = next)
82 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
90 GNUNET_free_non_null (e->name);
91 GNUNET_free_non_null (e->cfg_file);
96 load_episode (struct Experiment *e, struct Episode *cur,
97 struct GNUNET_CONFIGURATION_Handle *cfg)
99 struct GNUNET_ATS_TEST_Operation *o;
106 fprintf (stderr, "Parsing episode %u\n",cur->id);
107 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
112 GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
113 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
114 sec_name, op_name, &op))
116 GNUNET_free (op_name);
119 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
120 /* operations = set_rate, start_send, stop_send, set_preference */
121 if (0 == strcmp (op, "start_send"))
123 o->type = START_SEND;
125 else if (0 == strcmp (op, "stop_send"))
129 else if (0 == strcmp (op, "start_preference"))
131 o->type = START_PREFERENCE;
133 else if (0 == strcmp (op, "stop_preference"))
135 o->type = STOP_PREFERENCE;
139 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
140 op_counter, op, cur->id);
142 GNUNET_free (op_name);
143 GNUNET_free (sec_name);
145 return GNUNET_SYSERR;
147 GNUNET_free (op_name);
150 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
151 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
152 sec_name, op_name, &o->src_id))
154 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
155 op_counter, op, cur->id);
157 GNUNET_free (op_name);
158 GNUNET_free (sec_name);
160 return GNUNET_SYSERR;
162 if (o->src_id > (e->num_masters - 1))
164 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
165 o->src_id, op_counter, op, cur->id);
167 GNUNET_free (op_name);
168 GNUNET_free (sec_name);
170 return GNUNET_SYSERR;
172 GNUNET_free (op_name);
174 /* Get destination */
175 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
176 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
177 sec_name, op_name, &o->dest_id))
179 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
180 op_counter, op, cur->id);
182 GNUNET_free (op_name);
183 GNUNET_free (sec_name);
185 return GNUNET_SYSERR;
187 if (o->dest_id > (e->num_slaves - 1))
189 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
190 o->dest_id, op_counter, op, cur->id);
192 GNUNET_free (op_name);
193 GNUNET_free (sec_name);
195 return GNUNET_SYSERR;
197 GNUNET_free (op_name);
199 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
200 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
201 sec_name, op_name, &type)) &&
202 ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
204 /* Load arguments for set_rate, start_send, set_preference */
205 if (0 == strcmp (type, "constant"))
207 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
209 else if (0 == strcmp (type, "linear"))
211 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
213 else if (0 == strcmp (type, "sinus"))
215 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
217 else if (0 == strcmp (type, "random"))
219 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
223 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
224 op_counter, op, cur->id);
227 GNUNET_free (op_name);
228 GNUNET_free (sec_name);
230 return GNUNET_SYSERR;
232 GNUNET_free (op_name);
235 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
236 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
237 sec_name, op_name, &o->base_rate))
239 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
240 op_counter, op, cur->id);
243 GNUNET_free (op_name);
244 GNUNET_free (sec_name);
246 return GNUNET_SYSERR;
248 GNUNET_free (op_name);
251 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
252 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
253 sec_name, op_name, &o->max_rate))
255 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
256 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
257 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
259 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
260 op_counter, op, cur->id);
262 GNUNET_free (op_name);
264 GNUNET_free (sec_name);
266 return GNUNET_SYSERR;
269 GNUNET_free (op_name);
272 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
273 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
274 sec_name, op_name, &o->period))
276 o->period = cur->duration;
278 GNUNET_free (op_name);
280 if (START_PREFERENCE == o->type)
283 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
284 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
285 sec_name, op_name, &o->frequency))
287 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
288 op_counter, op, cur->id);
290 GNUNET_free (op_name);
292 GNUNET_free (sec_name);
294 return GNUNET_SYSERR;
296 GNUNET_free (op_name);
299 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
300 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
301 sec_name, op_name, &pref))
303 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
304 op_counter, op, cur->id);
306 GNUNET_free (op_name);
308 GNUNET_free (sec_name);
309 GNUNET_free_non_null (pref);
311 return GNUNET_SYSERR;
314 if (0 == strcmp(pref, "bandwidth"))
315 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
316 else if (0 == strcmp(pref, "latency"))
317 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
320 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
321 op_counter, op, cur->id);
323 GNUNET_free (op_name);
326 GNUNET_free (sec_name);
327 GNUNET_free_non_null (pref);
329 return GNUNET_SYSERR;
332 GNUNET_free (op_name);
337 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
338 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
340 if ((o->max_rate - o->base_rate) > o->base_rate)
342 /* This will cause an underflow */
345 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
348 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
349 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
350 op_counter, cur->id, print_op (o->type), o->src_id,
351 o->dest_id, (NULL != type) ? type : "",
352 o->base_rate, o->max_rate,
353 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
355 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
356 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
358 GNUNET_free_non_null (type);
361 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
364 GNUNET_free (sec_name);
370 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
374 struct GNUNET_TIME_Relative e_duration;
376 struct Episode *last;
382 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
383 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
384 sec_name, "duration", &e_duration))
386 GNUNET_free (sec_name);
390 cur = GNUNET_new (struct Episode);
391 cur->duration = e_duration;
394 if (GNUNET_OK != load_episode (e, cur, cfg))
396 GNUNET_free (sec_name);
398 return GNUNET_SYSERR;
401 fprintf (stderr, "Found episode %u with duration %s \n",
403 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
405 /* Update experiment */
407 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
408 /* Put in linked list */
414 GNUNET_free (sec_name);
423 timeout_experiment (void *cls)
425 struct Experiment *e = cls;
426 e->experiment_timeout_task = NULL;
427 fprintf (stderr, "Experiment timeout!\n");
429 if (NULL != e->episode_timeout_task)
431 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
432 e->episode_timeout_task = NULL;
435 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
441 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
444 struct BenchmarkPeer *peer;
445 struct BenchmarkPartner *partner;
447 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
454 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
461 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
463 if (NULL != partner->tg)
465 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
466 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
470 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
471 op->tg_type, op->base_rate, op->max_rate, op->period,
472 GNUNET_TIME_UNIT_FOREVER_REL);
477 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
480 struct BenchmarkPartner *p;
481 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
488 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
492 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
493 op->src_id, op->dest_id);
494 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
502 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
505 struct BenchmarkPeer *peer;
506 struct BenchmarkPartner *partner;
508 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
515 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
522 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
524 if (NULL != partner->pg)
526 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
527 op->src_id, op->dest_id);
528 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
532 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
533 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
539 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
542 struct BenchmarkPartner *p;
543 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
550 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
554 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
555 op->src_id, op->dest_id);
556 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
562 static void enforce_episode (struct Episode *ep)
564 struct GNUNET_ATS_TEST_Operation *cur;
565 for (cur = ep->head; NULL != cur; cur = cur->next)
568 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
569 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
572 enforce_start_send (cur);
575 enforce_stop_send (cur);
577 case START_PREFERENCE:
578 enforce_start_preference (cur);
580 case STOP_PREFERENCE:
581 enforce_stop_preference (cur);
591 timeout_episode (void *cls)
593 struct Experiment *e = cls;
594 e->episode_timeout_task = NULL;
595 if (NULL != e->ep_done_cb)
596 e->ep_done_cb (e->cur);
598 /* Scheduling next */
599 e->cur = e->cur->next;
603 fprintf (stderr, "Last episode done!\n");
604 if (NULL != e->experiment_timeout_task)
606 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
607 e->experiment_timeout_task = NULL;
609 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
613 fprintf (stderr, "Running episode %u with timeout %s\n",
615 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
616 enforce_episode(e->cur);
618 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
619 &timeout_episode, e);
624 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
625 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
626 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
628 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
629 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
630 e->e_done_cb = e_done_cb;
631 e->ep_done_cb = ep_done_cb;
632 e->start_time = GNUNET_TIME_absolute_get();
634 /* Start total time out */
635 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
636 &timeout_experiment, e);
640 fprintf (stderr, "Running episode %u with timeout %s\n",
642 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
643 enforce_episode(e->cur);
644 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
645 &timeout_episode, e);
652 GNUNET_ATS_solvers_experimentation_load (char *filename)
654 struct Experiment *e;
655 struct GNUNET_CONFIGURATION_Handle *cfg;
658 cfg = GNUNET_CONFIGURATION_create();
659 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
661 fprintf (stderr, "Failed to load `%s'\n", filename);
662 GNUNET_CONFIGURATION_destroy (cfg);
666 e = create_experiment ();
668 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
671 fprintf (stderr, "Invalid %s", "name");
676 fprintf (stderr, "Experiment name: `%s'\n", e->name);
678 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
679 "cfg_file", &e->cfg_file))
681 fprintf (stderr, "Invalid %s", "cfg_file");
686 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
688 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
689 "masters", &e->num_masters))
691 fprintf (stderr, "Invalid %s", "masters");
696 fprintf (stderr, "Experiment masters: `%llu'\n",
699 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
700 "slaves", &e->num_slaves))
702 fprintf (stderr, "Invalid %s", "slaves");
707 fprintf (stderr, "Experiment slaves: `%llu'\n",
710 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
711 "log_freq", &e->log_freq))
713 fprintf (stderr, "Invalid %s", "log_freq");
718 fprintf (stderr, "Experiment logging frequency: `%s'\n",
719 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
721 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
722 "max_duration", &e->max_duration))
724 fprintf (stderr, "Invalid %s", "max_duration");
729 fprintf (stderr, "Experiment duration: `%s'\n",
730 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
732 load_episodes (e, cfg);
733 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
735 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
737 GNUNET_CONFIGURATION_destroy (cfg);
742 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
744 if (NULL != e->experiment_timeout_task)
746 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
747 e->experiment_timeout_task = NULL;
749 if (NULL != e->episode_timeout_task)
751 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
752 e->episode_timeout_task = NULL;
761 struct GNUNET_ATS_TESTING_SolverHandle
764 struct GNUNET_ATS_PluginEnvironment env;
768 enum GNUNET_ATS_Solvers
770 GNUNET_ATS_SOLVER_PROPORTIONAL,
771 GNUNET_ATS_SOLVER_MLP,
772 GNUNET_ATS_SOLVER_RIL,
776 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
778 GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
779 GNUNET_free (sh->plugin);
783 struct GNUNET_ATS_TESTING_SolverHandle *
784 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
786 struct GNUNET_ATS_TESTING_SolverHandle *sh;
790 case GNUNET_ATS_SOLVER_PROPORTIONAL:
791 solver_str = "proportional";
793 case GNUNET_ATS_SOLVER_MLP:
796 case GNUNET_ATS_SOLVER_RIL:
805 sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
806 GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
807 //sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
808 if (NULL == sh->solver)
810 fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
817 static struct Experiment *e;
819 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
822 * cmd option -e: experiment file
824 static char *opt_exp_file;
826 static char *opt_solver;
829 * cmd option -l: enable logging
834 * cmd option -p: enable plots
839 * cmd option -v: verbose logs
841 static int opt_verbose;
844 run (void *cls, char * const *args, const char *cfgfile,
845 const struct GNUNET_CONFIGURATION_Handle *cfg)
847 enum GNUNET_ATS_Solvers solver;
849 if (NULL == opt_exp_file)
851 fprintf (stderr, "No experiment given ...\n");
855 if (NULL == opt_solver)
857 fprintf (stderr, "No solver given ...\n");
861 if (0 == strcmp(opt_solver, "mlp"))
863 solver = GNUNET_ATS_SOLVER_MLP;
865 else if (0 == strcmp(opt_solver, "proportional"))
867 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
869 else if (0 == strcmp(opt_solver, "ril"))
871 solver = GNUNET_ATS_SOLVER_RIL;
875 fprintf (stderr, "No solver given ...");
879 /* load experiment */
880 e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
883 fprintf (stderr, "Failed to load experiment ...\n");
888 sh = GNUNET_ATS_solvers_solver_start (solver);
891 fprintf (stderr, "Failed to start solver ...\n");
904 * Main function of the benchmark
906 * @param argc argument count
907 * @param argv argument values
910 main (int argc, char *argv[])
916 opt_plot = GNUNET_NO;
918 static struct GNUNET_GETOPT_CommandLineOption options[] =
920 { 's', "solver", NULL,
921 gettext_noop ("solver to use"),
922 1, &GNUNET_GETOPT_set_string, &opt_solver},
923 { 'e', "experiment", NULL,
924 gettext_noop ("experiment to use"),
925 1, &GNUNET_GETOPT_set_string, &opt_exp_file},
926 { 'e', "experiment", NULL,
927 gettext_noop ("experiment to use"),
928 1, &GNUNET_GETOPT_set_one, &opt_verbose},
929 GNUNET_GETOPT_OPTION_END
932 GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
936 /* end of file ats-testing-experiment.c*/