2 This file is part of GNUnet
3 (C) 2008--2012 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 testbed/testbed_api_operations.c
23 * @brief functions to manage operation queues
24 * @author Christian Grothoff
27 #include "testbed_api_operations.h"
31 * An entry in the operation queue
36 * The next DLL pointer
38 struct QueueEntry *next;
41 * The prev DLL pointer
43 struct QueueEntry *prev;
46 * The operation this entry holds
48 struct GNUNET_TESTBED_Operation *op;
51 * How many units of resources does the operation need
58 * Queue of operations where we can only support a certain
59 * number of concurrent operations of a particular type.
64 * DLL head for the wait queue. Operations which are waiting for this
65 * operation queue are put here
67 struct QueueEntry *wq_head;
70 * DLL tail for the wait queue.
72 struct QueueEntry *wq_tail;
75 * DLL head for the ready queue. Operations which are in this operation queue
76 * and are in ready state are put here
78 struct QueueEntry *rq_head;
81 * DLL tail for the ready queue
83 struct QueueEntry *rq_tail;
86 * DLL head for the active queue. Operations which are in this operation
87 * queue and are currently active are put here
89 struct QueueEntry *aq_head;
92 * DLL tail for the active queue.
94 struct QueueEntry *aq_tail;
97 * DLL head for the inactive queue. Operations which are inactive and can be
98 * evicted if the queues it holds are maxed out and another operation begins
101 struct QueueEntry *nq_head;
104 * DLL tail for the inactive queue.
106 struct QueueEntry *nq_tail;
109 * Number of operations that are currently active in this queue.
114 * Max number of operations which can be active at any time in this queue
116 unsigned int max_active;
127 * The operation is just created and is in initial state
132 * The operation is currently waiting for resources
137 * The operation is ready to be started
142 * The operation has started
147 * The operation is inactive. It still holds resources on the operation
148 * queues. However, this operation will be evicted when another operation
149 * requires resources from the maxed out queues this operation is holding
157 * An entry in the ready queue (implemented as DLL)
159 struct ReadyQueueEntry
164 struct ReadyQueueEntry *next;
169 struct ReadyQueueEntry *prev;
172 * The operation associated with this entry
174 struct GNUNET_TESTBED_Operation *op;
179 * Opaque handle to an abstract operation to be executed by the testing framework.
181 struct GNUNET_TESTBED_Operation
184 * Function to call when we have the resources to begin the operation.
186 OperationStart start;
189 * Function to call to clean up after the operation (which may or may
190 * not have been started yet).
192 OperationRelease release;
195 * Closure for callbacks.
200 * Array of operation queues this Operation belongs to.
202 struct OperationQueue **queues;
205 * Array of operation queue entries corresponding to this operation in
206 * operation queues for this operation
208 struct QueueEntry **qentries;
211 * Array of number of resources an operation need from each queue. The numbers
212 * in this array should correspond to the queues array
217 * Entry corresponding to this operation in ready queue. Will be NULL if the
218 * operation is not marked as READY
220 struct ReadyQueueEntry *rq_entry;
223 * Number of queues in the operation queues array
225 unsigned int nqueues;
228 * The state of the operation
230 enum OperationState state;
235 * DLL head for the ready queue
237 struct ReadyQueueEntry *rq_head;
240 * DLL tail for the ready queue
242 struct ReadyQueueEntry *rq_tail;
245 * The id of the task to process the ready queue
247 GNUNET_SCHEDULER_TaskIdentifier process_rq_task_id;
250 remove_queue_entry (struct GNUNET_TESTBED_Operation *op, unsigned int index)
252 struct OperationQueue *opq;
253 struct QueueEntry *entry;
255 opq = op->queues[index];
256 entry = op->qentries[index];
262 case OP_STATE_WAITING:
263 GNUNET_CONTAINER_DLL_remove (opq->wq_head, opq->wq_tail, entry);
266 GNUNET_CONTAINER_DLL_remove (opq->rq_head, opq->rq_tail, entry);
268 case OP_STATE_STARTED:
269 GNUNET_CONTAINER_DLL_remove (opq->aq_head, opq->aq_tail, entry);
271 case OP_STATE_INACTIVE:
272 GNUNET_CONTAINER_DLL_remove (opq->nq_head, opq->nq_tail, entry);
278 change_state (struct GNUNET_TESTBED_Operation *op, enum OperationState state)
280 struct QueueEntry *entry;
281 struct OperationQueue *opq;
285 GNUNET_assert (OP_STATE_INIT != state);
286 GNUNET_assert (NULL != op->queues);
287 GNUNET_assert (NULL != op->nres);
288 GNUNET_assert ((OP_STATE_INIT == op->state) || (NULL != op->qentries));
289 GNUNET_assert (op->state != state);
290 for (cnt = 0; cnt < op->nqueues; cnt++)
292 if (OP_STATE_INIT == op->state)
294 entry = GNUNET_malloc (sizeof (struct QueueEntry));
296 entry->nres = op->nres[cnt];
298 GNUNET_array_append (op->qentries, s, entry);
302 entry = op->qentries[cnt];
303 remove_queue_entry (op, cnt);
305 opq = op->queues[cnt];
311 case OP_STATE_WAITING:
312 GNUNET_CONTAINER_DLL_insert_tail (opq->wq_head, opq->wq_tail, entry);
315 GNUNET_CONTAINER_DLL_insert_tail (opq->rq_head, opq->rq_tail, entry);
317 case OP_STATE_STARTED:
318 GNUNET_CONTAINER_DLL_insert_tail (opq->aq_head, opq->aq_tail, entry);
320 case OP_STATE_INACTIVE:
321 GNUNET_CONTAINER_DLL_insert_tail (opq->nq_head, opq->nq_tail, entry);
330 * Removes an operation from the ready queue. Also stops the 'process_rq_task'
331 * if the given operation is the last one in the queue.
333 * @param op the operation to be removed
336 rq_remove (struct GNUNET_TESTBED_Operation *op)
338 GNUNET_assert (NULL != op->rq_entry);
339 GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
340 GNUNET_free (op->rq_entry);
342 if ( (NULL == rq_head) && (GNUNET_SCHEDULER_NO_TASK != process_rq_task_id) )
344 GNUNET_SCHEDULER_cancel (process_rq_task_id);
345 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
351 * Processes the ready queue by calling the operation start callback of the
352 * operation at the head. The operation is then removed from the queue. The
353 * task is scheduled to run again immediately until no more operations are in
357 * @param tc scheduler task context. Not used.
360 process_rq_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 struct GNUNET_TESTBED_Operation *op;
364 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
365 GNUNET_assert (NULL != rq_head);
366 GNUNET_assert (NULL != (op = rq_head->op));
369 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
370 change_state (op, OP_STATE_STARTED);
371 if (NULL != op->start)
372 op->start (op->cb_cls);
377 * Adds the operation to the ready queue and starts the 'process_rq_task'
379 * @param op the operation to be queued
382 rq_add (struct GNUNET_TESTBED_Operation *op)
384 struct ReadyQueueEntry *rq_entry;
386 GNUNET_assert (NULL == op->rq_entry);
387 rq_entry = GNUNET_malloc (sizeof (struct ReadyQueueEntry));
389 GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
390 op->rq_entry = rq_entry;
391 if (GNUNET_SCHEDULER_NO_TASK == process_rq_task_id)
392 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
397 is_queue_empty (struct OperationQueue *opq)
399 if ( (NULL != opq->wq_head)
400 || (NULL != opq->rq_head)
401 || (NULL != opq->aq_head)
402 || (NULL != opq->nq_head) )
409 decide_capacity (struct OperationQueue *opq,
410 struct QueueEntry *entry,
411 struct GNUNET_TESTBED_Operation ***ops_,
412 unsigned int *n_ops_)
414 struct QueueEntry **evict_entries;
415 struct GNUNET_TESTBED_Operation **ops;
416 struct GNUNET_TESTBED_Operation *op;
418 unsigned int n_evict_entries;
423 GNUNET_assert (NULL != (op = entry->op));
424 GNUNET_assert (0 < (need = entry->nres));
425 GNUNET_assert (opq->active <= opq->max_active);
428 evict_entries = NULL;
431 if ((opq->active + need) <= opq->max_active)
433 deficit = need - (opq->max_active - opq->active);
434 for (entry = opq->nq_head;
435 (0 < deficit) && (NULL != entry);
438 GNUNET_array_append (evict_entries, n_evict_entries, entry);
439 deficit -= entry->nres;
446 for (n_ops = 0; n_ops < n_evict_entries;)
448 op = evict_entries[n_ops]->op;
449 GNUNET_array_append (ops, n_ops, op); /* increments n-ops */
453 GNUNET_free_non_null (evict_entries);
454 if (NULL != ops_) *ops_ = ops;
455 if (NULL != n_ops_) *n_ops_ = n_ops;
459 /* FIXME: improve.. */
461 merge_ops (struct GNUNET_TESTBED_Operation ***old,
463 struct GNUNET_TESTBED_Operation **new,
466 struct GNUNET_TESTBED_Operation **cur;
471 GNUNET_assert (NULL != old);
474 for (i = 0; i < n_new; i++)
476 for (j = 0; j < *n_old; j++)
478 if (new[i] == cur[j])
483 GNUNET_array_append (cur, n_cur, new[j]);
492 * Checks for the readiness of an operation and schedules a operation start task
494 * @param op the operation
497 check_readiness (struct GNUNET_TESTBED_Operation *op)
499 struct GNUNET_TESTBED_Operation **evict_ops;
500 struct GNUNET_TESTBED_Operation **ops;
502 unsigned int n_evict_ops;
505 GNUNET_assert (NULL == op->rq_entry);
506 GNUNET_assert (OP_STATE_WAITING == op->state);
509 for (i = 0; i < op->nqueues; i++)
513 if (GNUNET_NO == decide_capacity (op->queues[i], op->qentries[i],
516 GNUNET_free_non_null (evict_ops);
521 merge_ops (&evict_ops, &n_evict_ops, ops, n_ops);
524 if (NULL != evict_ops)
526 for (i = 0; i < n_evict_ops; i++)
527 GNUNET_TESTBED_operation_release_ (evict_ops[i]);
528 GNUNET_free (evict_ops);
530 /* Evicting the operations should schedule this operation */
531 GNUNET_assert (OP_STATE_READY == op->state);
534 for (i = 0; i < op->nqueues; i++)
535 op->queues[i]->active += op->nres[i];
536 change_state (op, OP_STATE_READY);
542 * Defers a ready to be executed operation back to waiting
544 * @param op the operation to defer
547 defer (struct GNUNET_TESTBED_Operation *op)
551 GNUNET_assert (OP_STATE_READY == op->state);
553 for (i = 0; i < op->nqueues; i++)
554 op->queues[i]->active--;
555 change_state (op, OP_STATE_WAITING);
560 * Create an 'operation' to be performed.
562 * @param cls closure for the callbacks
563 * @param start function to call to start the operation
564 * @param release function to call to close down the operation
565 * @return handle to the operation
567 struct GNUNET_TESTBED_Operation *
568 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
569 OperationRelease release)
571 struct GNUNET_TESTBED_Operation *op;
573 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
575 op->state = OP_STATE_INIT;
576 op->release = release;
583 * Create an operation queue.
585 * @param max_active maximum number of operations in this
586 * queue that can be active in parallel at the same time
587 * @return handle to the queue
589 struct OperationQueue *
590 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
592 struct OperationQueue *queue;
594 queue = GNUNET_malloc (sizeof (struct OperationQueue));
595 queue->max_active = max_active;
601 * Destroy an operation queue. The queue MUST be empty
604 * @param queue queue to destroy
607 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
609 GNUNET_break (GNUNET_YES == is_queue_empty (queue));
615 * Destroys the operation queue if it is empty. If not empty return GNUNET_NO.
617 * @param queue the queue to destroy if empty
618 * @return GNUNET_YES if the queue is destroyed. GNUNET_NO if not (because it
622 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
624 if (GNUNET_NO == is_queue_empty (queue))
626 GNUNET_TESTBED_operation_queue_destroy_ (queue);
632 recheck_waiting (struct OperationQueue *opq)
634 struct QueueEntry *entry;
635 struct QueueEntry *entry2;
637 entry = opq->wq_head;
638 while (NULL != entry)
640 entry2 = entry->next;
641 check_readiness (entry->op);
648 * Function to reset the maximum number of operations in the given queue. If
649 * max_active is lesser than the number of currently active operations, the
650 * active operations are not stopped immediately.
652 * @param queue the operation queue which has to be modified
653 * @param max_active the new maximum number of active operations
656 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
657 unsigned int max_active)
659 struct QueueEntry *entry;
661 queue->max_active = max_active;
662 while ( (queue->active > queue->max_active)
663 && (NULL != (entry = queue->rq_head)) )
665 recheck_waiting (queue);
670 * Add an operation to a queue. An operation can be in multiple queues at
671 * once. Once the operation is inserted into all the queues
672 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
673 * waiting for the operation to become active.
675 * @param queue queue to add the operation to
676 * @param op operation to add to the queue
677 * @param nres the number of units of the resources of queue needed by the
678 * operation. Should be greater than 0.
681 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
682 struct GNUNET_TESTBED_Operation *op,
687 GNUNET_assert (0 < nres);
689 GNUNET_array_append (op->queues, op->nqueues, queue);
690 GNUNET_array_append (op->nres, qsize, nres);
691 GNUNET_assert (qsize == op->nqueues);
696 * Add an operation to a queue. An operation can be in multiple queues at
697 * once. Once the operation is inserted into all the queues
698 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
699 * waiting for the operation to become active. The operation is assumed to take
700 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
701 * requires more than 1
703 * @param queue queue to add the operation to
704 * @param op operation to add to the queue
707 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
708 struct GNUNET_TESTBED_Operation *op)
710 return GNUNET_TESTBED_operation_queue_insert2_ (queue, op, 1);
715 * Marks the given operation as waiting on the queues. Once all queues permit
716 * the operation to become active, the operation will be activated. The actual
717 * activation will occur in a separate task (thus allowing multiple queue
718 * insertions to be made without having the first one instantly trigger the
719 * operation if the first queue has sufficient resources).
721 * @param op the operation to marks as waiting
724 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op)
726 GNUNET_assert (NULL == op->rq_entry);
727 change_state (op, OP_STATE_WAITING);
728 check_readiness (op);
733 * Marks an active operation as inactive - the operation will be kept in a
734 * ready-to-be-released state and continues to hold resources until another
735 * operation contents for them.
737 * @param op the operation to be marked as inactive. The operation start
738 * callback should have been called before for this operation to mark
742 GNUNET_TESTBED_operation_inactivate_ (struct GNUNET_TESTBED_Operation *op)
744 struct OperationQueue **queues;
746 unsigned int nqueues;
749 GNUNET_assert (OP_STATE_STARTED == op->state);
750 change_state (op, OP_STATE_INACTIVE);
751 nqueues = op->nqueues;
752 ms = sizeof (struct OperationQueue *) * nqueues;
753 queues = GNUNET_malloc (ms);
754 GNUNET_assert (NULL != (queues = memcpy (queues, op->queues, ms)));
755 for (i = 0; i < nqueues; i++)
756 recheck_waiting (queues[i]);
757 GNUNET_free (queues);
762 * Marks and inactive operation as active. This fuction should be called to
763 * ensure that the oprelease callback will not be called until it is either
764 * marked as inactive or released.
766 * @param op the operation to be marked as active
769 GNUNET_TESTBED_operation_activate_ (struct GNUNET_TESTBED_Operation *op)
772 GNUNET_assert (OP_STATE_INACTIVE == op->state);
773 change_state (op, OP_STATE_STARTED);
778 * An operation is 'done' (was cancelled or finished); remove
779 * it from the queues and release associated resources.
781 * @param op operation that finished
784 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op)
786 struct QueueEntry *entry;
787 struct OperationQueue *opq;
790 if (OP_STATE_INIT == op->state)
795 if (OP_STATE_READY == op->state)
797 if (OP_STATE_INACTIVE == op->state) /* Activate the operation if inactive */
798 GNUNET_TESTBED_operation_activate_ (op);
799 GNUNET_assert (NULL != op->queues);
800 GNUNET_assert (NULL != op->qentries);
801 for (i = 0; i < op->nqueues; i++)
803 entry = op->qentries[i];
804 remove_queue_entry (op, i);
809 case OP_STATE_INACTIVE:
812 case OP_STATE_WAITING:
815 case OP_STATE_STARTED:
816 GNUNET_assert (0 != opq->active);
817 GNUNET_assert (opq->active >= entry->nres);
818 opq->active -= entry->nres;
819 recheck_waiting (opq);
824 GNUNET_free_non_null (op->qentries);
825 GNUNET_free (op->queues);
826 GNUNET_free (op->nres);
827 if (NULL != op->release)
828 op->release (op->cb_cls);
833 /* end of testbed_api_operations.c */