- simplify start
[oweals/gnunet.git] / src / regex / gnunet-regex-profiler.c
index 86183a5fc32481c00ef4dc809be5e17b00d0162e..cc8d5af6b5e6d7f044799778f3b3c6f7e5c5f80d 100644 (file)
@@ -19,7 +19,7 @@
 */
 
 /**
- * @file mesh/gnunet-regex-profiler.c
+ * @file regex/gnunet-regex-profiler.c
  * @brief Regex profiler for testing distributed regex use.
  * @author Bartlomiej Polot
  * @author Maximilian Szengel
 #include "gnunet_applications.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_regex_lib.h"
+#include "gnunet_arm_service.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_testbed_service.h"
 
+#define FIND_TIMEOUT \
+        GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
+#define ANNOUNCE_TIME \
+        GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200)
+#define SEARCHES_IN_PARALLEL 100
+
 /**
  * DLL of operations
  */
@@ -149,7 +156,12 @@ struct RegexPeer
   int search_str_matched;
 
   /**
-   * Peer's dht handle.
+   * Peer's ARM handle.
+   */
+  struct GNUNET_ARM_Handle *arm_handle;
+
+  /**
+   * Peer's DHT handle.
    */
   struct GNUNET_DHT_Handle *dht_handle;
 
@@ -159,9 +171,9 @@ struct RegexPeer
    struct GNUNET_REGEX_search_handle *search_handle;
 
   /**
-   * Testbed operation handle for the dht service.
+   * Testbed operation handle for the ARM and DHT services.
    */
-  struct GNUNET_TESTBED_Operation *dht_op_handle;
+  struct GNUNET_TESTBED_Operation *op_handle;
 
   /**
    * Peers's statistics handle.
@@ -177,6 +189,11 @@ struct RegexPeer
    * The starting time of a profiling step.
    */
   struct GNUNET_TIME_Absolute prof_start_time;
+
+  /**
+   * Operation timeout
+   */
+  GNUNET_SCHEDULER_TaskIdentifier timeout;
 };
 
 
@@ -287,15 +304,10 @@ static unsigned int linking_factor;
 static unsigned int num_links;
 
 /**
- * Number of times we try overlay connect operations
+ * Number of connect operations that have failed, candidates to retry
  */
 static unsigned int retry_links;
 
-/**
- * Continuous failures during overlay connect operations
- */
-static unsigned int cont_fails;
-
 /**
  * Global testing status
  */
@@ -321,11 +333,26 @@ static char **search_strings;
  */
 static int num_search_strings;
 
+/**
+ * How many searches are running in parallel
+ */
+static unsigned int parallel_searches;
+
+/**
+ * Index of peer/string search.
+ */
+static unsigned int peer_cnt;
+
 /**
  * Number of peers found with search strings.
  */
 static unsigned int peers_found;
 
+/**
+ * Index of peer to start next announce/search..
+ */
+static unsigned int search_index;
+
 /**
  * Search task identifier
  */
@@ -339,7 +366,7 @@ static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
 /**
  * Search timeout in seconds.
  */
-static struct GNUNET_TIME_Relative search_timeout = { 60000 };
+static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
 
 /**
  * How long do we wait before starting the search?
@@ -362,13 +389,6 @@ static char *data_filename;
  */
 static unsigned int max_path_compression;
 
-/**
- * If we should distribute the search evenly throught all peers (each
- * peer searches for a string) or if only one peer should search for
- * all strings.
- */
-static int no_distributed_search;
-
 /**
  * Prefix used for regex announcing. We need to prefix the search
  * strings with it, in order to find something.
@@ -380,28 +400,8 @@ static char * regex_prefix;
 /******************************  DECLARATIONS  ********************************/
 /******************************************************************************/
 
