-improve UDP logging
[oweals/gnunet.git] / src / testbed / testbed_api_operations.c
index 07fb4940dc4112c828cb9214552e9a2a6c4f0033..7f49ec062654d7df1d358c8288aaecd1b127e406 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--2013 Christian Grothoff (and other contributing authors)
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
@@ -33,7 +33,7 @@
  * The number of readings containing past operation's timing information that we
  * keep track of for adaptive queues
  */
-#define ADAPTIVE_QUEUE_DEFAULT_HISTORY 10
+#define ADAPTIVE_QUEUE_DEFAULT_HISTORY 40
 
 /**
  * The number of parallel opeartions we start with by default for adaptive
@@ -236,6 +236,11 @@ struct OperationQueue
    * #OPERATION_QUEUE_TYPE_ADAPTIVE
    */
   unsigned int overload;
+
+  /**
+   * Is this queue marked for expiry?
+   */
+  unsigned int expired;
 };
 
 
@@ -375,17 +380,27 @@ struct GNUNET_TESTBED_Operation
 /**
  * DLL head for the ready queue
  */
-struct ReadyQueueEntry *rq_head;
+static struct ReadyQueueEntry *rq_head;
 
 /**
  * DLL tail for the ready queue
  */
-struct ReadyQueueEntry *rq_tail;
+static struct ReadyQueueEntry *rq_tail;
+
+/**
+ * Array of operation queues which are to be destroyed
+ */
+static struct OperationQueue **expired_opqs;
+
+/**
+ * Number of expired operation queues in the above array
+ */
+static unsigned int n_expired_opqs;
 
 /**
  * The id of the task to process the ready queue
  */
-GNUNET_SCHEDULER_TaskIdentifier process_rq_task_id;
+struct GNUNET_SCHEDULER_Task * process_rq_task_id;
 
 
 /**
@@ -519,10 +534,10 @@ rq_remove (struct GNUNET_TESTBED_Operation *op)
   GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
   GNUNET_free (op->rq_entry);
   op->rq_entry = NULL;
-  if ( (NULL == rq_head) && (GNUNET_SCHEDULER_NO_TASK != process_rq_task_id) )
+  if ( (NULL == rq_head) && (NULL != process_rq_task_id) )
   {
     GNUNET_SCHEDULER_cancel (process_rq_task_id);
-    process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
+    process_rq_task_id = NULL;
   }
 }
 
@@ -543,7 +558,7 @@ process_rq_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct OperationQueue *queue;
   unsigned int cnt;
 
-  process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
+  process_rq_task_id = NULL;
   GNUNET_assert (NULL != rq_head);
   GNUNET_assert (NULL != (op = rq_head->op));
   rq_remove (op);
@@ -577,7 +592,7 @@ rq_add (struct GNUNET_TESTBED_Operation *op)
   rq_entry->op = op;
   GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
   op->rq_entry = rq_entry;
-  if (GNUNET_SCHEDULER_NO_TASK == process_rq_task_id)
+  if (NULL == process_rq_task_id)
     process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
 }
 
@@ -850,7 +865,6 @@ adaptive_queue_set_max_active (struct OperationQueue *queue, unsigned int n)
   n = GNUNET_MIN (n ,fctx->max_active_bound);
   fctx->tslots_freeptr = GNUNET_malloc (n * sizeof (struct TimeSlot));
   fctx->nfailed = 0;
-  FPRINTF (stderr, "Parallelism: %u\n", n);
   for (cnt = 0; cnt < n; cnt++)
   {
     tslot = &fctx->tslots_freeptr[cnt];
@@ -876,6 +890,7 @@ adapt_parallelism (struct OperationQueue *queue)
   int sd;
   unsigned int nvals;
   unsigned int cnt;
+  unsigned int parallelism;
 
   avg = GNUNET_TIME_UNIT_ZERO;
   nvals = 0;
@@ -898,34 +913,34 @@ adapt_parallelism (struct OperationQueue *queue)
     return;
   }
   avg = GNUNET_TIME_relative_divide (avg, nvals);
+  GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
   if (GNUNET_SYSERR ==
       GNUNET_TESTBED_SD_deviation_factor_ (fctx->sd,
                                            (unsigned int) avg.rel_value_us,
                                            &sd))
   {
-    GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
     adaptive_queue_set_max_active (queue, queue->max_active); /* no change */
     return;
   }
