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/>.
19 * @file ats-tests/ats-testing-experiment.c
20 * @brief ats benchmark: controlled experiment execution
21 * @author Christian Grothoff
22 * @author Matthias Wachs
25 #include "gnunet_util_lib.h"
26 #include "ats-testing.h"
29 print_op (enum OperationType op)
36 case START_PREFERENCE:
37 return "START_PREFERENCE";
39 return "STOP_PREFERENCE";
47 static struct Experiment *
51 e = GNUNET_new (struct Experiment);
56 e->total_duration = GNUNET_TIME_UNIT_ZERO;
61 free_experiment (struct Experiment *e)
65 struct GNUNET_ATS_TEST_Operation *cur_o;
66 struct GNUNET_ATS_TEST_Operation *next_o;
69 for (cur = next; NULL != cur; cur = next)
74 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
82 GNUNET_free_non_null (e->name);
83 GNUNET_free_non_null (e->cfg_file);
89 load_episode (struct Experiment *e,
91 struct GNUNET_CONFIGURATION_Handle *cfg)
93 struct GNUNET_ATS_TEST_Operation *o;
101 fprintf (stderr, "Parsing episode %u\n",cur->id);
102 GNUNET_asprintf (&sec_name, "episode-%u", cur->id);
107 GNUNET_asprintf (&op_name, "op-%u-operation", op_counter);
108 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
109 sec_name, op_name, &op))
111 GNUNET_free (op_name);
114 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
115 /* operations = set_rate, start_send, stop_send, set_preference */
116 if (0 == strcmp (op, "start_send"))
118 o->type = START_SEND;
120 else if (0 == strcmp (op, "stop_send"))
124 else if (0 == strcmp (op, "start_preference"))
126 o->type = START_PREFERENCE;
128 else if (0 == strcmp (op, "stop_preference"))
130 o->type = STOP_PREFERENCE;
134 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
135 op_counter, op, cur->id);
137 GNUNET_free (op_name);
139 GNUNET_free (sec_name);
140 return GNUNET_SYSERR;
142 GNUNET_free (op_name);
145 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
146 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
147 sec_name, op_name, &o->src_id))
149 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
150 op_counter, op, cur->id);
152 GNUNET_free (op_name);
154 GNUNET_free (sec_name);
155 return GNUNET_SYSERR;
157 if (o->src_id > (e->num_masters - 1))
159 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
160 o->src_id, op_counter, op, cur->id);
162 GNUNET_free (op_name);
164 GNUNET_free (sec_name);
165 return GNUNET_SYSERR;
167 GNUNET_free (op_name);
169 /* Get destination */
170 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
171 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
172 sec_name, op_name, &o->dest_id))
174 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
175 op_counter, op, cur->id);
177 GNUNET_free (op_name);
179 GNUNET_free (sec_name);
180 return GNUNET_SYSERR;
182 if (o->dest_id > (e->num_slaves - 1))
184 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
185 o->dest_id, op_counter, op, cur->id);
187 GNUNET_free (op_name);
189 GNUNET_free (sec_name);
190 return GNUNET_SYSERR;
192 GNUNET_free (op_name);
194 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
195 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
196 sec_name, op_name, &type)) &&
197 (STOP_SEND != o->type) &&
198 (STOP_PREFERENCE != o->type) )
200 /* Load arguments for set_rate, start_send, set_preference */
201 if (0 == strcmp (type, "constant"))
203 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
205 else if (0 == strcmp (type, "linear"))
207 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
209 else if (0 == strcmp (type, "sinus"))
211 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
213 else if (0 == strcmp (type, "random"))
215 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
219 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
220 op_counter, op, cur->id);
223 GNUNET_free (op_name);
224 GNUNET_free (sec_name);
226 return GNUNET_SYSERR;
228 GNUNET_free (op_name);
231 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
232 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
233 sec_name, op_name, &o->base_rate))
235 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
236 op_counter, op, cur->id);
239 GNUNET_free (op_name);
240 GNUNET_free (sec_name);
242 return GNUNET_SYSERR;
244 GNUNET_free (op_name);
247 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
248 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
249 sec_name, op_name, &o->max_rate))
251 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
252 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
253 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
255 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
256 op_counter, op, cur->id);
258 GNUNET_free (op_name);
261 GNUNET_free (sec_name);
262 return GNUNET_SYSERR;
265 GNUNET_free (op_name);
268 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
269 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
270 sec_name, op_name, &o->period))
272 o->period = cur->duration;
274 GNUNET_free (op_name);
276 if (START_PREFERENCE == o->type)
279 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
280 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
281 sec_name, op_name, &o->frequency))
283 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
284 op_counter, op, cur->id);
286 GNUNET_free (op_name);
289 GNUNET_free (sec_name);
290 return GNUNET_SYSERR;
292 GNUNET_free (op_name);
295 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
296 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
297 sec_name, op_name, &pref))
299 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
300 op_counter, op, cur->id);
302 GNUNET_free (op_name);
304 GNUNET_free_non_null (pref);
306 GNUNET_free (sec_name);
307 return GNUNET_SYSERR;
310 if (0 == strcmp(pref, "bandwidth"))
311 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
312 else if (0 == strcmp(pref, "latency"))
313 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
316 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
317 op_counter, op, cur->id);
319 GNUNET_free (op_name);
321 GNUNET_free_non_null (pref);
323 GNUNET_free (sec_name);
324 return GNUNET_SYSERR;
327 GNUNET_free (op_name);
332 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
333 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
335 if ((o->max_rate - o->base_rate) > o->base_rate)
337 /* This will cause an underflow */
340 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
343 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
344 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
345 op_counter, cur->id, print_op (o->type), o->src_id,
346 o->dest_id, (NULL != type) ? type : "",
347 o->base_rate, o->max_rate,
348 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
350 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
351 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
353 GNUNET_free_non_null (type);
356 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
359 GNUNET_free (sec_name);
366 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
370 struct GNUNET_TIME_Relative e_duration;
372 struct Episode *last;
378 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
379 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
380 sec_name, "duration", &e_duration))
382 GNUNET_free (sec_name);
386 cur = GNUNET_new (struct Episode);
387 cur->duration = e_duration;
390 if (GNUNET_OK != load_episode (e, cur, cfg))
392 GNUNET_free (sec_name);
394 return GNUNET_SYSERR;
397 fprintf (stderr, "Found episode %u with duration %s \n",
399 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
401 /* Update experiment */
403 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
404 /* Put in linked list */
410 GNUNET_free (sec_name);
419 timeout_experiment (void *cls)
421 struct Experiment *e = cls;
422 e->experiment_timeout_task = NULL;
423 fprintf (stderr, "Experiment timeout!\n");
425 if (NULL != e->episode_timeout_task)
427 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
428 e->episode_timeout_task = NULL;
431 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
437 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
439 struct BenchmarkPeer *peer;
440 struct BenchmarkPartner *partner;
442 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
449 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
456 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
458 if (NULL != partner->tg)
460 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
461 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
465 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
466 op->gen_type, op->base_rate, op->max_rate, op->period,
467 GNUNET_TIME_UNIT_FOREVER_REL);
471 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
473 struct BenchmarkPartner *p;
474 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
481 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
485 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
486 op->src_id, op->dest_id);
487 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
494 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
496 struct BenchmarkPeer *peer;
497 struct BenchmarkPartner *partner;
499 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
506 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
513 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
515 if (NULL != partner->pg)
517 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
518 op->src_id, op->dest_id);
519 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
523 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
524 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
529 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
531 struct BenchmarkPartner *p;
532 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
539 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
543 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
544 op->src_id, op->dest_id);
545 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
550 static void enforce_episode (struct Episode *ep)
552 struct GNUNET_ATS_TEST_Operation *cur;
553 for (cur = ep->head; NULL != cur; cur = cur->next)
556 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
557 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
560 enforce_start_send (cur);
563 enforce_stop_send (cur);
565 case START_PREFERENCE:
566 enforce_start_preference (cur);
568 case STOP_PREFERENCE:
569 enforce_stop_preference (cur);
579 timeout_episode (void *cls)
581 struct Experiment *e = cls;
583 e->episode_timeout_task = NULL;
584 if (NULL != e->ep_done_cb)
585 e->ep_done_cb (e->cur);
587 /* Scheduling next */
588 e->cur = e->cur->next;
592 fprintf (stderr, "Last episode done!\n");
593 if (NULL != e->experiment_timeout_task)
595 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
596 e->experiment_timeout_task = NULL;
598 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
602 fprintf (stderr, "Running episode %u with timeout %s\n",
604 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
605 enforce_episode(e->cur);
607 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
608 &timeout_episode, e);
613 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
614 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
615 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
617 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
618 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
619 e->e_done_cb = e_done_cb;
620 e->ep_done_cb = ep_done_cb;
621 e->start_time = GNUNET_TIME_absolute_get();
623 /* Start total time out */
624 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
625 &timeout_experiment, e);
629 fprintf (stderr, "Running episode %u with timeout %s\n",
631 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
632 enforce_episode(e->cur);
633 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
634 &timeout_episode, e);
641 GNUNET_ATS_TEST_experimentation_load (const char *filename)
643 struct Experiment *e;
644 struct GNUNET_CONFIGURATION_Handle *cfg;
647 cfg = GNUNET_CONFIGURATION_create();
648 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
650 fprintf (stderr, "Failed to load `%s'\n", filename);
651 GNUNET_CONFIGURATION_destroy (cfg);
655 e = create_experiment ();
657 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
660 fprintf (stderr, "Invalid %s", "name");
665 fprintf (stderr, "Experiment name: `%s'\n", e->name);
667 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
668 "cfg_file", &e->cfg_file))
670 fprintf (stderr, "Invalid %s", "cfg_file");
675 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
677 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
678 "masters", &e->num_masters))
680 fprintf (stderr, "Invalid %s", "masters");
685 fprintf (stderr, "Experiment masters: `%llu'\n",
688 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
689 "slaves", &e->num_slaves))
691 fprintf (stderr, "Invalid %s", "slaves");
696 fprintf (stderr, "Experiment slaves: `%llu'\n",
699 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
700 "log_freq", &e->log_freq))
702 fprintf (stderr, "Invalid %s", "log_freq");
707 fprintf (stderr, "Experiment logging frequency: `%s'\n",
708 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
710 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
711 "max_duration", &e->max_duration))
713 fprintf (stderr, "Invalid %s", "max_duration");
718 fprintf (stderr, "Experiment duration: `%s'\n",
719 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
721 load_episodes (e, cfg);
722 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
724 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
726 GNUNET_CONFIGURATION_destroy (cfg);
731 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
733 if (NULL != e->experiment_timeout_task)
735 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
736 e->experiment_timeout_task = NULL;
738 if (NULL != e->episode_timeout_task)
740 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
741 e->episode_timeout_task = NULL;
746 /* end of file ats-testing-experiment.c*/