-align core message, do not run psycstore here
[oweals/gnunet.git] / src / regex / gnunet-regex-profiler.c
index 2b136efd6a4a27f92e44f691daa411d55e24400c..638f1a1a43bd2cc1d78eb39d013df1e82fa756d9 100644 (file)
@@ -31,7 +31,7 @@
 #include "platform.h"
 #include "gnunet_applications.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_regex_lib.h"
+#include "regex_internal_lib.h"
 #include "gnunet_arm_service.h"
 #include "gnunet_dht_service.h"
 #include "gnunet_testbed_service.h"
@@ -150,7 +150,7 @@ struct RegexPeer
   /**
    * Handle to a running regex search.
    */
-   struct GNUNET_REGEX_search_handle *search_handle;
+   struct REGEX_INTERNAL_Search *search_handle;
 
   /**
    * Testbed operation handle for DHT.
@@ -162,11 +162,6 @@ struct RegexPeer
    */
   struct GNUNET_STATISTICS_Handle *stats_handle;
 
-  /**
-   * Testbed operation handle for the statistics service.
-   */
-  struct GNUNET_TESTBED_Operation *stats_op_handle;
-
   /**
    * The starting time of a profiling step.
    */
@@ -183,6 +178,10 @@ struct RegexPeer
   struct GNUNET_TESTBED_Operation *daemon_op;
 };
 
+/**
+ * Set when shutting down to avoid making more queries.
+ */
+static int in_shutdown;
 
 /**
  * The array of peers; we fill this as the peers are given to us by the testbed
@@ -270,15 +269,10 @@ static char *hosts_file;
 static char *strings_file;
 
 /**
- * Search strings.
+ * Search strings (num_peers of them).
  */
 static char **search_strings;
 
-/**
- * Number of search strings.
- */
-static int num_search_strings;
-
 /**
  * How many searches are we going to start in parallel
  */
@@ -290,9 +284,9 @@ static long long unsigned int init_parallel_searches;
 static unsigned int parallel_searches;
 
 /**
- * Number of peers found with search strings.
+ * Number of strings found in the published regexes.
  */
-static unsigned int peers_found;
+static unsigned int strings_found;
 
 /**
  * Index of peer to start next announce/search.
@@ -386,17 +380,6 @@ stats_connect_cb (void *cls,
                   const char *emsg);
 
 
-/**
- * Task to collect all statistics from s, will shutdown the
- * profiler, when done.
- *
- * @param cls NULL
- * @param tc the task context
- */
-static void
-do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
 /**
  * Start announcing the next regex in the DHT.
  *
@@ -457,15 +440,13 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
     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);
   }
 
   if (NULL != data_file)
     GNUNET_DISK_file_close (data_file);
 
   for (search_str_cnt = 0;
-       search_str_cnt < num_search_strings && NULL != search_strings;
+       search_str_cnt < num_peers && NULL != search_strings;
        search_str_cnt++)
   {
     GNUNET_free_non_null (search_strings[search_str_cnt]);
@@ -607,10 +588,10 @@ stats_cb (void *cls,
     return;
   }
 
-  GNUNET_assert (NULL != peer->stats_op_handle);
+  GNUNET_assert (NULL != peer->op_handle);
 
-  GNUNET_TESTBED_operation_done (peer->stats_op_handle);
-  peer->stats_op_handle = NULL;
+  GNUNET_TESTBED_operation_done (peer->op_handle);
+  peer->op_handle = NULL;
 
   peer_cnt++;
   peer = &peers[peer_cnt];
@@ -619,10 +600,11 @@ stats_cb (void *cls,
   {
     struct GNUNET_TIME_Relative delay = { 100 };
     shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
+    result = GNUNET_OK;
   }
   else
   {
-    peer->stats_op_handle =
+    peer->op_handle =
       GNUNET_TESTBED_service_connect (NULL,
                                       peer->peer_handle,
                                       "statistics",
@@ -689,7 +671,7 @@ do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_assert (NULL != peer->peer_handle);
 
-  peer->stats_op_handle =
+  peer->op_handle =
     GNUNET_TESTBED_service_connect (NULL,
                                     peer->peer_handle,
                                     "statistics",
@@ -720,7 +702,7 @@ 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.
  *
- * @param cls Closure provided in GNUNET_REGEX_search.
+ * @param cls Closure provided in REGEX_INTERNAL_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.
@@ -747,14 +729,15 @@ regex_found_handler (void *cls,
     return;
   }
 
-  peers_found++;
+  strings_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 (GNUNET_NO == in_shutdown)
+      GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
   }
 
   if (NULL == id)
@@ -762,7 +745,7 @@ regex_found_handler (void *cls,
     // FIXME not possible right now
     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);
+                peer->search_str, peer->id, strings_found, num_peers);
     peer->search_str_matched = GNUNET_SYSERR;
   }
   else
@@ -773,7 +756,7 @@ regex_found_handler (void *cls,
                 "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);
+                strings_found, num_peers, parallel_searches);
 
     peer->search_str_matched = GNUNET_YES;
 
@@ -799,7 +782,7 @@ regex_found_handler (void *cls,
   GNUNET_TESTBED_operation_done (peer->op_handle);
   peer->op_handle = NULL;
 
-  if (peers_found == num_search_strings)
+  if (strings_found == num_peers)
   {
     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -807,7 +790,10 @@ regex_found_handler (void *cls,
                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
 
     if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
+    {
       GNUNET_SCHEDULER_cancel (search_timeout_task);
+      search_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    }
 
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
     GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
@@ -823,14 +809,16 @@ regex_found_handler (void *cls,
  * @param tc the task context
  */
 static void
