2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file mesh/gnunet-mesh-profiler.c
23 * @brief Profiler for mesh experiments.
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include "gnunet_statistics_service.h"
36 * How namy peers to run
38 #define TOTAL_PEERS 10
41 * Duration of each round.
43 #define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46 * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
48 #define PING_PERIOD 2000
51 * How long until we give up on connecting the peers?
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
56 * Time to wait for stuff that should be rather fast
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
60 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
65 * Testbed Operation (to get peer id, etc).
67 struct GNUNET_TESTBED_Operation *op;
72 struct GNUNET_PeerIdentity id;
75 * Mesh handle for the root peer
77 struct GNUNET_MESH_Handle *mesh;
80 * Channel handle for the root peer
82 struct GNUNET_MESH_Channel *ch;
85 * Channel handle for the dest peer
87 struct GNUNET_MESH_Channel *incoming_ch;
90 * Number of payload packes sent
95 * Number of payload packets received
101 struct MeshPeer *dest;
102 struct MeshPeer *incoming;
103 GNUNET_SCHEDULER_TaskIdentifier ping_task;
104 struct GNUNET_TIME_Absolute timestamp;
108 * GNUNET_PeerIdentity -> MeshPeer
110 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
113 * Testbed peer handles.
115 static struct GNUNET_TESTBED_Peer **testbed_handles;
118 * Testbed Operation (to get stats).
120 static struct GNUNET_TESTBED_Operation *stats_op;
123 * How many events have happened
128 * Number of events expected to conclude the test successfully.
133 * Size of each test packet
135 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
138 * Operation to get peer ids.
140 struct MeshPeer peers[TOTAL_PEERS];
145 static unsigned int p_ids;
148 * Total number of currently running peers.
150 static unsigned long long peers_running;
153 * Test context (to shut down).
155 static struct GNUNET_MESH_TEST_Context *test_ctx;
158 * Task called to shutdown test.
160 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
163 * Task called to disconnect peers, before shutdown.
165 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
168 * Task to perform tests
170 static GNUNET_SCHEDULER_TaskIdentifier test_task;
174 * Flag to notify callbacks not to generate any new traffic anymore.
176 static int test_finished;
179 * Calculate a random delay.
181 * @param max Exclusive maximum, in ms.
183 * @return A time between 0 a max-1 ms.
185 static struct GNUNET_TIME_Relative
186 delay_ms_rnd (unsigned int max)
190 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
191 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
196 * Get the index of a peer in the peers array.
198 * @param peer Peer whose index to get.
200 * @return Index of peer in peers.
203 get_index (struct MeshPeer *peer)
210 * Show the results of the test (banwidth acheived) and log them to GAUGER
219 * Shut down peergroup, clean up.
221 * @param cls Closure (unused).
222 * @param tc Task Context.
225 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
228 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
233 * Disconnect from mesh services af all peers, call shutdown.
235 * @param cls Closure (unused).
236 * @param tc Task Context.
239 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 long line = (long) cls;
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "disconnecting mesh service of peers, called from line %ld\n",
247 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
248 for (i = 0; i < TOTAL_PEERS; i++)
250 if (NULL != peers[i].op)
251 GNUNET_TESTBED_operation_done (peers[i].op);
253 if (peers[i].up != GNUNET_YES)
256 GNUNET_MESH_channel_destroy (peers[i].ch);
257 if (NULL != peers[i].incoming_ch)
258 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
260 GNUNET_MESH_TEST_cleanup (test_ctx);
261 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
263 GNUNET_SCHEDULER_cancel (shutdown_handle);
265 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
270 * Finish test normally: schedule disconnect and shutdown
272 * @param line Line in the code the abort is requested from (__LINE__).
275 abort_test (long line)
277 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
279 GNUNET_SCHEDULER_cancel (disconnect_task);
280 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
286 * Stats callback. Finish the stats testbed operation and when all stats have
287 * been iterated, shutdown the test.
290 * @param op the operation that has been finished
291 * @param emsg error message in case the operation has failed; will be NULL if
292 * operation has executed successfully.
295 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
298 GNUNET_TESTBED_operation_done (stats_op);
300 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
301 GNUNET_SCHEDULER_cancel (disconnect_task);
302 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
309 * Process statistic values.
312 * @param peer the peer the statistic belong to
313 * @param subsystem name of subsystem that created the statistic
314 * @param name the name of the datum
315 * @param value the current value
316 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
317 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
320 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
321 const char *subsystem, const char *name,
322 uint64_t value, int is_persistent)
326 i = GNUNET_TESTBED_get_index (peer);
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
328 i, subsystem, name, value);
335 * Task check that keepalives were sent and received.
337 * @param cls Closure (NULL).
338 * @param tc Task Context.
341 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
346 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
348 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
350 stats_iterator, stats_cont, NULL);
355 * @brief Finish profiler normally. Signal finish and start collecting stats.
357 * @param cls Closure (unused).
358 * @param tc Task context.
361 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
363 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
366 test_finished = GNUNET_YES;
368 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
372 * Set the total number of running peers.
374 * @param target Desired number of running peers.
377 adjust_running_peers (unsigned int target)
379 struct GNUNET_TESTBED_Operation *op;
385 GNUNET_assert (target <= TOTAL_PEERS);
387 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
388 if (target > peers_running)
390 delta = target - peers_running;
395 delta = peers_running - target;
399 for (i = 0; i < delta; i++)
402 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
403 } while (peers[r].up == run);
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
405 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
407 if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
408 GNUNET_SCHEDULER_cancel (peers[r].ping_task);
409 peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
411 peers[r].up = GNUNET_NO;
412 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
413 "mesh", NULL, NULL, run);
414 GNUNET_break (NULL != op);
415 peers_running += run ? 1 : -1;
416 GNUNET_assert (peers_running > 0);
422 * @brief Move to next round.
424 * @param cls Closure (round #).
425 * @param tc Task context.
428 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
430 long round = (long) cls;
432 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
435 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
436 if (0.0 == rounds[round])
438 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
439 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
442 adjust_running_peers (rounds[round] * TOTAL_PEERS);
444 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
449 * Transmit ready callback.
451 * @param cls Closure (peer for PING, NULL for PONG).
452 * @param size Size of the tranmist buffer.
453 * @param buf Pointer to the beginning of the buffer.
455 * @return Number of bytes written to buf.
458 tmt_rdy (void *cls, size_t size, void *buf);
462 * @brief Send a ping to destination
464 * @param cls Closure (peer).
465 * @param tc Task context.
468 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 struct MeshPeer *peer = (struct MeshPeer *) cls;
472 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
474 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
475 || GNUNET_YES == test_finished
476 || 0 != peer->timestamp.abs_value_us)
479 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
480 get_index (peer), get_index (peer->dest));
482 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
483 GNUNET_TIME_UNIT_FOREVER_REL,
484 size_payload, &tmt_rdy, peer);
488 * @brief Reply with a pong to origin.
490 * @param cls Closure (peer).
491 * @param tc Task context.
494 pong (struct GNUNET_MESH_Channel *channel)
496 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
497 GNUNET_TIME_UNIT_FOREVER_REL,
498 size_payload, &tmt_rdy, NULL);
503 * Transmit ready callback
505 * @param cls Closure (peer for PING, NULL for PONG).
506 * @param size Size of the buffer we have.
507 * @param buf Buffer to copy data to.
510 tmt_rdy (void *cls, size_t size, void *buf)
512 struct MeshPeer *peer = (struct MeshPeer *) cls;
513 struct GNUNET_MessageHeader *msg = buf;
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
517 if (size < size_payload || NULL == buf)
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "size %u, buf %p, data_sent %u, data_received %u\n",
522 size, buf, peer->data_sent, peer->data_received);
526 msg->size = htons (size);
529 msg->type = htons (PONG);
530 return sizeof (*msg);
533 msg->type = htons (PING);
534 data = (uint32_t *) &msg[1];
535 *data = htonl (peer->data_sent);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
538 peer->timestamp = GNUNET_TIME_absolute_get ();
539 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
547 * Function is called whenever a PING message is received.
549 * @param cls closure (peer #, set from GNUNET_MESH_connect)
550 * @param channel connection to the other end
551 * @param channel_ctx place to store local state associated with the channel
552 * @param message the actual message
553 * @return GNUNET_OK to keep the connection open,
554 * GNUNET_SYSERR to close it (signal serious error)
557 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
559 const struct GNUNET_MessageHeader *message)
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
564 GNUNET_MESH_receive_done (channel);
565 if (GNUNET_NO == test_finished)
573 * Function is called whenever a PONG message is received.
575 * @param cls closure (peer #, set from GNUNET_MESH_connect)
576 * @param channel connection to the other end
577 * @param channel_ctx place to store local state associated with the channel
578 * @param message the actual message
579 * @return GNUNET_OK to keep the connection open,
580 * GNUNET_SYSERR to close it (signal serious error)
583 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
585 const struct GNUNET_MessageHeader *message)
588 struct MeshPeer *peer;
589 struct GNUNET_TIME_Relative latency;
591 GNUNET_MESH_receive_done (channel);
594 GNUNET_break (0 != peer->timestamp.abs_value_us);
595 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
596 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
597 get_index (peer), get_index (peer->dest),
598 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
600 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
602 peer->timestamp = GNUNET_TIME_absolute_get ();
603 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
608 peer->timestamp.abs_value_us = 0;
616 * Handlers, for diverse services
618 static struct GNUNET_MESH_MessageHandler handlers[] = {
619 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
620 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
626 * Method called whenever another peer has added us to a channel
627 * the other peer initiated.
629 * @param cls Closure.
630 * @param channel New handle to the channel.
631 * @param initiator Peer that started the channel.
632 * @param port Port this channel is connected to.
633 * @param options channel option flags
634 * @return Initial channel context for the channel
635 * (can be NULL -- that's not an error).
638 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
639 const struct GNUNET_PeerIdentity *initiator,
640 uint32_t port, enum GNUNET_MESH_ChannelOption options)
643 struct MeshPeer *peer;
645 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
646 GNUNET_assert (NULL != peer);
647 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
648 n, get_index (peer), channel);
649 peers[n].incoming_ch = channel;
651 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
653 GNUNET_SCHEDULER_cancel (disconnect_task);
654 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
655 &disconnect_mesh_peers,
663 * Function called whenever an inbound channel is destroyed. Should clean up
664 * any associated state.
666 * @param cls closure (set from GNUNET_MESH_connect)
667 * @param channel connection to the other end (henceforth invalid)
668 * @param channel_ctx place where local state associated
669 * with the channel is stored
672 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
676 struct MeshPeer *peer = &peers[n];
678 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
679 "Channel %p disconnected at peer %ld\n", channel, n);
680 if (peer->ch == channel)
685 static struct MeshPeer *
686 select_random_peer (struct MeshPeer *peer)
692 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
693 } while (NULL != peers[r].incoming);
694 peers[r].incoming = peer;
700 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
702 * Testcase continues when the root receives confirmation of connected peers,
703 * on callback funtion ch.
705 * @param cls Closure (unsued).
706 * @param tc Task Context.
709 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
711 enum GNUNET_MESH_ChannelOption flags;
714 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
717 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
719 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
720 GNUNET_SCHEDULER_cancel (disconnect_task);
721 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
722 &disconnect_mesh_peers,
725 flags = GNUNET_MESH_OPTION_DEFAULT;
726 for (i = 0; i < TOTAL_PEERS; i++)
729 peers[i].dest = select_random_peer (&peers[i]);
730 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
733 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
734 i, get_index (peers[i].dest), peers[i].ch);
735 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
738 peers_running = TOTAL_PEERS;
739 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
744 * Callback to be called when the requested peer information is available
746 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
747 * @param op the operation this callback corresponds to
748 * @param pinfo the result; will be NULL if the operation has failed
749 * @param emsg error message if the operation has failed;
750 * NULL if the operation is successfull
753 peer_id_cb (void *cls,
754 struct GNUNET_TESTBED_Operation *op,
755 const struct GNUNET_TESTBED_PeerInformation *pinfo,
760 if (NULL == pinfo || NULL != emsg)
762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
763 abort_test (__LINE__);
766 peers[n].id = *(pinfo->result.id);
767 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
768 n, GNUNET_i2s (&peers[n].id));
769 GNUNET_break (GNUNET_OK ==
770 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
771 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
773 GNUNET_TESTBED_operation_done (peers[n].op);
777 if (p_ids < TOTAL_PEERS)
779 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
780 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
785 * test main: start test when all peers are connected
787 * @param cls Closure.
788 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
789 * @param num_peers Number of peers that are running.
790 * @param testbed_peers Array of peers.
791 * @param meshes Handle to each of the MESHs of the peers.
795 struct GNUNET_MESH_TEST_Context *ctx,
796 unsigned int num_peers,
797 struct GNUNET_TESTBED_Peer **testbed_peers,
798 struct GNUNET_MESH_Handle **meshes)
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
805 GNUNET_assert (TOTAL_PEERS == num_peers);
806 peers_running = num_peers;
807 testbed_handles = testbed_peers;
808 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
809 &disconnect_mesh_peers,
811 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
812 &shutdown_task, NULL);
813 for (i = 0; i < TOTAL_PEERS; i++)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
816 peers[i].up = GNUNET_YES;
817 peers[i].mesh = meshes[i];
819 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
820 GNUNET_TESTBED_PIT_IDENTITY,
821 &peer_id_cb, (void *) i);
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
824 /* Continues from pi_cb -> do_test */
829 * Main: start profiler.
832 main (int argc, char *argv[])
834 static uint32_t ports[2];
835 const char *config_file;
837 config_file = "test_mesh.conf";
839 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
840 GNUNET_assert (NULL != ids);
842 test_finished = GNUNET_NO;
845 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
846 &tmain, NULL, /* tmain cls */
847 &incoming_channel, &channel_cleaner,
852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
853 "FAILED! (%d/%d)\n", ok, ok_goal);
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
860 /* end of gnunet-mesh-profiler.c */