-fix off-by-1
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
index 60c78c2805a0d2c85f6b56e2c9533c6014477ec2..065ea5d1bd9d2e406e58b648eb29aa99db239f65 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2014 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -406,28 +406,30 @@ static int in_shutdown = 0;
  */
 static unsigned int tries;
 
+
 /**
  * Task that collects successor statistics from all the peers.
+ *
  * @param cls
- * @param tc
  */
 static void
-collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+collect_stats (void *cls);
+
 
 /**
  * Connect to DHT services of active peers
  */
 static void
-start_profiling();
+start_profiling (void);
+
 
 /**
  * Shutdown task.  Cleanup all resources and operations.
  *
  * @param cls NULL
- * @param tc scheduler task context
  */
 static void
-do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
 {
   struct ActiveContext *ac;
   unsigned int cnt;
@@ -459,9 +461,21 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     a_ctx = NULL;
   }
   //FIXME: Should we collect stats only for put/get not for other messages.
-  if(NULL != bandwidth_stats_op)
+  if (NULL != bandwidth_stats_op)
+  {
     GNUNET_TESTBED_operation_done (bandwidth_stats_op);
-  bandwidth_stats_op = NULL;
+    bandwidth_stats_op = NULL;
+  }
+  if (NULL != successor_stats_op)
+  {
+    GNUNET_TESTBED_operation_done (successor_stats_op);
+    successor_stats_op = NULL;
+  }
+  if (NULL != successor_stats_task)
+  {
+    GNUNET_SCHEDULER_cancel (successor_stats_task);
+    successor_stats_task = NULL;
+  }
   GNUNET_free_non_null (a_ac);
 }
 
@@ -480,8 +494,10 @@ bandwidth_stats_cont (void *cls,
                       struct GNUNET_TESTBED_Operation *op,
                       const char *emsg)
 {
-  INFO ("# Outgoing bandwidth: %u\n", outgoing_bandwidth);
-  INFO ("# Incoming bandwidth: %u\n", incoming_bandwidth);
+  INFO ("# Outgoing bandwidth: %llu\n",
+        (unsigned long long) outgoing_bandwidth);
+  INFO ("# Incoming bandwidth: %llu\n",
+        (unsigned long long) incoming_bandwidth);
   GNUNET_SCHEDULER_shutdown ();
 }
 
