- fix old mesh
[oweals/gnunet.git] / src / mesh / gnunet-regex-profiler.c
index 536eacccfa544b0da3b3a7b61eaaecfa068355e0..1669666d08f9012c3344d4e1fa58f2a680d453b2 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
      This file is part of GNUnet.
      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
 
@@ -145,6 +145,14 @@ struct RegexPeer
    */
   const char *search_str;
 
+  /**
+   * Set to GNUNET_YES if the peer successfully matched the above
+   * search string. GNUNET_NO if the string could not be matched
+   * during the profiler run. GNUNET_SYSERR if the string matching
+   * timed out. Undefined if search_str is NULL
+   */
+  int search_str_matched;
+
   /**
    * Peer's mesh handle.
    */
@@ -272,11 +280,6 @@ static unsigned int linking_factor;
  */
 static unsigned int num_links;
 
-/**
- * Number of timeout failures to tolerate
- */
-static unsigned int num_cont_fails;
-
 /**
  * Number of times we try overlay connect operations
  */
@@ -338,6 +341,16 @@ static struct GNUNET_TIME_Relative search_timeout = { 60000 };
  */
 static struct GNUNET_TIME_Relative search_delay = { 60000 };
 
+/**
+ * Delay before setting mesh service op as done.
+ */
+static struct GNUNET_TIME_Relative mesh_done_delay = { 1000 };
+
+/**
+ * Delay to wait before starting to configure the overlay topology
+ */
+static struct GNUNET_TIME_Relative conf_topo_delay = { 10000 };
+
 /**
  * File to log statistics to.
  */
@@ -353,11 +366,6 @@ static char *data_filename;
  */
 static unsigned int max_path_compression;
 
-/**
- * Delay before setting mesh service op as done.
- */
-static struct GNUNET_TIME_Relative mesh_done_delay = { 1000 };
-
 /******************************************************************************/
 /******************************  DECLARATIONS  ********************************/
 /******************************************************************************/
@@ -441,26 +449,56 @@ static void
 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct DLLOperation *dll_op;
+  struct RegexPeer *peer;
   unsigned int nhost;
   unsigned int peer_cnt;
   unsigned int search_str_cnt;
+  char output_buffer[512];
+  size_t size;
+
+  if (GNUNET_SCHEDULER_NO_TASK != abort_task)
+    GNUNET_SCHEDULER_cancel (abort_task);
+  if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
+    GNUNET_SCHEDULER_cancel (register_hosts_task);
 
   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
   {
+    peer = &peers[peer_cnt];
+
+    if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
+    {
+      prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
+      size =
+       GNUNET_snprintf (output_buffer,
+                        sizeof (output_buffer),
+                        "Search string not found: %s (%d)\nOn peer: %u (%p)\nWith policy file: %s\nAfter: %s\n",
+                        peer->search_str,
+                        peer->search_str_matched,
+                        peer->id,
+                        peer,
+                        peer->policy_file,
+                        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].mesh_op_handle)
       GNUNET_TESTBED_operation_done (peers[peer_cnt].mesh_op_handle);
     if (NULL != peers[peer_cnt].stats_op_handle)
       GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
   }
-  for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++)
+
+  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++)
   {
-    GNUNET_free (search_strings[search_str_cnt]);
+    GNUNET_free_non_null (search_strings[search_str_cnt]);
   }
-  GNUNET_free (search_strings);
-  if (GNUNET_SCHEDULER_NO_TASK != abort_task)
-    GNUNET_SCHEDULER_cancel (abort_task);
-  if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
-    GNUNET_SCHEDULER_cancel (register_hosts_task);
+  GNUNET_free_non_null (search_strings);
+
   if (NULL != reg_handle)
     GNUNET_TESTBED_cancel_registration (reg_handle);
   if (NULL != topology_op)
@@ -469,6 +507,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     if (NULL != hosts[nhost])
       GNUNET_TESTBED_host_destroy (hosts[nhost]);
   GNUNET_free_non_null (hosts);
+
   while (NULL != (dll_op = dll_op_head))
   {
     GNUNET_TESTBED_operation_done (dll_op->op);
@@ -481,8 +520,6 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_TESTBED_controller_stop (mc_proc);
   if (NULL != cfg)
     GNUNET_CONFIGURATION_destroy (cfg);
-  if (NULL != data_file)
-    GNUNET_DISK_file_close (data_file);
 
   GNUNET_SCHEDULER_shutdown ();        /* Stop scheduler to shutdown testbed run */
 }
