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 many peers to run
38 #define TOTAL_PEERS 10
41 * How many peers do pinging
47 * Duration of each round.
49 #define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52 * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
54 #define PING_PERIOD 2000
57 * How long until we give up on connecting the peers?
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
62 * Time to wait for stuff that should be rather fast
64 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
66 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
71 * Testbed Operation (to get peer id, etc).
73 struct GNUNET_TESTBED_Operation *op;
78 struct GNUNET_PeerIdentity id;
81 * Mesh handle for the root peer
83 struct GNUNET_MESH_Handle *mesh;
86 * Channel handle for the root peer
88 struct GNUNET_MESH_Channel *ch;
91 * Channel handle for the dest peer
93 struct GNUNET_MESH_Channel *incoming_ch;
96 * Number of payload packes sent
101 * Number of payload packets received
107 struct MeshPeer *dest;
108 struct MeshPeer *incoming;
109 GNUNET_SCHEDULER_TaskIdentifier ping_task;
110 struct GNUNET_TIME_Absolute timestamp;
114 * GNUNET_PeerIdentity -> MeshPeer
116 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
119 * Testbed peer handles.
121 static struct GNUNET_TESTBED_Peer **testbed_handles;
124 * Testbed Operation (to get stats).
126 static struct GNUNET_TESTBED_Operation *stats_op;
129 * How many events have happened
134 * Number of events expected to conclude the test successfully.
139 * Size of each test packet
141 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
144 * Operation to get peer ids.
146 struct MeshPeer peers[TOTAL_PEERS];
151 static unsigned int p_ids;
154 * Total number of currently running peers.
156 static unsigned long long peers_running;
159 * Test context (to shut down).
161 static struct GNUNET_MESH_TEST_Context *test_ctx;
164 * Task called to shutdown test.
166 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
169 * Task called to disconnect peers, before shutdown.
171 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
174 * Task to perform tests
176 static GNUNET_SCHEDULER_TaskIdentifier test_task;
180 * Flag to notify callbacks not to generate any new traffic anymore.
182 static int test_finished;
185 * Calculate a random delay.
187 * @param max Exclusive maximum, in ms.
189 * @return A time between 0 a max-1 ms.
191 static struct GNUNET_TIME_Relative
192 delay_ms_rnd (unsigned int max)
196 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
197 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
202 * Get the index of a peer in the peers array.
204 * @param peer Peer whose index to get.
206 * @return Index of peer in peers.
209 get_index (struct MeshPeer *peer)
216 * Show the results of the test (banwidth acheived) and log them to GAUGER
225 * Shut down peergroup, clean up.
227 * @param cls Closure (unused).
228 * @param tc Task Context.
231 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
234 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
239 * Disconnect from mesh services af all peers, call shutdown.
241 * @param cls Closure (unused).
242 * @param tc Task Context.
245 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 long line = (long) cls;
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "disconnecting mesh service, called from line %ld\n", line);
252 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
253 for (i = 0; i < TOTAL_PEERS; i++)
255 if (NULL != peers[i].op)
256 GNUNET_TESTBED_operation_done (peers[i].op);
258 if (peers[i].up != GNUNET_YES)
261 if (NULL != peers[i].ch)
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
264 GNUNET_MESH_channel_destroy (peers[i].ch);
266 if (NULL != peers[i].incoming_ch)
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
269 i, peers[i].incoming_ch);
270 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
273 GNUNET_MESH_TEST_cleanup (test_ctx);
274 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
276 GNUNET_SCHEDULER_cancel (shutdown_handle);
278 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
283 * Finish test normally: schedule disconnect and shutdown
285 * @param line Line in the code the abort is requested from (__LINE__).
288 abort_test (long line)
290 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
292 GNUNET_SCHEDULER_cancel (disconnect_task);
293 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
299 * Stats callback. Finish the stats testbed operation and when all stats have
300 * been iterated, shutdown the test.
303 * @param op the operation that has been finished
304 * @param emsg error message in case the operation has failed; will be NULL if
305 * operation has executed successfully.
308 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
311 GNUNET_TESTBED_operation_done (stats_op);
313 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
314 GNUNET_SCHEDULER_cancel (disconnect_task);
315 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
322 * Process statistic values.
325 * @param peer the peer the statistic belong to
326 * @param subsystem name of subsystem that created the statistic
327 * @param name the name of the datum
328 * @param value the current value
329 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
330 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
333 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
334 const char *subsystem, const char *name,
335 uint64_t value, int is_persistent)
339 i = GNUNET_TESTBED_get_index (peer);
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
341 i, subsystem, name, value);
348 * Task check that keepalives were sent and received.
350 * @param cls Closure (NULL).
351 * @param tc Task Context.
354 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
360 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
362 stats_iterator, stats_cont, NULL);
367 * @brief Finish profiler normally. Signal finish and start collecting stats.
369 * @param cls Closure (unused).
370 * @param tc Task context.
373 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
378 test_finished = GNUNET_YES;
380 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
384 * Set the total number of running peers.
386 * @param target Desired number of running peers.
389 adjust_running_peers (unsigned int target)
391 struct GNUNET_TESTBED_Operation *op;
397 GNUNET_assert (target <= TOTAL_PEERS);
399 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
400 if (target > peers_running)
402 delta = target - peers_running;
407 delta = peers_running - target;
411 for (i = 0; i < delta; i++)
414 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
415 TOTAL_PEERS - PING_PEERS);
417 } while (peers[r].up == run || NULL != peers[r].incoming);
418 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
419 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
421 if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
422 GNUNET_SCHEDULER_cancel (peers[r].ping_task);
423 peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
427 if (NULL != peers[r].ch)
428 GNUNET_MESH_channel_destroy (peers[r].ch);
430 if (NULL != peers[r].dest)
432 if (NULL != peers[r].dest->incoming_ch)
433 GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
434 peers[r].dest->incoming_ch = NULL;
437 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
438 "mesh", NULL, NULL, run);
439 GNUNET_break (NULL != op);
440 peers_running += run ? 1 : -1;
441 GNUNET_assert (peers_running > 0);
447 * @brief Move to next round.
449 * @param cls Closure (round #).
450 * @param tc Task context.
453 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
455 long round = (long) cls;
457 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
460 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
461 if (0.0 == rounds[round])
463 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
464 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
467 adjust_running_peers (rounds[round] * TOTAL_PEERS);
469 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
474 * Transmit ready callback.
476 * @param cls Closure (peer for PING, NULL for PONG).
477 * @param size Size of the tranmist buffer.
478 * @param buf Pointer to the beginning of the buffer.
480 * @return Number of bytes written to buf.
483 tmt_rdy (void *cls, size_t size, void *buf);
487 * @brief Send a ping to destination
489 * @param cls Closure (peer).
490 * @param tc Task context.
493 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
495 struct MeshPeer *peer = (struct MeshPeer *) cls;
497 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
499 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
500 || GNUNET_YES == test_finished
501 || 0 != peer->timestamp.abs_value_us)
504 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
505 get_index (peer), get_index (peer->dest));
507 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
508 GNUNET_TIME_UNIT_FOREVER_REL,
509 size_payload, &tmt_rdy, peer);
513 * @brief Reply with a pong to origin.
515 * @param cls Closure (peer).
516 * @param tc Task context.
519 pong (struct GNUNET_MESH_Channel *channel)
521 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
522 GNUNET_TIME_UNIT_FOREVER_REL,
523 size_payload, &tmt_rdy, NULL);
528 * Transmit ready callback
530 * @param cls Closure (peer for PING, NULL for PONG).
531 * @param size Size of the buffer we have.
532 * @param buf Buffer to copy data to.
535 tmt_rdy (void *cls, size_t size, void *buf)
537 struct MeshPeer *peer = (struct MeshPeer *) cls;
538 struct GNUNET_MessageHeader *msg = buf;
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
542 if (size < size_payload || NULL == buf)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "size %u, buf %p, data_sent %u, data_received %u\n",
547 size, buf, peer->data_sent, peer->data_received);
551 msg->size = htons (size);
554 msg->type = htons (PONG);
555 return sizeof (*msg);
558 msg->type = htons (PING);
559 data = (uint32_t *) &msg[1];
560 *data = htonl (peer->data_sent);
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
563 peer->timestamp = GNUNET_TIME_absolute_get ();
564 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
572 * Function is called whenever a PING message is received.
574 * @param cls closure (peer #, set from GNUNET_MESH_connect)
575 * @param channel connection to the other end
576 * @param channel_ctx place to store local state associated with the channel
577 * @param message the actual message
578 * @return GNUNET_OK to keep the connection open,
579 * GNUNET_SYSERR to close it (signal serious error)
582 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
584 const struct GNUNET_MessageHeader *message)
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
589 GNUNET_MESH_receive_done (channel);
590 if (GNUNET_NO == test_finished)
598 * Function is called whenever a PONG message is received.
600 * @param cls closure (peer #, set from GNUNET_MESH_connect)
601 * @param channel connection to the other end
602 * @param channel_ctx place to store local state associated with the channel
603 * @param message the actual message
604 * @return GNUNET_OK to keep the connection open,
605 * GNUNET_SYSERR to close it (signal serious error)
608 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
610 const struct GNUNET_MessageHeader *message)
613 struct MeshPeer *peer;
614 struct GNUNET_TIME_Relative latency;
616 GNUNET_MESH_receive_done (channel);
619 GNUNET_break (0 != peer->timestamp.abs_value_us);
620 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
621 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
622 get_index (peer), get_index (peer->dest),
623 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
625 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
627 peer->timestamp = GNUNET_TIME_absolute_get ();
628 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
633 peer->timestamp.abs_value_us = 0;
641 * Handlers, for diverse services
643 static struct GNUNET_MESH_MessageHandler handlers[] = {
644 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
645 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
651 * Method called whenever another peer has added us to a channel
652 * the other peer initiated.
654 * @param cls Closure.
655 * @param channel New handle to the channel.
656 * @param initiator Peer that started the channel.
657 * @param port Port this channel is connected to.
658 * @param options channel option flags
659 * @return Initial channel context for the channel
660 * (can be NULL -- that's not an error).
663 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
664 const struct GNUNET_PeerIdentity *initiator,
665 uint32_t port, enum GNUNET_MESH_ChannelOption options)
668 struct MeshPeer *peer;
670 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
671 GNUNET_assert (NULL != peer);
672 GNUNET_assert (peer == peers[n].incoming);
673 GNUNET_assert (peer->dest == &peers[n]);
674 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
675 n, get_index (peer), channel);
676 peers[n].incoming_ch = channel;
682 * Function called whenever an inbound channel is destroyed. Should clean up
683 * any associated state.
685 * @param cls closure (set from GNUNET_MESH_connect)
686 * @param channel connection to the other end (henceforth invalid)
687 * @param channel_ctx place where local state associated
688 * with the channel is stored
691 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
695 struct MeshPeer *peer = &peers[n];
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698 "Channel %p disconnected at peer %ld\n", channel, n);
699 if (peer->ch == channel)
704 static struct MeshPeer *
705 select_random_peer (struct MeshPeer *peer)
711 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
712 } while (NULL != peers[r].incoming);
713 peers[r].incoming = peer;
719 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
721 * Testcase continues when the root receives confirmation of connected peers,
722 * on callback funtion ch.
724 * @param cls Closure (unsued).
725 * @param tc Task Context.
728 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
730 enum GNUNET_MESH_ChannelOption flags;
733 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
736 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
738 flags = GNUNET_MESH_OPTION_DEFAULT;
739 for (i = 0; i < PING_PEERS; i++)
742 peers[i].dest = select_random_peer (&peers[i]);
743 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
747 i, get_index (peers[i].dest), peers[i].ch);
748 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
751 peers_running = TOTAL_PEERS;
752 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
757 * Callback to be called when the requested peer information is available
759 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
760 * @param op the operation this callback corresponds to
761 * @param pinfo the result; will be NULL if the operation has failed
762 * @param emsg error message if the operation has failed;
763 * NULL if the operation is successfull
766 peer_id_cb (void *cls,
767 struct GNUNET_TESTBED_Operation *op,
768 const struct GNUNET_TESTBED_PeerInformation *pinfo,
773 if (NULL == pinfo || NULL != emsg)
775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
776 abort_test (__LINE__);
779 peers[n].id = *(pinfo->result.id);
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
781 n, GNUNET_i2s (&peers[n].id));
782 GNUNET_break (GNUNET_OK ==
783 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
784 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
786 GNUNET_TESTBED_operation_done (peers[n].op);
790 if (p_ids < TOTAL_PEERS)
792 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
793 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
798 * test main: start test when all peers are connected
800 * @param cls Closure.
801 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
802 * @param num_peers Number of peers that are running.
803 * @param testbed_peers Array of peers.
804 * @param meshes Handle to each of the MESHs of the peers.
808 struct GNUNET_MESH_TEST_Context *ctx,
809 unsigned int num_peers,
810 struct GNUNET_TESTBED_Peer **testbed_peers,
811 struct GNUNET_MESH_Handle **meshes)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
818 GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
819 GNUNET_assert (TOTAL_PEERS == num_peers);
820 peers_running = num_peers;
821 testbed_handles = testbed_peers;
822 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
823 &disconnect_mesh_peers,
825 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
826 &shutdown_task, NULL);
827 for (i = 0; i < TOTAL_PEERS; i++)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
830 peers[i].up = GNUNET_YES;
831 peers[i].mesh = meshes[i];
833 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
834 GNUNET_TESTBED_PIT_IDENTITY,
835 &peer_id_cb, (void *) i);
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
838 /* Continues from pi_cb -> do_test */
843 * Main: start profiler.
846 main (int argc, char *argv[])
848 static uint32_t ports[2];
849 const char *config_file;
851 config_file = "test_mesh.conf";
853 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
854 GNUNET_assert (NULL != ids);
856 test_finished = GNUNET_NO;
859 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
860 &tmain, NULL, /* tmain cls */
861 &incoming_channel, &channel_cleaner,
866 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
867 "FAILED! (%d/%d)\n", ok, ok_goal);
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
874 /* end of gnunet-mesh-profiler.c */