-
-/**
- * Search callback function.
- *
- * @param cls Closure provided in GNUNET_REGEX_search.
- * @param id Peer providing a regex that matches the string.
- * @param get_path Path of the get request.
- * @param get_path_length Lenght of get_path.
- * @param put_path Path of the put request.
- * @param put_path_length Length of the put_path.
- */
-static void
-regex_found_handler (void *cls,
-                     const struct GNUNET_PeerIdentity *id,
-                     const struct GNUNET_PeerIdentity *get_path,
-                     unsigned int get_path_length,
-                     const struct GNUNET_PeerIdentity *put_path,
-                     unsigned int put_path_length);
-
-
 /**
- * Mesh connect callback.
+ * DHT connect callback.
  *
  * @param cls internal peer id.
  * @param op operation handle.
@@ -410,7 +410,7 @@ regex_found_handler (void *cls,
  */
 static void
 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
-                 void *ca_result, const char *emsg);
+                void *ca_result, const char *emsg);
 
 /**
  * DHT connect adapter.
@@ -452,7 +452,7 @@ stats_connect_cb (void *cls,
 
 
 /**
- * Task to collect all statistics from all peers, will shutdown the
+ * Task to collect all statistics from s, will shutdown the
  * profiler, when done.
  *
  * @param cls NULL
@@ -462,6 +462,16 @@ static void
 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
+/**
+ * Start announcing the next regex in the DHT.
+ *
+ * @param cls Index of the next peer in the peers array.
+ * @param tc TaskContext.
+ */
+static void
+announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
 /******************************************************************************/
 /********************************  SHUTDOWN  **********************************/
 /******************************************************************************/
@@ -509,22 +519,18 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         GNUNET_snprintf (output_buffer,
                          sizeof (output_buffer),
                          "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
-                        peer,
-                         peer->search_str,
-                         peer->search_str_matched,
-                        peer,
-                         peer->id,
+                         peer, peer->search_str, peer->search_str_matched,
+                         peer, peer->id, peer,
+                         peer, peer->policy_file,
                          peer,
-                        peer,
-                         peer->policy_file,
-                        peer,
-                         GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
+                         GNUNET_STRINGS_relative_time_to_string (prof_time,
+                                                                 GNUNET_NO));
       if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
     }
 
-    if (NULL != peers[peer_cnt].dht_op_handle)
-      GNUNET_TESTBED_operation_done (peers[peer_cnt].dht_op_handle);
+    if (NULL != peers[peer_cnt].op_handle)
+      GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
     if (NULL != peers[peer_cnt].stats_op_handle)
       GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
   }
@@ -562,7 +568,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (NULL != cfg)
     GNUNET_CONFIGURATION_destroy (cfg);
 
-  GNUNET_SCHEDULER_shutdown ();        /* Stop scheduler to shutdown testbed run */
+  GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
 }
 
 
@@ -704,13 +710,13 @@ stats_cb (void *cls,
   {
     peer->stats_op_handle =
       GNUNET_TESTBED_service_connect (NULL,
-                                     peer->peer_handle,
-                                     "statistics",
-                                     &stats_connect_cb,
-                                     peer,
-                                     &stats_ca,
-                                     &stats_da,
-                                     peer);
+                                      peer->peer_handle,
+                                      "statistics",
+                                      &stats_connect_cb,
+                                      peer,
+                                      &stats_ca,
+                                      &stats_da,
+                                      peer);
   }
 }
 
@@ -745,12 +751,12 @@ stats_connect_cb (void *cls,
   peer->stats_handle = ca_result;
 
   if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
-                                    GNUNET_TIME_UNIT_FOREVER_REL,
-                                    &stats_cb,
-                                    &stats_iterator, peer))
+                                     GNUNET_TIME_UNIT_FOREVER_REL,
+                                     &stats_cb,
+                                     &stats_iterator, peer))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               "Could not get statistics of peer %u!\n", peer->id);
+                "Could not get statistics of peer %u!\n", peer->id);
   }
 }
 