@@ -535,7 +572,12 @@ stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 static void
 stats_da (void *cls, void *op_result)
 {
-  GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
+  struct RegexPeer *peer = cls;
+
+  GNUNET_assert (op_result == peer->stats_handle);
+
+  GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
+  peer->stats_handle = NULL;
 }
 
 
@@ -604,7 +646,8 @@ stats_cb (void *cls,
 
   if (++peer_cnt == num_search_strings)
   {
-    GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+    struct GNUNET_TIME_Relative delay = { 100 };
+    GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
   }
 }
 
@@ -647,6 +690,7 @@ stats_connect_cb (void *cls,
 
   peer->prof_start_time = GNUNET_TIME_absolute_get ();
 
+  peer->search_str_matched = GNUNET_NO;
   GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
                                               peer->search_str);
 }
@@ -744,6 +788,11 @@ mesh_peer_connect_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
   {
@@ -758,6 +807,8 @@ mesh_peer_connect_handler (void *cls,
            peers_found, num_search_strings);
     fflush (stdout);
 
+    peer->search_str_matched = GNUNET_YES;
+
     if (NULL != data_file)
     {
       size =
@@ -863,6 +914,7 @@ do_connect_by_string (void *cls,
   {
     peer = &peers[search_cnt % num_peers];
     peer->search_str = search_strings[search_cnt];
+    peer->search_str_matched = GNUNET_NO;
 
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "Searching for string \"%s\" on peer %d with file %s\n",
@@ -897,8 +949,22 @@ do_mesh_op_done (void *cls,
                 const struct GNUNET_SCHEDULER_TaskContext * tc)
 {
   struct RegexPeer *peer = cls;
+  static unsigned int peer_cnt;
   GNUNET_TESTBED_operation_done (peer->mesh_op_handle);
   peer->mesh_op_handle = NULL;
+
+  if (++peer_cnt < num_peers)
+  {
+    peers[peer_cnt].mesh_op_handle =
+      GNUNET_TESTBED_service_connect (NULL,
+                                     peers[peer_cnt].peer_handle,
+                                     "mesh",
+                                     &mesh_connect_cb,
+                                     &peers[peer_cnt],
+                                     &mesh_ca,
+                                     &mesh_da,
+                                     &peers[peer_cnt]);
+  }
 }
 
 
@@ -914,7 +980,6 @@ void
 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
                  void *ca_result, const char *emsg)
 {
-  static unsigned int peer_cnt;
   struct RegexPeer *peer = (struct RegexPeer *) cls;
   char *regex;
   char *data;
@@ -987,19 +1052,6 @@ mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
 
       GNUNET_SCHEDULER_add_delayed (mesh_done_delay, &do_mesh_op_done, peer);
       
-      if (++peer_cnt < num_peers)
-      {
-         peers[peer_cnt].mesh_op_handle =
-           GNUNET_TESTBED_service_connect (NULL,
-                                           peers[peer_cnt].peer_handle,
-                                           "mesh",
-                                           &mesh_connect_cb,
-                                           &peers[peer_cnt],
-                                           &mesh_ca,
-                                           &mesh_da,
-                                           &peers[peer_cnt]);
-      }
-
       if (++num_files_announced == num_peers)
       {
        state = STATE_SEARCH_REGEX;
@@ -1102,6 +1154,41 @@ mesh_da (void *cls, void *op_result)
 /******************************************************************************/
 
 
+/**
+ * Configure the peer overlay topology.
+ *
+ * @param cls NULL
+ * @param tc the task context
+ */
+static void
+do_configure_topology (void *cls,
+                      const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+  /*
+    if (0 == linking_factor)
+    linking_factor = 1;
+    num_links = linking_factor * num_peers;
+  */
+  /* num_links = num_peers - 1; */
+  num_links = linking_factor;
+
+  /* Do overlay connect */
+  prof_start_time = GNUNET_TIME_absolute_get ();
+  topology_op =
+    GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
+                                              GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
+                                              num_links,
+                                              GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
+                                              GNUNET_TESTBED_TOPOLOGY_OPTION_END);
+  if (NULL == topology_op)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               "Cannot create topology, op handle was NULL\n");
+    GNUNET_assert (0);
+  }
+}
+
+
 /**
  * Functions of this signature are called when a peer has been successfully
  * started or stopped.
@@ -1143,28 +1230,16 @@ peer_churn_cb (void *cls, const char *emsg)
     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
 
-    /*
-    if (0 == linking_factor)
-      linking_factor = 1;
-    num_links = linking_factor * num_peers;
-    */
-    /* num_links = num_peers - 1; */
-    num_links = linking_factor;
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               "Waiting %s before starting to link peers\n", 
+               GNUNET_STRINGS_relative_time_to_string (conf_topo_delay, GNUNET_YES));
+
+    printf ("Waiting %s before starting to link peers\n", 
+           GNUNET_STRINGS_relative_time_to_string (conf_topo_delay, GNUNET_YES));
+    fflush (stdout);
+
     state = STATE_PEERS_LINKING;
-    /* Do overlay connect */
-    prof_start_time = GNUNET_TIME_absolute_get ();
-    topology_op =
-        GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
-                                                  GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
-                                                  num_links,
-                                                  GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
-                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
-    if (NULL == topology_op)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  "Cannot create topology, op handle was NULL\n");
-      GNUNET_assert (0);
-    }
+    GNUNET_SCHEDULER_add_delayed (conf_topo_delay, &do_configure_topology, NULL);
   }
 }
 
