convert fs publish to MQ
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
index f6223b3ddefb930851b5991272abd8811f747704..460eaa5724d964004a95cac12f9fcbb2ea3a9319 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (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
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  */
 #define PUT_PROBABILITY 50
 
-#if ENABLE_MALICIOUS
-/**
- * Number of peers which should act as malicious peers
- */
-#define MALICIOUS_PROBABILITY 20
-
-/**
- * Context for a peer which should act maliciously.
- */
-struct MaliciousContext;
-#endif
-
-
 /**
  * Configuration
  */
@@ -88,69 +75,8 @@ struct Context
    */
   struct ActiveContext *ac;
 
-#if ENABLE_MALICIOUS
-  /**
-   * Malicious context; NULL if this peer is NOT malicious.
-   */
-  struct MaliciousContext *mc;
-#endif
-};
-
-
-#if ENABLE_MALICIOUS
-/**
- * Context for a peer which should act maliciously.
- */
-struct MaliciousContext
-{
-  /**
-   * The linked peer context
-   */
-  struct Context *ctx;
-
-  /**
-   * Handler to the DHT service
-   */
-  struct GNUNET_DHT_Handle *dht;
-
-  /**
-   * Handler to malicious api
-   */
-  struct GNUNET_DHT_ActMaliciousHandle *dht_malicious;
 };
 
-/**
- * List of all the malicious peers contexts.
- */
-struct Context **malicious_peer_contexts = NULL;
-
-/**
- * Context for a peer which should act maliciously.
- */
-struct Malicious_Context
-{
-  /**
-   * The linked peer context
-   */
-  struct Context *ctx;
-
-  /**
-   * Handler to the DHT service
-   */
-  struct GNUNET_DHT_Handle *dht;
-};
-
-/**
- * Array of malicious peers.
- */
-static struct MaliciousContext *a_mc;
-
-/**
- * Number or malicious peers.
- */
-static unsigned int n_malicious;
-
-#endif
 
 /**
  * Context for a peer which actively does DHT PUT/GET
@@ -195,7 +121,7 @@ struct ActiveContext
   /**
    * Delay task
    */
-  GNUNET_SCHEDULER_TaskIdentifier delay_task;
+  struct GNUNET_SCHEDULER_Task * delay_task;
 
   /**
    * The size of the @e put_data
@@ -357,7 +283,7 @@ static int flag = 0;
 /**
  * Task to collect peer and its current successor statistics.
  */
-static GNUNET_SCHEDULER_TaskIdentifier successor_stats_task;
+static struct GNUNET_SCHEDULER_Task * successor_stats_task;
 
 /**
  * Closure for successor_stats_task.
@@ -406,28 +332,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;
@@ -441,7 +369,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       ac = a_ctx[cnt].ac;
       if (NULL != ac)
       {
-        if (GNUNET_SCHEDULER_NO_TASK != ac->delay_task)
+        if (NULL != ac->delay_task)
           GNUNET_SCHEDULER_cancel (ac->delay_task);
         if (NULL != ac->put_data)
           GNUNET_free (ac->put_data);
@@ -459,9 +387,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 +420,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 +436,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,15 +488,14 @@ 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;
 
-  ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
+  ac->delay_task = NULL;
   GNUNET_assert (NULL != ac->dht_get);
   GNUNET_DHT_get_stop (ac->dht_get);
   ac->dht_get = NULL;
@@ -613,16 +554,17 @@ get_iter (void *cls,
   get_ac->nrefs--;
   GNUNET_DHT_get_stop (ac->dht_get);
   ac->dht_get = NULL;
-  if (ac->delay_task != GNUNET_SCHEDULER_NO_TASK)
+  if (ac->delay_task != NULL)
     GNUNET_SCHEDULER_cancel (ac->delay_task);
-  ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
+  ac->delay_task = NULL;
   GNUNET_assert (NULL != ctx->op);
   GNUNET_TESTBED_operation_done (ctx->op);
   ctx->op = NULL;
 
   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,16 +579,15 @@ 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;
   unsigned int r;
 
-  ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
+  ac->delay_task = NULL;
   get_ac = NULL;
   while (1)
   {
@@ -681,16 +622,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,17 +663,16 @@ 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;
 
-  ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
+  ac->delay_task = NULL;
   /* Generate and DHT PUT some random data */
   ac->put_data_size = 16;       /* minimum */
   ac->put_data_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