@@ -771,20 +777,31 @@ do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   peer->stats_op_handle =
     GNUNET_TESTBED_service_connect (NULL,
-                                   peer->peer_handle,
-                                   "statistics",
-                                   &stats_connect_cb,
-                                   peer,
-                                   &stats_ca,
-                                   &stats_da,
-                                   peer);
+                                    peer->peer_handle,
+                                    "statistics",
+                                    &stats_connect_cb,
+                                    peer,
+                                    &stats_ca,
+                                    &stats_da,
+                                    peer);
 }
 
 
 /******************************************************************************/
-/************************  MESH SERVICE CONNECTIONS  **************************/
+/************************   REGEX FIND CONNECTIONS   **************************/
 /******************************************************************************/
 
+
+/**
+ * Start searching for the next string in the DHT.
+ *
+ * @param cls Index of the next peer in the peers array.
+ * @param tc TaskContext.
+ */
+static void
+find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
 /**
  * Method called when we've found a peer that announced a regex
  * that matches our search string. Now get the statistics.
@@ -817,6 +834,14 @@ regex_found_handler (void *cls,
   }
 
   peers_found++;
+  parallel_searches--;
+
+  if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
+  {
+    GNUNET_SCHEDULER_cancel (peer->timeout);
+    peer->timeout = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
+  }
 
   if (NULL == id)
   {
@@ -824,24 +849,17 @@ regex_found_handler (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "String matching timed out for string %s on peer %u (%i/%i)\n",
                 peer->search_str, peer->id, peers_found, num_search_strings);
-
-    printf ("String matching timed out for string %s on peer %u (%i/%i)\n",
-            peer->search_str, peer->id, peers_found, num_search_strings);
-
     peer->search_str_matched = GNUNET_SYSERR;
   }
   else
   {
     prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "String %s successfully matched on peer %u after %s (%i/%i)\n",
-                peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
-                peers_found, num_search_strings);
 
-    printf ("String %s successfully matched on peer %u after %s (%i/%i)\n",
-            peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
-            peers_found, num_search_strings);
-    fflush (stdout);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "String %s found on peer %u after %s (%i/%i) (%u||)\n",
+                peer->search_str, peer->id,
+                GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
+                peers_found, num_search_strings, parallel_searches);
 
     peer->search_str_matched = GNUNET_YES;
 
@@ -866,8 +884,8 @@ regex_found_handler (void *cls,
     }
   }
 
-  GNUNET_TESTBED_operation_done (peer->dht_op_handle);
-  peer->dht_op_handle = NULL;
+  GNUNET_TESTBED_operation_done (peer->op_handle);
+  peer->op_handle = NULL;
 
   if (peers_found == num_search_strings)
   {
@@ -875,13 +893,11 @@ regex_found_handler (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "All strings successfully matched in %s\n",
                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
-    printf ("All strings successfully matched.\n");
-    fflush (stdout);
 
     if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
       GNUNET_SCHEDULER_cancel (search_timeout_task);
 
-    printf ("Collecting stats and shutting down.\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
     GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
   }
 }
@@ -895,62 +911,299 @@ regex_found_handler (void *cls,
  * @param tc the task context
  */
 static void
-do_connect_by_string_timeout (void *cls,
+search_timeout (void *cls,
                               const struct GNUNET_SCHEDULER_TaskContext * tc)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Finding matches to all strings did not succeed after %s.\n",
-              GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
+              GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
+                                                      GNUNET_NO));
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Found %i of %i strings\n", peers_found, num_search_strings);
 
-  printf ("Search timed out after %s. Collecting stats and shutting down.\n", 
-         GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
-  fflush (stdout);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Search timed out after %s."
+              "Collecting stats and shutting down.\n", 
+              GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
+                                                      GNUNET_NO));
 
   GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
 }
 
 
 /**
- * Connect by string task that is run to search for a string in the
- * NFA. It first connects to the mesh service and when a connection is
- * established it starts to search for the string.
+ * Search timed out. It might still complete in the future,
+ * but we should start another one.
  *
- * @param cls NULL
- * @param tc the task context
+ * @param cls Index of the next peer in the peers array.
+ * @param tc TaskContext.
  */
 static void