+
+  parallelism = 0;
   if (-1 == sd)
-    adaptive_queue_set_max_active (queue, queue->max_active + 1);
+    parallelism = queue->max_active + 1;
   if (sd <= -2)
-    adaptive_queue_set_max_active (queue, queue->max_active * 2);
-  if (1 == queue->max_active)
-  {
-    adaptive_queue_set_max_active (queue, 1);
-    return;
-  }
+    parallelism = queue->max_active * 2;
   if (1 == sd)
-    adaptive_queue_set_max_active (queue, queue->max_active - 1);
+    parallelism = queue->max_active - 1;
   if (2 <= sd)
-    adaptive_queue_set_max_active (queue, queue->max_active / 2);
+    parallelism = queue->max_active / 2;
+  parallelism = GNUNET_MAX (parallelism, ADAPTIVE_QUEUE_DEFAULT_MAX_ACTIVE);
+  adaptive_queue_set_max_active (queue, parallelism);
 
-#if 0                           /* old algorithm */
+#if 0
+  /* old algorithm */
   if (sd < 0)
     sd = 0;
   GNUNET_assert (0 <= sd);
-  GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
+  //GNUNET_TESTBED_SD_add_data_ (fctx->sd, (unsigned int) avg.rel_value_us);
   if (0 == sd)
   {
     adaptive_queue_set_max_active (queue, queue->max_active * 2);
@@ -1052,17 +1067,15 @@ GNUNET_TESTBED_operation_queue_create_ (enum OperationQueueType type,
 
 
 /**
- * Destroy an operation queue.  The queue MUST be empty
- * at this time.
+ * Cleanup the given operation queue.
  *
- * @param queue queue to destroy
+ * @param queue the operation queue to destroy
  */
-void
-GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
+static void
+queue_destroy (struct OperationQueue *queue)
 {
   struct FeedbackCtx *fctx;
 
-  GNUNET_break (GNUNET_YES == is_queue_empty (queue));
   if (OPERATION_QUEUE_TYPE_ADAPTIVE == queue->type)
   {
     cleanup_tslots (queue);
@@ -1074,6 +1087,27 @@ GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
 }
 
 
+/**
+ * Destroys an operation queue.  If the queue is still in use by operations it
+ * is marked as expired and its resources are released in the destructor
+ * GNUNET_TESTBED_operations_fini().
+ *
+ * @param queue queue to destroy
+ */
+void
+GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
+{
+  if (GNUNET_YES != is_queue_empty (queue))
+  {
+    GNUNET_assert (0 == queue->expired); /* Are you calling twice on same queue? */
+    queue->expired = 1;
+    GNUNET_array_append (expired_opqs, n_expired_opqs, queue);
+    return;
+  }
+  queue_destroy (queue);
+}
+
+
 /**
  * Destroys the operation queue if it is empty.  If not empty return GNUNET_NO.
  *
@@ -1317,4 +1351,29 @@ GNUNET_TESTBED_operation_mark_failed (struct GNUNET_TESTBED_Operation *op)
 }
 
 
+/**
+ * Cleanup expired operation queues.  While doing so, also check for any
+ * operations which are not completed and warn about them.
+ */
+void __attribute__ ((destructor))
+GNUNET_TESTBED_operations_fini ()
+{
+  struct OperationQueue *queue;
+  unsigned int i;
+  int warn = 0;
+
+  for (i=0; i < n_expired_opqs; i++)
+  {
+    queue = expired_opqs[i];
+    if (GNUNET_NO == is_queue_empty (queue))
+      warn = 1;
+    queue_destroy (queue);
+  }
+  GNUNET_free_non_null (expired_opqs);
+  n_expired_opqs = 0;
+  if (warn)
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Be disciplined.  Some operations were not marked as done.\n");
+
+}
 /* end of testbed_api_operations.c */