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 "ats-testing.h"
31 print_op (enum OperationType op)
38 case START_PREFERENCE:
39 return "START_PREFERENCE";
41 return "STOP_PREFERENCE";
49 static struct Experiment *
53 e = GNUNET_new (struct Experiment);
58 e->total_duration = GNUNET_TIME_UNIT_ZERO;
63 free_experiment (struct Experiment *e)
67 struct GNUNET_ATS_TEST_Operation *cur_o;
68 struct GNUNET_ATS_TEST_Operation *next_o;
71 for (cur = next; NULL != cur; cur = next)
76 for (cur_o = next_o; NULL != cur_o; cur_o = next_o)
84 GNUNET_free_non_null (e->name);
85 GNUNET_free_non_null (e->cfg_file);
90 load_episode (struct Experiment *e, struct Episode *cur,
91 struct GNUNET_CONFIGURATION_Handle *cfg)
93 struct GNUNET_ATS_TEST_Operation *o;
100 fprintf (stderr, "Parsing episode %u\n",cur->id);
101 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
106 GNUNET_asprintf(&op_name, "op-%u-operation", op_counter);
107 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
108 sec_name, op_name, &op))
110 GNUNET_free (op_name);
113 o = GNUNET_new (struct GNUNET_ATS_TEST_Operation);
114 /* operations = set_rate, start_send, stop_send, set_preference */
115 if (0 == strcmp (op, "start_send"))
117 o->type = START_SEND;
119 else if (0 == strcmp (op, "stop_send"))
123 else if (0 == strcmp (op, "start_preference"))
125 o->type = START_PREFERENCE;
127 else if (0 == strcmp (op, "stop_preference"))
129 o->type = STOP_PREFERENCE;
133 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
134 op_counter, op, cur->id);
136 GNUNET_free (op_name);
137 return GNUNET_SYSERR;
139 GNUNET_free (op_name);
142 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
143 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
144 sec_name, op_name, &o->src_id))
146 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
147 op_counter, op, cur->id);
149 GNUNET_free (op_name);
150 return GNUNET_SYSERR;
152 if (o->src_id > (e->num_masters - 1))
154 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
155 o->src_id, op_counter, op, cur->id);
157 GNUNET_free (op_name);
158 return GNUNET_SYSERR;
160 GNUNET_free (op_name);
162 /* Get destination */
163 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
164 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
165 sec_name, op_name, &o->dest_id))
167 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
168 op_counter, op, cur->id);
170 GNUNET_free (op_name);
171 return GNUNET_SYSERR;
173 if (o->dest_id > (e->num_slaves - 1))
175 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
176 o->dest_id, op_counter, op, cur->id);
178 GNUNET_free (op_name);
179 return GNUNET_SYSERR;
181 GNUNET_free (op_name);
183 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
184 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
185 sec_name, op_name, &type)) &&
186 ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
188 /* Load arguments for set_rate, start_send, set_preference */
189 if (0 == strcmp (type, "constant"))
191 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
193 else if (0 == strcmp (type, "linear"))
195 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
197 else if (0 == strcmp (type, "sinus"))
199 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
201 else if (0 == strcmp (type, "random"))
203 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
207 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
208 op_counter, op, cur->id);
211 GNUNET_free (op_name);
212 return GNUNET_SYSERR;
214 GNUNET_free (op_name);
217 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
218 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
219 sec_name, op_name, &o->base_rate))
221 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
222 op_counter, op, cur->id);
225 GNUNET_free (op_name);
226 return GNUNET_SYSERR;
228 GNUNET_free (op_name);
231 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
232 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
233 sec_name, op_name, &o->max_rate))
235 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
236 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
237 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
239 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
240 op_counter, op, cur->id);
242 GNUNET_free (op_name);
244 return GNUNET_SYSERR;
247 GNUNET_free (op_name);
250 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
251 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
252 sec_name, op_name, &o->period))
254 o->period = cur->duration;
256 GNUNET_free (op_name);
258 if (START_PREFERENCE == o->type)
261 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
262 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
263 sec_name, op_name, &o->frequency))
265 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
266 op_counter, op, cur->id);
268 GNUNET_free (op_name);
270 return GNUNET_SYSERR;
272 GNUNET_free (op_name);
275 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
276 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
277 sec_name, op_name, &pref))
279 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
280 op_counter, op, cur->id);
282 GNUNET_free (op_name);
284 GNUNET_free_non_null (pref);
285 return GNUNET_SYSERR;
288 if (0 == strcmp(pref, "bandwidth"))
289 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
290 else if (0 == strcmp(pref, "latency"))
291 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
294 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
295 op_counter, op, cur->id);
297 GNUNET_free (op_name);
300 GNUNET_free_non_null (pref);
301 return GNUNET_SYSERR;
304 GNUNET_free (op_name);
309 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
310 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
312 if ((o->max_rate - o->base_rate) > o->base_rate)
314 /* This will cause an underflow */
317 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
320 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
321 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
322 op_counter, cur->id, print_op (o->type), o->src_id,
323 o->dest_id, (NULL != type) ? type : "",
324 o->base_rate, o->max_rate,
325 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
327 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
328 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
330 GNUNET_free_non_null (type);
333 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
336 GNUNET_free (sec_name);
342 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
346 struct GNUNET_TIME_Relative e_duration;
348 struct Episode *last;
354 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
355 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
356 sec_name, "duration", &e_duration))
358 GNUNET_free (sec_name);
362 cur = GNUNET_new (struct Episode);
363 cur->duration = e_duration;
366 if (GNUNET_OK != load_episode (e, cur, cfg))
368 GNUNET_free (sec_name);
370 return GNUNET_SYSERR;
373 fprintf (stderr, "Found episode %u with duration %s \n",
375 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
377 /* Update experiment */
379 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
380 /* Put in linked list */
386 GNUNET_free (sec_name);
394 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
396 struct Experiment *e = cls;
397 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
398 fprintf (stderr, "Experiment timeout!\n");
400 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
402 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
403 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
406 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
411 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
413 struct BenchmarkPeer *peer;
414 struct BenchmarkPartner *partner;
416 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
423 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
430 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
432 if (NULL != partner->tg)
434 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
435 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
439 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
440 op->gen_type, op->base_rate, op->max_rate, op->period,
441 GNUNET_TIME_UNIT_FOREVER_REL);
445 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
447 struct BenchmarkPartner *p;
448 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
455 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
459 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
460 op->src_id, op->dest_id);
461 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
468 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
470 struct BenchmarkPeer *peer;
471 struct BenchmarkPartner *partner;
473 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
480 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
487 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
489 if (NULL != partner->pg)
491 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
492 op->src_id, op->dest_id);
493 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
497 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
498 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
503 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
505 struct BenchmarkPartner *p;
506 p = 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);
517 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
518 op->src_id, op->dest_id);
519 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
524 static void enforce_episode (struct Episode *ep)
526 struct GNUNET_ATS_TEST_Operation *cur;
527 for (cur = ep->head; NULL != cur; cur = cur->next)
530 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
531 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
534 enforce_start_send (cur);
537 enforce_stop_send (cur);
539 case START_PREFERENCE:
540 enforce_start_preference (cur);
542 case STOP_PREFERENCE:
543 enforce_stop_preference (cur);
552 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
554 struct Experiment *e = cls;
555 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
556 if (NULL != e->ep_done_cb)
557 e->ep_done_cb (e->cur);
559 /* Scheduling next */
560 e->cur = e->cur->next;
564 fprintf (stderr, "Last episode done!\n");
565 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
567 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
568 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
570 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
574 fprintf (stderr, "Running episode %u with timeout %s\n",
576 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
577 enforce_episode(e->cur);
579 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
580 &timeout_episode, e);
585 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
586 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
587 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
589 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
590 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
591 e->e_done_cb = e_done_cb;
592 e->ep_done_cb = ep_done_cb;
593 e->start_time = GNUNET_TIME_absolute_get();
595 /* Start total time out */
596 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
597 &timeout_experiment, e);
601 fprintf (stderr, "Running episode %u with timeout %s\n",
603 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
604 enforce_episode(e->cur);
605 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
606 &timeout_episode, e);
613 GNUNET_ATS_TEST_experimentation_load (char *filename)
615 struct Experiment *e;
616 struct GNUNET_CONFIGURATION_Handle *cfg;
619 cfg = GNUNET_CONFIGURATION_create();
620 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
622 fprintf (stderr, "Failed to load `%s'\n", filename);
623 GNUNET_CONFIGURATION_destroy (cfg);
627 e = create_experiment ();
629 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
632 fprintf (stderr, "Invalid %s", "name");
637 fprintf (stderr, "Experiment name: `%s'\n", e->name);
639 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
640 "cfg_file", &e->cfg_file))
642 fprintf (stderr, "Invalid %s", "cfg_file");
647 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
649 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
650 "masters", &e->num_masters))
652 fprintf (stderr, "Invalid %s", "masters");
657 fprintf (stderr, "Experiment masters: `%llu'\n",
660 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
661 "slaves", &e->num_slaves))
663 fprintf (stderr, "Invalid %s", "slaves");
668 fprintf (stderr, "Experiment slaves: `%llu'\n",
671 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
672 "log_freq", &e->log_freq))
674 fprintf (stderr, "Invalid %s", "log_freq");
679 fprintf (stderr, "Experiment logging frequency: `%s'\n",
680 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
682 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
683 "max_duration", &e->max_duration))
685 fprintf (stderr, "Invalid %s", "max_duration");
690 fprintf (stderr, "Experiment duration: `%s'\n",
691 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
693 load_episodes (e, cfg);
694 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
696 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
698 GNUNET_CONFIGURATION_destroy (cfg);
703 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
705 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
707 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
708 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
710 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
712 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
713 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
718 /* end of file ats-testing-experiment.c*/