-search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
+search_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
 {
+  unsigned int i;
+
   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_time,
                                                       GNUNET_NO));
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Found %i of %i strings\n", peers_found, num_search_strings);
+              "Found %i of %i strings\n", strings_found, num_peers);
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Search timed out after %s."
@@ -838,6 +826,15 @@ search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
               GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
                                                       GNUNET_NO));
 
+  in_shutdown = GNUNET_YES;
+  for (i = 0; i < num_peers; i++)
+  {
+    if (NULL != peers[i].op_handle)
+    {
+      GNUNET_TESTBED_operation_done (peers[i].op_handle);
+      peers[i].op_handle = NULL;
+    }
+  }
   GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
 }
 
@@ -850,7 +847,7 @@ search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
  * @param tc TaskContext.
  */
 static void
-find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+find_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct RegexPeer *p = cls;
 
@@ -859,10 +856,11 @@ find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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",
+              "Searching for string \"%s\" on peer %d timed out.\n",
               p->search_str,
               p->id);
-  GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
+  if (GNUNET_NO == in_shutdown)
+    GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
 }
 
 
@@ -878,7 +876,8 @@ find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned int search_peer = (unsigned int) (long) cls;
 
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
-      search_peer >= num_search_strings)
+      search_peer >= num_peers ||
+      GNUNET_YES == in_shutdown)
     return;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -898,7 +897,7 @@ find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                     &peers[search_peer]);
   GNUNET_assert (NULL != peers[search_peer].op_handle);
   peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
-                                                          &find_timeout,
+                                                          &find_timed_out,
                                                           &peers[search_peer]);
 }
 
@@ -919,15 +918,13 @@ daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
   struct RegexPeer *peer = (struct RegexPeer *) cls;
   unsigned long search_peer;
   unsigned int i;
-  unsigned int me;
 
   GNUNET_TESTBED_operation_done (peer->daemon_op);
   peer->daemon_op = NULL;
-  me = peer - peers;
   if (NULL != emsg)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
+                "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg);
     GNUNET_abort ();
   }
 
@@ -940,7 +937,7 @@ daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
     if (i > num_peers)
       GNUNET_abort (); /* we ran out of peers, must be a bug */
   }
-  peers[search_peer].search_str = search_strings[me];
+  peers[search_peer].search_str = search_strings[peer->id];
   peers[search_peer].search_str_matched = GNUNET_NO;
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
                                   reannounce_period_max,
@@ -985,9 +982,22 @@ announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct RegexPeer *peer;
 
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
-            next_search >= num_peers)
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
+  if (next_search >= num_peers)
+  {
+    if (strings_found != num_peers)
+    {
+      struct GNUNET_TIME_Relative new_delay;
+      if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
+        GNUNET_SCHEDULER_cancel (search_timeout_task);
+      new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
+      search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay,
+                                                          &search_timed_out,
+                                                          NULL);
+    }
+    return;
+  }
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
   peer = &peers[next_search];
@@ -1029,7 +1039,7 @@ dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *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_handle = REGEX_INTERNAL_search (peer->dht_handle,
                                              peer->search_str,
                                              &regex_found_handler, peer,
                                              NULL);
@@ -1071,7 +1081,7 @@ dht_da (void *cls, void *op_result)
 
   if (NULL != peer->search_handle)
   {
-    GNUNET_REGEX_search_cancel (peer->search_handle);
+    REGEX_INTERNAL_search_cancel (peer->search_handle);
     peer->search_handle = NULL;
   }
 
@@ -1088,7 +1098,7 @@ dht_da (void *cls, void *op_result)
  *
  * @param cls NULL
  * @param num_peers_ number of peers in 'peers'
