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 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time),
267 timeout_episode (void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
269 struct Experiment *e = cls;
270 e->episode_timeout_task = GNUNET_SCHEDULER_NO_TASK;
271 e->ep_done_cb (e->cur);
273 /* Scheduling next */
274 e->cur = e->cur->next;
278 fprintf (stderr, "Last episode done!\n");
279 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
281 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
282 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
284 e->e_done_cb (e, GNUNET_TIME_absolute_get_duration(e->start_time), GNUNET_OK);
288 fprintf (stderr, "Running episode %u with timeout %s\n",
290 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
291 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
292 &timeout_episode, e);
297 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
298 GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
299 GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb)
301 fprintf (stderr, "Running experiment `%s' with timeout %s\n", e->name,
302 GNUNET_STRINGS_relative_time_to_string(e->max_duration, GNUNET_YES));
303 e->e_done_cb = e_done_cb;
304 e->ep_done_cb = ep_done_cb;
305 e->start_time = GNUNET_TIME_absolute_get();
307 /* Start total time out */
308 e->experiment_timeout_task = GNUNET_SCHEDULER_add_delayed (e->max_duration,
309 &timeout_experiment, e);
313 fprintf (stderr, "Running episode %u with timeout %s\n",
315 GNUNET_STRINGS_relative_time_to_string(e->cur->duration, GNUNET_YES));
316 e->episode_timeout_task = GNUNET_SCHEDULER_add_delayed (e->cur->duration,
317 &timeout_episode, e);
324 GNUNET_ATS_TEST_experimentation_load (char *filename)
326 struct Experiment *e;
327 struct GNUNET_CONFIGURATION_Handle *cfg;
330 cfg = GNUNET_CONFIGURATION_create();
331 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, filename))
333 fprintf (stderr, "Failed to load `%s'\n", filename);
334 GNUNET_CONFIGURATION_destroy (cfg);
338 e = create_experiment ();
340 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "experiment",
343 fprintf (stderr, "Invalid %s", "name");
348 fprintf (stderr, "Experiment name: `%s'\n", e->name);
350 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_filename (cfg, "experiment",
351 "cfg_file", &e->cfg_file))
353 fprintf (stderr, "Invalid %s", "cfg_file");
358 fprintf (stderr, "Experiment name: `%s'\n", e->cfg_file);
360 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
361 "masters", &e->num_masters))
363 fprintf (stderr, "Invalid %s", "masters");
368 fprintf (stderr, "Experiment masters: `%llu'\n",
371 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg, "experiment",
372 "slaves", &e->num_slaves))
374 fprintf (stderr, "Invalid %s", "slaves");
379 fprintf (stderr, "Experiment slaves: `%llu'\n",
382 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time(cfg, "experiment",
383 "max_duration", &e->max_duration))
385 fprintf (stderr, "Invalid %s", "max_duration");
390 fprintf (stderr, "Experiment duration: `%s'\n",
391 GNUNET_STRINGS_relative_time_to_string (e->max_duration, GNUNET_YES));
393 load_episodes (e, cfg);
394 fprintf (stderr, "Loaded %u episodes with total duration %s\n",
396 GNUNET_STRINGS_relative_time_to_string (e->total_duration, GNUNET_YES));
398 GNUNET_CONFIGURATION_destroy (cfg);
403 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e)
405 if (GNUNET_SCHEDULER_NO_TASK != e->experiment_timeout_task)
407 GNUNET_SCHEDULER_cancel (e->experiment_timeout_task);
408 e->experiment_timeout_task = GNUNET_SCHEDULER_NO_TASK;
413 /* end of file ats-testing-experiment.c*/