-do_connect_by_string (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext * tc)
+find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  printf ("Starting string search.\n");
-  fflush (stdout);
+  struct RegexPeer *p = cls;
 
-  peers[0].search_str = search_strings[0];
-  peers[0].search_str_matched = GNUNET_NO;
+  p->timeout = GNUNET_SCHEDULER_NO_TASK;
 
+  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+    return;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
+              p->search_str,
+              p->id);
+  GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
+}
+
+
+/**
+ * Start searching for a string in the DHT.
+ *
+ * @param cls Index of the next peer in the peers array.
+ * @param tc TaskContext.
+ */
+static void
+find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
+      peer_cnt >= num_search_strings)
+    return;
+
+  parallel_searches++;
+  peers[peer_cnt].search_str = search_strings[peer_cnt];
+  peers[peer_cnt].search_str_matched = GNUNET_NO;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             "Searching for string \"%s\" on peer %d with file %s\n",
-             peers[0].search_str, 0, peers[0].policy_file);
+              "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
+              peers[peer_cnt].search_str,
+              peer_cnt,
+              peers[peer_cnt].policy_file,
+              parallel_searches);
 
-    /* First connect to mesh service, then search for string. Next
-       connect will be in mesh_connect_cb */
-    peers[0].dht_op_handle =
-      GNUNET_TESTBED_service_connect (NULL,
-                                      peers[0].peer_handle,
-                                      "dht",
-                                      &dht_connect_cb,
-                                      &peers[0],
-                                      &dht_ca,
-                                      &dht_da,
-                                      &peers[0]);
-
-  search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
-                                                      &do_connect_by_string_timeout, NULL);
+  peers[peer_cnt].op_handle =
+    GNUNET_TESTBED_service_connect (NULL,
+                                    peers[peer_cnt].peer_handle,
+                                    "dht",
+                                    &dht_connect_cb,
+                                    &peers[peer_cnt],
+                                    &dht_ca,
+                                    &dht_da,
+                                    &peers[peer_cnt]);
+  peers[peer_cnt].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
+                                                          &find_timeout,
+                                                          &peers[peer_cnt]);
+  peer_cnt++;
+}
+
+
+/**
+ * ARM connect adapter. Opens a connection to the ARM service.
+ *
+ * @param cls Closure (peer).
+ * @param cfg Configuration handle.
+ *
+ * @return
+ */
+static void *
+arm_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  struct RegexPeer *peer = cls;
+
+  peer->arm_handle = GNUNET_ARM_alloc (cfg);
+  GNUNET_ARM_connect (peer->arm_handle, NULL, NULL);
+
+  return peer->arm_handle;
+}
+
+
+/**
+ * Adapter function called to destroy a connection to the ARM service.
+ *
+ * @param cls Closure (peer).
+ * @param op_result Service handle returned from the connect adapter.
+ */
+static void
+arm_da (void *cls, void *op_result)
+{
+  struct RegexPeer *peer = (struct RegexPeer *) cls;
+
+  GNUNET_assert (peer->arm_handle == op_result);
+
+  if (NULL != peer->arm_handle)
+  {
+    GNUNET_ARM_disconnect (peer->arm_handle);
+    peer->arm_handle = NULL;
+  }
+}
+
+/**
+ * Finish and free the operation used to start the regex daemon.
+ * operation_done calls ARM_disconnect, which cannot happen inside an
+ * ARM callback.
+ *
+ * @param cls Closure (Peer info)
+ * @param tc TaskContext
+ */
+static void
+arm_op_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct RegexPeer *peer = (struct RegexPeer *) cls;
+
+  GNUNET_TESTBED_operation_done (peer->op_handle);
+  peer->op_handle = NULL;
+}
+
+
+/**
+ * Callback called when arm has started the daemon we asked for.
+ * 
+ * @param cls           Closure ().
+ * @param arm           Arm handle.
+ * @param rs            Status of the request.
+ * @param service       Service we asked to start (deamon).
+ * @param result        Result of the request.
+ */
+static void
+arm_start_cb (void *cls, struct GNUNET_ARM_Handle *arm,
+    enum GNUNET_ARM_RequestStatus rs, const char *service,
+    enum GNUNET_ARM_Result result)
+{
+  struct RegexPeer *peer = (struct RegexPeer *) cls;
+
+  if (rs != GNUNET_ARM_REQUEST_SENT_OK)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM request was not sent: %u\n", rs);
+    GNUNET_abort ();
+  }
+  switch (result)
+  {
+      /**
+       * Asked to start it, but it's already starting.
+       */
+    case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
+      GNUNET_break (0); /* Shouldn't be starting, however it's not fatal. */
+      /* fallthrough */
+
+      /**
+       * Service is currently being started (due to client request).
+       */
+    case GNUNET_ARM_RESULT_STARTING:
+      GNUNET_SCHEDULER_add_now (&arm_op_done, peer);
+
+      if (search_index < (num_peers - 1))
+      {
+        long search_peer;
+        unsigned int i = 0;
+
+        /* Find a peer to look for a string matching the regex announced */
+        search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                                num_peers);
+        for (i = 0; peers[search_peer].search_str != NULL; i++)
+        {
+          search_peer = (search_peer + 1) % num_peers;
+          if (i > num_peers)
+            GNUNET_abort (); /* we run out of peers, must be a bug */
+        }
+        peers[search_peer].search_str = search_strings[search_index];
+        GNUNET_SCHEDULER_add_delayed (ANNOUNCE_TIME,
+                                      &find_string,
+                                      (void *) search_peer);
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                    "All daemons started."
+                    " Waiting %s to start string searches\n",
+                    GNUNET_STRINGS_relative_time_to_string (search_delay,
+                                                            GNUNET_NO));
+        /* FIXME start GLOBAL timeout to abort experiment */
+        search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout_time,
+                                                            &search_timeout,
+                                                            NULL);
+      }
+      break;
+
+    default:
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
+      GNUNET_abort ();
+  }
+}
+
+/**
+ * ARM connect callback. Called when we are connected to the arm service for
+ * the peer in 'cls'. If successfull we start the regex deamon to start
+ * announcing the regex of this peer.
+ *
+ * @param cls internal peer id.
+ * @param op operation handle.
+ * @param ca_result connect adapter result.
+ * @param emsg error message.
+ */
+static void
+arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
+                void *ca_result, const char *emsg)
+{
+  struct RegexPeer *peer = (struct RegexPeer *) cls;
+
+  if (NULL != emsg || NULL == op || NULL == ca_result)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
+    GNUNET_abort ();
+  }
+
+  GNUNET_assert (NULL != peer->arm_handle);
+  GNUNET_assert (peer->op_handle == op);
+  GNUNET_assert (peer->arm_handle == ca_result);
+
+  GNUNET_ARM_request_service_start (ca_result, "regexprofiler",
+                                    GNUNET_OS_INHERIT_STD_NONE,
+                                    GNUNET_TIME_UNIT_FOREVER_REL,
+                                    arm_start_cb, cls);
+}
+
+
+/**
+ * Task to start the daemons on each peer so that the regexes are announced
+ * into the DHT.
+ *
+ * @param cls NULL
+ * @param tc the task context
+ */
+static void
+do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
+
+  for (search_index = 0; search_index < SEARCHES_IN_PARALLEL; search_index++)
+    (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
 }
 
 
