2 This file is part of GNUnet.
3 (C) 2009 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.
22 * @file experimentation/gnunet-daemon-experimentation_scheduler.c
23 * @brief experimentation daemon: execute experiments
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet-daemon-experimentation.h"
35 * An experiment is added during startup as not running NOT_RUNNING
37 * The scheduler then decides to schedule it and sends a request to the
38 * remote peer, if core cannot send since it is busy we wait for some time
39 * and change state to BUSY, if we can send we change to REQUESTED and wait
40 * for remote peers ACK.
42 * When we receive an ACK we change to STARTED and when scheduler decides that
43 * the experiment is finished we change to STOPPED.
48 /* Experiment is added and waiting to be executed */
50 /* Cannot send request to remote peer, core is busy*/
52 /* We requested experiment and wait for remote peer to ACK */
54 /* Experiment is running */
56 /* Experiment is done */
60 struct ScheduledExperiment {
61 struct ScheduledExperiment *next;
62 struct ScheduledExperiment *prev;
68 GNUNET_SCHEDULER_TaskIdentifier task;
71 struct ScheduledExperiment *waiting_in_head;
72 struct ScheduledExperiment *waiting_in_tail;
74 struct ScheduledExperiment *running_in_head;
75 struct ScheduledExperiment *running_in_tail;
77 struct ScheduledExperiment *waiting_out_head;
78 struct ScheduledExperiment *waiting_out_tail;
80 struct ScheduledExperiment *running_out_head;
81 struct ScheduledExperiment *running_out_tail;
84 static unsigned int experiments_scheduled;
85 static unsigned int experiments_outbound_running;
86 static unsigned int experiments_inbound_running;
87 static unsigned int experiments_requested;
90 static struct ScheduledExperiment *
91 find_experiment (struct ScheduledExperiment *head, struct ScheduledExperiment *tail,
92 struct Node *n, struct Experiment *e, int outbound)
94 struct ScheduledExperiment *cur;
95 for (cur = head; NULL != cur; cur = cur->next)
97 if ((cur->n == n) && (cur->e == e) && (cur->outbound == outbound)) /* Node and experiment are equal */
104 request_timeout (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
106 struct ScheduledExperiment *se = cls;
107 se->task = GNUNET_SCHEDULER_NO_TASK;
109 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Peer `%s' did not respond to request for experiment `%s'\n"),
110 GNUNET_i2s (&se->n->id), se->e->name);
112 GNUNET_CONTAINER_DLL_remove (waiting_out_head, waiting_out_tail, se);
115 /* Remove experiment */
116 GNUNET_assert (experiments_requested > 0);
117 experiments_requested --;
118 GNUNET_STATISTICS_set (GED_stats, "# experiments requested", experiments_requested, GNUNET_NO);
121 static void run_experiment_inbound (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
123 struct ScheduledExperiment *se = cls;
124 struct GNUNET_TIME_Relative start;
125 struct GNUNET_TIME_Relative end;
127 se->task = GNUNET_SCHEDULER_NO_TASK;
131 /* Send START_ACK message */
132 GED_nodes_send_start_ack (se->n, se->e);
133 se->state = REQUESTED;
134 /* Schedule to run */
135 start = GNUNET_TIME_absolute_get_remaining(se->e->start);
136 if (0 == start.rel_value_us)
137 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_inbound, se);
139 se->task = GNUNET_SCHEDULER_add_delayed (start, &run_experiment_inbound, se);
142 experiments_inbound_running ++;
143 GNUNET_STATISTICS_set (GED_stats, "# experiments inbound running", experiments_inbound_running, GNUNET_NO);
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting inbound experiment `%s' with peer `%s'\n"),
145 se->e->name, GNUNET_i2s (&se->n->id));
147 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_inbound, se);
150 /* Experiment is running */
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running %s experiment `%s' peer for `%s'\n",
152 "inbound", GNUNET_i2s (&se->n->id), se->e->name);
157 end = GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add (se->e->stop, se->e->frequency));
158 if (0 == end.rel_value_us)
161 return; /* End of experiment is reached */
164 se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &run_experiment_inbound, se);
167 /* Experiment expired */
175 static void run_experiment_outbound (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
177 struct ScheduledExperiment *se = cls;
178 struct GNUNET_TIME_Relative end;
180 se->task = GNUNET_SCHEDULER_NO_TASK;
184 /* Send START message */
185 GED_nodes_send_start (se->n, se->e);
186 se->state = REQUESTED;
187 se->task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &request_timeout, se);
188 experiments_requested ++;
189 GNUNET_STATISTICS_set (GED_stats, "# experiments requested", experiments_requested, GNUNET_NO);
192 /* Expecting START_ACK */
196 /* Experiment is running */
197 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running %s experiment `%s' peer for `%s'\n",
198 "outbound", GNUNET_i2s (&se->n->id), se->e->name);
203 end = GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add (se->e->stop, se->e->frequency));
204 if (0 == end.rel_value_us)
207 return; /* End of experiment is reached */
210 se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &run_experiment_outbound, se);
213 /* Experiment expired */
222 * Handle a START message from a remote node
225 * @param e the experiment
228 GED_scheduler_handle_start (struct Node *n, struct Experiment *e)
230 if ((NULL != find_experiment (waiting_in_head, waiting_in_tail, n, e, GNUNET_NO)) ||
231 (NULL != find_experiment (running_in_head, running_in_tail, n, e, GNUNET_NO)))
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Received %s message from peer %s for experiment `%s'\n",
239 "START", GNUNET_i2s (&n->id), e->name);
240 GED_scheduler_add (n, e, GNUNET_NO);
245 * Handle a START_ACK message from a remote node
248 * @param e the experiment
251 GED_scheduler_handle_start_ack (struct Node *n, struct Experiment *e)
253 struct ScheduledExperiment *se;
255 if (NULL == (se = find_experiment (waiting_out_head, waiting_out_tail, n, e, GNUNET_YES)))
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for requested experiment `%s'\n",
262 "START_ACK", GNUNET_i2s (&n->id), e->name);
264 if (GNUNET_SCHEDULER_NO_TASK != se->task)
266 GNUNET_SCHEDULER_cancel (se->task); /* *Canceling timeout task */
267 se->task = GNUNET_SCHEDULER_NO_TASK;
270 /* Remove from waiting list, add to running list */
271 GNUNET_CONTAINER_DLL_remove (waiting_out_head, waiting_out_tail, se);
272 GNUNET_CONTAINER_DLL_insert (running_out_head, running_out_tail, se);
274 /* Change state and schedule to run */
275 experiments_outbound_running ++;
276 GNUNET_STATISTICS_set (GED_stats, "# experiments outbound running", experiments_outbound_running, GNUNET_NO);
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting outbound experiment `%s' with peer `%s'\n"),
278 e->name, GNUNET_i2s (&n->id));
280 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_outbound, se);
285 * Handle a STOP message from a remote node
288 * @param e the experiment
291 GED_scheduler_handle_stop (struct Node *n, struct Experiment *e)
293 struct ScheduledExperiment *se;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Received %s message from peer %s for experiment `%s'\n"),
296 "STOP", GNUNET_i2s (&n->id), e->name);
298 if (NULL != (se = find_experiment (waiting_in_head, waiting_in_tail, n, e, GNUNET_NO)))
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for waiting experiment `%s'\n",
301 "STOP", GNUNET_i2s (&n->id), e->name);
304 if (NULL != (se = find_experiment (running_in_head, running_in_tail, n, e, GNUNET_NO)))
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for running experiment `%s'\n",
307 "STOP", GNUNET_i2s (&n->id), e->name);
313 * Add a new experiment for a node
316 * @param e the experiment
317 * @param outbound are we initiator (GNUNET_YES) or client (GNUNET_NO)?
320 GED_scheduler_add (struct Node *n, struct Experiment *e, int outbound)
322 struct ScheduledExperiment *se;
323 struct GNUNET_TIME_Relative start;
324 struct GNUNET_TIME_Relative end;
326 GNUNET_assert ((GNUNET_YES == outbound) || (GNUNET_NO == outbound));
328 start = GNUNET_TIME_absolute_get_remaining(e->start);
329 end = GNUNET_TIME_absolute_get_remaining(e->stop);
330 if (0 == end.rel_value_us)
331 return; /* End of experiment is reached */
333 /* Add additional checks here if required */
334 se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
335 se->state = NOT_RUNNING;
336 se->outbound = outbound;
340 if (GNUNET_YES == outbound)
342 if (0 == start.rel_value_us)
343 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_outbound, se);
345 se->task = GNUNET_SCHEDULER_add_delayed (start, &run_experiment_outbound, se);
346 GNUNET_CONTAINER_DLL_insert (waiting_out_head, waiting_out_tail, se);
350 if (0 == start.rel_value_us)
351 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_inbound, se);
353 se->task = GNUNET_SCHEDULER_add_delayed (start, &run_experiment_inbound, se);
354 GNUNET_CONTAINER_DLL_insert (waiting_in_head, waiting_in_tail, se);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added %s experiment `%s' for node to be scheduled\n",
358 (GNUNET_YES == outbound) ? "outbound" : "inbound", e->name, GNUNET_i2s(&se->n->id));
359 experiments_scheduled ++;
360 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
365 * Start the scheduler component
368 GED_scheduler_start ()
370 experiments_requested = 0;
371 experiments_scheduled = 0;
376 * Stop the scheduler component
379 GED_scheduler_stop ()
381 struct ScheduledExperiment *cur;
382 struct ScheduledExperiment *next;
384 next = waiting_in_head;
385 while (NULL != (cur = next))
388 GNUNET_CONTAINER_DLL_remove (waiting_in_head, waiting_in_tail, cur);
389 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
391 GNUNET_SCHEDULER_cancel (cur->task);
392 cur->task = GNUNET_SCHEDULER_NO_TASK;
395 GNUNET_assert (experiments_scheduled > 0);
396 experiments_scheduled --;
397 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
400 next = running_in_head;
401 while (NULL != (cur = next))
404 GNUNET_CONTAINER_DLL_remove (running_in_head, running_in_tail, cur);
405 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
407 GNUNET_SCHEDULER_cancel (cur->task);
408 cur->task = GNUNET_SCHEDULER_NO_TASK;
411 GNUNET_assert (experiments_outbound_running > 0);
412 experiments_inbound_running --;
413 GNUNET_STATISTICS_set (GED_stats, "# experiments inbound running", experiments_inbound_running, GNUNET_NO);
416 next = waiting_out_head;
417 while (NULL != (cur = next))
420 GNUNET_CONTAINER_DLL_remove (waiting_out_head, waiting_out_tail, cur);
421 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
423 GNUNET_SCHEDULER_cancel (cur->task);
424 cur->task = GNUNET_SCHEDULER_NO_TASK;
427 GNUNET_assert (experiments_scheduled > 0);
428 experiments_scheduled --;
429 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
432 next = running_out_head;
433 while (NULL != (cur = next))
436 GNUNET_CONTAINER_DLL_remove (running_out_head, running_out_tail, cur);
437 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
439 GNUNET_SCHEDULER_cancel (cur->task);
440 cur->task = GNUNET_SCHEDULER_NO_TASK;
443 GNUNET_assert (experiments_outbound_running > 0);
444 experiments_outbound_running --;
445 GNUNET_STATISTICS_set (GED_stats, "# experiments outbound running", experiments_outbound_running, GNUNET_NO);
449 /* end of gnunet-daemon-experimentation_scheduler.c */