@@ -1246,14 +1321,7 @@ policy_filename_cb (void *cls, const char *filename)
 
   GNUNET_assert (NULL != peer);
 
-  peer->id = peer_cnt;
   peer->policy_file = GNUNET_strdup (filename);
-  /* Do not start peers on hosts[0] (master controller) */
-  peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
-  peer->mesh_handle = NULL;
-  peer->mesh_tunnel_handle = NULL;
-  peer->stats_handle = NULL;
-  peer->stats_op_handle = NULL;
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
               peer->id,
@@ -1295,6 +1363,7 @@ controller_event_cb (void *cls,
     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
       {
         static unsigned int slaves_started;
+       unsigned int peer_cnt;
 
         dll_op = event->details.operation_finished.op_cls;
         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
@@ -1325,6 +1394,22 @@ controller_event_cb (void *cls,
                                                   NULL);
           peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
 
+         /* Initialize peers */
+         for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
+         {
+           struct RegexPeer *peer = &peers[peer_cnt];
+           peer->id = peer_cnt;
+           peer->policy_file = NULL;
+           /* Do not start peers on hosts[0] (master controller) */
+           peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
+           peer->mesh_handle = NULL;
+           peer->mesh_tunnel_handle = NULL;
+           peer->stats_handle = NULL;
+           peer->stats_op_handle = NULL;
+           peer->search_str = NULL;
+           peer->search_str_matched = GNUNET_NO;
+         }
+
           GNUNET_DISK_directory_scan (policy_dir,
                                       &policy_filename_cb,
                                       NULL);
@@ -1360,15 +1445,7 @@ controller_event_cb (void *cls,
        printf ("F");
        fflush (stdout);
        retry_links++;
-       
-       if (++cont_fails > num_cont_fails)
-       {
-        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                    "We have a very high peer linking failure rate: %u (threshold: %u)\n",
-                    cont_fails,
-                    num_cont_fails);
-       }
-     }
+     }       
      /* We do no retries, consider this link as established */
      /* break; */
    case GNUNET_TESTBED_ET_CONNECT:
@@ -1401,12 +1478,13 @@ controller_event_cb (void *cls,
                            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",
+                           "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,
-                           max_path_compression);
+                           max_path_compression,
+                           num_search_strings);
 
         if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
           GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
@@ -1658,6 +1736,7 @@ run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *config)
 {
   unsigned int nhost;
+  unsigned int nsearchstrs;
 
   if (NULL == args[0])
   {
@@ -1720,8 +1799,10 @@ run (void *cls, char *const *args, const char *cfgfile,
     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
   }
-  if (num_search_strings != load_search_strings (args[2], &search_strings, num_search_strings))
+  nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
+  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_SCHEDULER_add_now (&do_shutdown, NULL);
     return;
@@ -1764,12 +1845,9 @@ main (int argc, char *const *argv)
     {'d', "details", "FILENAME",
      gettext_noop ("name of the file for writing statistics"),
      1, &GNUNET_GETOPT_set_string, &data_filename},
-    {'n', "linking-factor", "FACTOR",
-      gettext_noop ("create FACTOR times number of peers random links"),
+    {'n', "num-links", "COUNT",
+      gettext_noop ("create COUNT number of random links between peers"),
       GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
-    {'e', "num-errors", "COUNT",
-      gettext_noop ("tolerate COUNT number of continious timeout failures"),
-      GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
     {'t', "matching-timeout", "TIMEOUT",
       gettext_noop ("wait TIMEOUT before considering a string match as failed"),
       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
@@ -1794,6 +1872,7 @@ main (int argc, char *const *argv)
       GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
                           _("Profiler for regex/mesh"),
                           options, &run, NULL);
+  GNUNET_free ((void*) argv);
   if (GNUNET_OK != ret)
     return ret;
   if (GNUNET_OK != result)