@@ -744,16 +681,17 @@ delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
                               ac->put_data, ac->put_data_size);
   GNUNET_CRYPTO_hash (ac->put_data, ac->put_data_size, &ac->hash);
-  DEBUG ("PUT_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
-  ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
+  DEBUG ("PUT_REQUEST_START key %s\n",
+         GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
+  ac->dht_put = GNUNET_DHT_put (ac->dht,
+                                &ac->hash,
                                 replication,
                                 GNUNET_DHT_RO_RECORD_ROUTE,
                                 GNUNET_BLOCK_TYPE_TEST,
                                 ac->put_data_size,
                                 ac->put_data,
                                 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
-                                timeout,                      /* PUT timeout */
-                                put_cont, ac);                /* continuation and its closure */
+                                &put_cont, ac);                /* continuation and its closure */
   n_puts++;
 }
 
@@ -894,122 +832,6 @@ start_profiling()
   }
 }
 
-#if ENABLE_MALICIOUS
-/**
- * Count of total number of malicious peers.
- */
-static unsigned int count_malicious;
-
-/**
- * Continuation of GNUNET_DHT_act_malicious
- * @param cls Malicious context
-  * @param success #GNUNET_OK if the ACT_MALICIOUS was transmitted,
- *                 #GNUNET_NO on timeout,
- *                 #GNUNET_SYSERR on disconnect from service
- *                 after the ACT_MALICIOUS message was transmitted
- *                 (so we don't know if it was received or not)
- */
-static void
-act_malicious_cont (void *cls, int success)
-{
-  struct MaliciousContext *mc = cls;
-  struct Context *ctx = mc->ctx;
-
-  GNUNET_TESTBED_operation_done (ctx->op);
-  ctx->op = NULL;
-  return;
-}
-
-
-/**
- * Call malicious API for all the malicious peers.
- * @param cls the malicious context.
- * @param op the operation that has been finished
- * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
- * @param emsg error message in case the operation has failed; will be NULL if
- *          operation has executed successfully.
- */
-static void
-dht_set_malicious(void *cls,
-                  struct GNUNET_TESTBED_Operation *op,
-                  void *ca_result,
-                  const char *emsg)
-{
-  struct MaliciousContext *mc = cls;
-  struct Context *ctx = mc->ctx;
-
-  GNUNET_assert (NULL != ctx);
-  GNUNET_assert (NULL != ctx->op);
-  GNUNET_assert (ctx->op == op);
-  mc->dht = (struct GNUNET_DHT_Handle *) ca_result;
-  if (NULL != emsg)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
-    GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect_malicious() */
-    ctx->op = NULL;
-    return;
-  }
-  mc->dht_malicious = GNUNET_DHT_act_malicious(mc->dht, 1, act_malicious_cont, mc);
-}
-
-
-/**
- * Adapter function called to destroy a connection to
- * a service.
- *
- * @param cls the active context
- * @param op_result service handle returned from the connect adapter
- */
-static void
-dht_disconnect_malicious (void *cls, void *op_result)
-{
-  struct MaliciousContext *mc = cls;
-  count_malicious++;
-  GNUNET_assert (NULL != mc->dht);
-  GNUNET_assert (mc->dht == op_result);
-  GNUNET_DHT_disconnect (mc->dht);
-  mc->dht = NULL;
-  mc->ctx->op = NULL;
-  n_dht--;
-
-  if (0 != n_dht)
-    return;
-
-  if(n_malicious == count_malicious)
-  {
-    DEBUG("\n Call start_profiling()");
-    start_profiling();
-  }
-}
-
-
-/**
- * Set the malicious variable in peer malicious context.
- */
-static void
-set_malicious()
-{
-  unsigned int i;
-
-  DEBUG ("Setting %u peers malicious",
-         n_malicious);
-  for(i = 0; i < n_malicious; i++)
-  {
-    struct MaliciousContext *mc = &a_mc[i];
-    mc->ctx->op =
-        GNUNET_TESTBED_service_connect (mc->ctx,
-                                        mc->ctx->peer,
-                                        "dht",
-                                        &dht_set_malicious, mc,
-                                        &dht_connect,
-                                        &dht_disconnect_malicious,
-                                        mc);
-  }
-}
-
-#endif
-
-
 /**
  * Start collecting relevant statistics. If ENABLE_MALICIOUS set, first
  * set the malicious peers. If not, then start with PUT operation on active
@@ -1018,11 +840,7 @@ set_malicious()
 static void
 start_func()
 {
-#if ENABLE_MALICIOUS
-  set_malicious();
-#else
   start_profiling();
-#endif
 }
 
 
@@ -1065,12 +883,12 @@ successor_stats_cont (void *cls,
   int count;
 
   /* Don't schedule the task till we are looking for circle here. */
