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;
141 * Number of operations that have failed
143 unsigned int nfailed;
149 * Queue of operations where we can only support a certain
150 * number of concurrent operations of a particular type.
152 struct OperationQueue
155 * DLL head for the wait queue. Operations which are waiting for this
156 * operation queue are put here
158 struct QueueEntry *wq_head;
161 * DLL tail for the wait queue.
163 struct QueueEntry *wq_tail;
166 * DLL head for the ready queue. Operations which are in this operation queue
167 * and are in ready state are put here
169 struct QueueEntry *rq_head;
172 * DLL tail for the ready queue
174 struct QueueEntry *rq_tail;
177 * DLL head for the active queue. Operations which are in this operation
178 * queue and are currently active are put here
180 struct QueueEntry *aq_head;
183 * DLL tail for the active queue.
185 struct QueueEntry *aq_tail;
188 * DLL head for the inactive queue. Operations which are inactive and can be
189 * evicted if the queues it holds are maxed out and another operation begins
192 struct QueueEntry *nq_head;
195 * DLL tail for the inactive queue.
197 struct QueueEntry *nq_tail;
200 * Feedback context; only relevant for adaptive operation queues. NULL for
201 * fixed operation queues
203 struct FeedbackCtx *fctx;
206 * The type of this opeartion queue
208 enum OperationQueueType type;
211 * Number of operations that are currently active in this queue.
216 * Max number of operations which can be active at any time in this queue.
217 * This value can be changed either by calling
218 * GNUNET_TESTBED_operation_queue_reset_max_active_() or by the adaptive
219 * algorithm if this operation queue is of type OPERATION_QUEUE_TYPE_ADAPTIVE
221 unsigned int max_active;
232 * The operation is just created and is in initial state
237 * The operation is currently waiting for resources
242 * The operation is ready to be started
247 * The operation has started and is active
252 * The operation is inactive. It still holds resources on the operation
253 * queues. However, this operation will be evicted when another operation
254 * requires resources from the maxed out queues this operation is holding
262 * An entry in the ready queue (implemented as DLL)
264 struct ReadyQueueEntry
269 struct ReadyQueueEntry *next;
274 struct ReadyQueueEntry *prev;
277 * The operation associated with this entry
279 struct GNUNET_TESTBED_Operation *op;
284 * Opaque handle to an abstract operation to be executed by the testing framework.
286 struct GNUNET_TESTBED_Operation
289 * Function to call when we have the resources to begin the operation.
291 OperationStart start;
294 * Function to call to clean up after the operation (which may or may
295 * not have been started yet).
297 OperationRelease release;
300 * Closure for callbacks.
305 * Array of operation queues this Operation belongs to.
307 struct OperationQueue **queues;
310 * Array of operation queue entries corresponding to this operation in
311 * operation queues for this operation
313 struct QueueEntry **qentries;
316 * Array of number of resources an operation need from each queue. The numbers
317 * in this array should correspond to the queues array
322 * Entry corresponding to this operation in ready queue. Will be NULL if the
323 * operation is not marked as READY
325 struct ReadyQueueEntry *rq_entry;
328 * Head pointer for DLL of tslots allocated to this operation
330 struct TimeSlot *tslots_head;
333 * Tail pointer for DLL of tslots allocated to this operation
335 struct TimeSlot *tslots_tail;
338 * The time at which the operation is started
340 struct GNUNET_TIME_Absolute tstart;
343 * Number of queues in the operation queues array
345 unsigned int nqueues;
348 * The state of the operation
350 enum OperationState state;
353 * Is this a failed operation?
360 * DLL head for the ready queue
362 struct ReadyQueueEntry *rq_head;
365 * DLL tail for the ready queue
367 struct ReadyQueueEntry *rq_tail;
370 * The id of the task to process the ready queue
372 GNUNET_SCHEDULER_TaskIdentifier process_rq_task_id;
376 * Assigns the given operation a time slot from the given operation queue
378 * @param op the operation
379 * @param queue the operation queue
380 * @return the timeslot
383 assign_timeslot (struct GNUNET_TESTBED_Operation *op,
384 struct OperationQueue *queue)
386 struct FeedbackCtx *fctx = queue->fctx;
387 struct TimeSlot *tslot;
389 GNUNET_assert (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type);
390 tslot = fctx->alloc_head;
391 GNUNET_assert (NULL != tslot);
392 GNUNET_CONTAINER_DLL_remove (fctx->alloc_head, fctx->alloc_tail, tslot);
393 GNUNET_CONTAINER_DLL_insert_tail (op->tslots_head, op->tslots_tail, tslot);
399 * Removes a queue entry of an operation from one of the operation queues' lists
400 * depending on the state of the operation
402 * @param op the operation whose entry has to be removed
403 * @param index the index of the entry in the operation's array of queue entries
406 remove_queue_entry (struct GNUNET_TESTBED_Operation *op, unsigned int index)
408 struct OperationQueue *opq;
409 struct QueueEntry *entry;
411 opq = op->queues[index];
412 entry = op->qentries[index];
418 case OP_STATE_WAITING:
419 GNUNET_CONTAINER_DLL_remove (opq->wq_head, opq->wq_tail, entry);
422 GNUNET_CONTAINER_DLL_remove (opq->rq_head, opq->rq_tail, entry);
424 case OP_STATE_ACTIVE:
425 GNUNET_CONTAINER_DLL_remove (opq->aq_head, opq->aq_tail, entry);
427 case OP_STATE_INACTIVE:
428 GNUNET_CONTAINER_DLL_remove (opq->nq_head, opq->nq_tail, entry);
435 * Changes the state of the operation while moving its associated queue entries
436 * in the operation's operation queues
438 * @param op the operation whose state has to be changed
439 * @param state the state the operation should have. It cannot be OP_STATE_INIT
442 change_state (struct GNUNET_TESTBED_Operation *op, enum OperationState state)
444 struct QueueEntry *entry;
445 struct OperationQueue *opq;
449 GNUNET_assert (OP_STATE_INIT != state);
450 GNUNET_assert (NULL != op->queues);
451 GNUNET_assert (NULL != op->nres);
452 GNUNET_assert ((OP_STATE_INIT == op->state) || (NULL != op->qentries));
453 GNUNET_assert (op->state != state);
454 for (cnt = 0; cnt < op->nqueues; cnt++)
456 if (OP_STATE_INIT == op->state)
458 entry = GNUNET_new (struct QueueEntry);
460 entry->nres = op->nres[cnt];
462 GNUNET_array_append (op->qentries, s, entry);
466 entry = op->qentries[cnt];
467 remove_queue_entry (op, cnt);
469 opq = op->queues[cnt];
475 case OP_STATE_WAITING:
476 GNUNET_CONTAINER_DLL_insert_tail (opq->wq_head, opq->wq_tail, entry);
479 GNUNET_CONTAINER_DLL_insert_tail (opq->rq_head, opq->rq_tail, entry);
481 case OP_STATE_ACTIVE:
482 GNUNET_CONTAINER_DLL_insert_tail (opq->aq_head, opq->aq_tail, entry);
484 case OP_STATE_INACTIVE:
485 GNUNET_CONTAINER_DLL_insert_tail (opq->nq_head, opq->nq_tail, entry);
494 * Removes an operation from the ready queue. Also stops the 'process_rq_task'
495 * if the given operation is the last one in the queue.
497 * @param op the operation to be removed
500 rq_remove (struct GNUNET_TESTBED_Operation *op)
502 GNUNET_assert (NULL != op->rq_entry);
503 GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
504 GNUNET_free (op->rq_entry);
506 if ( (NULL == rq_head) && (GNUNET_SCHEDULER_NO_TASK != process_rq_task_id) )
508 GNUNET_SCHEDULER_cancel (process_rq_task_id);
509 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
515 * Processes the ready queue by calling the operation start callback of the
516 * operation at the head. The operation is then removed from the queue. The
517 * task is scheduled to run again immediately until no more operations are in
521 * @param tc scheduler task context. Not used.
524 process_rq_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
526 struct GNUNET_TESTBED_Operation *op;
527 struct OperationQueue *queue;
530 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
531 GNUNET_assert (NULL != rq_head);
532 GNUNET_assert (NULL != (op = rq_head->op));
535 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
536 change_state (op, OP_STATE_ACTIVE);
537 for (cnt = 0; cnt < op->nqueues; cnt++)
539 queue = op->queues[cnt];
540 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
541 assign_timeslot (op, queue);
543 op->tstart = GNUNET_TIME_absolute_get ();
544 if (NULL != op->start)
545 op->start (op->cb_cls);
550 * Adds the operation to the ready queue and starts the 'process_rq_task'
552 * @param op the operation to be queued
555 rq_add (struct GNUNET_TESTBED_Operation *op)
557 struct ReadyQueueEntry *rq_entry;
559 GNUNET_assert (NULL == op->rq_entry);
560 rq_entry = GNUNET_new (struct ReadyQueueEntry);
562 GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
563 op->rq_entry = rq_entry;
564 if (GNUNET_SCHEDULER_NO_TASK == process_rq_task_id)
565 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
570 * Checks if the given operation queue is empty or not
572 * @param opq the operation queue
573 * @return GNUNET_YES if the given operation queue has no operations; GNUNET_NO
577 is_queue_empty (struct OperationQueue *opq)
579 if ( (NULL != opq->wq_head)
580 || (NULL != opq->rq_head)
581 || (NULL != opq->aq_head)
582 || (NULL != opq->nq_head) )
589 * Checks if the given operation queue has enough resources to provide for the
590 * operation of the given queue entry. It also checks if any inactive
591 * operations are to be released in order to accommodate the needed resources
592 * and returns them as an array.
594 * @param opq the operation queue to check for resource accommodation
595 * @param entry the operation queue entry whose operation's resources are to be
597 * @param ops_ pointer to return the array of operations which are to be released
598 * in order to accommodate the new operation. Can be NULL
599 * @param n_ops_ the number of operations in ops_
600 * @return GNUNET_YES if the given entry's operation can be accommodated in this
601 * queue. GNUNET_NO if it cannot be accommodated; ops_ and n_ops_ will
602 * be set to NULL and 0 respectively.
605 decide_capacity (struct OperationQueue *opq,
606 struct QueueEntry *entry,
607 struct GNUNET_TESTBED_Operation ***ops_,
608 unsigned int *n_ops_)
610 struct QueueEntry **evict_entries;
611 struct GNUNET_TESTBED_Operation **ops;
612 struct GNUNET_TESTBED_Operation *op;
614 unsigned int n_evict_entries;
619 GNUNET_assert (NULL != (op = entry->op));
620 GNUNET_assert (0 < (need = entry->nres));
623 evict_entries = NULL;
626 if (opq->active > opq->max_active)
631 if ((opq->active + need) <= opq->max_active)
633 deficit = need - (opq->max_active - opq->active);
634 for (entry = opq->nq_head;
635 (0 < deficit) && (NULL != entry);
638 GNUNET_array_append (evict_entries, n_evict_entries, entry);
639 deficit -= entry->nres;
646 for (n_ops = 0; n_ops < n_evict_entries;)
648 op = evict_entries[n_ops]->op;
649 GNUNET_array_append (ops, n_ops, op); /* increments n-ops */
653 GNUNET_free_non_null (evict_entries);
665 * Merges an array of operations into another, eliminating duplicates. No
666 * ordering is guaranteed.
668 * @param old the array into which the merging is done.
669 * @param n_old the number of operations in old array
670 * @param new the array from which operations are to be merged
671 * @param n_new the number of operations in new array
674 merge_ops (struct GNUNET_TESTBED_Operation ***old,
676 struct GNUNET_TESTBED_Operation **new,
679 struct GNUNET_TESTBED_Operation **cur;
684 GNUNET_assert (NULL != old);
687 for (i = 0; i < n_new; i++)
689 for (j = 0; j < *n_old; j++)
691 if (new[i] == cur[j])
696 GNUNET_array_append (cur, n_cur, new[j]);
705 * Checks for the readiness of an operation and schedules a operation start task
707 * @param op the operation
710 check_readiness (struct GNUNET_TESTBED_Operation *op)
712 struct GNUNET_TESTBED_Operation **evict_ops;
713 struct GNUNET_TESTBED_Operation **ops;
715 unsigned int n_evict_ops;
718 GNUNET_assert (NULL == op->rq_entry);
719 GNUNET_assert (OP_STATE_WAITING == op->state);
722 for (i = 0; i < op->nqueues; i++)
726 if (GNUNET_NO == decide_capacity (op->queues[i], op->qentries[i],
729 GNUNET_free_non_null (evict_ops);
734 merge_ops (&evict_ops, &n_evict_ops, ops, n_ops);
737 if (NULL != evict_ops)
739 for (i = 0; i < n_evict_ops; i++)
740 GNUNET_TESTBED_operation_release_ (evict_ops[i]);
741 GNUNET_free (evict_ops);
743 /* Evicting the operations should schedule this operation */
744 GNUNET_assert (OP_STATE_READY == op->state);
747 for (i = 0; i < op->nqueues; i++)
748 op->queues[i]->active += op->nres[i];
749 change_state (op, OP_STATE_READY);
756 * Defers a ready to be executed operation back to waiting
758 * @param op the operation to defer
761 defer (struct GNUNET_TESTBED_Operation *op)
765 GNUNET_assert (OP_STATE_READY == op->state);
767 for (i = 0; i < op->nqueues; i++)
769 GNUNET_assert (op->queues[i]->active >= op->nres[i]);
770 op->queues[i]->active -= op->nres[i];
772 change_state (op, OP_STATE_WAITING);
777 * Cleanups the array of timeslots of an operation queue. For each time slot in
778 * the array, if it is allocated to an operation, it will be deallocated from
781 * @param queue the operation queue
784 cleanup_tslots (struct OperationQueue *queue)
786 struct FeedbackCtx *fctx = queue->fctx;
787 struct TimeSlot *tslot;
788 struct GNUNET_TESTBED_Operation *op;
791 GNUNET_assert (NULL != fctx);
792 for (cnt = 0; cnt < queue->max_active; cnt++)
794 tslot = &fctx->tslots_freeptr[cnt];
798 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
800 GNUNET_free_non_null (fctx->tslots_freeptr);
801 fctx->tslots_freeptr = NULL;
802 fctx->alloc_head = NULL;
803 fctx->alloc_tail = NULL;
804 fctx->tslots_filled = 0;
809 * Cleansup the existing timing slots and sets new timing slots in the given
810 * queue to accommodate given number of max active operations.
812 * @param queue the queue
813 * @param n the number of maximum active operations. If n is greater than the
814 * maximum limit set while creating the queue, then the minimum of these two
815 * will be selected as n
818 adaptive_queue_set_max_active (struct OperationQueue *queue, unsigned int n)
820 struct FeedbackCtx *fctx = queue->fctx;
821 struct TimeSlot *tslot;
824 cleanup_tslots (queue);
825 n = GNUNET_MIN (n ,fctx->max_active_bound);
826 fctx->tslots_freeptr = GNUNET_malloc (n * sizeof (struct TimeSlot));
828 for (cnt = 0; cnt < n; cnt++)
830 tslot = &fctx->tslots_freeptr[cnt];
831 tslot->queue = queue;
832 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail, tslot);
834 GNUNET_TESTBED_operation_queue_reset_max_active_ (queue, n);
839 * Adapts parallelism in an adaptive queue by using the statistical data from
840 * the feedback context.
842 * @param queue the queue
845 adapt_parallelism (struct OperationQueue *queue)
847 struct GNUNET_TIME_Relative avg;
848 struct FeedbackCtx *fctx;
849 struct TimeSlot *tslot;
854 avg = GNUNET_TIME_UNIT_ZERO;
857 for (cnt = 0; cnt < queue->max_active; cnt++)
859 tslot = &fctx->tslots_freeptr[cnt];
860 avg = GNUNET_TIME_relative_add (avg, tslot->tsum);
861 nvals += tslot->nvals;
863 GNUNET_assert (nvals >= queue->max_active);
864 GNUNET_assert (fctx->nfailed <= nvals);
865 nvals -= fctx->nfailed;
868 if (1 == queue->max_active)
869 adaptive_queue_set_max_active (queue, 1);
871 adaptive_queue_set_max_active (queue, queue->max_active / 2);
874 avg = GNUNET_TIME_relative_divide (avg, nvals);
876 GNUNET_TESTBED_SD_deviation_factor_ (fctx->sd,
877 (unsigned int) avg.rel_value_us,
880 GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
881 adaptive_queue_set_max_active (queue, queue->max_active); /* no change */
885 adaptive_queue_set_max_active (queue, queue->max_active - 1);
887 adaptive_queue_set_max_active (queue, queue->max_active / 2);
889 adaptive_queue_set_max_active (queue, queue->max_active + 1);
891 adaptive_queue_set_max_active (queue, queue->max_active * 2);
893 #if 0 /* old algorithm */
896 GNUNET_assert (0 <= sd);
897 GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
900 adaptive_queue_set_max_active (queue, queue->max_active * 2);
905 adaptive_queue_set_max_active (queue, queue->max_active + 1);
908 if (1 == queue->max_active)
910 adaptive_queue_set_max_active (queue, 1);
915 adaptive_queue_set_max_active (queue, queue->max_active - 1);
918 adaptive_queue_set_max_active (queue, queue->max_active / 2);
924 * update tslots with the operation's completion time. Additionally, if
925 * updating a timeslot makes all timeslots filled in an adaptive operation
926 * queue, call adapt_parallelism() for that queue.
928 * @param op the operation
931 update_tslots (struct GNUNET_TESTBED_Operation *op)
933 struct OperationQueue *queue;
934 struct GNUNET_TIME_Relative t;
935 struct TimeSlot *tslot;
936 struct FeedbackCtx *fctx;
938 t = GNUNET_TIME_absolute_get_duration (op->tstart);
939 while (NULL != (tslot = op->tslots_head)) /* update time slots */
941 queue = tslot->queue;
943 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
945 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail,
949 tslot->tsum = GNUNET_TIME_relative_add (tslot->tsum, t);
950 if (0 != tslot->nvals++)
952 fctx->tslots_filled++;
953 if (queue->max_active == fctx->tslots_filled)
954 adapt_parallelism (queue);
960 * Create an 'operation' to be performed.
962 * @param cls closure for the callbacks
963 * @param start function to call to start the operation
964 * @param release function to call to close down the operation
965 * @return handle to the operation
967 struct GNUNET_TESTBED_Operation *
968 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
969 OperationRelease release)
971 struct GNUNET_TESTBED_Operation *op;
973 op = GNUNET_new (struct GNUNET_TESTBED_Operation);
975 op->state = OP_STATE_INIT;
976 op->release = release;
983 * Create an operation queue.
985 * @param type the type of operation queue
986 * @param max_active maximum number of operations in this
987 * queue that can be active in parallel at the same time
988 * @return handle to the queue
990 struct OperationQueue *
991 GNUNET_TESTBED_operation_queue_create_ (enum OperationQueueType type,
992 unsigned int max_active)
994 struct OperationQueue *queue;
995 struct FeedbackCtx *fctx;
997 queue = GNUNET_new (struct OperationQueue);
999 if (OPERATION_QUEUE_TYPE_FIXED == type)
1001 queue->max_active = max_active;
1005 fctx = GNUNET_new (struct FeedbackCtx);
1006 fctx->max_active_bound = max_active;
1007 fctx->sd = GNUNET_TESTBED_SD_init_ (10); /* FIXME: Why 10? */
1009 adaptive_queue_set_max_active (queue, 4); /* start with 4 */
1016 * Destroy an operation queue. The queue MUST be empty
1019 * @param queue queue to destroy
1022 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
1024 struct FeedbackCtx *fctx;
1026 GNUNET_break (GNUNET_YES == is_queue_empty (queue));
1027 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
1029 cleanup_tslots (queue);
1031 GNUNET_TESTBED_SD_destroy_ (fctx->sd);
1034 GNUNET_free (queue);
1039 * Destroys the operation queue if it is empty. If not empty return GNUNET_NO.
1041 * @param queue the queue to destroy if empty
1042 * @return GNUNET_YES if the queue is destroyed. GNUNET_NO if not (because it
1046 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
1048 if (GNUNET_NO == is_queue_empty (queue))
1050 GNUNET_TESTBED_operation_queue_destroy_ (queue);
1056 * Rechecks if any of the operations in the given operation queue's waiting list
1057 * can be made active
1059 * @param opq the operation queue
1062 recheck_waiting (struct OperationQueue *opq)
1064 struct QueueEntry *entry;
1065 struct QueueEntry *entry2;
1067 entry = opq->wq_head;
1068 while (NULL != entry)
1070 entry2 = entry->next;
1071 if (GNUNET_NO == check_readiness (entry->op))
1079 * Function to reset the maximum number of operations in the given queue. If
1080 * max_active is lesser than the number of currently active operations, the
1081 * active operations are not stopped immediately.
1083 * @param queue the operation queue which has to be modified
1084 * @param max_active the new maximum number of active operations
1087 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
1088 unsigned int max_active)
1090 struct QueueEntry *entry;
1092 queue->max_active = max_active;
1093 while ( (queue->active > queue->max_active)
1094 && (NULL != (entry = queue->rq_head)) )
1096 recheck_waiting (queue);
1101 * Add an operation to a queue. An operation can be in multiple queues at
1102 * once. Once the operation is inserted into all the queues
1103 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1104 * waiting for the operation to become active.
1106 * @param queue queue to add the operation to
1107 * @param op operation to add to the queue
1108 * @param nres the number of units of the resources of queue needed by the
1109 * operation. Should be greater than 0.
1112 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
1113 struct GNUNET_TESTBED_Operation *op,
1118 GNUNET_assert (0 < nres);
1119 qsize = op->nqueues;
1120 GNUNET_array_append (op->queues, op->nqueues, queue);
1121 GNUNET_array_append (op->nres, qsize, nres);
1122 GNUNET_assert (qsize == op->nqueues);
1127 * Add an operation to a queue. An operation can be in multiple queues at
1128 * once. Once the operation is inserted into all the queues
1129 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1130 * waiting for the operation to become active. The operation is assumed to take
1131 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
1132 * requires more than 1
1134 * @param queue queue to add the operation to
1135 * @param op operation to add to the queue
1138 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
1139 struct GNUNET_TESTBED_Operation *op)
1141 return GNUNET_TESTBED_operation_queue_insert2_ (queue, op, 1);
1146 * Marks the given operation as waiting on the queues. Once all queues permit
1147 * the operation to become active, the operation will be activated. The actual
1148 * activation will occur in a separate task (thus allowing multiple queue
1149 * insertions to be made without having the first one instantly trigger the
1150 * operation if the first queue has sufficient resources).
1152 * @param op the operation to marks as waiting
1155 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op)
1157 GNUNET_assert (NULL == op->rq_entry);
1158 change_state (op, OP_STATE_WAITING);
1159 (void) check_readiness (op);
1164 * Marks an active operation as inactive - the operation will be kept in a
1165 * ready-to-be-released state and continues to hold resources until another
1166 * operation contents for them.
1168 * @param op the operation to be marked as inactive. The operation start
1169 * callback should have been called before for this operation to mark
1173 GNUNET_TESTBED_operation_inactivate_ (struct GNUNET_TESTBED_Operation *op)
1175 struct OperationQueue **queues;
1177 unsigned int nqueues;
1180 GNUNET_assert (OP_STATE_ACTIVE == op->state);
1181 change_state (op, OP_STATE_INACTIVE);
1182 nqueues = op->nqueues;
1183 ms = sizeof (struct OperationQueue *) * nqueues;
1184 queues = GNUNET_malloc (ms);
1185 /* Cloning is needed as the operation be released by waiting operations and
1186 hence its nqueues memory ptr will be freed */
1187 GNUNET_assert (NULL != (queues = memcpy (queues, op->queues, ms)));
1188 for (i = 0; i < nqueues; i++)
1189 recheck_waiting (queues[i]);
1190 GNUNET_free (queues);
1195 * Marks and inactive operation as active. This fuction should be called to
1196 * ensure that the oprelease callback will not be called until it is either
1197 * marked as inactive or released.
1199 * @param op the operation to be marked as active
1202 GNUNET_TESTBED_operation_activate_ (struct GNUNET_TESTBED_Operation *op)
1205 GNUNET_assert (OP_STATE_INACTIVE == op->state);
1206 change_state (op, OP_STATE_ACTIVE);
1211 * An operation is 'done' (was cancelled or finished); remove
1212 * it from the queues and release associated resources.
1214 * @param op operation that finished
1217 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op)
1219 struct QueueEntry *entry;
1220 struct OperationQueue *opq;
1223 if (OP_STATE_INIT == op->state)
1228 if (OP_STATE_READY == op->state)
1230 if (OP_STATE_INACTIVE == op->state) /* Activate the operation if inactive */
1231 GNUNET_TESTBED_operation_activate_ (op);
1232 if (OP_STATE_ACTIVE == op->state)
1234 GNUNET_assert (NULL != op->queues);
1235 GNUNET_assert (NULL != op->qentries);
1236 for (i = 0; i < op->nqueues; i++)
1238 entry = op->qentries[i];
1239 remove_queue_entry (op, i);
1240 opq = op->queues[i];
1244 case OP_STATE_INACTIVE:
1247 case OP_STATE_WAITING:
1249 case OP_STATE_ACTIVE:
1250 case OP_STATE_READY:
1251 GNUNET_assert (0 != opq->active);
1252 GNUNET_assert (opq->active >= entry->nres);
1253 opq->active -= entry->nres;
1254 recheck_waiting (opq);
1257 GNUNET_free (entry);
1259 GNUNET_free_non_null (op->qentries);
1260 GNUNET_free (op->queues);
1261 GNUNET_free (op->nres);
1262 if (NULL != op->release)
1263 op->release (op->cb_cls);
1269 * Marks an operation as failed
1271 * @param op the operation to be marked as failed
1274 GNUNET_TESTBED_operation_mark_failed (struct GNUNET_TESTBED_Operation *op)
1276 op->failed = GNUNET_YES;
1280 /* end of testbed_api_operations.c */