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 "ats-testing.h"
31 print_op (enum OperationType op)
41 case START_PREFERENCE:
42 return "START_PREFERENCE";
45 return "STOP_PREFERENCE";
54 static struct Experiment *
59 e = GNUNET_new (struct Experiment);
64 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);
98 load_episode (struct Experiment *e,
100 struct GNUNET_CONFIGURATION_Handle *cfg)
102 struct GNUNET_ATS_TEST_Operation *o;
110 fprintf (stderr, "Parsing episode %u\n", cur->id);
111 GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
116 GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
117 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
121 GNUNET_free (op_name);
124 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
125 /* operations = set_rate, start_send, stop_send, set_preference */
126 if (0 == strcmp (op, "start_send"))
128 o->type = START_SEND;
130 else if (0 == strcmp (op, "stop_send"))
134 else if (0 == strcmp (op, "start_preference"))
136 o->type = START_PREFERENCE;
138 else if (0 == strcmp (op, "stop_preference"))
140 o->type = STOP_PREFERENCE;
144 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
145 op_counter, op, cur->id);
147 GNUNET_free (op_name);
149 GNUNET_free (sec_name);
150 return GNUNET_SYSERR;
152 GNUNET_free (op_name);
155 GNUNET_asprintf (&op_name, "op-%u-src", op_counter);
156 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
161 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
162 op_counter, op, cur->id);
164 GNUNET_free (op_name);
166 GNUNET_free (sec_name);
167 return GNUNET_SYSERR;
169 if (o->src_id > (e->num_masters - 1))
171 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
172 o->src_id, op_counter, op, cur->id);
174 GNUNET_free (op_name);
176 GNUNET_free (sec_name);
177 return GNUNET_SYSERR;
179 GNUNET_free (op_name);
181 /* Get destination */
182 GNUNET_asprintf (&op_name, "op-%u-dest", op_counter);
183 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
188 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
189 op_counter, op, cur->id);
191 GNUNET_free (op_name);
193 GNUNET_free (sec_name);
194 return GNUNET_SYSERR;
196 if (o->dest_id > (e->num_slaves - 1))
199 "Invalid destination %llu in operation %u `%s' in episode %u\n",
200 o->dest_id, op_counter, op, cur->id);
202 GNUNET_free (op_name);
204 GNUNET_free (sec_name);
205 return GNUNET_SYSERR;
207 GNUNET_free (op_name);
209 GNUNET_asprintf (&op_name, "op-%u-type", op_counter);
210 if ((GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string (cfg,
214 (STOP_SEND != o->type) &&
215 (STOP_PREFERENCE != o->type))
217 /* Load arguments for set_rate, start_send, set_preference */
218 if (0 == strcmp (type, "constant"))
220 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
222 else if (0 == strcmp (type, "linear"))
224 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
226 else if (0 == strcmp (type, "sinus"))
228 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
230 else if (0 == strcmp (type, "random"))
232 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
236 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
237 op_counter, op, cur->id);
240 GNUNET_free (op_name);
241 GNUNET_free (sec_name);
243 return GNUNET_SYSERR;
245 GNUNET_free (op_name);
248 GNUNET_asprintf (&op_name, "op-%u-base-rate", op_counter);
249 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
255 "Missing base rate in operation %u `%s' in episode %u\n",
256 op_counter, op, cur->id);
259 GNUNET_free (op_name);
260 GNUNET_free (sec_name);
262 return GNUNET_SYSERR;
264 GNUNET_free (op_name);
267 GNUNET_asprintf (&op_name, "op-%u-max-rate", op_counter);
268 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
273 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
274 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
275 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
278 "Missing max rate in operation %u `%s' in episode %u\n",
279 op_counter, op, cur->id);
281 GNUNET_free (op_name);
284 GNUNET_free (sec_name);
285 return GNUNET_SYSERR;
288 GNUNET_free (op_name);
291 GNUNET_asprintf (&op_name, "op-%u-period", op_counter);
292 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
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,
311 "Missing frequency in operation %u `%s' in episode %u\n",
312 op_counter, op, cur->id);
314 GNUNET_free (op_name);
317 GNUNET_free (sec_name);
318 return GNUNET_SYSERR;
320 GNUNET_free (op_name);
323 GNUNET_asprintf (&op_name, "op-%u-pref", op_counter);
324 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
330 "Missing preference in operation %u `%s' in episode %u\n",
331 op_counter, op, cur->id);
333 GNUNET_free (op_name);
335 GNUNET_free_non_null (pref);
337 GNUNET_free (sec_name);
338 return GNUNET_SYSERR;
341 if (0 == strcmp (pref, "bandwidth"))
342 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
343 else if (0 == strcmp (pref, "latency"))
344 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
348 "Invalid preference in operation %u `%s' in episode %u\n",
349 op_counter, op, cur->id);
351 GNUNET_free (op_name);
353 GNUNET_free_non_null (pref);
355 GNUNET_free (sec_name);
356 return GNUNET_SYSERR;
359 GNUNET_free (op_name);
364 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
365 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
367 if ((o->max_rate - o->base_rate) > o->base_rate)
369 /* This will cause an underflow */
373 "Selected max rate and base rate cannot be used for desired traffic form!\n");
376 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
378 "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
379 op_counter, cur->id, print_op (o->type), o->src_id,
380 o->dest_id, (NULL != type) ? type : "",
381 o->base_rate, o->max_rate,
382 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
384 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
385 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
387 GNUNET_free_non_null (type);
390 GNUNET_CONTAINER_DLL_insert (cur->head, cur->tail, o);
393 GNUNET_free (sec_name);
400 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
404 struct GNUNET_TIME_Relative e_duration;
406 struct Episode *last;
412 GNUNET_asprintf (&sec_name, "episode-%u", e_counter);
413 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
418 GNUNET_free (sec_name);
422 cur = GNUNET_new (struct Episode);
423 cur->duration = e_duration;
426 if (GNUNET_OK != load_episode (e, cur, cfg))
428 GNUNET_free (sec_name);
430 return GNUNET_SYSERR;
433 fprintf (stderr, "Found episode %u with duration %s \n",
435 GNUNET_STRINGS_relative_time_to_string (cur->duration,
438 /* Update experiment */
440 e->total_duration = GNUNET_TIME_relative_add (e->total_duration,
442 /* Put in linked list */
448 GNUNET_free (sec_name);
457 timeout_experiment (void *cls)
459 struct Experiment *e = cls;
461 e->experiment_timeout_task = NULL;
462 fprintf (stderr, "Experiment timeout!\n");
464 if (NULL != e->episode_timeout_task)
466 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
467 e->episode_timeout_task = NULL;
470 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration (e->start_time),
476 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
478 struct BenchmarkPeer *peer;
479 struct BenchmarkPartner *partner;
481 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
488 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
495 fprintf (stderr, "Found master %llu slave %llu\n", op->src_id, op->dest_id);
497 if (NULL != partner->tg)
499 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
500 op->src_id, op->dest_id);
501 GNUNET_ATS_TEST_generate_traffic_stop (partner->tg);
505 partner->tg = GNUNET_ATS_TEST_generate_traffic_start (peer, partner,
510 GNUNET_TIME_UNIT_FOREVER_REL);
515 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
517 struct BenchmarkPartner *p;
519 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
526 fprintf (stderr, "Found master %llu slave %llu\n", op->src_id, op->dest_id);
530 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
531 op->src_id, op->dest_id);
532 GNUNET_ATS_TEST_generate_traffic_stop (p->tg);
539 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
541 struct BenchmarkPeer *peer;
542 struct BenchmarkPartner *partner;
544 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
551 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
558 fprintf (stderr, "Found master %llu slave %llu\n", op->src_id, op->dest_id);
560 if (NULL != partner->pg)
562 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
563 op->src_id, op->dest_id);
564 GNUNET_ATS_TEST_generate_preferences_stop (partner->pg);
568 partner->pg = GNUNET_ATS_TEST_generate_preferences_start (peer, partner,
579 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
581 struct BenchmarkPartner *p;
583 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
590 fprintf (stderr, "Found master %llu slave %llu\n", op->src_id, op->dest_id);
594 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
595 op->src_id, op->dest_id);
596 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
603 enforce_episode (struct Episode *ep)
605 struct GNUNET_ATS_TEST_Operation *cur;
607 for (cur = ep->head; NULL != cur; cur = cur->next)
609 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
610 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
614 enforce_start_send (cur);
618 enforce_stop_send (cur);
621 case START_PREFERENCE:
622 enforce_start_preference (cur);
625 case STOP_PREFERENCE:
626 enforce_stop_preference (cur);
637 timeout_episode (void *cls)
639 struct Experiment *e = cls;
641 e->episode_timeout_task = NULL;
642 if (NULL != e->ep_done_cb)
643 e->ep_done_cb (e->cur);
645 /* Scheduling next */
646 e->cur = e->cur->next;
650 fprintf (stderr, "Last episode done!\n");
651 if (NULL != e->experiment_timeout_task)
653 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
654 e->experiment_timeout_task = NULL;
656 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration (e->start_time),
661 fprintf (stderr, "Running episode %u with timeout %s\n",
663 GNUNET_STRINGS_relative_time_to_string (e->cur->duration,
665 enforce_episode (e->cur);
667 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
668 &timeout_episode, e);
673 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
674 GNUNET_ATS_TESTING_EpisodeDoneCallback
676 GNUNET_ATS_TESTING_ExperimentDoneCallback
679 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
680 GNUNET_STRINGS_relative_time_to_string (e->max_duration,
682 e->e_done_cb = e_done_cb;
683 e->ep_done_cb = ep_done_cb;
684 e->start_time = GNUNET_TIME_absolute_get ();
686 /* Start total time out */
687 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
693 fprintf (stderr, "Running episode %u with timeout %s\n",
695 GNUNET_STRINGS_relative_time_to_string (e->cur->duration,
697 enforce_episode (e->cur);
698 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
699 &timeout_episode, e);
704 GNUNET_ATS_TEST_experimentation_load (const char *filename)
706 struct Experiment *e;
707 struct GNUNET_CONFIGURATION_Handle *cfg;
711 cfg = GNUNET_CONFIGURATION_create ();
712 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
714 fprintf (stderr, "Failed to load `%s'\n", filename);
715 GNUNET_CONFIGURATION_destroy (cfg);
719 e = create_experiment ();
721 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "experiment",
724 fprintf (stderr, "Invalid %s", "name");
729 fprintf (stderr, "Experiment name: `%s'\n", e->name);
731 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg,
736 fprintf (stderr, "Invalid %s", "cfg_file");
741 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
743 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "experiment",
747 fprintf (stderr, "Invalid %s", "masters");
752 fprintf (stderr, "Experiment masters: `%llu'\n",
755 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "experiment",
759 fprintf (stderr, "Invalid %s", "slaves");
764 fprintf (stderr, "Experiment slaves: `%llu'\n",
767 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg, "experiment",
771 fprintf (stderr, "Invalid %s", "log_freq");
776 fprintf (stderr, "Experiment logging frequency: `%s'\n",
777 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
779 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg, "experiment",
783 fprintf (stderr, "Invalid %s", "max_duration");
788 fprintf (stderr, "Experiment duration: `%s'\n",
789 GNUNET_STRINGS_relative_time_to_string (e->max_duration,
792 load_episodes (e, cfg);
793 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
795 GNUNET_STRINGS_relative_time_to_string (e->total_duration,
798 GNUNET_CONFIGURATION_destroy (cfg);
804 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
806 if (NULL != e->experiment_timeout_task)
808 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
809 e->experiment_timeout_task = NULL;
811 if (NULL != e->episode_timeout_task)
813 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
814 e->episode_timeout_task = NULL;
820 /* end of file ats-testing-experiment.c*/