- * @param peers handle to peers run in the testbed.  NULL upon timeout (see
+ * @param testbed_peers handle to peers run in the testbed.  NULL upon timeout (see
  *          GNUNET_TESTBED_test_run()).
  * @param links_succeeded the number of overlay link connection attempts that
  *          succeeded
@@ -1121,9 +1131,25 @@ test_master (void *cls,
   {
     peers[i].peer_handle = testbed_peers[i];
   }
-  GNUNET_SCHEDULER_add_now (&do_announce, NULL);
+  if (GNUNET_NO ==
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT"))
+  {
+    struct GNUNET_TIME_Relative settle_time;
+
+    settle_time =
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
+                                     10 * num_peers);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+                "Waiting for DHT for %s to settle new connections.\n\n",
+                GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO));
+    GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL);
+  }
+  else
+  {
+    GNUNET_SCHEDULER_add_now (&do_announce, NULL);
+  }
   search_timeout_task =
-      GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timeout, NULL);
+      GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL);
 }
 
 /**
@@ -1242,7 +1268,10 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   unsigned int nsearchstrs;
   unsigned int i;
-  
+  struct GNUNET_TIME_Relative abort_time;
+
+  in_shutdown = GNUNET_NO;
+
   /* Check config */
   if (NULL == config)
   {
@@ -1284,12 +1313,6 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
 
   /* Check arguments */
-  if (NULL == hosts_file)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("No hosts-file specified on command line. Exiting.\n"));
-    return;
-  }
   if (NULL == policy_dir)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -1321,23 +1344,25 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   nsearchstrs = load_search_strings (strings_file,
                                      &search_strings,
-                                     num_search_strings);
-  if (num_search_strings != nsearchstrs)
+                                     num_peers);
+  if (num_peers != nsearchstrs)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Error loading search strings."
-                  "Given file does not contain enough strings. Exiting.\n"));
+                "Error loading search strings.\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "File (%s) does not contain enough strings (%u/%u).\n",
+                strings_file, nsearchstrs, num_peers);
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
-  if (0 >= num_search_strings || NULL == search_strings)
+  if (0 >= num_peers || NULL == search_strings)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Error loading search strings. Exiting.\n"));
     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
-  for (i = 0; i < num_search_strings; i++)
+  for (i = 0; i < num_peers; i++)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "search string: %s\n",
                 search_strings[i]);
@@ -1365,6 +1390,13 @@ run (void *cls, char *const *args, const char *cfgfile,
     peers[i].id = i;
   }
 
+  GNUNET_CONFIGURATION_set_value_number (cfg,
+                                         "TESTBED", "OVERLAY_RANDOM_LINKS",
+                                         num_peers * 20);
+  GNUNET_CONFIGURATION_set_value_number (cfg,
+                                         "DHT", "FORCE_NSE",
+                                         (long long unsigned)
+                                         (log (num_peers) / log (2.0)));
   event_mask = 0LL;
 /* For feedback about the start process activate these and pass master_cb */
   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
@@ -1380,9 +1412,17 @@ run (void *cls, char *const *args, const char *cfgfile,
                       NULL,     /* master_controller_cb cls */
                       &test_master,
                       NULL);    /* test_master cls */
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
+                                           "SETUP_TIMEOUT",
+                                           &abort_time))
+  {
+    abort_time =
+      GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
+  }
+  abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES);
   abort_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
-                                    (GNUNET_TIME_UNIT_MINUTES, 15),
+      GNUNET_SCHEDULER_add_delayed (abort_time,
                                     &do_abort,
                                     (void*) __LINE__);
 }
@@ -1401,22 +1441,19 @@ main (int argc, char *const *argv)
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'o', "output-file", "FILENAME",
      gettext_noop ("name of the file for writing statistics"),
-     1, &GNUNET_GETOPT_set_string, &data_filename},
+     GNUNET_YES, &GNUNET_GETOPT_set_string, &data_filename},
     {'t', "matching-timeout", "TIMEOUT",
-      gettext_noop ("wait TIMEOUT before considering a string match as failed"),
-      GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time },
-    {'n', "num-search-strings", "COUNT",
-      gettext_noop ("number of search strings to read from search strings file"),
-      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
+      gettext_noop ("wait TIMEOUT before ending the experiment"),
+      GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time},
     {'p', "policy-dir", "DIRECTORY",
       gettext_noop ("directory with policy files"),
-      GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir },
+      GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir},
     {'s', "strings-file", "FILENAME",
       gettext_noop ("name of file with input strings"),
-      GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file },
+      GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file},
     {'H', "hosts-file", "FILENAME",
       gettext_noop ("name of file with hosts' names"),
-      GNUNET_NO, &GNUNET_GETOPT_set_filename, &hosts_file },
+      GNUNET_YES, &GNUNET_GETOPT_set_filename, &hosts_file},
     GNUNET_GETOPT_OPTION_END
   };
   int ret;