-log
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.c
index ec2d646060ac3e23a2ab8e516d13afa604adbe53..f1c0992907fa2a8a2c7751b14c8c5ad7d3e2c684 100644 (file)
 #define PONG 2
 
 /**
- * How namy peers to run
+ * How many peers to run
  */
 #define TOTAL_PEERS 10
 
+/**
+ * How many peers do pinging
+ */
+#define PING_PEERS 1
+
+
 /**
  * Duration of each round.
  */
 
 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
 
+/**
+ * Message type for pings.
+ */
+struct MeshPingMessage
+{
+  /**
+   * Header. Type PING/PONG.
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * Message number.
+   */
+  uint32_t counter;
+
+  /**
+   * Time the message was sent.
+   */
+  struct GNUNET_TIME_AbsoluteNBO timestamp;
+};
+
+/**
+ * Peer description.
+ */
 struct MeshPeer
 {
   /**
@@ -98,10 +128,20 @@ struct MeshPeer
 
   int up;
 
+  /**
+   * Destinaton to ping.
+   */
   struct MeshPeer *dest;
+
+  /**
+   * Incoming channel for pings.
+   */
   struct MeshPeer *incoming;
+
+  /**
+   * Task to do the next ping.
+   */
   GNUNET_SCHEDULER_TaskIdentifier ping_task;
-  struct GNUNET_TIME_Absolute timestamp;
 };
 
 /**
@@ -129,11 +169,6 @@ static int ok;
  */
 static int ok_goal;
 
-/**
- * Size of each test packet
- */
-size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
-
 /**
  * Operation to get peer ids.
  */
@@ -242,19 +277,27 @@ disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   unsigned int i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "disconnecting mesh service of peers, called from line %ld\n",
-              line);
+              "disconnecting mesh service, called from line %ld\n", line);
   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
   for (i = 0; i < TOTAL_PEERS; i++)
   {
-    GNUNET_TESTBED_operation_done (peers[i].op);
+    if (NULL != peers[i].op)
+      GNUNET_TESTBED_operation_done (peers[i].op);
 
     if (peers[i].up != GNUNET_YES)
       continue;
 
-    GNUNET_MESH_channel_destroy (peers[i].ch);
+    if (NULL != peers[i].ch)
+    {
+      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].incoming_ch)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
+                  i, peers[i].incoming_ch);
       GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
+    }
   }
   GNUNET_MESH_TEST_cleanup (test_ctx);
   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
@@ -342,7 +385,6 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
     return;
 
-  disconnect_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
   stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
                                             NULL, NULL,
@@ -398,8 +440,10 @@ adjust_running_peers (unsigned int target)
   for (i = 0; i < delta; i++)
   {
     do {
-      r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
-    } while (peers[r].up == run);
+      r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                    TOTAL_PEERS - PING_PEERS);
+      r += PING_PEERS;
+    } 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));
 
@@ -407,7 +451,18 @@ adjust_running_peers (unsigned int target)
       GNUNET_SCHEDULER_cancel (peers[r].ping_task);
     peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
 
-    peers[r].up = GNUNET_NO;
+    peers[r].up = run;
+
+    if (NULL != peers[r].ch)
+      GNUNET_MESH_channel_destroy (peers[r].ch);
+    peers[r].ch = NULL;
+    if (NULL != peers[r].dest)
+    {
+      if (NULL != peers[r].dest->incoming_ch)
+        GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
+      peers[r].dest->incoming_ch = NULL;
+    }
+
     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
                                              "mesh", NULL, NULL, run);
     GNUNET_break (NULL != op);
@@ -445,7 +500,7 @@ next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 
 /**
- * Transmit ready callback.
+ * Transmit ping callback.
  *
  * @param cls Closure (peer for PING, NULL for PONG).
  * @param size Size of the tranmist buffer.
@@ -454,7 +509,34 @@ next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return Number of bytes written to buf.
  */
 static size_t
-tmt_rdy (void *cls, size_t size, void *buf);
+tmt_rdy_ping (void *cls, size_t size, void *buf);
+
+
+/**
+ * Transmit pong callback.
+ *
+ * @param cls Closure (copy of PING message, to be freed).
+ * @param size Size of the buffer we have.
+ * @param buf Buffer to copy data to.
+ */
+static size_t
+tmt_rdy_pong (void *cls, size_t size, void *buf)
+{
+  struct MeshPingMessage *ping = cls;
+  struct MeshPingMessage *pong;
+
+  if (0 == size || NULL == buf)
+  {
+    GNUNET_free (ping);
+    return 0;
+  }
+  pong = (struct MeshPingMessage *) buf;
+  memcpy (pong, ping, sizeof (*ping));
+  pong->header.type = htons (PONG);
+
+  GNUNET_free (ping);
+  return sizeof (*ping);
+}
 
 
 /**
@@ -471,16 +553,16 @@ ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
 
   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
-      || GNUNET_YES == test_finished
-      || 0 != peer->timestamp.abs_value_us)
+      || GNUNET_YES == test_finished)
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
-              get_index (peer), get_index (peer->dest));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u (%u)\n",
+              get_index (peer), get_index (peer->dest), peer->data_sent);
 
   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                     size_payload, &tmt_rdy, peer);
+                                     sizeof (struct MeshPingMessage),
+                                     &tmt_rdy_ping, peer);
 }
 
 /**
@@ -490,30 +572,34 @@ ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param tc Task context.
  */
 static void
