- don-t re-scheduler, destroy immediately (comes from scheduler)
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.c
index 049e32bcb85f930605cc9e8a811382dee852149e..5e2b08b28ca2760a4d352dc7aa675a7025498683 100644 (file)
 #define PONG 2
 
 
-/**
- * How many peers do pinging
- */
-#define PING_PEERS 3
-
-/**
- * Duration of each round.
- */
-#define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
-
 /**
  * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
  */
@@ -66,7 +56,7 @@
 /**
  * Ratio of peers active. First round always is 1.0.
  */
-static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
+static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.0};
 
 /**
  * Message type for pings.
@@ -124,6 +114,11 @@ struct MeshPeer
    */
   struct GNUNET_MESH_Channel *incoming_ch;
 
+  /**
+   * Channel handle for a warmup channel.
+   */
+  struct GNUNET_MESH_Channel *warmup_ch;
+
   /**
    * Number of payload packes sent
    */
@@ -161,6 +156,11 @@ struct MeshPeer
 
 };
 
+/**
+ * Duration of each round.
+ */
+static struct GNUNET_TIME_Relative round_time;
+
 /**
  * GNUNET_PeerIdentity -> MeshPeer
  */
@@ -196,6 +196,11 @@ static unsigned long long peers_total;
  */
 static unsigned long long peers_running;
 
+/**
+ * Number of peers doing pings.
+ */
+static unsigned long long peers_pinging;
+
 /**
  * Test context (to shut down).
  */
@@ -221,11 +226,36 @@ static GNUNET_SCHEDULER_TaskIdentifier test_task;
  */
 static unsigned int current_round;
 
+
+/**
+ * Do preconnect? (Each peer creates a tunnel to one other peer).
+ */
+static int do_warmup;
+
+/**
+ * Warmup progress.
+ */
+static unsigned int peers_warmup;
+
 /**
  * Flag to notify callbacks not to generate any new traffic anymore.
  */
 static int test_finished;
 
+
+/**
+ * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
+ *
+ * Testcase continues when the root receives confirmation of connected peers,
+ * on callback funtion ch.
+ *
+ * @param cls Closure (unsued).
+ * @param tc Task Context.
+ */
+static void
+start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+
 /**
  * Calculate a random delay.
  *
@@ -269,7 +299,7 @@ show_end_data (void)
 
   for (i = 0; i < number_rounds; i++)
   {
-    for (j = 0; j < PING_PEERS; j++)
+    for (j = 0; j < peers_pinging; j++)
     {
       peer = &peers[j];
       FPRINTF (stdout,
@@ -323,6 +353,12 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
       GNUNET_MESH_channel_destroy (peers[i].ch);
     }
+    if (NULL != peers[i].warmup_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: warmup channel %p\n",
+                  i, peers[i].warmup_ch);
+      GNUNET_MESH_channel_destroy (peers[i].warmup_ch);
+    }
     if (NULL != peers[i].incoming_ch)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
@@ -472,8 +508,8 @@ adjust_running_peers (unsigned int target)
   {
     do {
       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
-                                    peers_total - PING_PEERS);
-      r += PING_PEERS;
+                                    peers_total - peers_pinging);
+      r += peers_pinging;
     } while (peers[r].up == run || NULL != peers[r].incoming);
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
                 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
@@ -525,7 +561,7 @@ next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   adjust_running_peers (rounds[current_round] * peers_total);
   current_round++;
 
-  GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
+  GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
 }
 
 
@@ -755,6 +791,17 @@ incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
 
   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
   GNUNET_assert (NULL != peer);
+  if (NULL == peers[n].incoming)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %3u: %u <= %u\n",
+                peers_warmup, n, get_index (peer));
+    peers_warmup++;
+    if (peers_warmup < peers_total)
+      return NULL;
+    test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                              &start_test, NULL);
+    return NULL;
+  }
   GNUNET_assert (peer == peers[n].incoming);
   GNUNET_assert (peer->dest == &peers[n]);
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
@@ -787,6 +834,13 @@ channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
 }
 
 
+/**
+ * Select a random peer that has no incoming channel
+ *
+ * @param peer ID of the peer connecting. NULL if irrelevant (warmup).
+ *
+ * @return Random peer not yet connected to.
+ */
 static struct MeshPeer *
 select_random_peer (struct MeshPeer *peer)
 {
@@ -822,9 +876,8 @@ start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
 
   flags = GNUNET_MESH_OPTION_DEFAULT;
-  for (i = 0; i < PING_PEERS; i++)
+  for (i = 0; i < peers_pinging; i++)
   {
-
     peers[i].dest = select_random_peer (&peers[i]);
     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
                                               &peers[i].dest->id,
@@ -832,7 +885,7 @@ start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     if (NULL == peers[i].ch)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i);
-      GNUNET_SCHEDULER_shutdown ();
+      GNUNET_MESH_TEST_cleanup (test_ctx);
       return;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
@@ -844,14 +897,40 @@ start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
     GNUNET_SCHEDULER_cancel (disconnect_task);
   disconnect_task =
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(ROUND_TIME,
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(round_time,
                                                                 number_rounds + 1),
                                   &disconnect_mesh_peers,
                                   (void *) __LINE__);
-  GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
+  GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
 }
 
 