+/**
+ * Start announcing the next regex in the DHT.
+ *
+ * @param cls Closure (unused).
+ * @param tc TaskContext.
+ */
+static void
+announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  /* First connect to arm service, then announce. Next
+   * a nnounce will be in arm_connect_cb */
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", search_index);
+  peers[search_index].op_handle =
+    GNUNET_TESTBED_service_connect (NULL,
+                                    peers[search_index].peer_handle,
+                                    "arm",
+                                    &arm_connect_cb,
+                                    &peers[search_index],
+                                    &arm_ca,
+                                    &arm_da,
+                                    &peers[search_index]);
+  parallel_searches++;
+}
+
 /**
  * DHT connect callback. Called when we are connected to the dht service for
  * the peer in 'cls'. If successfull we connect to the stats service of this
@@ -966,8 +1219,6 @@ dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
                 void *ca_result, const char *emsg)
 {
   struct RegexPeer *peer = (struct RegexPeer *) cls;
-  static unsigned int peer_cnt;
-  unsigned int next_p;
 
   if (NULL != emsg || NULL == op || NULL == ca_result)
   {
@@ -976,44 +1227,15 @@ dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
   }
 
   GNUNET_assert (NULL != peer->dht_handle);
-  GNUNET_assert (peer->dht_op_handle == op);
+  GNUNET_assert (peer->op_handle == op);
   GNUNET_assert (peer->dht_handle == ca_result);
 
   peer->search_str_matched = GNUNET_NO;
   peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
                                              peer->search_str,
-                                             &regex_found_handler, NULL,
+                                             &regex_found_handler, peer,
                                              NULL);
   peer->prof_start_time = GNUNET_TIME_absolute_get ();
-
-  if (peer_cnt < (num_search_strings - 1))
-  {
-    if (GNUNET_YES == no_distributed_search)
-      next_p = 0;
-    else
-      next_p = (++peer_cnt % num_peers);
-
-    peers[next_p].search_str = search_strings[next_p];
-    peers[next_p].search_str_matched = GNUNET_NO;
-
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "Searching for string \"%s\" on peer %d with file %s\n",
-                peers[next_p].search_str, next_p, peers[next_p].policy_file);
-
-    /* FIXME
-     * dont connect to a new dht for each peer, we might want to seach for n
-     * strings on m peers where n > m
-     */
-    peers[next_p].dht_op_handle =
-      GNUNET_TESTBED_service_connect (NULL,
-                                      peers[next_p].peer_handle,
-                                      "dht",
-                                      &dht_connect_cb,
-                                      &peers[next_p],
-                                      &dht_ca,
-                                      &dht_da,
-                                      &peers[next_p]);
-  }
 }
 
 
