refactoring since names are too long
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_scheduler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file experimentation/gnunet-daemon-experimentation_scheduler.c
23  * @brief experimentation daemon: execute experiments
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
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"
33
34 /**
35  * An experiment is added during startup as not running NOT_RUNNING
36  *
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.
41  *
42  * When we receive an ACK we change to STARTED and when scheduler decides that
43  * the experiment is finished we change to STOPPED.
44  */
45
46 enum ExperimentState
47 {
48         /* Experiment is added and waiting to be executed */
49         NOT_RUNNING,
50         /* Cannot send request to remote peer, core is busy*/
51         BUSY,
52         /* We requested experiment and wait for remote peer to ACK */
53         REQUESTED,
54         /* Experiment is running */
55         STARTED,
56         /* Experiment is done */
57         STOPPED
58 };
59
60 struct ScheduledExperiment {
61         struct ScheduledExperiment *next;
62         struct ScheduledExperiment *prev;
63
64         struct Experiment *e;
65         struct Node *n;
66         int state;
67         GNUNET_SCHEDULER_TaskIdentifier task;
68 };
69
70 struct ScheduledExperiment *waiting_head;
71 struct ScheduledExperiment *waiting_tail;
72
73 struct ScheduledExperiment *running_head;
74 struct ScheduledExperiment *running_tail;
75
76 static unsigned int experiments_scheduled;
77 static unsigned int experiments_running;
78 static unsigned int experiments_requested;
79
80 static void
81 request_timeout (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
82 {
83         struct ScheduledExperiment *se = cls;
84         se->task = GNUNET_SCHEDULER_NO_TASK;
85
86         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer `%s' did not respond to request for experiment `%s'\n",
87                         GNUNET_i2s (&se->n->id), se->e->name);
88
89         GNUNET_CONTAINER_DLL_remove (waiting_head, waiting_tail, se);
90         GNUNET_free (se);
91
92         /* Remove experiment */
93
94         GNUNET_assert (experiments_requested > 0);
95         experiments_requested --;
96         GNUNET_STATISTICS_set (GSE_stats, "# experiments requested", experiments_requested, GNUNET_NO);
97 }
98
99 static void start_experiment (void *cls,const struct GNUNET_SCHEDULER_TaskContext* tc)
100 {
101         struct ScheduledExperiment *se = cls;
102         struct GNUNET_TIME_Relative end;
103         struct GNUNET_TIME_Relative backoff;
104
105         se->task = GNUNET_SCHEDULER_NO_TASK;
106
107         if (GNUNET_NO == GED_nodes_rts (se->n))
108         {
109                 se->state = BUSY;
110                 backoff = GNUNET_TIME_UNIT_SECONDS;
111                 backoff.rel_value += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000);
112                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying start request to peer `%s' for `%s' for %llu ms\n",
113                                 GNUNET_i2s (&se->n->id), se->e->name, (unsigned long long) backoff.rel_value);
114                 se->task = GNUNET_SCHEDULER_add_delayed (backoff, &start_experiment, se);
115                 return;
116         }
117         else if (BUSY == se->state)
118                 se->state = NOT_RUNNING;
119
120         if (NOT_RUNNING == se->state)
121         {
122                         /* Send start message */
123                         GED_nodes_request_start (se->n, se->e);
124                         se->state = REQUESTED;
125                         se->task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &request_timeout, se);
126
127                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending start request to peer `%s' for `%s'\n",
128                                         GNUNET_i2s (&se->n->id), se->e->name);
129                         experiments_requested ++;
130                         GNUNET_STATISTICS_set (GSE_stats, "# experiments requested", experiments_requested, GNUNET_NO);
131                         return;
132         }
133         else if (REQUESTED == se->state)
134         {
135                         /* Already requested */
136                         return;
137         }
138         else if (STARTED == se->state)
139         {
140                         /* Experiment is running */
141                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running experiment `%s' peer for `%s'\n",
142                                         GNUNET_i2s (&se->n->id), se->e->name);
143
144                         /* do work here */
145
146                         /* Reschedule */
147                         end = GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add (se->e->stop, se->e->frequency));
148                         if (0 == end.rel_value)
149                         {
150                                 se->state = STOPPED;
151                                 return; /* End of experiment is reached */
152                         }
153                         /* Reschedule */
154                 se->task = GNUNET_SCHEDULER_add_delayed (se->e->frequency, &start_experiment, se);
155         }
156
157         else if (STOPPED == se->state)
158         {
159                         /* Experiment expired */
160         }
161 }
162
163 /**
164  * Handle a START message from a remote node
165  *
166  * @param n the node
167  * @param e the experiment
168  */
169 void
170 GED_scheduler_handle_start (struct Node *n, struct Experiment *e)
171 {
172
173 }
174
175 /**
176  * Handle a START_ACL message from a remote node
177  *
178  * @param n the node
179  * @param e the experiment
180  */
181 void
182 GED_scheduler_handle_start_ack (struct Node *n, struct Experiment *e)
183 {
184
185 }
186
187
188 /**
189  * Handle a STOP message from a remote node
190  *
191  * @param n the node
192  * @param e the experiment
193  */
194 void
195 GED_scheduler_handle_stop (struct Node *n, struct Experiment *e)
196 {
197
198 }
199
200 /**
201  * Add a new experiment for a node
202  *
203  * @param n the node
204  * @param e the experiment
205  */
206 void
207 GED_scheduler_add (struct Node *n, struct Experiment *e)
208 {
209         struct ScheduledExperiment *se;
210         struct GNUNET_TIME_Relative start;
211         struct GNUNET_TIME_Relative end;
212
213         start = GNUNET_TIME_absolute_get_remaining(e->start);
214         end = GNUNET_TIME_absolute_get_remaining(e->stop);
215         if (0 == end.rel_value)
216                         return; /* End of experiment is reached */
217
218         /* Add additional checks here if required */
219
220         se = GNUNET_malloc (sizeof (struct ScheduledExperiment));
221         se->state = NOT_RUNNING;
222         se->e = e;
223         se->n = n;
224         if (0 == start.rel_value)
225                         se->task = GNUNET_SCHEDULER_add_now (&start_experiment, se);
226         else
227                         se->task = GNUNET_SCHEDULER_add_delayed (start, &start_experiment, se);
228
229         GNUNET_CONTAINER_DLL_insert (waiting_head, waiting_tail, se);
230         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Added experiment `%s' for node to be scheduled\n",
231                         e->name, GNUNET_i2s(&se->n->id));
232         experiments_scheduled ++;
233         GNUNET_STATISTICS_set (GSE_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
234 }
235
236 /**
237  * Start the scheduler component
238  */
239 void
240 GED_scheduler_start ()
241 {
242         experiments_requested = 0;
243         experiments_scheduled = 0;
244 }
245
246
247 /**
248  * Stop the scheduler component
249  */
250 void
251 GED_scheduler_stop ()
252 {
253         struct ScheduledExperiment *cur;
254         struct ScheduledExperiment *next;
255
256         next = waiting_head;
257         while (NULL != (cur = next))
258         {
259                         next = cur->next;
260                         GNUNET_CONTAINER_DLL_remove (waiting_head, waiting_tail, cur);
261                         if (GNUNET_SCHEDULER_NO_TASK != cur->task)
262                         {
263                                         GNUNET_SCHEDULER_cancel (cur->task);
264                                         cur->task = GNUNET_SCHEDULER_NO_TASK;
265                         }
266                         GNUNET_free (cur);
267                         GNUNET_assert (experiments_scheduled > 0);
268                         experiments_scheduled --;
269                         GNUNET_STATISTICS_set (GSE_stats, "# experiments scheduled", experiments_scheduled, GNUNET_NO);
270         }
271
272         next = running_head;
273         while (NULL != (cur = next))
274         {
275                         next = cur->next;
276                         GNUNET_CONTAINER_DLL_remove (running_head, running_tail, cur);
277                         if (GNUNET_SCHEDULER_NO_TASK != cur->task)
278                         {
279                                         GNUNET_SCHEDULER_cancel (cur->task);
280                                         cur->task = GNUNET_SCHEDULER_NO_TASK;
281                         }
282                         GNUNET_free (cur);
283                         GNUNET_assert (experiments_running > 0);
284                         experiments_running --;
285                         GNUNET_STATISTICS_set (GSE_stats, "# experiments running", experiments_running, GNUNET_NO);
286         }
287 }
288
289 /* end of gnunet-daemon-experimentation_scheduler.c */