+/**
+ * Do warmup: create some channels to spread information about the topology.
+ */
+static void
+warmup (void)
+{
+  struct MeshPeer *peer;
+  unsigned int i;
+
+  for (i = 0; i < peers_total; i++)
+  {
+    peer = select_random_peer (NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
+                i, get_index (peer));
+    peers[i].warmup_ch =
+      GNUNET_MESH_channel_create (peers[i].mesh, NULL, &peer->id,
+                                  1, GNUNET_MESH_OPTION_DEFAULT);
+    if (NULL == peers[i].warmup_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i);
+      GNUNET_MESH_TEST_cleanup (test_ctx);
+      return;
+    }
+  }
+}
+
 /**
  * Callback to be called when the requested peer information is available
  *
@@ -889,6 +968,11 @@ peer_id_cb (void *cls,
   if (p_ids < peers_total)
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
+  if (do_warmup)
+  {
+    warmup();
+    return; /* start_test from incoming_channel */
+  }
   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                             &start_test, NULL);
 }
@@ -913,7 +997,6 @@ tmain (void *cls,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
   test_ctx = ctx;
-  GNUNET_assert (peers_total > 2 * PING_PEERS);
   GNUNET_assert (peers_total == num_peers);
   peers_running = num_peers;
   testbed_handles = testbed_peers;
@@ -948,17 +1031,37 @@ main (int argc, char *argv[])
 
   config_file = ".profiler.conf";
 
-  if (2 > argc)
+  if (4 > argc)
+  {
+    fprintf (stderr, "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", argv[0]);
+    fprintf (stderr, "example: %s 30s 16 1 Y\n", argv[0]);
+    return 1;
+  }
+
+  if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[1], &round_time))
   {
-    fprintf (stderr, "usage: %s PEERS\n", argv[0]);
+    fprintf (stderr, "%s is not a valid time\n", argv[1]);
     return 1;
   }
-  peers_total = atoll (argv[1]);
+
+  peers_total = atoll (argv[2]);
   if (2 > peers_total)
   {
     fprintf (stderr, "%s peers is not valid (> 2)\n", argv[1]);
     return 1;
   }
+  peers = GNUNET_malloc (sizeof (struct MeshPeer) * peers_total);
+
+  peers_pinging = atoll (argv[3]);
+
+  if (peers_total < 2 * peers_pinging)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "not enough peers, total should be > 2 * peers_pinging\n");
+    return 1;
+  }
+
+  do_warmup = (5 > argc || argv[4][0] == 'N');
 
   ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, GNUNET_YES);
   GNUNET_assert (NULL != ids);
@@ -970,6 +1073,7 @@ main (int argc, char *argv[])
                         &tmain, NULL, /* tmain cls */
                         &incoming_channel, &channel_cleaner,
                         handlers, ports);
+  GNUNET_free (peers);
 
   return 0;
 }