@@ -1090,10 +1312,13 @@ do_configure_topology (void *cls,
   prof_start_time = GNUNET_TIME_absolute_get ();
   topology_op =
     GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
+                                               NULL,
+                                               NULL,
                                                NULL,
                                                GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
                                                num_links,
-                                               GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
+                                               GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
+                                               (unsigned int) 0,
                                                GNUNET_TESTBED_TOPOLOGY_OPTION_END);
   if (NULL == topology_op)
   {
@@ -1125,7 +1350,7 @@ peer_churn_cb (void *cls, const char *emsg)
   if (NULL != emsg)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-         _("An operation has failed while starting peers\n"));
+         _("An operation has failed while starting peers: %s\n"), emsg);
     GNUNET_TESTBED_operation_done (op);
     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
       GNUNET_SCHEDULER_cancel (abort_task);
@@ -1136,7 +1361,7 @@ peer_churn_cb (void *cls, const char *emsg)
   if (++started_peers == num_peers)
   {
     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
                 "All peers started successfully in %s\n",
                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
     result = GNUNET_OK;
@@ -1206,7 +1431,8 @@ peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
     {
       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
-      dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
+      dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
+                                              peers[peer_cnt].peer_handle,
                                               &peer_churn_cb, dll_op);
       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
     }
@@ -1238,9 +1464,9 @@ policy_filename_cb (void *cls, const char *filename)
 
   peer->policy_file = GNUNET_strdup (filename);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