@@ -494,8 +510,8 @@ bandwidth_stats_cont (void *cls,
  * @param subsystem name of subsystem that created the statistic
  * @param name the name of the datum
  * @param value the current value
- * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
- * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
+ * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
+ * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  */
 static int
 bandwidth_stats_iterator (void *cls,
@@ -546,10 +562,9 @@ summarize ()
  * Task to cancel DHT GET.
  *
  * @param cls NULL
- * @param tc scheduler task context
  */
 static void
-cancel_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+cancel_get (void *cls)
 {
   struct ActiveContext *ac = cls;
   struct Context *ctx = ac->ctx;
@@ -622,7 +637,8 @@ get_iter (void *cls,
 
   total_put_path_length = total_put_path_length + (double)put_path_length;
   total_get_path_length = total_get_path_length + (double)get_path_length;
-  DEBUG ("total_put_path_length = %f,put_path \n",total_put_path_length);
+  DEBUG ("total_put_path_length = %u,put_path \n",
+         total_put_path_length);
   /* Summarize if profiling is complete */
   if (n_active == n_gets_fail + n_gets_ok)
   {
@@ -637,10 +653,9 @@ get_iter (void *cls,
  * Task to do DHT GETs
  *
  * @param cls the active context
- * @param tc the scheduler task context
  */
 static void
-delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+delayed_get (void *cls)
 {
   struct ActiveContext *ac = cls;
   struct ActiveContext *get_ac;
@@ -681,16 +696,13 @@ delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * clear the operation during shutdown.
  *
  * @param cls the context
- * @return tc scheduler task context.
  */
 static void
-teardown_dht_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+teardown_dht_connection (void *cls)
 {
   struct Context *ctx = cls;
   struct GNUNET_TESTBED_Operation *op;
 
-  if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
-    return;
   GNUNET_assert (NULL != ctx);
   GNUNET_assert (NULL != (op = ctx->op));
   ctx->op = NULL;
@@ -725,13 +737,12 @@ put_cont (void *cls, int success)
 
 
 /**
- * Task to do DHT PUTS
+ * Task to do DHT PUTs
  *
  * @param cls the active context
- * @param tc the scheduler task context
  */
 static void
-delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+delayed_put (void *cls)
 {
   struct ActiveContext *ac = cls;
 
@@ -1070,7 +1081,7 @@ successor_stats_cont (void *cls,
   successor_stats_op = NULL;
   if (0 == max_searches)
   {
-    start_func();
+    start_func ();
     return;
   }
 
@@ -1108,39 +1119,27 @@ successor_stats_cont (void *cls,
                                                                  GNUNET_NO);
   if ((start_val == val) && (count == num_peers))
   {
-    DEBUG("CIRCLE COMPLETED after %u tries", tries);
+    DEBUG ("CIRCLE COMPLETED after %u tries", tries);
     if(NULL == successor_stats_task)
     {
       start_func();
     }
     return;
   }
-  else
+  if (max_searches == ++tries)
   {
-    if (max_searches == ++tries)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
-                  " circle formation.  Exiting\n",
-                  max_searches,tries);
-      if (NULL != successor_stats_task)
-      {
-        successor_stats_task = NULL;
-      }
-      if(NULL == successor_stats_task)
-      {
-        start_func();
-      }
-
-      return;
-    }
-    else
-    {
-      flag = 0;
-      successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
-                                                           &collect_stats, cls);
-    }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
+               " circle formation.  Exiting\n",
+               max_searches,tries);
+    start_func();
+    return;
   }
+  flag = 0;
+  successor_stats_task
+    = GNUNET_SCHEDULER_add_delayed (delay_stats,
+                                   &collect_stats,
+                                   cls);
 }
 
 
@@ -1210,27 +1209,25 @@ successor_stats_iterator (void *cls,
  * Task that collects peer and its corresponding successors.
  *
  * @param cls Closure (NULL).
- * @param tc Task Context.
  */
 static void
-collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+collect_stats (void *cls)
 {
-  if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
-    return;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
+  successor_stats_task = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Start collecting statistics...\n");
   GNUNET_assert(NULL != testbed_handles);
 
   if (0 != max_searches)
-  successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers,
-                                                                    GNUNET_NO);
-  successor_stats_op =
-          GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
-                                         "dht", NULL,
-                                          successor_stats_iterator,
-                                          successor_stats_cont, cls);
-
-  GNUNET_assert(NULL != successor_stats_op);
+    successor_peer_hashmap
+      = GNUNET_CONTAINER_multihashmap_create (num_peers,
+                                             GNUNET_NO);
+  successor_stats_op
+    = GNUNET_TESTBED_get_statistics (num_peers, testbed_handles,
+                                    "dht", NULL,
+                                    successor_stats_iterator,
+                                    successor_stats_cont, cls);
+  GNUNET_assert (NULL != successor_stats_op);
 }
 
 
@@ -1262,9 +1259,10 @@ service_started (void *cls,
      collect_stat_cls->service_connect_ctx = cls;
      collect_stat_cls->op = op;
 
-     successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
-                                                          &collect_stats,
-                                                          collect_stat_cls);
+     successor_stats_task
+       = GNUNET_SCHEDULER_add_delayed (delay_stats,
+                                      &collect_stats,
+                                      collect_stat_cls);
   }
 }
 
@@ -1390,7 +1388,8 @@ run (void *cls, char *const *args, const char *cfgfile,
 
   if (0 == num_peers)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("Exiting as the number of peers is %u\n"),
                 num_peers);
     return;
   }
@@ -1398,8 +1397,8 @@ run (void *cls, char *const *args, const char *cfgfile,
   event_mask = 0;
   GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, NULL,
                       NULL, &test_run, NULL);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                NULL);
 }