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)
41 return "SET_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 Operation *cur_o;
68 struct 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)
98 fprintf (stderr, "Parsing episode %u\n",cur->id);
99 GNUNET_asprintf(&sec_name, "episode-%u", cur->id);
104 GNUNET_asprintf(&op_name, "op-%u-operation", ep_counter);
105 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg,
106 sec_name, op_name, &op))
110 o = GNUNET_new (struct Operation);
111 /* operations = set_rate, start_send, stop_send, set_preference */
112 if (0 == strcmp (op, "start_send"))
114 o->type = START_SEND;
116 else if (0 == strcmp (op, "stop_send"))
120 else if (0 == strcmp (op, "set_rate"))
124 else if (0 == strcmp (op, "set_preference"))
126 o->type = SET_PREFERENCE;
130 fprintf (stderr, "Invalid operation %u `%s' in episode %u\n",
131 ep_counter, op, cur->id);
133 return GNUNET_SYSERR;
136 GNUNET_free (op_name);
137 GNUNET_asprintf(&op_name, "op-%u-src", ep_counter);
138 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
139 sec_name, op_name, &o->src_id))
141 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
142 ep_counter, op, cur->id);
144 return GNUNET_SYSERR;
146 if (o->src_id > e->num_masters)
148 fprintf (stderr, "Invalid src %llu in operation %u `%s' in episode %u\n",
149 o->src_id, ep_counter, op, cur->id);
151 return GNUNET_SYSERR;
154 GNUNET_free (op_name);
155 GNUNET_asprintf(&op_name, "op-%u-dest", ep_counter);
156 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
157 sec_name, op_name, &o->dest_id))
159 fprintf (stderr, "Missing src in operation %u `%s' in episode %u\n",
160 ep_counter, op, cur->id);
162 return GNUNET_SYSERR;
164 if (o->dest_id > e->num_slaves)
166 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
167 o->dest_id, ep_counter, op, cur->id);
169 return GNUNET_SYSERR;
173 GNUNET_free (op_name);
174 GNUNET_asprintf(&op_name, "op-%u-value", ep_counter);
175 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
176 sec_name, op_name, &o->value))
178 fprintf (stderr, "Missing value in operation %u `%s' in episode %u\n",
179 ep_counter, op, cur->id);
181 return GNUNET_SYSERR;
183 if (o->dest_id > e->num_slaves)
185 fprintf (stderr, "Invalid destination %llu in operation %u `%s' in episode %u\n",
186 o->dest_id, ep_counter, op, cur->id);
188 return GNUNET_SYSERR;
191 fprintf (stderr, "Found operation %u in episode %u: %s [%llu]->[%llu] == %llu\n",
192 ep_counter, cur->id, print_op (o->type), o->src_id, o->dest_id, o->value);
194 GNUNET_CONTAINER_DLL_insert (cur->head,cur->tail, o);
195 GNUNET_free (op_name);
198 GNUNET_free (sec_name);
204 load_episodes (struct Experiment *e, struct GNUNET_CONFIGURATION_Handle *cfg)
208 struct GNUNET_TIME_Relative e_duration;
210 struct Episode *last;
216 GNUNET_asprintf(&sec_name, "episode-%u", e_counter);
217 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg,
218 sec_name, "duration", &e_duration))
220 GNUNET_free (sec_name);
224 cur = GNUNET_new (struct Episode);
225 cur->duration = e_duration;
228 if (GNUNET_OK != load_episode (e, cur, cfg))
230 GNUNET_free (sec_name);
232 return GNUNET_SYSERR;
235 fprintf (stderr, "Found episode %u with duration %s \n",
237 GNUNET_STRINGS_relative_time_to_string(cur->duration, GNUNET_YES));
239 /* Update experiment */
241 e->total_duration = GNUNET_TIME_relative_add(e->total_duration, cur->duration);
242 /* Put in linked list */
248 GNUNET_free (sec_name);
256 timeout_experiment (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
258 struct Experiment *e = cls;
259 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
260 fprintf (stderr, "Experiment timeout!\n");
262 if (GNUNET_SCHEDULER_NO_TASK != e->episode_timeout_task)
264 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
265 GNUNET_SCHEDULER_cancel (e->episode_timeout_task);
268 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
273 enforce_start_send (struct Operation *op)
279 enforce_stop_send (struct Operation *op)
285 enforce_set_rate (struct Operation *op)
291 enforce_set_preference (struct Operation *op)
296 static void enforce_episode (struct Episode *ep)
298 struct Operation *cur;
299 for (cur = ep->head; NULL != cur; cur = cur->next)
302 fprintf (stderr, "Enforcing operation: %s [%llu]->[%llu] == %llu\n",
303 print_op (cur->type), cur->src_id, cur->dest_id, cur->value);
306 enforce_start_send (cur);
309 enforce_stop_send (cur);
312 enforce_set_rate (cur);
315 enforce_set_preference (cur);
324 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
326 struct Experiment *e = cls;
327 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
328 if (NULL != e->ep_done_cb)
329 e->ep_done_cb (e->cur);
331 /* Scheduling next */
332 e->cur = e->cur->next;
336 fprintf (stderr, "Last episode done!\n");
337 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
339 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
340 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
342 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
346 fprintf (stderr, "Running episode %u with timeout %s\n",
348 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
349 enforce_episode(e->cur);
351 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
352 &timeout_episode, e);
357 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
358 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
359 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
361 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
362 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
363 e->e_done_cb = e_done_cb;
364 e->ep_done_cb = ep_done_cb;
365 e->start_time = GNUNET_TIME_absolute_get();
367 /* Start total time out */
368 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
369 &timeout_experiment, e);
373 fprintf (stderr, "Running episode %u with timeout %s\n",
375 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
376 enforce_episode(e->cur);
377 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
378 &timeout_episode, e);
385 GNUNET_ATS_TEST_experimentation_load (char *filename)
387 struct Experiment *e;
388 struct GNUNET_CONFIGURATION_Handle *cfg;
391 cfg = GNUNET_CONFIGURATION_create();
392 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
394 fprintf (stderr, "Failed to load `%s'\n", filename);
395 GNUNET_CONFIGURATION_destroy (cfg);
399 e = create_experiment ();
401 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
404 fprintf (stderr, "Invalid %s", "name");
409 fprintf (stderr, "Experiment name: `%s'\n", e->name);
411 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
412 "cfg_file", &e->cfg_file))
414 fprintf (stderr, "Invalid %s", "cfg_file");
419 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
421 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
422 "masters", &e->num_masters))
424 fprintf (stderr, "Invalid %s", "masters");
429 fprintf (stderr, "Experiment masters: `%llu'\n",
432 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
433 "slaves", &e->num_slaves))
435 fprintf (stderr, "Invalid %s", "slaves");
440 fprintf (stderr, "Experiment slaves: `%llu'\n",
443 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
444 "max_duration", &e->max_duration))
446 fprintf (stderr, "Invalid %s", "max_duration");
451 fprintf (stderr, "Experiment duration: `%s'\n",
452 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
454 load_episodes (e, cfg);
455 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
457 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
459 GNUNET_CONFIGURATION_destroy (cfg);
464 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
466 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
468 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
469 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
474 /* end of file ats-testing-experiment.c*/