2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
33 * The number of readings containing past operation's timing information that we
34 * keep track of for adaptive queues
36 #define ADAPTIVE_QUEUE_DEFAULT_HISTORY 40
39 * The number of parallel opeartions we start with by default for adaptive
42 #define ADAPTIVE_QUEUE_DEFAULT_MAX_ACTIVE 4
45 * An entry in the operation queue
50 * The next DLL pointer
52 struct QueueEntry *next;
55 * The prev DLL pointer
57 struct QueueEntry *prev;
60 * The operation this entry holds
62 struct GNUNET_TESTBED_Operation *op;
65 * How many units of resources does the operation need
72 * Queue of operations where we can only support a certain
73 * number of concurrent operations of a particular type.
75 struct OperationQueue;
79 * A slot to record time taken by an operation
86 struct TimeSlot *next;
91 struct TimeSlot *prev;
94 * This operation queue to which this time slot belongs to
96 struct OperationQueue *queue;
99 * The operation to which this timeslot is currently allocated to
101 struct GNUNET_TESTBED_Operation *op;
106 struct GNUNET_TIME_Relative tsum;
109 * Number of timing values accumulated
116 * Context for operation queues of type OPERATION_QUEUE_TYPE_ADAPTIVE
121 * Handle for calculating standard deviation
126 * Head for DLL of time slots which are free to be allocated to operations
128 struct TimeSlot *alloc_head;
131 * Tail for DLL of time slots which are free to be allocated to operations
133 struct TimeSlot *alloc_tail;
136 * Pointer to the chunk of time slots. Free all time slots at a time using
139 struct TimeSlot *tslots_freeptr;
142 * Number of time slots filled so far
144 unsigned int tslots_filled;
147 * Bound on the maximum number of operations which can be active
149 unsigned int max_active_bound;
152 * Number of operations that have failed
154 unsigned int nfailed;
159 * Queue of operations where we can only support a certain
160 * number of concurrent operations of a particular type.
162 struct OperationQueue
165 * DLL head for the wait queue. Operations which are waiting for this
166 * operation queue are put here
168 struct QueueEntry *wq_head;
171 * DLL tail for the wait queue.
173 struct QueueEntry *wq_tail;
176 * DLL head for the ready queue. Operations which are in this operation queue
177 * and are in ready state are put here
179 struct QueueEntry *rq_head;
182 * DLL tail for the ready queue
184 struct QueueEntry *rq_tail;
187 * DLL head for the active queue. Operations which are in this operation
188 * queue and are currently active are put here
190 struct QueueEntry *aq_head;
193 * DLL tail for the active queue.
195 struct QueueEntry *aq_tail;
198 * DLL head for the inactive queue. Operations which are inactive and can be
199 * evicted if the queues it holds are maxed out and another operation begins
202 struct QueueEntry *nq_head;
205 * DLL tail for the inactive queue.
207 struct QueueEntry *nq_tail;
210 * Feedback context; only relevant for adaptive operation queues. NULL for
211 * fixed operation queues
213 struct FeedbackCtx *fctx;
216 * The type of this opeartion queue
218 enum OperationQueueType type;
221 * Number of operations that are currently active in this queue.
226 * Max number of operations which can be active at any time in this queue.
227 * This value can be changed either by calling
228 * GNUNET_TESTBED_operation_queue_reset_max_active_() or by the adaptive
229 * algorithm if this operation queue is of type #OPERATION_QUEUE_TYPE_ADAPTIVE
231 unsigned int max_active;
234 * The number of resources occupied by failed operations in the current shot.
235 * This is only relavant if the operation queue is of type
236 * #OPERATION_QUEUE_TYPE_ADAPTIVE
238 unsigned int overload;
241 * Is this queue marked for expiry?
243 unsigned int expired;
253 * The operation is just created and is in initial state
258 * The operation is currently waiting for resources
263 * The operation is ready to be started
268 * The operation has started and is active
273 * The operation is inactive. It still holds resources on the operation
274 * queues. However, this operation will be evicted when another operation
275 * requires resources from the maxed out queues this operation is holding
283 * An entry in the ready queue (implemented as DLL)
285 struct ReadyQueueEntry
290 struct ReadyQueueEntry *next;
295 struct ReadyQueueEntry *prev;
298 * The operation associated with this entry
300 struct GNUNET_TESTBED_Operation *op;
305 * Opaque handle to an abstract operation to be executed by the testing framework.
307 struct GNUNET_TESTBED_Operation
310 * Function to call when we have the resources to begin the operation.
312 OperationStart start;
315 * Function to call to clean up after the operation (which may or may
316 * not have been started yet).
318 OperationRelease release;
321 * Closure for callbacks.
326 * Array of operation queues this Operation belongs to.
328 struct OperationQueue **queues;
331 * Array of operation queue entries corresponding to this operation in
332 * operation queues for this operation
334 struct QueueEntry **qentries;
337 * Array of number of resources an operation need from each queue. The numbers
338 * in this array should correspond to the queues array
343 * Entry corresponding to this operation in ready queue. Will be NULL if the
344 * operation is not marked as READY
346 struct ReadyQueueEntry *rq_entry;
349 * Head pointer for DLL of tslots allocated to this operation
351 struct TimeSlot *tslots_head;
354 * Tail pointer for DLL of tslots allocated to this operation
356 struct TimeSlot *tslots_tail;
359 * The time at which the operation is started
361 struct GNUNET_TIME_Absolute tstart;
364 * Number of queues in the operation queues array
366 unsigned int nqueues;
369 * The state of the operation
371 enum OperationState state;
374 * Is this a failed operation?
380 * DLL head for the ready queue
382 static struct ReadyQueueEntry *rq_head;
385 * DLL tail for the ready queue
387 static struct ReadyQueueEntry *rq_tail;
390 * Array of operation queues which are to be destroyed
392 static struct OperationQueue **expired_opqs;
395 * Number of expired operation queues in the above array
397 static unsigned int n_expired_opqs;
400 * The id of the task to process the ready queue
402 struct GNUNET_SCHEDULER_Task *process_rq_task_id;
406 * Assigns the given operation a time slot from the given operation queue
408 * @param op the operation
409 * @param queue the operation queue
410 * @return the timeslot
413 assign_timeslot (struct GNUNET_TESTBED_Operation *op,
414 struct OperationQueue *queue)
416 struct FeedbackCtx *fctx = queue->fctx;
417 struct TimeSlot *tslot;
419 GNUNET_assert (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type);
420 tslot = fctx->alloc_head;
421 GNUNET_assert (NULL != tslot);
422 GNUNET_CONTAINER_DLL_remove (fctx->alloc_head, fctx->alloc_tail, tslot);
423 GNUNET_CONTAINER_DLL_insert_tail (op->tslots_head, op->tslots_tail, tslot);
429 * Removes a queue entry of an operation from one of the operation queues' lists
430 * depending on the state of the operation
432 * @param op the operation whose entry has to be removed
433 * @param index the index of the entry in the operation's array of queue entries
436 remove_queue_entry (struct GNUNET_TESTBED_Operation *op, unsigned int index)
438 struct OperationQueue *opq;
439 struct QueueEntry *entry;
441 opq = op->queues[index];
442 entry = op->qentries[index];
449 case OP_STATE_WAITING:
450 GNUNET_CONTAINER_DLL_remove (opq->wq_head, opq->wq_tail, entry);
454 GNUNET_CONTAINER_DLL_remove (opq->rq_head, opq->rq_tail, entry);
457 case OP_STATE_ACTIVE:
458 GNUNET_CONTAINER_DLL_remove (opq->aq_head, opq->aq_tail, entry);
461 case OP_STATE_INACTIVE:
462 GNUNET_CONTAINER_DLL_remove (opq->nq_head, opq->nq_tail, entry);
469 * Changes the state of the operation while moving its associated queue entries
470 * in the operation's operation queues
472 * @param op the operation whose state has to be changed
473 * @param state the state the operation should have. It cannot be OP_STATE_INIT
476 change_state (struct GNUNET_TESTBED_Operation *op, enum OperationState state)
478 struct QueueEntry *entry;
479 struct OperationQueue *opq;
483 GNUNET_assert (OP_STATE_INIT != state);
484 GNUNET_assert (NULL != op->queues);
485 GNUNET_assert (NULL != op->nres);
486 GNUNET_assert ((OP_STATE_INIT == op->state) || (NULL != op->qentries));
487 GNUNET_assert (op->state != state);
488 for (cnt = 0; cnt < op->nqueues; cnt++)
490 if (OP_STATE_INIT == op->state)
492 entry = GNUNET_new (struct QueueEntry);
494 entry->nres = op->nres[cnt];
496 GNUNET_array_append (op->qentries, s, entry);
500 entry = op->qentries[cnt];
501 remove_queue_entry (op, cnt);
503 opq = op->queues[cnt];
510 case OP_STATE_WAITING:
511 GNUNET_CONTAINER_DLL_insert_tail (opq->wq_head, opq->wq_tail, entry);
515 GNUNET_CONTAINER_DLL_insert_tail (opq->rq_head, opq->rq_tail, entry);
518 case OP_STATE_ACTIVE:
519 GNUNET_CONTAINER_DLL_insert_tail (opq->aq_head, opq->aq_tail, entry);
522 case OP_STATE_INACTIVE:
523 GNUNET_CONTAINER_DLL_insert_tail (opq->nq_head, opq->nq_tail, entry);
532 * Removes an operation from the ready queue. Also stops the 'process_rq_task'
533 * if the given operation is the last one in the queue.
535 * @param op the operation to be removed
538 rq_remove (struct GNUNET_TESTBED_Operation *op)
540 GNUNET_assert (NULL != op->rq_entry);
541 GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
542 GNUNET_free (op->rq_entry);
544 if ((NULL == rq_head) && (NULL != process_rq_task_id))
546 GNUNET_SCHEDULER_cancel (process_rq_task_id);
547 process_rq_task_id = NULL;
553 * Processes the ready queue by calling the operation start callback of the
554 * operation at the head. The operation is then removed from the queue. The
555 * task is scheduled to run again immediately until no more operations are in
561 process_rq_task (void *cls)
563 struct GNUNET_TESTBED_Operation *op;
564 struct OperationQueue *queue;
567 process_rq_task_id = NULL;
568 GNUNET_assert (NULL != rq_head);
569 GNUNET_assert (NULL != (op = rq_head->op));
572 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
573 change_state (op, OP_STATE_ACTIVE);
574 for (cnt = 0; cnt < op->nqueues; cnt++)
576 queue = op->queues[cnt];
577 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
578 assign_timeslot (op, queue);
580 op->tstart = GNUNET_TIME_absolute_get ();
581 if (NULL != op->start)
582 op->start (op->cb_cls);
587 * Adds the operation to the ready queue and starts the 'process_rq_task'
589 * @param op the operation to be queued
592 rq_add (struct GNUNET_TESTBED_Operation *op)
594 struct ReadyQueueEntry *rq_entry;
596 GNUNET_assert (NULL == op->rq_entry);
597 rq_entry = GNUNET_new (struct ReadyQueueEntry);
599 GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
600 op->rq_entry = rq_entry;
601 if (NULL == process_rq_task_id)
602 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
607 * Checks if the given operation queue is empty or not
609 * @param opq the operation queue
610 * @return GNUNET_YES if the given operation queue has no operations; GNUNET_NO
614 is_queue_empty (struct OperationQueue *opq)
616 if ((NULL != opq->wq_head)
617 || (NULL != opq->rq_head)
618 || (NULL != opq->aq_head)
619 || (NULL != opq->nq_head))
626 * Checks if the given operation queue has enough resources to provide for the
627 * operation of the given queue entry. It also checks if any inactive
628 * operations are to be released in order to accommodate the needed resources
629 * and returns them as an array.
631 * @param opq the operation queue to check for resource accommodation
632 * @param entry the operation queue entry whose operation's resources are to be
634 * @param ops_ pointer to return the array of operations which are to be released
635 * in order to accommodate the new operation. Can be NULL
636 * @param n_ops_ the number of operations in ops_
637 * @return GNUNET_YES if the given entry's operation can be accommodated in this
638 * queue. GNUNET_NO if it cannot be accommodated; ops_ and n_ops_ will
639 * be set to NULL and 0 respectively.
642 decide_capacity (struct OperationQueue *opq,
643 struct QueueEntry *entry,
644 struct GNUNET_TESTBED_Operation ***ops_,
645 unsigned int *n_ops_)
647 struct QueueEntry **evict_entries;
648 struct GNUNET_TESTBED_Operation **ops;
649 struct GNUNET_TESTBED_Operation *op;
651 unsigned int n_evict_entries;
657 GNUNET_assert (NULL != (op = entry->op));
658 GNUNET_assert (0 < (need = entry->nres));
661 evict_entries = NULL;
664 if (OPERATION_QUEUE_TYPE_ADAPTIVE == opq->type)
666 GNUNET_assert (NULL != opq->fctx);
667 GNUNET_assert (opq->max_active >= opq->overload);
668 max = opq->max_active - opq->overload;
671 max = opq->max_active;
672 if (opq->active > max)
677 if ((opq->active + need) <= max)
679 deficit = need - (max - opq->active);
680 for (entry = opq->nq_head;
681 (0 < deficit) && (NULL != entry);
684 GNUNET_array_append (evict_entries, n_evict_entries, entry);
685 deficit -= entry->nres;
692 for (n_ops = 0; n_ops < n_evict_entries;)
694 op = evict_entries[n_ops]->op;
695 GNUNET_array_append (ops, n_ops, op); /* increments n-ops */
699 GNUNET_free_non_null (evict_entries);
711 * Merges an array of operations into another, eliminating duplicates. No
712 * ordering is guaranteed.
714 * @param old the array into which the merging is done.
715 * @param n_old the number of operations in old array
716 * @param new the array from which operations are to be merged
717 * @param n_new the number of operations in new array
720 merge_ops (struct GNUNET_TESTBED_Operation ***old,
722 struct GNUNET_TESTBED_Operation **new,
725 struct GNUNET_TESTBED_Operation **cur;
730 GNUNET_assert (NULL != old);
733 for (i = 0; i < n_new; i++)
735 for (j = 0; j < *n_old; j++)
737 if (new[i] == cur[j])
742 GNUNET_array_append (cur, n_cur, new[j]);
750 * Checks for the readiness of an operation and schedules a operation start task
752 * @param op the operation
755 check_readiness (struct GNUNET_TESTBED_Operation *op)
757 struct GNUNET_TESTBED_Operation **evict_ops;
758 struct GNUNET_TESTBED_Operation **ops;
760 unsigned int n_evict_ops;
763 GNUNET_assert (NULL == op->rq_entry);
764 GNUNET_assert (OP_STATE_WAITING == op->state);
767 for (i = 0; i < op->nqueues; i++)
771 if (GNUNET_NO == decide_capacity (op->queues[i], op->qentries[i],
774 GNUNET_free_non_null (evict_ops);
779 merge_ops (&evict_ops, &n_evict_ops, ops, n_ops);
782 if (NULL != evict_ops)
784 for (i = 0; i < n_evict_ops; i++)
785 GNUNET_TESTBED_operation_release_ (evict_ops[i]);
786 GNUNET_free (evict_ops);
788 /* Evicting the operations should schedule this operation */
789 GNUNET_assert (OP_STATE_READY == op->state);
792 for (i = 0; i < op->nqueues; i++)
793 op->queues[i]->active += op->nres[i];
794 change_state (op, OP_STATE_READY);
801 * Defers a ready to be executed operation back to waiting
803 * @param op the operation to defer
806 defer (struct GNUNET_TESTBED_Operation *op)
810 GNUNET_assert (OP_STATE_READY == op->state);
812 for (i = 0; i < op->nqueues; i++)
814 GNUNET_assert (op->queues[i]->active >= op->nres[i]);
815 op->queues[i]->active -= op->nres[i];
817 change_state (op, OP_STATE_WAITING);
822 * Cleanups the array of timeslots of an operation queue. For each time slot in
823 * the array, if it is allocated to an operation, it will be deallocated from
826 * @param queue the operation queue
829 cleanup_tslots (struct OperationQueue *queue)
831 struct FeedbackCtx *fctx = queue->fctx;
832 struct TimeSlot *tslot;
833 struct GNUNET_TESTBED_Operation *op;
836 GNUNET_assert (NULL != fctx);
837 for (cnt = 0; cnt < queue->max_active; cnt++)
839 tslot = &fctx->tslots_freeptr[cnt];
843 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
845 GNUNET_free_non_null (fctx->tslots_freeptr);
846 fctx->tslots_freeptr = NULL;
847 fctx->alloc_head = NULL;
848 fctx->alloc_tail = NULL;
849 fctx->tslots_filled = 0;
854 * Cleansup the existing timing slots and sets new timing slots in the given
855 * queue to accommodate given number of max active operations.
857 * @param queue the queue
858 * @param n the number of maximum active operations. If n is greater than the
859 * maximum limit set while creating the queue, then the minimum of these two
860 * will be selected as n
863 adaptive_queue_set_max_active (struct OperationQueue *queue, unsigned int n)
865 struct FeedbackCtx *fctx = queue->fctx;
866 struct TimeSlot *tslot;
869 cleanup_tslots (queue);
870 n = GNUNET_MIN (n, fctx->max_active_bound);
871 fctx->tslots_freeptr = GNUNET_malloc (n * sizeof(struct TimeSlot));
873 for (cnt = 0; cnt < n; cnt++)
875 tslot = &fctx->tslots_freeptr[cnt];
876 tslot->queue = queue;
877 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail,
880 GNUNET_TESTBED_operation_queue_reset_max_active_ (queue, n);
885 * Adapts parallelism in an adaptive queue by using the statistical data from
886 * the feedback context.
888 * @param queue the queue
891 adapt_parallelism (struct OperationQueue *queue)
893 struct GNUNET_TIME_Relative avg;
894 struct FeedbackCtx *fctx;
895 struct TimeSlot *tslot;
899 unsigned int parallelism;
901 avg = GNUNET_TIME_UNIT_ZERO;
904 for (cnt = 0; cnt < queue->max_active; cnt++)
906 tslot = &fctx->tslots_freeptr[cnt];
907 avg = GNUNET_TIME_relative_add (avg, tslot->tsum);
908 nvals += tslot->nvals;
910 GNUNET_assert (nvals >= queue->max_active);
911 GNUNET_assert (fctx->nfailed <= nvals);
912 nvals -= fctx->nfailed;
915 if (1 == queue->max_active)
916 adaptive_queue_set_max_active (queue, 1);
918 adaptive_queue_set_max_active (queue, queue->max_active / 2);
921 avg = GNUNET_TIME_relative_divide (avg, nvals);
922 GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
924 GNUNET_TESTBED_SD_deviation_factor_ (fctx->sd,
925 (unsigned int) avg.rel_value_us,
928 adaptive_queue_set_max_active (queue, queue->max_active); /* no change */
934 parallelism = queue->max_active + 1;
936 parallelism = queue->max_active * 2;
938 parallelism = queue->max_active - 1;
940 parallelism = queue->max_active / 2;
941 parallelism = GNUNET_MAX (parallelism, ADAPTIVE_QUEUE_DEFAULT_MAX_ACTIVE);
942 adaptive_queue_set_max_active (queue, parallelism);
948 GNUNET_assert (0 <= sd);
949 // GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
952 adaptive_queue_set_max_active (queue, queue->max_active * 2);
957 adaptive_queue_set_max_active (queue, queue->max_active + 1);
960 if (1 == queue->max_active)
962 adaptive_queue_set_max_active (queue, 1);
967 adaptive_queue_set_max_active (queue, queue->max_active - 1);
970 adaptive_queue_set_max_active (queue, queue->max_active / 2);
976 * update tslots with the operation's completion time. Additionally, if
977 * updating a timeslot makes all timeslots filled in an adaptive operation
978 * queue, call adapt_parallelism() for that queue.
980 * @param op the operation
983 update_tslots (struct GNUNET_TESTBED_Operation *op)
985 struct OperationQueue *queue;
986 struct GNUNET_TIME_Relative t;
987 struct TimeSlot *tslot;
988 struct FeedbackCtx *fctx;
991 t = GNUNET_TIME_absolute_get_duration (op->tstart);
992 while (NULL != (tslot = op->tslots_head)) /* update time slots */
994 queue = tslot->queue;
996 GNUNET_CONTAINER_DLL_remove (op->tslots_head, op->tslots_tail, tslot);
998 GNUNET_CONTAINER_DLL_insert_tail (fctx->alloc_head, fctx->alloc_tail,
1003 for (i = 0; i < op->nqueues; i++)
1004 if (queue == op->queues[i])
1006 GNUNET_assert (i != op->nqueues);
1007 op->queues[i]->overload += op->nres[i];
1009 tslot->tsum = GNUNET_TIME_relative_add (tslot->tsum, t);
1010 if (0 != tslot->nvals++)
1012 fctx->tslots_filled++;
1013 if (queue->max_active == fctx->tslots_filled)
1014 adapt_parallelism (queue);
1020 * Create an 'operation' to be performed.
1022 * @param cls closure for the callbacks
1023 * @param start function to call to start the operation
1024 * @param release function to call to close down the operation
1025 * @return handle to the operation
1027 struct GNUNET_TESTBED_Operation *
1028 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
1029 OperationRelease release)
1031 struct GNUNET_TESTBED_Operation *op;
1033 op = GNUNET_new (struct GNUNET_TESTBED_Operation);
1035 op->state = OP_STATE_INIT;
1036 op->release = release;
1043 * Create an operation queue.
1045 * @param type the type of operation queue
1046 * @param max_active maximum number of operations in this
1047 * queue that can be active in parallel at the same time
1048 * @return handle to the queue
1050 struct OperationQueue *
1051 GNUNET_TESTBED_operation_queue_create_ (enum OperationQueueType type,
1052 unsigned int max_active)
1054 struct OperationQueue *queue;
1055 struct FeedbackCtx *fctx;
1057 queue = GNUNET_new (struct OperationQueue);
1059 if (OPERATION_QUEUE_TYPE_FIXED == type)
1061 queue->max_active = max_active;
1065 fctx = GNUNET_new (struct FeedbackCtx);
1066 fctx->max_active_bound = max_active;
1067 fctx->sd = GNUNET_TESTBED_SD_init_ (ADAPTIVE_QUEUE_DEFAULT_HISTORY);
1069 adaptive_queue_set_max_active (queue, ADAPTIVE_QUEUE_DEFAULT_MAX_ACTIVE);
1076 * Cleanup the given operation queue.
1078 * @param queue the operation queue to destroy
1081 queue_destroy (struct OperationQueue *queue)
1083 struct FeedbackCtx *fctx;
1085 if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
1087 cleanup_tslots (queue);
1089 GNUNET_TESTBED_SD_destroy_ (fctx->sd);
1092 GNUNET_free (queue);
1097 * Destroys an operation queue. If the queue is still in use by operations it
1098 * is marked as expired and its resources are released in the destructor
1099 * GNUNET_TESTBED_operations_fini().
1101 * @param queue queue to destroy
1104 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
1106 if (GNUNET_YES != is_queue_empty (queue))
1108 GNUNET_assert (0 == queue->expired); /* Are you calling twice on same queue? */
1110 GNUNET_array_append (expired_opqs, n_expired_opqs, queue);
1113 queue_destroy (queue);
1118 * Destroys the operation queue if it is empty. If not empty return GNUNET_NO.
1120 * @param queue the queue to destroy if empty
1121 * @return GNUNET_YES if the queue is destroyed. GNUNET_NO if not (because it
1125 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
1127 if (GNUNET_NO == is_queue_empty (queue))
1129 GNUNET_TESTBED_operation_queue_destroy_ (queue);
1135 * Rechecks if any of the operations in the given operation queue's waiting list
1136 * can be made active
1138 * @param opq the operation queue
1141 recheck_waiting (struct OperationQueue *opq)
1143 struct QueueEntry *entry;
1144 struct QueueEntry *entry2;
1146 entry = opq->wq_head;
1147 while (NULL != entry)
1149 entry2 = entry->next;
1150 if (GNUNET_NO == check_readiness (entry->op))
1158 * Function to reset the maximum number of operations in the given queue. If
1159 * max_active is lesser than the number of currently active operations, the
1160 * active operations are not stopped immediately.
1162 * @param queue the operation queue which has to be modified
1163 * @param max_active the new maximum number of active operations
1166 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
1167 unsigned int max_active)
1169 struct QueueEntry *entry;
1171 queue->max_active = max_active;
1172 queue->overload = 0;
1173 while ((queue->active > queue->max_active)
1174 && (NULL != (entry = queue->rq_head)))
1176 recheck_waiting (queue);
1181 * Add an operation to a queue. An operation can be in multiple queues at
1182 * once. Once the operation is inserted into all the queues
1183 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1184 * waiting for the operation to become active.
1186 * @param queue queue to add the operation to
1187 * @param op operation to add to the queue
1188 * @param nres the number of units of the resources of queue needed by the
1189 * operation. Should be greater than 0.
1192 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
1193 struct GNUNET_TESTBED_Operation *op,
1198 GNUNET_assert (0 < nres);
1199 qsize = op->nqueues;
1200 GNUNET_array_append (op->queues, op->nqueues, queue);
1201 GNUNET_array_append (op->nres, qsize, nres);
1202 GNUNET_assert (qsize == op->nqueues);
1207 * Add an operation to a queue. An operation can be in multiple queues at
1208 * once. Once the operation is inserted into all the queues
1209 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
1210 * waiting for the operation to become active. The operation is assumed to take
1211 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
1212 * requires more than 1
1214 * @param queue queue to add the operation to
1215 * @param op operation to add to the queue
1218 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
1219 struct GNUNET_TESTBED_Operation *op)
1221 return GNUNET_TESTBED_operation_queue_insert2_ (queue, op, 1);
1226 * Marks the given operation as waiting on the queues. Once all queues permit
1227 * the operation to become active, the operation will be activated. The actual
1228 * activation will occur in a separate task (thus allowing multiple queue
1229 * insertions to be made without having the first one instantly trigger the
1230 * operation if the first queue has sufficient resources).
1232 * @param op the operation to marks as waiting
1235 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op)
1237 GNUNET_assert (NULL == op->rq_entry);
1238 change_state (op, OP_STATE_WAITING);
1239 (void) check_readiness (op);
1244 * Marks an active operation as inactive - the operation will be kept in a
1245 * ready-to-be-released state and continues to hold resources until another
1246 * operation contents for them.
1248 * @param op the operation to be marked as inactive. The operation start
1249 * callback should have been called before for this operation to mark
1253 GNUNET_TESTBED_operation_inactivate_ (struct GNUNET_TESTBED_Operation *op)
1255 struct OperationQueue **queues;
1257 unsigned int nqueues;
1260 GNUNET_assert (OP_STATE_ACTIVE == op->state);
1261 change_state (op, OP_STATE_INACTIVE);
1262 nqueues = op->nqueues;
1263 ms = sizeof(struct OperationQueue *) * nqueues;
1264 queues = GNUNET_malloc (ms);
1265 /* Cloning is needed as the operation be released by waiting operations and
1266 hence its nqueues memory ptr will be freed */
1267 GNUNET_memcpy (queues, op->queues, ms);
1268 for (i = 0; i < nqueues; i++)
1269 recheck_waiting (queues[i]);
1270 GNUNET_free (queues);
1275 * Marks and inactive operation as active. This fuction should be called to
1276 * ensure that the oprelease callback will not be called until it is either
1277 * marked as inactive or released.
1279 * @param op the operation to be marked as active
1282 GNUNET_TESTBED_operation_activate_ (struct GNUNET_TESTBED_Operation *op)
1284 GNUNET_assert (OP_STATE_INACTIVE == op->state);
1285 change_state (op, OP_STATE_ACTIVE);
1290 * An operation is 'done' (was cancelled or finished); remove
1291 * it from the queues and release associated resources.
1293 * @param op operation that finished
1296 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op)
1298 struct QueueEntry *entry;
1299 struct OperationQueue *opq;
1302 if (OP_STATE_INIT == op->state)
1307 if (OP_STATE_READY == op->state)
1309 if (OP_STATE_INACTIVE == op->state) /* Activate the operation if inactive */
1310 GNUNET_TESTBED_operation_activate_ (op);
1311 if (OP_STATE_ACTIVE == op->state)
1313 GNUNET_assert (NULL != op->queues);
1314 GNUNET_assert (NULL != op->qentries);
1315 for (i = 0; i < op->nqueues; i++)
1317 entry = op->qentries[i];
1318 remove_queue_entry (op, i);
1319 opq = op->queues[i];
1323 case OP_STATE_INACTIVE:
1327 case OP_STATE_WAITING:
1330 case OP_STATE_ACTIVE:
1331 case OP_STATE_READY:
1332 GNUNET_assert (0 != opq->active);
1333 GNUNET_assert (opq->active >= entry->nres);
1334 opq->active -= entry->nres;
1335 recheck_waiting (opq);
1338 GNUNET_free (entry);
1340 GNUNET_free_non_null (op->qentries);
1341 GNUNET_free (op->queues);
1342 GNUNET_free (op->nres);
1343 if (NULL != op->release)
1344 op->release (op->cb_cls);
1350 * Marks an operation as failed
1352 * @param op the operation to be marked as failed
1355 GNUNET_TESTBED_operation_mark_failed (struct GNUNET_TESTBED_Operation *op)
1357 op->failed = GNUNET_YES;
1362 * Cleanup expired operation queues. While doing so, also check for any
1363 * operations which are not completed and warn about them.
1365 void __attribute__ ((destructor))
1366 GNUNET_TESTBED_operations_fini ()
1368 struct OperationQueue *queue;
1372 for (i = 0; i < n_expired_opqs; i++)
1374 queue = expired_opqs[i];
1375 if (GNUNET_NO == is_queue_empty (queue))
1377 queue_destroy (queue);
1379 GNUNET_free_non_null (expired_opqs);
1382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1383 "Be disciplined. Some operations were not marked as done.\n");
1387 /* end of testbed_api_operations.c */