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;
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 return GNUNET_SYSERR;
141 GNUNET_free (op_name);
144 GNUNET_asprintf(&op_name, "op-%u-src", op_counter);
145 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
146 sec_name, op_name, &o->src_id))
148 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
149 op_counter, op, cur->id);
151 GNUNET_free (op_name);
153 return GNUNET_SYSERR;
155 if (o->src_id > (e->num_masters - 1))
157 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
158 o->src_id, op_counter, op, cur->id);
160 GNUNET_free (op_name);
162 return GNUNET_SYSERR;
164 GNUNET_free (op_name);
166 /* Get destination */
167 GNUNET_asprintf(&op_name, "op-%u-dest", op_counter);
168 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
169 sec_name, op_name, &o->dest_id))
171 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
172 op_counter, op, cur->id);
174 GNUNET_free (op_name);
176 return GNUNET_SYSERR;
178 if (o->dest_id > (e->num_slaves - 1))
180 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
181 o->dest_id, op_counter, op, cur->id);
183 GNUNET_free (op_name);
185 return GNUNET_SYSERR;
187 GNUNET_free (op_name);
189 GNUNET_asprintf(&op_name, "op-%u-type", op_counter);
190 if ( (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_string(cfg,
191 sec_name, op_name, &type)) &&
192 ((STOP_SEND != o->type) || (STOP_PREFERENCE != o->type)))
194 /* Load arguments for set_rate, start_send, set_preference */
195 if (0 == strcmp (type, "constant"))
197 o->gen_type = GNUNET_ATS_TEST_TG_CONSTANT;
199 else if (0 == strcmp (type, "linear"))
201 o->gen_type = GNUNET_ATS_TEST_TG_LINEAR;
203 else if (0 == strcmp (type, "sinus"))
205 o->gen_type = GNUNET_ATS_TEST_TG_SINUS;
207 else if (0 == strcmp (type, "random"))
209 o->gen_type = GNUNET_ATS_TEST_TG_RANDOM;
213 fprintf (stderr, "Invalid type %u `%s' in episode %u\n",
214 op_counter, op, cur->id);
217 GNUNET_free (op_name);
218 GNUNET_free (sec_name);
220 return GNUNET_SYSERR;
222 GNUNET_free (op_name);
225 GNUNET_asprintf(&op_name, "op-%u-base-rate", op_counter);
226 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
227 sec_name, op_name, &o->base_rate))
229 fprintf (stderr, "Missing base rate in operation %u `%s' in episode %u\n",
230 op_counter, op, cur->id);
233 GNUNET_free (op_name);
234 GNUNET_free (sec_name);
236 return GNUNET_SYSERR;
238 GNUNET_free (op_name);
241 GNUNET_asprintf(&op_name, "op-%u-max-rate", op_counter);
242 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
243 sec_name, op_name, &o->max_rate))
245 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
246 (GNUNET_ATS_TEST_TG_RANDOM == o->gen_type) ||
247 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
249 fprintf (stderr, "Missing max rate in operation %u `%s' in episode %u\n",
250 op_counter, op, cur->id);
252 GNUNET_free (op_name);
255 return GNUNET_SYSERR;
258 GNUNET_free (op_name);
261 GNUNET_asprintf(&op_name, "op-%u-period", op_counter);
262 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
263 sec_name, op_name, &o->period))
265 o->period = cur->duration;
267 GNUNET_free (op_name);
269 if (START_PREFERENCE == o->type)
272 GNUNET_asprintf(&op_name, "op-%u-frequency", op_counter);
273 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (cfg,
274 sec_name, op_name, &o->frequency))
276 fprintf (stderr, "Missing frequency in operation %u `%s' in episode %u\n",
277 op_counter, op, cur->id);
279 GNUNET_free (op_name);
282 return GNUNET_SYSERR;
284 GNUNET_free (op_name);
287 GNUNET_asprintf(&op_name, "op-%u-pref", op_counter);
288 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
289 sec_name, op_name, &pref))
291 fprintf (stderr, "Missing preference in operation %u `%s' in episode %u\n",
292 op_counter, op, cur->id);
294 GNUNET_free (op_name);
296 GNUNET_free_non_null (pref);
298 return GNUNET_SYSERR;
301 if (0 == strcmp(pref, "bandwidth"))
302 o->pref_type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
303 else if (0 == strcmp(pref, "latency"))
304 o->pref_type = GNUNET_ATS_PREFERENCE_LATENCY;
307 fprintf (stderr, "Invalid preference in operation %u `%s' in episode %u\n",
308 op_counter, op, cur->id);
310 GNUNET_free (op_name);
313 GNUNET_free_non_null (pref);
315 return GNUNET_SYSERR;
318 GNUNET_free (op_name);
323 if ((GNUNET_ATS_TEST_TG_LINEAR == o->gen_type) ||
324 (GNUNET_ATS_TEST_TG_SINUS == o->gen_type))
326 if ((o->max_rate - o->base_rate) > o->base_rate)
328 /* This will cause an underflow */
331 fprintf (stderr, "Selected max rate and base rate cannot be used for desired traffic form!\n");
334 if ((START_SEND == o->type) || (START_PREFERENCE == o->type))
335 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %s, %llu -> %llu in %s\n",
336 op_counter, cur->id, print_op (o->type), o->src_id,
337 o->dest_id, (NULL != type) ? type : "",
338 o->base_rate, o->max_rate,
339 GNUNET_STRINGS_relative_time_to_string (o->period, GNUNET_YES));
341 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu]\n",
342 op_counter, cur->id, print_op (o->type), o->src_id, o->dest_id);
344 GNUNET_free_non_null (type);
347 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
350 GNUNET_free (sec_name);
356 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
360 struct GNUNET_TIME_Relative e_duration;
362 struct Episode *last;
368 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
369 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
370 sec_name, "duration", &e_duration))
372 GNUNET_free (sec_name);
376 cur = GNUNET_new (struct Episode);
377 cur->duration = e_duration;
380 if (GNUNET_OK != load_episode (e, cur, cfg))
382 GNUNET_free (sec_name);
384 return GNUNET_SYSERR;
387 fprintf (stderr, "Found episode %u with duration %s \n",
389 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
391 /* Update experiment */
393 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
394 /* Put in linked list */
400 GNUNET_free (sec_name);
408 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
410 struct Experiment *e = cls;
411 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
412 fprintf (stderr, "Experiment timeout!\n");
414 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
416 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
417 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
420 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
425 enforce_start_send (struct GNUNET_ATS_TEST_Operation *op)
427 struct BenchmarkPeer *peer;
428 struct BenchmarkPartner *partner;
430 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
437 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
444 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
446 if (NULL != partner->tg)
448 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",op->src_id, op->dest_id);
449 GNUNET_ATS_TEST_generate_traffic_stop(partner->tg);
453 partner->tg = GNUNET_ATS_TEST_generate_traffic_start(peer, partner,
454 op->gen_type, op->base_rate, op->max_rate, op->period,
455 GNUNET_TIME_UNIT_FOREVER_REL);
459 enforce_stop_send (struct GNUNET_ATS_TEST_Operation *op)
461 struct BenchmarkPartner *p;
462 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
469 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
473 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
474 op->src_id, op->dest_id);
475 GNUNET_ATS_TEST_generate_traffic_stop(p->tg);
482 enforce_start_preference (struct GNUNET_ATS_TEST_Operation *op)
484 struct BenchmarkPeer *peer;
485 struct BenchmarkPartner *partner;
487 peer = GNUNET_ATS_TEST_get_peer (op->src_id);
494 partner = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
501 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
503 if (NULL != partner->pg)
505 fprintf (stderr, "Stopping traffic between master %llu slave %llu\n",
506 op->src_id, op->dest_id);
507 GNUNET_ATS_TEST_generate_preferences_stop(partner->pg);
511 partner->pg = GNUNET_ATS_TEST_generate_preferences_start(peer, partner,
512 op->gen_type, op->base_rate, op->max_rate, op->period, op->frequency,
517 enforce_stop_preference (struct GNUNET_ATS_TEST_Operation *op)
519 struct BenchmarkPartner *p;
520 p = GNUNET_ATS_TEST_get_partner (op->src_id, op->dest_id);
527 fprintf (stderr, "Found master %llu slave %llu\n",op->src_id, op->dest_id);
531 fprintf (stderr, "Stopping preference between master %llu slave %llu\n",
532 op->src_id, op->dest_id);
533 GNUNET_ATS_TEST_generate_preferences_stop (p->pg);
538 static void enforce_episode (struct Episode *ep)
540 struct GNUNET_ATS_TEST_Operation *cur;
541 for (cur = ep->head; NULL != cur; cur = cur->next)
544 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
545 print_op (cur->type), cur->src_id, cur->dest_id, cur->base_rate);
548 enforce_start_send (cur);
551 enforce_stop_send (cur);
553 case START_PREFERENCE:
554 enforce_start_preference (cur);
556 case STOP_PREFERENCE:
557 enforce_stop_preference (cur);
566 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
568 struct Experiment *e = cls;
569 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
570 if (NULL != e->ep_done_cb)
571 e->ep_done_cb (e->cur);
573 /* Scheduling next */
574 e->cur = e->cur->next;
578 fprintf (stderr, "Last episode done!\n");
579 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
581 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
582 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
584 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
588 fprintf (stderr, "Running episode %u with timeout %s\n",
590 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
591 enforce_episode(e->cur);
593 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
594 &timeout_episode, e);
599 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
600 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
601 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
603 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
604 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
605 e->e_done_cb = e_done_cb;
606 e->ep_done_cb = ep_done_cb;
607 e->start_time = GNUNET_TIME_absolute_get();
609 /* Start total time out */
610 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
611 &timeout_experiment, e);
615 fprintf (stderr, "Running episode %u with timeout %s\n",
617 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
618 enforce_episode(e->cur);
619 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
620 &timeout_episode, e);
627 GNUNET_ATS_TEST_experimentation_load (char *filename)
629 struct Experiment *e;
630 struct GNUNET_CONFIGURATION_Handle *cfg;
633 cfg = GNUNET_CONFIGURATION_create();
634 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
636 fprintf (stderr, "Failed to load `%s'\n", filename);
637 GNUNET_CONFIGURATION_destroy (cfg);
641 e = create_experiment ();
643 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
646 fprintf (stderr, "Invalid %s", "name");
651 fprintf (stderr, "Experiment name: `%s'\n", e->name);
653 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
654 "cfg_file", &e->cfg_file))
656 fprintf (stderr, "Invalid %s", "cfg_file");
661 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
663 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
664 "masters", &e->num_masters))
666 fprintf (stderr, "Invalid %s", "masters");
671 fprintf (stderr, "Experiment masters: `%llu'\n",
674 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
675 "slaves", &e->num_slaves))
677 fprintf (stderr, "Invalid %s", "slaves");
682 fprintf (stderr, "Experiment slaves: `%llu'\n",
685 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
686 "log_freq", &e->log_freq))
688 fprintf (stderr, "Invalid %s", "log_freq");
693 fprintf (stderr, "Experiment logging frequency: `%s'\n",
694 GNUNET_STRINGS_relative_time_to_string (e->log_freq, GNUNET_YES));
696 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
697 "max_duration", &e->max_duration))
699 fprintf (stderr, "Invalid %s", "max_duration");
704 fprintf (stderr, "Experiment duration: `%s'\n",
705 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
707 load_episodes (e, cfg);
708 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
710 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
712 GNUNET_CONFIGURATION_destroy (cfg);
717 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
719 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
721 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
722 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
724 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
726 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
727 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
732 /* end of file ats-testing-experiment.c*/