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;
436 GNUNET_MESH_disconnect (peers[r].mesh);
437 peers[r].mesh = NULL;
439 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
440 "mesh", NULL, NULL, run);
441 GNUNET_break (NULL != op);
442 peers_running += run ? 1 : -1;
443 GNUNET_assert (peers_running > 0);
449 * @brief Move to next round.
451 * @param cls Closure (round #).
452 * @param tc Task context.
455 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
457 long round = (long) cls;
459 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
462 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
463 if (0.0 == rounds[round])
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
466 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
469 adjust_running_peers (rounds[round] * TOTAL_PEERS);
471 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
476 * Transmit ready callback.
478 * @param cls Closure (peer for PING, NULL for PONG).
479 * @param size Size of the tranmist buffer.
480 * @param buf Pointer to the beginning of the buffer.
482 * @return Number of bytes written to buf.
485 tmt_rdy (void *cls, size_t size, void *buf);
489 * @brief Send a ping to destination
491 * @param cls Closure (peer).
492 * @param tc Task context.
495 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
497 struct MeshPeer *peer = (struct MeshPeer *) cls;
499 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
501 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
502 || GNUNET_YES == test_finished
503 || 0 != peer->timestamp.abs_value_us)
506 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
507 get_index (peer), get_index (peer->dest));
509 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
510 GNUNET_TIME_UNIT_FOREVER_REL,
511 size_payload, &tmt_rdy, peer);
515 * @brief Reply with a pong to origin.
517 * @param cls Closure (peer).
518 * @param tc Task context.
521 pong (struct GNUNET_MESH_Channel *channel)
523 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
524 GNUNET_TIME_UNIT_FOREVER_REL,
525 size_payload, &tmt_rdy, NULL);
530 * Transmit ready callback
532 * @param cls Closure (peer for PING, NULL for PONG).
533 * @param size Size of the buffer we have.
534 * @param buf Buffer to copy data to.
537 tmt_rdy (void *cls, size_t size, void *buf)
539 struct MeshPeer *peer = (struct MeshPeer *) cls;
540 struct GNUNET_MessageHeader *msg = buf;
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
544 if (size < size_payload || NULL == buf)
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "size %u, buf %p, data_sent %u, data_received %u\n",
549 size, buf, peer->data_sent, peer->data_received);
553 msg->size = htons (size);
556 msg->type = htons (PONG);
557 return sizeof (*msg);
560 msg->type = htons (PING);
561 data = (uint32_t *) &msg[1];
562 *data = htonl (peer->data_sent);
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
565 peer->timestamp = GNUNET_TIME_absolute_get ();
566 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
574 * Function is called whenever a PING message is received.
576 * @param cls closure (peer #, set from GNUNET_MESH_connect)
577 * @param channel connection to the other end
578 * @param channel_ctx place to store local state associated with the channel
579 * @param message the actual message
580 * @return GNUNET_OK to keep the connection open,
581 * GNUNET_SYSERR to close it (signal serious error)
584 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
586 const struct GNUNET_MessageHeader *message)
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
591 GNUNET_MESH_receive_done (channel);
592 if (GNUNET_NO == test_finished)
600 * Function is called whenever a PONG message is received.
602 * @param cls closure (peer #, set from GNUNET_MESH_connect)
603 * @param channel connection to the other end
604 * @param channel_ctx place to store local state associated with the channel
605 * @param message the actual message
606 * @return GNUNET_OK to keep the connection open,
607 * GNUNET_SYSERR to close it (signal serious error)
610 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
612 const struct GNUNET_MessageHeader *message)
615 struct MeshPeer *peer;
616 struct GNUNET_TIME_Relative latency;
618 GNUNET_MESH_receive_done (channel);
621 GNUNET_break (0 != peer->timestamp.abs_value_us);
622 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
623 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
624 get_index (peer), get_index (peer->dest),
625 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
627 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
629 peer->timestamp = GNUNET_TIME_absolute_get ();
630 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
635 peer->timestamp.abs_value_us = 0;
643 * Handlers, for diverse services
645 static struct GNUNET_MESH_MessageHandler handlers[] = {
646 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
647 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
653 * Method called whenever another peer has added us to a channel
654 * the other peer initiated.
656 * @param cls Closure.
657 * @param channel New handle to the channel.
658 * @param initiator Peer that started the channel.
659 * @param port Port this channel is connected to.
660 * @param options channel option flags
661 * @return Initial channel context for the channel
662 * (can be NULL -- that's not an error).
665 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
666 const struct GNUNET_PeerIdentity *initiator,
667 uint32_t port, enum GNUNET_MESH_ChannelOption options)
670 struct MeshPeer *peer;
672 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
673 GNUNET_assert (NULL != peer);
674 GNUNET_assert (peer == peers[n].incoming);
675 GNUNET_assert (peer->dest == &peers[n]);
676 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
677 n, get_index (peer), channel);
678 peers[n].incoming_ch = channel;
684 * Function called whenever an inbound channel is destroyed. Should clean up
685 * any associated state.
687 * @param cls closure (set from GNUNET_MESH_connect)
688 * @param channel connection to the other end (henceforth invalid)
689 * @param channel_ctx place where local state associated
690 * with the channel is stored
693 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
697 struct MeshPeer *peer = &peers[n];
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
700 "Channel %p disconnected at peer %ld\n", channel, n);
701 if (peer->ch == channel)
706 static struct MeshPeer *
707 select_random_peer (struct MeshPeer *peer)
713 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
714 } while (NULL != peers[r].incoming);
715 peers[r].incoming = peer;
721 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
723 * Testcase continues when the root receives confirmation of connected peers,
724 * on callback funtion ch.
726 * @param cls Closure (unsued).
727 * @param tc Task Context.
730 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
732 enum GNUNET_MESH_ChannelOption flags;
735 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
738 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
740 flags = GNUNET_MESH_OPTION_DEFAULT;
741 for (i = 0; i < PING_PEERS; i++)
744 peers[i].dest = select_random_peer (&peers[i]);
745 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
748 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
749 i, get_index (peers[i].dest), peers[i].ch);
750 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
753 peers_running = TOTAL_PEERS;
754 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
759 * Callback to be called when the requested peer information is available
761 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
762 * @param op the operation this callback corresponds to
763 * @param pinfo the result; will be NULL if the operation has failed
764 * @param emsg error message if the operation has failed;
765 * NULL if the operation is successfull
768 peer_id_cb (void *cls,
769 struct GNUNET_TESTBED_Operation *op,
770 const struct GNUNET_TESTBED_PeerInformation *pinfo,
775 if (NULL == pinfo || NULL != emsg)
777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
778 abort_test (__LINE__);
781 peers[n].id = *(pinfo->result.id);
782 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
783 n, GNUNET_i2s (&peers[n].id));
784 GNUNET_break (GNUNET_OK ==
785 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
786 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
788 GNUNET_TESTBED_operation_done (peers[n].op);
792 if (p_ids < TOTAL_PEERS)
794 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
795 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
800 * test main: start test when all peers are connected
802 * @param cls Closure.
803 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
804 * @param num_peers Number of peers that are running.
805 * @param testbed_peers Array of peers.
806 * @param meshes Handle to each of the MESHs of the peers.
810 struct GNUNET_MESH_TEST_Context *ctx,
811 unsigned int num_peers,
812 struct GNUNET_TESTBED_Peer **testbed_peers,
813 struct GNUNET_MESH_Handle **meshes)
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
820 GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
821 GNUNET_assert (TOTAL_PEERS == num_peers);
822 peers_running = num_peers;
823 testbed_handles = testbed_peers;
824 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
825 &disconnect_mesh_peers,
827 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
828 &shutdown_task, NULL);
829 for (i = 0; i < TOTAL_PEERS; i++)
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
832 peers[i].up = GNUNET_YES;
833 peers[i].mesh = meshes[i];
835 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
836 GNUNET_TESTBED_PIT_IDENTITY,
837 &peer_id_cb, (void *) i);
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
840 /* Continues from pi_cb -> do_test */
845 * Main: start profiler.
848 main (int argc, char *argv[])
850 static uint32_t ports[2];
851 const char *config_file;
853 config_file = "test_mesh.conf";
855 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
856 GNUNET_assert (NULL != ids);
858 test_finished = GNUNET_NO;
861 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
862 &tmain, NULL, /* tmain cls */
863 &incoming_channel, &channel_cleaner,
868 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
869 "FAILED! (%d/%d)\n", ok, ok_goal);
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
876 /* end of gnunet-mesh-profiler.c */