-              peer->id,
-              GNUNET_TESTBED_host_get_hostname (peer->host_handle),
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Creating peer %i on host %s for policy file %s\n",
+              peer->id, GNUNET_TESTBED_host_get_hostname (peer->host_handle),
               filename);
 
   /* Set configuration options specific for this peer
@@ -1248,7 +1474,8 @@ policy_filename_cb (void *cls, const char *filename)
   peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
   GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
                                          "MAX_PATH_COMPRESSION",
-                                         (unsigned long long)max_path_compression);
+                                         (unsigned long long)
+                                         max_path_compression);
   GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
                                          "POLICY_FILE", filename);
 
@@ -1291,10 +1518,10 @@ controller_event_cb (void *cls,
         static unsigned int slaves_started;
         unsigned int peer_cnt;
 
-        dll_op = event->details.operation_finished.op_cls;
+        dll_op = event->op_cls;
         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
         GNUNET_free (dll_op);
-        op = event->details.operation_finished.operation;
+        op = event->op;
         if (NULL != event->details.operation_finished.emsg)
         {
           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -1374,9 +1601,11 @@ controller_event_cb (void *cls,
      /* Control reaches here when a peer linking operation fails */
      if (NULL != event->details.operation_finished.emsg)
      {
-       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                    _("An operation has failed while linking\n"));
-       printf ("F");
+       printf ("F%u/%u(%s)",
+               retry_links + 1, established_links + 1, 
+               event->details.operation_finished.emsg);
        fflush (stdout);
        retry_links++;
      }
@@ -1384,24 +1613,28 @@ controller_event_cb (void *cls,
      /* break; */
    case GNUNET_TESTBED_ET_CONNECT:
    {
-     char output_buffer[512];
+     char output_buffer[1024];
      size_t size;
 
      if (0 == established_links)
-       printf ("Establishing links .");
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Establishing links .");
      else
      {
-       printf (".");
-       fflush (stdout);
+       printf (".");fflush (stdout);
      }
      if (++established_links == num_links)
      {
-       fflush (stdout);
        prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                   "%u links established in %s\n",
+                   "\n%u links established in %s\n",
                    num_links,
-                   GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
+                   GNUNET_STRINGS_relative_time_to_string (prof_time,
+                                                           GNUNET_NO));
+       prof_time = GNUNET_TIME_relative_divide(prof_time, num_links);
+       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                   "Average of %s per connection\n",
+                   GNUNET_STRINGS_relative_time_to_string (prof_time,
+                                                           GNUNET_NO));
        result = GNUNET_OK;
        GNUNET_free (peer_handles);
 
@@ -1411,12 +1644,15 @@ controller_event_cb (void *cls,
            GNUNET_snprintf (output_buffer,
                             sizeof (output_buffer),
                             "# of peers: %u\n# of links established: %u\n"
-                            "Time to establish links: %s\nLinking failures: %u\n"
-                            "path compression length: %u\n# of search strings: %u\n",
+                            "Time to establish links: %s\n"
+                            "Linking failures: %u\n"
+                            "path compression length: %u\n"
+                            "# of search strings: %u\n",
                             num_peers,
-                            (established_links - cont_fails),
-                            GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
-                            cont_fails,
+                            (established_links - retry_links),
+                            GNUNET_STRINGS_relative_time_to_string (prof_time,
+                                                                    GNUNET_NO),
+                            retry_links,
                             max_path_compression,
                             num_search_strings);
 
@@ -1424,18 +1660,13 @@ controller_event_cb (void *cls,
            GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
        }
 
-       printf ("\nWaiting %s before starting to search.\n",
-               GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
-       fflush (stdout);
-
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                   "Waiting %s before starting to search.\n",
-                   GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_NO));
-
+                   "\nWaiting %s before starting to announce.\n",
+                   GNUNET_STRINGS_relative_time_to_string (search_delay,
+                                                           GNUNET_NO));
        state = STATE_SEARCH_REGEX;
-
        search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
-                                                   &do_connect_by_string, NULL);
+                                                   &do_announce, NULL);
      }
    }
    break;
@@ -1552,7 +1783,7 @@ status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int stat
   if (GNUNET_OK != status)
   {
     mc_proc = NULL;
-    printf("CRAPPP\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Oh, dear!\n");
     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
     return;
   }
@@ -1712,34 +1943,42 @@ run (void *cls, char *const *args, const char *cfgfile,
 
   if (NULL == args[0])
   {
-    fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("No hosts-file specified on command line. Exiting.\n"));
     return;
   }
   if (NULL == args[1])
   {
-    fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("No policy directory specified on command line. Exiting.\n"));
     return;
   }
