2 This file is part of GNUnet
3 (C) 2008--2013 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
25 * @author Sree Harsha Totakura
29 #include "testbed_api_operations.h"
30 #include "testbed_api_sd.h"
34 * An entry in the operation queue
39 * The next DLL pointer
41 struct QueueEntry *next;
44 * The prev DLL pointer
46 struct QueueEntry *prev;
49 * The operation this entry holds
51 struct GNUNET_TESTBED_Operation *op;
54 * How many units of resources does the operation need
61 * Queue of operations where we can only support a certain
62 * number of concurrent operations of a particular type.
64 struct OperationQueue;
68 * A slot to record time taken by an operation
75 struct TimeSlot *next;
80 struct TimeSlot *prev;
83 * This operation queue to which this time slot belongs to
85 struct OperationQueue *queue;
88 * The operation to which this timeslot is currently allocated to
90 struct GNUNET_TESTBED_Operation *op;
95 struct GNUNET_TIME_Relative tsum;
98 * Number of timing values accumulated
105 * Context for operation queues of type OPERATION_QUEUE_TYPE_ADAPTIVE
110 * Handle for calculating standard deviation
115 * Head for DLL of time slots which are free to be allocated to operations
117 struct TimeSlot *alloc_head;
120 * Tail for DLL of time slots which are free to be allocated to operations
122 struct TimeSlot *alloc_tail;
125 * Pointer to the chunk of time slots. Free all time slots at a time using
128 struct TimeSlot *tslots_freeptr;
131 * Number of time slots filled so far
133 unsigned int tslots_filled;
136 * Bound on the maximum number of operations which can be active
138 unsigned int max_active_bound;
144 * Queue of operations where we can only support a certain
145 * number of concurrent operations of a particular type.
147 struct OperationQueue
150 * DLL head for the wait queue. Operations which are waiting for this
151 * operation queue are put here
153 struct QueueEntry *wq_head;
156 * DLL tail for the wait queue.
158 struct QueueEntry *wq_tail;
161 * DLL head for the ready queue. Operations which are in this operation queue
162 * and are in ready state are put here
164 struct QueueEntry *rq_head;
167 * DLL tail for the ready queue
169 struct QueueEntry *rq_tail;
172 * DLL head for the active queue. Operations which are in this operation
173 * queue and are currently active are put here
175 struct QueueEntry *aq_head;
178 * DLL tail for the active queue.
180 struct QueueEntry *aq_tail;
183 * DLL head for the inactive queue. Operations which are inactive and can be
184 * evicted if the queues it holds are maxed out and another operation begins
187 struct QueueEntry *nq_head;
190 * DLL tail for the inactive queue.
192 struct QueueEntry *nq_tail;
195 * Feedback context; only relevant for adaptive operation queues. NULL for
196 * fixed operation queues
198 struct FeedbackCtx *fctx;
201 * The type of this opeartion queue
203 enum OperationQueueType type;
206 * Number of operations that are currently active in this queue.
211 * Max number of operations which can be active at any time in this queue.
212 * This value can be changed either by calling
213 * GNUNET_TESTBED_operation_queue_reset_max_active_() or by the adaptive
214 * algorithm if this operation queue is of type OPERATION_QUEUE_TYPE_ADAPTIVE
216 unsigned int max_active;
227 * The operation is just created and is in initial state
232 * The operation is currently waiting for resources
237 * The operation is ready to be started
242 * The operation has started and is active
247 * The operation is inactive. It still holds resources on the operation
248 * queues. However, this operation will be evicted when another operation
249 * requires resources from the maxed out queues this operation is holding
257 * An entry in the ready queue (implemented as DLL)
259 struct ReadyQueueEntry
264 struct ReadyQueueEntry *next;
269 struct ReadyQueueEntry *prev;
272 * The operation associated with this entry
274 struct GNUNET_TESTBED_Operation *op;
279 * Opaque handle to an abstract operation to be executed by the testing framework.
281 struct GNUNET_TESTBED_Operation
284 * Function to call when we have the resources to begin the operation.
286 OperationStart start;
289 * Function to call to clean up after the operation (which may or may
290 * not have been started yet).
292 OperationRelease release;
295 * Closure for callbacks.
300 * Array of operation queues this Operation belongs to.
302 struct OperationQueue **queues;
305 * Array of operation queue entries corresponding to this operation in
306 * operation queues for this operation
308 struct QueueEntry **qentries;
311 * Array of number of resources an operation need from each queue. The numbers
312 * in this array should correspond to the queues array
317 * Entry corresponding to this operation in ready queue. Will be NULL if the
318 * operation is not marked as READY
320 struct ReadyQueueEntry *rq_entry;
323 * Head pointer for DLL of tslots allocated to this operation
325 struct TimeSlot *tslots_head;
328 * Tail pointer for DLL of tslots allocated to this operation
330 struct TimeSlot *tslots_tail;
333 * The time at which the operation is started
335 struct GNUNET_TIME_Absolute tstart;
338 * Number of queues in the operation queues array
340 unsigned int nqueues;
343 * The state of the operation
345 enum OperationState state;
348 * Is this a failed operation?
355 * DLL head for the ready queue
357 struct ReadyQueueEntry *rq_head;
360 * DLL tail for the ready queue
362 struct ReadyQueueEntry *rq_tail;
365 * The id of the task to process the ready queue
367 GNUNET_SCHEDULER_TaskIdentifier process_rq_task_id;
371 * Assigns the given operation a time slot from the given operation queue
373 * @param op the operation
374 * @param queue the operation queue
375 * @return the timeslot
378 assign_timeslot (struct GNUNET_TESTBED_Operation *op,
379 struct OperationQueue *queue)
381 struct FeedbackCtx *fctx = queue->fctx;
382 struct TimeSlot *tslot;
384 GNUNET_assert (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type);
385 tslot = fctx->alloc_head;
386 GNUNET_assert (NULL != tslot);
387 GNUNET_CONTAINER_DLL_remove (fctx->alloc_head, fctx->alloc_tail, tslot);
388 GNUNET_CONTAINER_DLL_insert_tail (op->tslots_head, op->tslots_tail, tslot);
394 * Removes a queue entry of an operation from one of the operation queues' lists
395 * depending on the state of the operation
397 * @param op the operation whose entry has to be removed
398 * @param index the index of the entry in the operation's array of queue entries
401 remove_queue_entry (struct GNUNET_TESTBED_Operation *op, unsigned int index)
403 struct OperationQueue *opq;
404 struct QueueEntry *entry;
406 opq = op->queues[index];
407 entry = op->qentries[index];
413 case OP_STATE_WAITING:
414 GNUNET_CONTAINER_DLL_remove (opq->wq_head, opq->wq_tail, entry);
417 GNUNET_CONTAINER_DLL_remove (opq->rq_head, opq->rq_tail, entry);
419 case OP_STATE_ACTIVE:
420 GNUNET_CONTAINER_DLL_remove (opq->aq_head, opq->aq_tail, entry);
422 case OP_STATE_INACTIVE:
423 GNUNET_CONTAINER_DLL_remove (opq->nq_head, opq->nq_tail, entry);
430 * Changes the state of the operation while moving its associated queue entries
431 * in the operation's operation queues
433 * @param op the operation whose state has to be changed
434 * @param state the state the operation should have. It cannot be OP_STATE_INIT
437 change_state (struct GNUNET_TESTBED_Operation *op, enum OperationState state)
439 struct QueueEntry *entry;
440 struct OperationQueue *opq;
444 GNUNET_assert (OP_STATE_INIT != state);
445 GNUNET_assert (NULL != op->queues);
446 GNUNET_assert (NULL != op->nres);
447 GNUNET_assert ((OP_STATE_INIT == op->state) || (NULL != op->qentries));
448 GNUNET_assert (op->state != state);
449 for (cnt = 0; cnt < op->nqueues; cnt++)
451 if (OP_STATE_INIT == op->state)
453 entry = GNUNET_malloc (sizeof (struct QueueEntry));
455 entry->nres = op->nres[cnt];
457 GNUNET_array_append (op->qentries, s, entry);
461 entry = op->qentries[cnt];
462 remove_queue_entry (op, cnt);
464 opq = op->queues[cnt];
470 case OP_STATE_WAITING:
471 GNUNET_CONTAINER_DLL_insert_tail (opq->wq_head, opq->wq_tail, entry);
474 GNUNET_CONTAINER_DLL_insert_tail (opq->rq_head, opq->rq_tail, entry);
476 case OP_STATE_ACTIVE:
477 GNUNET_CONTAINER_DLL_insert_tail (opq->aq_head, opq->aq_tail, entry);
479 case OP_STATE_INACTIVE:
480 GNUNET_CONTAINER_DLL_insert_tail (opq->nq_head, opq->nq_tail, entry);
489 * Removes an operation from the ready queue. Also stops the 'process_rq_task'
490 * if the given operation is the last one in the queue.
492 * @param op the operation to be removed
495 rq_remove (struct GNUNET_TESTBED_Operation *op)
497 GNUNET_assert (NULL != op->rq_entry);
498 GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
499 GNUNET_free (op->rq_entry);
501 if ( (NULL == rq_head) && (GNUNET_SCHEDULER_NO_TASK != process_rq_task_id) )
503 GNUNET_SCHEDULER_cancel (process_rq_task_id);
504 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
510 * Processes the ready queue by calling the operation start callback of the
511 * operation at the head. The operation is then removed from the queue. The
512 * task is scheduled to run again immediately until no more operations are in
516 * @param tc scheduler task context. Not used.
519 process_rq_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
521 struct GNUNET_TESTBED_Operation *op;
522 struct OperationQueue *queue;
525 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
526 GNUNET_assert (NULL != rq_head);
527 GNUNET_assert (NULL != (op = rq_head->op));
530 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
531 change_state (op, OP_STATE_ACTIVE);
532 for (cnt = 0; cnt < op->nqueues; cnt++)
534 queue = op->queues[cnt];
535 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
536 assign_timeslot (op, queue);
538 op->tstart = GNUNET_TIME_absolute_get ();
539 if (NULL != op->start)
540 op->start (op->cb_cls);
545 * Adds the operation to the ready queue and starts the 'process_rq_task'
547 * @param op the operation to be queued
550 rq_add (struct GNUNET_TESTBED_Operation *op)
552 struct ReadyQueueEntry *rq_entry;
554 GNUNET_assert (NULL == op->rq_entry);
555 rq_entry = GNUNET_malloc (sizeof (struct ReadyQueueEntry));
557 GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
558 op->rq_entry = rq_entry;
559 if (GNUNET_SCHEDULER_NO_TASK == process_rq_task_id)
560 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
565 * Checks if the given operation queue is empty or not
567 * @param opq the operation queue
568 * @return GNUNET_YES if the given operation queue has no operations; GNUNET_NO
572 is_queue_empty (struct OperationQueue *opq)
574 if ( (NULL != opq->wq_head)
575 || (NULL != opq->rq_head)
576 || (NULL != opq->aq_head)
577 || (NULL != opq->nq_head) )
584 * Checks if the given operation queue has enough resources to provide for the
585 * operation of the given queue entry. It also checks if any inactive
586 * operations are to be released in order to accommodate the needed resources
587 * and returns them as an array.
589 * @param opq the operation queue to check for resource accommodation
590 * @param entry the operation queue entry whose operation's resources are to be
592 * @param ops_ pointer to return the array of operations which are to be released
593 * in order to accommodate the new operation. Can be NULL
594 * @param n_ops_ the number of operations in ops_
595 * @return GNUNET_YES if the given entry's operation can be accommodated in this
596 * queue. GNUNET_NO if it cannot be accommodated; ops_ and n_ops_ will
597 * be set to NULL and 0 respectively.
600 decide_capacity (struct OperationQueue *opq,
601 struct QueueEntry *entry,
602 struct GNUNET_TESTBED_Operation ***ops_,
603 unsigned int *n_ops_)
605 struct QueueEntry **evict_entries;
606 struct GNUNET_TESTBED_Operation **ops;
607 struct GNUNET_TESTBED_Operation *op;
609 unsigned int n_evict_entries;
614 GNUNET_assert (NULL != (op = entry->op));
615 GNUNET_assert (0 < (need = entry->nres));
618 evict_entries = NULL;
621 if (opq->active > opq->max_active)
626 if ((opq->active + need) <= opq->max_active)
628 deficit = need - (opq->max_active - opq->active);
629 for (entry = opq->nq_head;
630 (0 < deficit) && (NULL != entry);
633 GNUNET_array_append (evict_entries, n_evict_entries, entry);
634 deficit -= entry->nres;
641 for (n_ops = 0; n_ops < n_evict_entries;)
643 op = evict_entries[n_ops]->op;
644 GNUNET_array_append (ops, n_ops, op); /* increments n-ops */
648 GNUNET_free_non_null (evict_entries);
660 * Merges an array of operations into another, eliminating duplicates. No
661 * ordering is guaranteed.
663 * @param old the array into which the merging is done.
664 * @param n_old the number of operations in old array
665 * @param new the array from which operations are to be merged
666 * @param n_new the number of operations in new array
669 merge_ops (struct GNUNET_TESTBED_Operation ***old,
671 struct GNUNET_TESTBED_Operation **new,
674 struct GNUNET_TESTBED_Operation **cur;
679 GNUNET_assert (NULL != old);
682 for (i = 0; i < n_new; i++)
684 for (j = 0; j < *n_old; j++)
686 if (new[i] == cur[j])
691 GNUNET_array_append (cur, n_cur, new[j]);
700 * Checks for the readiness of an operation and schedules a operation start task
702 * @param op the operation
705 check_readiness (struct GNUNET_TESTBED_Operation *op)
707 struct GNUNET_TESTBED_Operation **evict_ops;
708 struct GNUNET_TESTBED_Operation **ops;
710 unsigned int n_evict_ops;
713 GNUNET_assert (NULL == op->rq_entry);
714 GNUNET_assert (OP_STATE_WAITING == op->state);
717 for (i = 0; i < op->nqueues; i++)
721 if (GNUNET_NO == decide_capacity (op->queues[i], op->qentries[i],
724 GNUNET_free_non_null (evict_ops);
729 merge_ops (&evict_ops, &n_evict_ops, ops, n_ops);
732 if (NULL != evict_ops)
734 for (i = 0; i < n_evict_ops; i++)
735 GNUNET_TESTBED_operation_release_ (evict_ops[i]);
736 GNUNET_free (evict_ops);
738 /* Evicting the operations should schedule this operation */
739 GNUNET_assert (OP_STATE_READY == op->state);
742 for (i = 0; i < op->nqueues; i++)
743 op->queues[i]->active += op->nres[i];
744 change_state (op, OP_STATE_READY);
751 * Defers a ready to be executed operation back to waiting
753 * @param op the operation to defer
756 defer (struct GNUNET_TESTBED_Operation *op)
760 GNUNET_assert (OP_STATE_READY == op->state);
762 for (i = 0; i < op->nqueues; i++)
764 GNUNET_assert (op->queues[i]->active >= op->nres[i]);
765 op->queues[i]->active -= op->nres[i];
767 change_state (op, OP_STATE_WAITING);
772 * Cleanups the array of timeslots of an operation queue. For each time slot in
773 * the array, if it is allocated to an operation, it will be deallocated from
776 * @param queue the operation queue
779 cleanup_tslots (struct OperationQueue *queue)
781 struct FeedbackCtx *fctx = queue->fctx;
782 struct TimeSlot *tslot;
783 struct GNUNET_TESTBED_Operation *op;
786 GNUNET_assert (NULL != fctx);
787 for (cnt = 0; cnt < queue->max_active; cnt++)
789 tslot = &fctx->tslots_freeptr[cnt];
793 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
795 GNUNET_free_non_null (fctx->tslots_freeptr);
796 fctx->tslots_freeptr = NULL;
797 fctx->alloc_head = NULL;
798 fctx->alloc_tail = NULL;
799 fctx->tslots_filled = 0;
804 * Initializes the operation queue for parallel overlay connects
806 * @param h the host handle
807 * @param npoc the number of parallel overlay connects - the queue size
810 adaptive_queue_set_max_active (struct OperationQueue *queue, unsigned int n)
812 struct FeedbackCtx *fctx = queue->fctx;
813 struct TimeSlot *tslot;
816 cleanup_tslots (queue);
817 n = GNUNET_MIN (n ,fctx->max_active_bound);
818 fctx->tslots_freeptr = GNUNET_malloc (n * sizeof (struct TimeSlot));
819 for (cnt = 0; cnt < n; cnt++)
821 tslot = &fctx->tslots_freeptr[cnt];
822 tslot->queue = queue;
823 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail, tslot);
825 GNUNET_TESTBED_operation_queue_reset_max_active_ (queue, n);
830 * Adapts parallelism in an adaptive queue by using the statistical data from
831 * the feedback context.
833 * @param queue the queue
834 * @param fail GNUNET_YES if the last operation failed; GNUNET_NO if not;
837 adapt_parallelism (struct OperationQueue *queue, int fail)
839 struct GNUNET_TIME_Relative avg;
840 struct FeedbackCtx *fctx;
841 struct TimeSlot *tslot;
846 avg = GNUNET_TIME_UNIT_ZERO;
849 for (cnt = 0; cnt < queue->max_active; cnt++)
851 tslot = &fctx->tslots_freeptr[cnt];
852 avg = GNUNET_TIME_relative_add (avg, tslot->tsum);
853 nvals += tslot->nvals;
855 GNUNET_assert (nvals >= queue->max_active);
856 avg = GNUNET_TIME_relative_divide (avg, nvals);
858 GNUNET_TESTBED_SD_deviation_factor_ (fctx->sd,
859 (unsigned int) avg.rel_value_us,
862 GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
863 adaptive_queue_set_max_active (queue, queue->max_active); /* no change */
866 if ((0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, queue->max_active)))
867 GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
870 GNUNET_assert (0 <= sd);
871 if ((0 == sd) && (! fail))
873 adaptive_queue_set_max_active (queue, queue->max_active * 2);
876 if ((1 == sd) && (! fail))
878 adaptive_queue_set_max_active (queue, queue->max_active + 1);
881 if (1 == queue->max_active)
883 adaptive_queue_set_max_active (queue, 1);
886 if (((sd < 2) && (fail)) || (2 == sd))
888 adaptive_queue_set_max_active (queue, queue->max_active - 1);
891 adaptive_queue_set_max_active (queue, queue->max_active / 2);
896 * update tslots with the operation's completion time. Additionally, if
897 * updating a timeslot makes all timeslots filled in an adaptive operation
898 * queue, call adapt_parallelism() for that queue.
900 * @param op the operation
903 update_tslots (struct GNUNET_TESTBED_Operation *op)
905 struct OperationQueue *queue;
906 struct GNUNET_TIME_Relative t;
907 struct TimeSlot *tslot;
908 struct FeedbackCtx *fctx;
910 t = GNUNET_TIME_absolute_get_duration (op->tstart);
911 while (NULL != (tslot = op->tslots_head)) /* update time slots */
913 queue = tslot->queue;
915 tslot->tsum = GNUNET_TIME_relative_add (tslot->tsum, t);
916 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
918 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail,
920 if (0 != tslot->nvals++)
922 fctx->tslots_filled++;
923 if (queue->max_active == fctx->tslots_filled)
924 adapt_parallelism (queue, op->failed);
930 * Create an 'operation' to be performed.
932 * @param cls closure for the callbacks
933 * @param start function to call to start the operation
934 * @param release function to call to close down the operation
935 * @return handle to the operation
937 struct GNUNET_TESTBED_Operation *
938 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
939 OperationRelease release)
941 struct GNUNET_TESTBED_Operation *op;
943 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
945 op->state = OP_STATE_INIT;
946 op->release = release;
953 * Create an operation queue.
955 * @param type the type of operation queue
956 * @param max_active maximum number of operations in this
957 * queue that can be active in parallel at the same time
958 * @return handle to the queue
960 struct OperationQueue *
961 GNUNET_TESTBED_operation_queue_create_ (enum OperationQueueType type,
962 unsigned int max_active)
964 struct OperationQueue *queue;
965 struct FeedbackCtx *fctx;
967 queue = GNUNET_malloc (sizeof (struct OperationQueue));
969 if (OPERATION_QUEUE_TYPE_FIXED == type)
971 queue->max_active = max_active;
975 fctx = GNUNET_malloc (sizeof (struct FeedbackCtx));
976 fctx->max_active_bound = max_active;
977 fctx->sd = GNUNET_TESTBED_SD_init_ (10); /* FIXME: Why 10? */
979 adaptive_queue_set_max_active (queue, 1); /* start with 1 */
986 * Destroy an operation queue. The queue MUST be empty
989 * @param queue queue to destroy
992 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
994 struct FeedbackCtx *fctx;
996 GNUNET_break (GNUNET_YES == is_queue_empty (queue));
997 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
999 cleanup_tslots (queue);
1001 GNUNET_TESTBED_SD_destroy_ (fctx->sd);
1004 GNUNET_free (queue);
1009 * Destroys the operation queue if it is empty. If not empty return GNUNET_NO.
1011 * @param queue the queue to destroy if empty
1012 * @return GNUNET_YES if the queue is destroyed. GNUNET_NO if not (because it
1016 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
1018 if (GNUNET_NO == is_queue_empty (queue))
1020 GNUNET_TESTBED_operation_queue_destroy_ (queue);
1026 * Rechecks if any of the operations in the given operation queue's waiting list
1027 * can be made active
1029 * @param opq the operation queue
1032 recheck_waiting (struct OperationQueue *opq)
1034 struct QueueEntry *entry;
1035 struct QueueEntry *entry2;
1037 entry = opq->wq_head;
1038 while (NULL != entry)
1040 entry2 = entry->next;
1041 if (GNUNET_NO == check_readiness (entry->op))
1049 * Function to reset the maximum number of operations in the given queue. If
1050 * max_active is lesser than the number of currently active operations, the
1051 * active operations are not stopped immediately.
1053 * @param queue the operation queue which has to be modified
1054 * @param max_active the new maximum number of active operations
1057 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
1058 unsigned int max_active)
1060 struct QueueEntry *entry;
1062 queue->max_active = max_active;
1063 while ( (queue->active > queue->max_active)
1064 && (NULL != (entry = queue->rq_head)) )
1066 recheck_waiting (queue);
1071 * Add an operation to a queue. An operation can be in multiple queues at
1072 * once. Once the operation is inserted into all the queues
1073 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1074 * waiting for the operation to become active.
1076 * @param queue queue to add the operation to
1077 * @param op operation to add to the queue
1078 * @param nres the number of units of the resources of queue needed by the
1079 * operation. Should be greater than 0.
1082 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
1083 struct GNUNET_TESTBED_Operation *op,
1088 GNUNET_assert (0 < nres);
1089 qsize = op->nqueues;
1090 GNUNET_array_append (op->queues, op->nqueues, queue);
1091 GNUNET_array_append (op->nres, qsize, nres);
1092 GNUNET_assert (qsize == op->nqueues);
1097 * Add an operation to a queue. An operation can be in multiple queues at
1098 * once. Once the operation is inserted into all the queues
1099 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1100 * waiting for the operation to become active. The operation is assumed to take
1101 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
1102 * requires more than 1
1104 * @param queue queue to add the operation to
1105 * @param op operation to add to the queue
1108 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
1109 struct GNUNET_TESTBED_Operation *op)
1111 return GNUNET_TESTBED_operation_queue_insert2_ (queue, op, 1);
1116 * Marks the given operation as waiting on the queues. Once all queues permit
1117 * the operation to become active, the operation will be activated. The actual
1118 * activation will occur in a separate task (thus allowing multiple queue
1119 * insertions to be made without having the first one instantly trigger the
1120 * operation if the first queue has sufficient resources).
1122 * @param op the operation to marks as waiting
1125 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op)
1127 GNUNET_assert (NULL == op->rq_entry);
1128 change_state (op, OP_STATE_WAITING);
1129 (void) check_readiness (op);
1134 * Marks an active operation as inactive - the operation will be kept in a
1135 * ready-to-be-released state and continues to hold resources until another
1136 * operation contents for them.
1138 * @param op the operation to be marked as inactive. The operation start
1139 * callback should have been called before for this operation to mark
1143 GNUNET_TESTBED_operation_inactivate_ (struct GNUNET_TESTBED_Operation *op)
1145 struct OperationQueue **queues;
1147 unsigned int nqueues;
1150 GNUNET_assert (OP_STATE_ACTIVE == op->state);
1151 change_state (op, OP_STATE_INACTIVE);
1152 nqueues = op->nqueues;
1153 ms = sizeof (struct OperationQueue *) * nqueues;
1154 queues = GNUNET_malloc (ms);
1155 /* Cloning is needed as the operation be released by waiting operations and
1156 hence its nqueues memory ptr will be freed */
1157 GNUNET_assert (NULL != (queues = memcpy (queues, op->queues, ms)));
1158 for (i = 0; i < nqueues; i++)
1159 recheck_waiting (queues[i]);
1160 GNUNET_free (queues);
1165 * Marks and inactive operation as active. This fuction should be called to
1166 * ensure that the oprelease callback will not be called until it is either
1167 * marked as inactive or released.
1169 * @param op the operation to be marked as active
1172 GNUNET_TESTBED_operation_activate_ (struct GNUNET_TESTBED_Operation *op)
1175 GNUNET_assert (OP_STATE_INACTIVE == op->state);
1176 change_state (op, OP_STATE_ACTIVE);
1181 * An operation is 'done' (was cancelled or finished); remove
1182 * it from the queues and release associated resources.
1184 * @param op operation that finished
1187 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op)
1189 struct QueueEntry *entry;
1190 struct OperationQueue *opq;
1193 if (OP_STATE_INIT == op->state)
1198 if (OP_STATE_READY == op->state)
1200 if (OP_STATE_INACTIVE == op->state) /* Activate the operation if inactive */
1201 GNUNET_TESTBED_operation_activate_ (op);
1202 if (OP_STATE_ACTIVE == op->state)
1204 GNUNET_assert (NULL != op->queues);
1205 GNUNET_assert (NULL != op->qentries);
1206 for (i = 0; i < op->nqueues; i++)
1208 entry = op->qentries[i];
1209 remove_queue_entry (op, i);
1210 opq = op->queues[i];
1214 case OP_STATE_INACTIVE:
1217 case OP_STATE_WAITING:
1219 case OP_STATE_ACTIVE:
1220 case OP_STATE_READY:
1221 GNUNET_assert (0 != opq->active);
1222 GNUNET_assert (opq->active >= entry->nres);
1223 opq->active -= entry->nres;
1224 recheck_waiting (opq);
1227 GNUNET_free (entry);
1229 GNUNET_free_non_null (op->qentries);
1230 GNUNET_free (op->queues);
1231 GNUNET_free (op->nres);
1232 if (NULL != op->release)
1233 op->release (op->cb_cls);
1239 * Marks an operation as failed
1241 * @param op the operation to be marked as failed
1244 GNUNET_TESTBED_operation_mark_failed (struct GNUNET_TESTBED_Operation *op)
1246 op->failed = GNUNET_YES;
1250 /* end of testbed_api_operations.c */