- restructure
[oweals/gnunet.git] / src / testbed / testbed_api_operations.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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 testbed/testbed_api_operations.c
23  * @brief functions to manage operation queues
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "testbed_api_operations.h"
28
29
30 /**
31  * An entry in the operation queue
32  */
33 struct QueueEntry
34 {
35   /**
36    * The next DLL pointer
37    */
38   struct QueueEntry *next;
39
40   /**
41    * The prev DLL pointer
42    */
43   struct QueueEntry *prev;
44
45   /**
46    * The operation this entry holds
47    */
48   struct GNUNET_TESTBED_Operation *op;
49
50   /**
51    * How many units of resources does the operation need
52    */
53   unsigned int nres;
54 };
55
56
57 /**
58  * Queue of operations where we can only support a certain
59  * number of concurrent operations of a particular type.
60  */
61 struct OperationQueue
62 {
63  /**
64    * The head of the operation queue
65    */
66   struct QueueEntry *head;
67
68   /**
69    * The tail of the operation queue
70    */
71   struct QueueEntry *tail;
72
73   /**
74    * Number of operations that are currently active in this queue.
75    */
76   unsigned int active;
77
78   /**
79    * Max number of operations which can be active at any time in this queue
80    */
81   unsigned int max_active;
82
83 };
84
85
86 /**
87  * Operation state
88  */
89 enum OperationState
90 {
91   /**
92    * The operation is just created and is in initial state
93    */
94   OP_STATE_INIT,
95
96   /**
97    * The operation is currently waiting for resources
98    */
99   OP_STATE_WAITING,
100
101   /**
102    * The operation is ready to be started
103    */
104   OP_STATE_READY,
105
106   /**
107    * The operation has started
108    */
109   OP_STATE_STARTED
110 };
111
112
113 /**
114  * Opaque handle to an abstract operation to be executed by the testing framework.
115  */
116 struct GNUNET_TESTBED_Operation
117 {
118   /**
119    * Function to call when we have the resources to begin the operation.
120    */
121   OperationStart start;
122
123   /**
124    * Function to call to clean up after the operation (which may or may
125    * not have been started yet).
126    */
127   OperationRelease release;
128
129   /**
130    * Closure for callbacks.
131    */
132   void *cb_cls;
133
134   /**
135    * Array of operation queues this Operation belongs to.
136    */
137   struct OperationQueue **queues;
138
139   /**
140    * Array of number resources an operation need from each queue. This numbers
141    * in this array should correspond to the queues array
142    */
143   unsigned int *nres;
144
145   /**
146    * The id of the task which calls OperationStart for this operation
147    */
148   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
149
150   /**
151    * Number of queues in the operation queues array
152    */
153   unsigned int nqueues;
154
155   /**
156    * The state of the operation
157    */
158   enum OperationState state;
159
160 };
161
162
163 /**
164  * Task for calling OperationStart on operation
165  *
166  * @param cls the Operation
167  * @param tc the TaskContext from scheduler
168  */
169 static void
170 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   struct GNUNET_TESTBED_Operation *op = cls;
173
174   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
175   op->state = OP_STATE_STARTED;
176   if (NULL != op->start)
177     op->start (op->cb_cls);
178 }
179
180
181 /**
182  * Checks for the readiness of an operation and schedules a operation start task
183  *
184  * @param op the operation
185  */
186 static void
187 check_readiness (struct GNUNET_TESTBED_Operation *op)
188 {
189   unsigned int i;
190
191   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
192   for (i = 0; i < op->nqueues; i++)
193   {
194     GNUNET_assert (0 < op->nres[i]);
195     if ((op->queues[i]->active + op->nres[i]) > op->queues[i]->max_active)
196       return;
197   }
198   for (i = 0; i < op->nqueues; i++)
199     op->queues[i]->active += op->nres[i];
200   op->state = OP_STATE_READY;
201   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
202 }
203
204
205 /**
206  * Defers a ready to be executed operation back to waiting
207  *
208  * @param op the operation to defer
209  */
210 static void
211 defer (struct GNUNET_TESTBED_Operation *op)
212 {
213   unsigned int i;
214
215   GNUNET_assert (OP_STATE_READY == op->state);
216   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != op->start_task_id);
217   GNUNET_SCHEDULER_cancel (op->start_task_id);
218   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
219   for (i = 0; i < op->nqueues; i++)
220     op->queues[i]->active--;
221   op->state = OP_STATE_WAITING;
222 }
223
224
225 /**
226  * Create an 'operation' to be performed.
227  *
228  * @param cls closure for the callbacks
229  * @param start function to call to start the operation
230  * @param release function to call to close down the operation
231  * @return handle to the operation
232  */
233 struct GNUNET_TESTBED_Operation *
234 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
235                                   OperationRelease release)
236 {
237   struct GNUNET_TESTBED_Operation *op;
238
239   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
240   op->start = start;
241   op->state = OP_STATE_INIT;
242   op->release = release;
243   op->cb_cls = cls;
244   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
245   return op;
246 }
247
248
249 /**
250  * Create an operation queue.
251  *
252  * @param max_active maximum number of operations in this
253  *        queue that can be active in parallel at the same time
254  * @return handle to the queue
255  */
256 struct OperationQueue *
257 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
258 {
259   struct OperationQueue *queue;
260
261   queue = GNUNET_malloc (sizeof (struct OperationQueue));
262   queue->max_active = max_active;
263   return queue;
264 }
265
266
267 /**
268  * Destroy an operation queue.  The queue MUST be empty
269  * at this time.
270  *
271  * @param queue queue to destroy
272  */
273 void
274 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
275 {
276   GNUNET_break (NULL == queue->head);
277   GNUNET_break (NULL == queue->tail);
278   GNUNET_free (queue);
279 }
280
281
282 /**
283  * Destroys the operation queue if it is empty.  If not empty return GNUNET_NO.
284  *
285  * @param queue the queue to destroy if empty
286  * @return GNUNET_YES if the queue is destroyed.  GNUNET_NO if not (because it
287  *           is not empty)
288  */
289 int
290 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
291 {
292   if (NULL != queue->head)
293     return GNUNET_NO;
294   GNUNET_TESTBED_operation_queue_destroy_ (queue);
295   return GNUNET_YES;
296 }
297
298
299 /**
300  * Function to reset the maximum number of operations in the given queue. If
301  * max_active is lesser than the number of currently active operations, the
302  * active operations are not stopped immediately.
303  *
304  * @param queue the operation queue which has to be modified
305  * @param max_active the new maximum number of active operations
306  */
307 void
308 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
309                                                   unsigned int max_active)
310 {
311   struct QueueEntry *entry;
312
313   queue->max_active = max_active;
314   /* if (queue->active >= queue->max_active) */
315   /*   return; */
316
317   entry = queue->head;
318   while ((queue->active > queue->max_active) && (NULL != entry))
319   {
320     if (entry->op->state == OP_STATE_READY)
321       defer (entry->op);
322     entry = entry->next;
323   }
324
325   entry = queue->head;
326   while ((NULL != entry) && (queue->active < queue->max_active))
327   {
328     if (OP_STATE_WAITING == entry->op->state)
329       check_readiness (entry->op);
330     entry = entry->next;
331   }
332 }
333
334
335 /**
336  * Add an operation to a queue.  An operation can be in multiple queues at
337  * once. Once the operation is inserted into all the queues
338  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
339  * waiting for the operation to become active.
340  *
341  * @param queue queue to add the operation to
342  * @param operation operation to add to the queue
343  * @param nres the number of units of the resources of queue needed by the
344  *          operation. Should be greater than 0.
345  */
346 void
347 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
348                                          struct GNUNET_TESTBED_Operation
349                                          *operation, unsigned int nres)
350 {
351   struct QueueEntry *entry;
352   unsigned int qsize;
353
354   GNUNET_assert (0 < nres);
355   entry = GNUNET_malloc (sizeof (struct QueueEntry));
356   entry->op = operation;
357   entry->nres = nres;
358   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
359   qsize = operation->nqueues;
360   GNUNET_array_append (operation->queues, operation->nqueues, queue);
361   GNUNET_array_append (operation->nres, qsize, nres);
362   GNUNET_assert (qsize == operation->nqueues);
363 }
364
365
366 /**
367  * Add an operation to a queue.  An operation can be in multiple queues at
368  * once. Once the operation is inserted into all the queues
369  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
370  * waiting for the operation to become active. The operation is assumed to take
371  * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
372  * requires more than 1
373  *
374  * @param queue queue to add the operation to
375  * @param operation operation to add to the queue
376  */
377 void
378 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
379                                         struct GNUNET_TESTBED_Operation
380                                         *operation)
381 {
382   return GNUNET_TESTBED_operation_queue_insert2_ (queue, operation, 1);
383 }
384
385
386 /**
387  * Marks the given operation as waiting on the queues.  Once all queues permit
388  * the operation to become active, the operation will be activated.  The actual
389  * activation will occur in a separate task (thus allowing multiple queue
390  * insertions to be made without having the first one instantly trigger the
391  * operation if the first queue has sufficient resources).
392  *
393  * @param operation the operation to marks as waiting
394  */
395 void
396 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
397                                       *operation)
398 {
399   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
400   operation->state = OP_STATE_WAITING;
401   check_readiness (operation);
402 }
403
404
405 /**
406  * Remove an operation from a queue.  This can be because the
407  * oeration was active and has completed (and the resources have
408  * been released), or because the operation was cancelled and
409  * thus scheduling the operation is no longer required.
410  *
411  * @param queue queue to add the operation to
412  * @param operation operation to add to the queue
413  */
414 void
415 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
416                                         struct GNUNET_TESTBED_Operation
417                                         *operation)
418 {
419   struct QueueEntry *entry;
420   struct QueueEntry *entry2;
421
422   for (entry = queue->head; NULL != entry; entry = entry->next)
423     if (entry->op == operation)
424       break;
425   GNUNET_assert (NULL != entry);
426   GNUNET_assert (0 < entry->nres);
427   switch (operation->state)
428   {
429   case OP_STATE_INIT:
430   case OP_STATE_WAITING:
431     break;
432   case OP_STATE_READY:
433   case OP_STATE_STARTED:
434     GNUNET_assert (0 != queue->active);
435     GNUNET_assert (queue->active >= entry->nres);
436     queue->active -= entry->nres;
437     break;
438   }
439   entry2 = entry->next;
440   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
441   GNUNET_free (entry);
442   for (; NULL != entry2; entry2 = entry2->next)
443     if (OP_STATE_WAITING == entry2->op->state)
444       break;
445   if (NULL == entry2)
446     return;
447   check_readiness (entry2->op);
448 }
449
450
451 /**
452  * An operation is 'done' (was cancelled or finished); remove
453  * it from the queues and release associated resources.
454  *
455  * @param operation operation that finished
456  */
457 void
458 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
459 {
460   unsigned int i;
461
462   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
463   {
464     GNUNET_SCHEDULER_cancel (operation->start_task_id);
465     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
466   }
467   for (i = 0; i < operation->nqueues; i++)
468     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
469   GNUNET_free (operation->queues);
470   GNUNET_free (operation->nres);
471   if (NULL != operation->release)
472     operation->release (operation->cb_cls);
473   GNUNET_free (operation);
474 }
475
476
477 /* end of testbed_api_operations.c */