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"
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);
422 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
424 struct Experiment *e = cls;
425 e->experiment_timeout_task = NULL;
426 fprintf (stderr, "Experiment timeout!\n");
428 if (NULL != e->episode_timeout_task)
430 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
431 e->episode_timeout_task = NULL;
434 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
439 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
442 struct BenchmarkPeer *peer;
443 struct BenchmarkPartner *partner;
445 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
452 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
459 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
461 if (NULL != partner->tg)
463 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
464 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
468 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
469 op->tg_type, op->base_rate, op->max_rate, op->period,
470 GNUNET_TIME_UNIT_FOREVER_REL);
475 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
478 struct BenchmarkPartner *p;
479 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
486 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
490 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
491 op->src_id, op->dest_id);
492 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
500 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
503 struct BenchmarkPeer *peer;
504 struct BenchmarkPartner *partner;
506 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
513 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
520 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
522 if (NULL != partner->pg)
524 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
525 op->src_id, op->dest_id);
526 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
530 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
531 op->tg_type, op->base_rate, op->max_rate, op->period, op->frequency,
537 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
540 struct BenchmarkPartner *p;
541 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
548 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
552 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
553 op->src_id, op->dest_id);
554 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
560 static void enforce_episode (struct Episode *ep)
562 struct GNUNET_ATS_TEST_Operation *cur;
563 for (cur = ep->head; NULL != cur; cur = cur->next)
566 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
567 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
570 enforce_start_send (cur);
573 enforce_stop_send (cur);
575 case START_PREFERENCE:
576 enforce_start_preference (cur);
578 case STOP_PREFERENCE:
579 enforce_stop_preference (cur);
588 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
590 struct Experiment *e = cls;
591 e->episode_timeout_task = NULL;
592 if (NULL != e->ep_done_cb)
593 e->ep_done_cb (e->cur);
595 /* Scheduling next */
596 e->cur = e->cur->next;
600 fprintf (stderr, "Last episode done!\n");
601 if (NULL != e->experiment_timeout_task)
603 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
604 e->experiment_timeout_task = NULL;
606 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
610 fprintf (stderr, "Running episode %u with timeout %s\n",
612 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
613 enforce_episode(e->cur);
615 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
616 &timeout_episode, e);
621 GNUNET_ATS_solvers_experimentation_run (struct Experiment *e,
622 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
623 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
625 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
626 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
627 e->e_done_cb = e_done_cb;
628 e->ep_done_cb = ep_done_cb;
629 e->start_time = GNUNET_TIME_absolute_get();
631 /* Start total time out */
632 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
633 &timeout_experiment, e);
637 fprintf (stderr, "Running episode %u with timeout %s\n",
639 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
640 enforce_episode(e->cur);
641 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
642 &timeout_episode, e);
649 GNUNET_ATS_solvers_experimentation_load (char *filename)
651 struct Experiment *e;
652 struct GNUNET_CONFIGURATION_Handle *cfg;
655 cfg = GNUNET_CONFIGURATION_create();
656 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
658 fprintf (stderr, "Failed to load `%s'\n", filename);
659 GNUNET_CONFIGURATION_destroy (cfg);
663 e = create_experiment ();
665 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
668 fprintf (stderr, "Invalid %s", "name");
673 fprintf (stderr, "Experiment name: `%s'\n", e->name);
675 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
676 "cfg_file", &e->cfg_file))
678 fprintf (stderr, "Invalid %s", "cfg_file");
683 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
685 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
686 "masters", &e->num_masters))
688 fprintf (stderr, "Invalid %s", "masters");
693 fprintf (stderr, "Experiment masters: `%llu'\n",
696 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
697 "slaves", &e->num_slaves))
699 fprintf (stderr, "Invalid %s", "slaves");
704 fprintf (stderr, "Experiment slaves: `%llu'\n",
707 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
708 "log_freq", &e->log_freq))
710 fprintf (stderr, "Invalid %s", "log_freq");
715 fprintf (stderr, "Experiment logging frequency: `%s'\n",
716 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
718 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
719 "max_duration", &e->max_duration))
721 fprintf (stderr, "Invalid %s", "max_duration");
726 fprintf (stderr, "Experiment duration: `%s'\n",
727 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
729 load_episodes (e, cfg);
730 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
732 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
734 GNUNET_CONFIGURATION_destroy (cfg);
739 GNUNET_ATS_solvers_experimentation_stop (struct Experiment *e)
741 if (NULL != e->experiment_timeout_task)
743 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
744 e->experiment_timeout_task = NULL;
746 if (NULL != e->episode_timeout_task)
748 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
749 e->episode_timeout_task = NULL;
758 struct GNUNET_ATS_TESTING_SolverHandle
761 struct GNUNET_ATS_PluginEnvironment env;
765 enum GNUNET_ATS_Solvers
767 GNUNET_ATS_SOLVER_PROPORTIONAL,
768 GNUNET_ATS_SOLVER_MLP,
769 GNUNET_ATS_SOLVER_RIL,
773 GNUNET_ATS_solvers_solver_stop (struct GNUNET_ATS_TESTING_SolverHandle *sh)
775 GNUNET_PLUGIN_unload (sh->plugin, sh->solver);
776 GNUNET_free (sh->plugin);
780 struct GNUNET_ATS_TESTING_SolverHandle *
781 GNUNET_ATS_solvers_solver_start (enum GNUNET_ATS_Solvers type)
783 struct GNUNET_ATS_TESTING_SolverHandle *sh;
787 case GNUNET_ATS_SOLVER_PROPORTIONAL:
788 solver_str = "proportional";
790 case GNUNET_ATS_SOLVER_MLP:
793 case GNUNET_ATS_SOLVER_RIL:
802 sh = GNUNET_new (struct GNUNET_ATS_TESTING_SolverHandle);
803 GNUNET_asprintf (&sh->plugin, "libgnunet_plugin_ats_%s", solver_str);
804 //sh->solver = GNUNET_PLUGIN_load (sh->plugin, &sh->env);
805 if (NULL == sh->solver)
807 fprintf (stderr, "Failed to load solver `%s'\n", sh->plugin);
814 static struct Experiment *e;
816 static struct GNUNET_ATS_TESTING_SolverHandle *sh;
819 * cmd option -e: experiment file
821 static char *opt_exp_file;
823 static char *opt_solver;
826 * cmd option -l: enable logging
831 * cmd option -p: enable plots
836 * cmd option -v: verbose logs
838 static int opt_verbose;
841 run (void *cls, char * const *args, const char *cfgfile,
842 const struct GNUNET_CONFIGURATION_Handle *cfg)
844 enum GNUNET_ATS_Solvers solver;
846 if (NULL == opt_exp_file)
848 fprintf (stderr, "No experiment given ...\n");
852 if (NULL == opt_solver)
854 fprintf (stderr, "No solver given ...\n");
858 if (0 == strcmp(opt_solver, "mlp"))
860 solver = GNUNET_ATS_SOLVER_MLP;
862 else if (0 == strcmp(opt_solver, "proportional"))
864 solver = GNUNET_ATS_SOLVER_PROPORTIONAL;
866 else if (0 == strcmp(opt_solver, "ril"))
868 solver = GNUNET_ATS_SOLVER_RIL;
872 fprintf (stderr, "No solver given ...");
876 /* load experiment */
877 e = GNUNET_ATS_solvers_experimentation_load (opt_exp_file);
880 fprintf (stderr, "Failed to load experiment ...\n");
885 sh = GNUNET_ATS_solvers_solver_start (solver);
888 fprintf (stderr, "Failed to start solver ...\n");
901 * Main function of the benchmark
903 * @param argc argument count
904 * @param argv argument values
907 main (int argc, char *argv[])
913 opt_plot = GNUNET_NO;
915 static struct GNUNET_GETOPT_CommandLineOption options[] =
917 { 's', "solver", NULL,
918 gettext_noop ("solver to use"),
919 1, &GNUNET_GETOPT_set_string, &opt_solver},
920 { 'e', "experiment", NULL,
921 gettext_noop ("experiment to use"),
922 1, &GNUNET_GETOPT_set_string, &opt_exp_file},
923 { 'e', "experiment", NULL,
924 gettext_noop ("experiment to use"),
925 1, &GNUNET_GETOPT_set_one, &opt_verbose},
926 GNUNET_GETOPT_OPTION_END
929 GNUNET_PROGRAM_run (argc, argv, argv[0], NULL, options, &run, argv[0]);
933 /* end of file ats-testing-experiment.c*/