-  num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
+  num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], config, &hosts);
   if (0 == num_hosts)
   {
-    fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("No hosts loaded. Need at least one host\n"));
     return;
   }
-  printf (_("Checking whether given hosts can start testbed. Please wait\n"));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+              _("Checking whether given hosts can start testbed."
+                "Please wait\n"));
   hc_handles = GNUNET_malloc (sizeof (struct
                                       GNUNET_TESTBED_HostHabitableCheckHandle *) 
                               * num_hosts);
   for (nhost = 0; nhost < num_hosts; nhost++)
-  {    
-    if (NULL == (hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
-                                                                       &host_habitable_cb,
-                                                                       &hc_handles[nhost])))
+  {
+    hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
+                                                          &host_habitable_cb,
+                                                          &hc_handles[nhost]);
+    if (NULL == hc_handles[nhost])
     {
+      int i;
+
       GNUNET_break (0);
-      for (nhost = 0; nhost < num_hosts; nhost++)
-        if (NULL != hc_handles[nhost])
-          GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
+      for (i = 0; i <= nhost; i++)
+        if (NULL != hc_handles[i])
+          GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
       GNUNET_free (hc_handles);
       hc_handles = NULL;
       break;
@@ -1747,22 +1986,24 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (num_hosts != nhost)
   {
-    fprintf (stderr, _("Exiting\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
   if (NULL == config)
   {
-    fprintf (stderr, _("No configuration file given. Exiting\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("No configuration file given. Exiting\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
 
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
-                                            &regex_prefix))
+                                             &regex_prefix))
   {
-    fprintf (stderr, _("Configuration option (regex_prefix) missing. Exiting\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Configuration option \"regex_prefix\" missing. Exiting\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
@@ -1780,14 +2021,16 @@ run (void *cls, char *const *args, const char *cfgfile,
                               data_filename);
   if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
   {
-    fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Specified policies directory does not exist. Exiting.\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
   policy_dir = args[1];
   if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
   {
-    fprintf (stderr, _("No search strings file given. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("No search strings file given. Exiting.\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
@@ -1795,13 +2038,16 @@ run (void *cls, char *const *args, const char *cfgfile,
   if (num_search_strings != nsearchstrs)
   {
     num_search_strings = nsearchstrs;
-    fprintf (stderr, _("Error loading search strings. Given file does not contain enough strings. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Error loading search strings."
+                  "Given file does not contain enough strings. Exiting.\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
   if (0 >= num_search_strings || NULL == search_strings)
   {
-    fprintf (stderr, _("Error loading search strings. Exiting.\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Error loading search strings. Exiting.\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
@@ -1835,7 +2081,8 @@ main (int argc, char *const *argv)
       GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
     {'t', "matching-timeout", "TIMEOUT",
       gettext_noop ("wait TIMEOUT before considering a string match as failed"),
-      GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
+      GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time
+        },
     {'s', "search-delay", "DELAY",
       gettext_noop ("wait DELAY before starting string search"),
       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
@@ -1845,9 +2092,6 @@ main (int argc, char *const *argv)
     {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
      gettext_noop ("maximum path compression length"),
      1, &GNUNET_GETOPT_set_uint, &max_path_compression},
-    {'i', "no-distributed-search", "",
-     gettext_noop ("if this option is set, only one peer is responsible for searching all strings"),
-     0, &GNUNET_GETOPT_set_one, &no_distributed_search},
     GNUNET_GETOPT_OPTION_END
   };
   int ret;
@@ -1857,8 +2101,9 @@ main (int argc, char *const *argv)
 
   result = GNUNET_SYSERR;
   ret =
-      GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
-                          _("Profiler for regex/mesh"),
+      GNUNET_PROGRAM_run (argc, argv,
+                          "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
+                          _("Profiler for regex"),
                           options, &run, NULL);
 
   if (GNUNET_OK != ret)