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 struct ScheduledExperiment *se;
232 if ((NULL != (se = find_experiment (waiting_in_head, waiting_in_tail, n, e, GNUNET_NO))) ||
233 (NULL != (se = find_experiment (running_in_head, running_in_tail, n, e, GNUNET_NO))))
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for experiment `%s'\n",
240 "START", GNUNET_i2s (&n->id), e->name);
242 GED_scheduler_add (n, e, GNUNET_NO);
246 * Handle a START_ACK message from a remote node
249 * @param e the experiment
252 GED_scheduler_handle_start_ack (struct Node *n, struct Experiment *e)
254 struct ScheduledExperiment *se;
256 if (NULL == (se = find_experiment (waiting_out_head, waiting_out_tail, n, e, GNUNET_YES)))
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for requested experiment `%s'\n",
263 "START_ACK", GNUNET_i2s (&n->id), e->name);
265 if (GNUNET_SCHEDULER_NO_TASK != se->task)
267 GNUNET_SCHEDULER_cancel (se->task); /* *Canceling timeout task */
268 se->task = GNUNET_SCHEDULER_NO_TASK;
271 /* Remove from waiting list, add to running list */
272 GNUNET_CONTAINER_DLL_remove (waiting_out_head, waiting_out_tail, se);
273 GNUNET_CONTAINER_DLL_insert (running_out_head, running_out_tail, se);
275 /* Change state and schedule to run */
276 experiments_outbound_running ++;
277 GNUNET_STATISTICS_set (GED_stats, "# experiments outbound running", experiments_outbound_running, GNUNET_NO);
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Starting outbound experiment `%s' with peer `%s'\n"),
279 e->name, GNUNET_i2s (&n->id));
281 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_outbound, se);
286 * Handle a STOP message from a remote node
289 * @param e the experiment
292 GED_scheduler_handle_stop (struct Node *n, struct Experiment *e)
294 struct ScheduledExperiment *se;
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Received %s message from peer %s for experiment `%s'\n"),
297 "STOP", GNUNET_i2s (&n->id), e->name);
299 if (NULL != (se = find_experiment (waiting_in_head, waiting_in_tail, n, e, GNUNET_NO)))
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for waiting experiment `%s'\n",
302 "STOP", GNUNET_i2s (&n->id), e->name);
305 if (NULL != (se = find_experiment (running_in_head, running_in_tail, n, e, GNUNET_NO)))
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s message from peer %s for running experiment `%s'\n",
308 "STOP", GNUNET_i2s (&n->id), e->name);
314 * Add a new experiment for a node
317 * @param e the experiment
318 * @param outbound are we initiator (GNUNET_YES) or client (GNUNET_NO)?
321 GED_scheduler_add (struct Node *n, struct Experiment *e, int outbound)
323 struct ScheduledExperiment *se;
324 struct GNUNET_TIME_Relative start;
325 struct GNUNET_TIME_Relative end;
327 GNUNET_assert ((GNUNET_YES == outbound) || (GNUNET_NO == outbound));
329 start = GNUNET_TIME_absolute_get_remaining(e->start);
330 end = GNUNET_TIME_absolute_get_remaining(e->stop);
331 if (0 == end.rel_value_us)
332 return; /* End of experiment is reached */
334 /* Add additional checks here if required */
335 se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
336 se->state = NOT_RUNNING;
337 se->outbound = outbound;
341 if (GNUNET_YES == outbound)
343 if (0 == start.rel_value_us)
344 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_outbound, se);
346 se->task = GNUNET_SCHEDULER_add_delayed (start, &run_experiment_outbound, se);
347 GNUNET_CONTAINER_DLL_insert (waiting_out_head, waiting_out_tail, se);
351 if (0 == start.rel_value_us)
352 se->task = GNUNET_SCHEDULER_add_now (&run_experiment_inbound, se);
354 se->task = GNUNET_SCHEDULER_add_delayed (start, &run_experiment_inbound, se);
355 GNUNET_CONTAINER_DLL_insert (waiting_in_head, waiting_in_tail, se);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added %s experiment `%s' for node to be scheduled\n",
359 (GNUNET_YES == outbound) ? "outbound" : "inbound", e->name, GNUNET_i2s(&se->n->id));
360 experiments_scheduled ++;
361 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
366 * Start the scheduler component
369 GED_scheduler_start ()
371 experiments_requested = 0;
372 experiments_scheduled = 0;
377 * Stop the scheduler component
380 GED_scheduler_stop ()
382 struct ScheduledExperiment *cur;
383 struct ScheduledExperiment *next;
385 next = waiting_in_head;
386 while (NULL != (cur = next))
389 GNUNET_CONTAINER_DLL_remove (waiting_in_head, waiting_in_tail, cur);
390 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
392 GNUNET_SCHEDULER_cancel (cur->task);
393 cur->task = GNUNET_SCHEDULER_NO_TASK;
396 GNUNET_assert (experiments_scheduled > 0);
397 experiments_scheduled --;
398 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
401 next = running_in_head;
402 while (NULL != (cur = next))
405 GNUNET_CONTAINER_DLL_remove (running_in_head, running_in_tail, cur);
406 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
408 GNUNET_SCHEDULER_cancel (cur->task);
409 cur->task = GNUNET_SCHEDULER_NO_TASK;
412 GNUNET_assert (experiments_outbound_running > 0);
413 experiments_inbound_running --;
414 GNUNET_STATISTICS_set (GED_stats, "# experiments inbound running", experiments_inbound_running, GNUNET_NO);
417 next = waiting_out_head;
418 while (NULL != (cur = next))
421 GNUNET_CONTAINER_DLL_remove (waiting_out_head, waiting_out_tail, cur);
422 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
424 GNUNET_SCHEDULER_cancel (cur->task);
425 cur->task = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_assert (experiments_scheduled > 0);
429 experiments_scheduled --;
430 GNUNET_STATISTICS_set (GED_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
433 next = running_out_head;
434 while (NULL != (cur = next))
437 GNUNET_CONTAINER_DLL_remove (running_out_head, running_out_tail, cur);
438 if (GNUNET_SCHEDULER_NO_TASK != cur->task)
440 GNUNET_SCHEDULER_cancel (cur->task);
441 cur->task = GNUNET_SCHEDULER_NO_TASK;
444 GNUNET_assert (experiments_outbound_running > 0);
445 experiments_outbound_running --;
446 GNUNET_STATISTICS_set (GED_stats, "# experiments outbound running", experiments_outbound_running, GNUNET_NO);
450 /* end of gnunet-daemon-experimentation_scheduler.c */