-  successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
+  successor_stats_task = NULL;
   GNUNET_TESTBED_operation_done (successor_stats_op);
   successor_stats_op = NULL;
   if (0 == max_searches)
   {
-    start_func();
+    start_func ();
     return;
   }
 
@@ -1108,39 +926,27 @@ successor_stats_cont (void *cls,
                                                                  GNUNET_NO);
   if ((start_val == val) && (count == num_peers))
   {
-    DEBUG("CIRCLE COMPLETED after %u tries", tries);
-    if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
+    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 (GNUNET_SCHEDULER_NO_TASK != successor_stats_task)
-      {
-        successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
-      }
-      if(GNUNET_SCHEDULER_NO_TASK == 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 +1016,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);
 }
 
 
@@ -1255,16 +1059,17 @@ service_started (void *cls,
   ctx->op = NULL;
   peers_started++;
   DEBUG("Peers Started = %d; num_peers = %d \n", peers_started, num_peers);
-  if (GNUNET_SCHEDULER_NO_TASK == successor_stats_task && peers_started == num_peers)
+  if (NULL == successor_stats_task && peers_started == num_peers)
   {
      DEBUG("successor_stats_task \n");
      struct Collect_Stat_Context *collect_stat_cls = GNUNET_new(struct Collect_Stat_Context);
      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);
   }
 }
 
@@ -1309,50 +1114,12 @@ test_run (void *cls,
 
   a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
   ac_cnt = 0;
-
-#if ENABLE_MALICIOUS
-  unsigned int malicious_peers;
-  if(PUT_PROBABILITY + MALICIOUS_PROBABILITY > 100)
-  {
-    DEBUG ("Reduce either number of malicious peer or active peers. ");
-    GNUNET_SCHEDULER_shutdown ();
-    GNUNET_free (a_ctx);
-    return;
-  }
-
-  /* Select the peers which should act maliciously. */
-  n_malicious = num_peers * MALICIOUS_PROBABILITY / 100;
-
-  a_mc = GNUNET_malloc (n_malicious * sizeof (struct MaliciousContext));
-  malicious_peers = 0;
-
-  for (cnt = 0; cnt < num_peers && malicious_peers < n_malicious; cnt++)
-  {
-    if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
-        MALICIOUS_PROBABILITY)
-      continue;
-    a_ctx[cnt].mc = &a_mc[malicious_peers];
-    a_mc[malicious_peers].ctx = &a_ctx[cnt];
-    malicious_peers++;
-  }
-  n_malicious = malicious_peers;
-  INFO ("Malicious Peers: %u\n",malicious_peers);
-
-#endif
-
-  a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
-  ac_cnt = 0;
   for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
   {
     if ((GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
         PUT_PROBABILITY))
       continue;
 
-#if ENABLE_MALICIOUS
-    if(a_ctx[ac_cnt].mc != NULL)
-      continue;
-#endif
-
     a_ctx[cnt].ac = &a_ac[ac_cnt];
     a_ac[ac_cnt].ctx = &a_ctx[cnt];
     ac_cnt++;
@@ -1390,7 +1157,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 +1166,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);
 }