-pong (struct GNUNET_MESH_Channel *channel)
+pong (struct GNUNET_MESH_Channel *channel, const struct MeshPingMessage *ping)
 {
+  struct MeshPingMessage *copy;
+
+  copy = GNUNET_new (struct MeshPingMessage);
+  memcpy (copy, ping, sizeof (*ping));
   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
-                                     size_payload, &tmt_rdy, NULL);
+                                     sizeof (struct MeshPingMessage),
+                                     &tmt_rdy_pong, copy);
 }
 
 
 /**
- * Transmit ready callback
+ * Transmit ping callback
  *
- * @param cls Closure (peer for PING, NULL for PONG).
+ * @param cls Closure (peer).
  * @param size Size of the buffer we have.
  * @param buf Buffer to copy data to.
  */
 static size_t
-tmt_rdy (void *cls, size_t size, void *buf)
+tmt_rdy_ping (void *cls, size_t size, void *buf)
 {
   struct MeshPeer *peer = (struct MeshPeer *) cls;
-  struct GNUNET_MessageHeader *msg = buf;
-  uint32_t *data;
+  struct MeshPingMessage *msg = buf;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
-  if (size < size_payload || NULL == buf)
+  if (size < sizeof (struct MeshPingMessage) || NULL == buf)
   {
     GNUNET_break (0);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -522,23 +608,15 @@ tmt_rdy (void *cls, size_t size, void *buf)
 
     return 0;
   }
-  msg->size = htons (size);
-  if (NULL == peer)
-  {
-    msg->type = htons (PONG);
-    return sizeof (*msg);
-  }
-
-  msg->type = htons (PING);
-  data = (uint32_t *) &msg[1];
-  *data = htonl (peer->data_sent);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
-  peer->data_sent++;
-  peer->timestamp = GNUNET_TIME_absolute_get ();
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending: msg %d\n", peer->data_sent);
+  msg->header.size = htons (size);
+  msg->header.type = htons (PING);
+  msg->counter = htonl (peer->data_sent++);
+  msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
                                                   &ping, peer);
 
-  return size_payload;
+  return sizeof (struct MeshPingMessage);
 }
 
 
@@ -562,7 +640,7 @@ ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
   GNUNET_MESH_receive_done (channel);
   if (GNUNET_NO == test_finished)
-    pong (channel);
+    pong (channel, (struct MeshPingMessage *) message);
 
   return GNUNET_OK;
 }
@@ -585,27 +663,20 @@ pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
 {
   long n = (long) cls;
   struct MeshPeer *peer;
+  struct MeshPingMessage *msg;
+  struct GNUNET_TIME_Absolute send_time;
   struct GNUNET_TIME_Relative latency;
 
   GNUNET_MESH_receive_done (channel);
   peer = &peers[n];
 
-  GNUNET_break (0 != peer->timestamp.abs_value_us);
-  latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
-              get_index (peer), get_index (peer->dest),
-              GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
+  msg = (struct MeshPingMessage *) message;
 
-  if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
-  {
-    peer->timestamp = GNUNET_TIME_absolute_get ();
-    peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
-                                                    &ping, peer);
-  }
-  else
-  {
-    peer->timestamp.abs_value_us = 0;
-  }
+  send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp);
+  latency = GNUNET_TIME_absolute_get_duration (send_time);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n",
+              get_index (peer), get_index (peer->dest), ntohl (msg->counter),
+              GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
 
   return GNUNET_OK;
 }
@@ -615,8 +686,8 @@ pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
  * Handlers, for diverse services
  */
 static struct GNUNET_MESH_MessageHandler handlers[] = {
-  {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
-  {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
+  {&ping_handler, PING, sizeof (struct MeshPingMessage)},
+  {&pong_handler, PONG, sizeof (struct MeshPingMessage)},
   {NULL, 0, 0}
 };
 
@@ -643,18 +714,12 @@ incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
 
   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
   GNUNET_assert (NULL != peer);
+  GNUNET_assert (peer == peers[n].incoming);
+  GNUNET_assert (peer->dest == &peers[n]);
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
               n, get_index (peer), channel);
   peers[n].incoming_ch = channel;
 
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
-                                                    &disconnect_mesh_peers,
-                                                    (void *) __LINE__);
-  }
-
   return NULL;
 }
 
@@ -675,7 +740,7 @@ channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
   struct MeshPeer *peer = &peers[n];
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Incoming channel %p disconnected at peer %ld\n", channel, n);
+              "Channel %p disconnected at peer %ld\n", channel, n);
   if (peer->ch == channel)
     peer->ch = NULL;
 }
@@ -715,14 +780,8 @@ start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
 
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-  disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
-                                                  &disconnect_mesh_peers,
-                                                  (void *) __LINE__);
-
   flags = GNUNET_MESH_OPTION_DEFAULT;
-  for (i = 0; i < TOTAL_PEERS; i++)
+  for (i = 0; i < PING_PEERS; i++)
   {
 
     peers[i].dest = select_random_peer (&peers[i]);
@@ -768,6 +827,10 @@ peer_id_cb (void *cls,
   GNUNET_break (GNUNET_OK ==
                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+
+  GNUNET_TESTBED_operation_done (peers[n].op);
+  peers[n].op = NULL;
+
   p_ids++;
   if (p_ids < TOTAL_PEERS)
     return;
@@ -797,6 +860,7 @@ tmain (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
   ok = 0;
   test_ctx = ctx;
+  GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
   GNUNET_assert (TOTAL_PEERS == num_peers);
   peers_running = num_peers;
   testbed_handles = testbed_peers;