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
108 * Destinaton to ping.
110 struct MeshPeer *dest;
113 * Incoming channel for pings.
115 struct MeshPeer *incoming;
118 * Task to do the next ping.
120 GNUNET_SCHEDULER_TaskIdentifier ping_task;
123 * Time the last ping was sent.
125 struct GNUNET_TIME_Absolute timestamp;
129 * GNUNET_PeerIdentity -> MeshPeer
131 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
134 * Testbed peer handles.
136 static struct GNUNET_TESTBED_Peer **testbed_handles;
139 * Testbed Operation (to get stats).
141 static struct GNUNET_TESTBED_Operation *stats_op;
144 * How many events have happened
149 * Number of events expected to conclude the test successfully.
154 * Size of each test packet
156 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
159 * Operation to get peer ids.
161 struct MeshPeer peers[TOTAL_PEERS];
166 static unsigned int p_ids;
169 * Total number of currently running peers.
171 static unsigned long long peers_running;
174 * Test context (to shut down).
176 static struct GNUNET_MESH_TEST_Context *test_ctx;
179 * Task called to shutdown test.
181 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
184 * Task called to disconnect peers, before shutdown.
186 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
189 * Task to perform tests
191 static GNUNET_SCHEDULER_TaskIdentifier test_task;
195 * Flag to notify callbacks not to generate any new traffic anymore.
197 static int test_finished;
200 * Calculate a random delay.
202 * @param max Exclusive maximum, in ms.
204 * @return A time between 0 a max-1 ms.
206 static struct GNUNET_TIME_Relative
207 delay_ms_rnd (unsigned int max)
211 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
212 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
217 * Get the index of a peer in the peers array.
219 * @param peer Peer whose index to get.
221 * @return Index of peer in peers.
224 get_index (struct MeshPeer *peer)
231 * Show the results of the test (banwidth acheived) and log them to GAUGER
240 * Shut down peergroup, clean up.
242 * @param cls Closure (unused).
243 * @param tc Task Context.
246 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
249 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
254 * Disconnect from mesh services af all peers, call shutdown.
256 * @param cls Closure (unused).
257 * @param tc Task Context.
260 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 long line = (long) cls;
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "disconnecting mesh service, called from line %ld\n", line);
267 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
268 for (i = 0; i < TOTAL_PEERS; i++)
270 if (NULL != peers[i].op)
271 GNUNET_TESTBED_operation_done (peers[i].op);
273 if (peers[i].up != GNUNET_YES)
276 if (NULL != peers[i].ch)
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
279 GNUNET_MESH_channel_destroy (peers[i].ch);
281 if (NULL != peers[i].incoming_ch)
283 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
284 i, peers[i].incoming_ch);
285 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
288 GNUNET_MESH_TEST_cleanup (test_ctx);
289 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
291 GNUNET_SCHEDULER_cancel (shutdown_handle);
293 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
298 * Finish test normally: schedule disconnect and shutdown
300 * @param line Line in the code the abort is requested from (__LINE__).
303 abort_test (long line)
305 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
307 GNUNET_SCHEDULER_cancel (disconnect_task);
308 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
314 * Stats callback. Finish the stats testbed operation and when all stats have
315 * been iterated, shutdown the test.
318 * @param op the operation that has been finished
319 * @param emsg error message in case the operation has failed; will be NULL if
320 * operation has executed successfully.
323 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
325 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
326 GNUNET_TESTBED_operation_done (stats_op);
328 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
329 GNUNET_SCHEDULER_cancel (disconnect_task);
330 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
337 * Process statistic values.
340 * @param peer the peer the statistic belong to
341 * @param subsystem name of subsystem that created the statistic
342 * @param name the name of the datum
343 * @param value the current value
344 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
345 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
348 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
349 const char *subsystem, const char *name,
350 uint64_t value, int is_persistent)
354 i = GNUNET_TESTBED_get_index (peer);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
356 i, subsystem, name, value);
363 * Task check that keepalives were sent and received.
365 * @param cls Closure (NULL).
366 * @param tc Task Context.
369 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
371 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
374 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
375 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
377 stats_iterator, stats_cont, NULL);
382 * @brief Finish profiler normally. Signal finish and start collecting stats.
384 * @param cls Closure (unused).
385 * @param tc Task context.
388 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
390 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
393 test_finished = GNUNET_YES;
395 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
399 * Set the total number of running peers.
401 * @param target Desired number of running peers.
404 adjust_running_peers (unsigned int target)
406 struct GNUNET_TESTBED_Operation *op;
412 GNUNET_assert (target <= TOTAL_PEERS);
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
415 if (target > peers_running)
417 delta = target - peers_running;
422 delta = peers_running - target;
426 for (i = 0; i < delta; i++)
429 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
430 TOTAL_PEERS - PING_PEERS);
432 } while (peers[r].up == run || NULL != peers[r].incoming);
433 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
434 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
436 if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
437 GNUNET_SCHEDULER_cancel (peers[r].ping_task);
438 peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
442 if (NULL != peers[r].ch)
443 GNUNET_MESH_channel_destroy (peers[r].ch);
445 if (NULL != peers[r].dest)
447 if (NULL != peers[r].dest->incoming_ch)
448 GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
449 peers[r].dest->incoming_ch = NULL;
452 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
453 "mesh", NULL, NULL, run);
454 GNUNET_break (NULL != op);
455 peers_running += run ? 1 : -1;
456 GNUNET_assert (peers_running > 0);
462 * @brief Move to next round.
464 * @param cls Closure (round #).
465 * @param tc Task context.
468 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 long round = (long) cls;
472 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
476 if (0.0 == rounds[round])
478 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
479 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
482 adjust_running_peers (rounds[round] * TOTAL_PEERS);
484 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
489 * Transmit ready callback.
491 * @param cls Closure (peer for PING, NULL for PONG).
492 * @param size Size of the tranmist buffer.
493 * @param buf Pointer to the beginning of the buffer.
495 * @return Number of bytes written to buf.
498 tmt_rdy (void *cls, size_t size, void *buf);
502 * @brief Send a ping to destination
504 * @param cls Closure (peer).
505 * @param tc Task context.
508 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
510 struct MeshPeer *peer = (struct MeshPeer *) cls;
512 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
514 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
515 || GNUNET_YES == test_finished
516 || 0 != peer->timestamp.abs_value_us)
519 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
520 get_index (peer), get_index (peer->dest));
522 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
523 GNUNET_TIME_UNIT_FOREVER_REL,
524 size_payload, &tmt_rdy, peer);
528 * @brief Reply with a pong to origin.
530 * @param cls Closure (peer).
531 * @param tc Task context.
534 pong (struct GNUNET_MESH_Channel *channel)
536 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
537 GNUNET_TIME_UNIT_FOREVER_REL,
538 size_payload, &tmt_rdy, NULL);
543 * Transmit ready callback
545 * @param cls Closure (peer for PING, NULL for PONG).
546 * @param size Size of the buffer we have.
547 * @param buf Buffer to copy data to.
550 tmt_rdy (void *cls, size_t size, void *buf)
552 struct MeshPeer *peer = (struct MeshPeer *) cls;
553 struct GNUNET_MessageHeader *msg = buf;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
557 if (size < size_payload || NULL == buf)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "size %u, buf %p, data_sent %u, data_received %u\n",
562 size, buf, peer->data_sent, peer->data_received);
566 msg->size = htons (size);
569 msg->type = htons (PONG);
570 return sizeof (*msg);
573 msg->type = htons (PING);
574 data = (uint32_t *) &msg[1];
575 *data = htonl (peer->data_sent);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
578 peer->timestamp = GNUNET_TIME_absolute_get ();
579 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
587 * Function is called whenever a PING message is received.
589 * @param cls closure (peer #, set from GNUNET_MESH_connect)
590 * @param channel connection to the other end
591 * @param channel_ctx place to store local state associated with the channel
592 * @param message the actual message
593 * @return GNUNET_OK to keep the connection open,
594 * GNUNET_SYSERR to close it (signal serious error)
597 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
599 const struct GNUNET_MessageHeader *message)
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
604 GNUNET_MESH_receive_done (channel);
605 if (GNUNET_NO == test_finished)
613 * Function is called whenever a PONG message is received.
615 * @param cls closure (peer #, set from GNUNET_MESH_connect)
616 * @param channel connection to the other end
617 * @param channel_ctx place to store local state associated with the channel
618 * @param message the actual message
619 * @return GNUNET_OK to keep the connection open,
620 * GNUNET_SYSERR to close it (signal serious error)
623 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
625 const struct GNUNET_MessageHeader *message)
628 struct MeshPeer *peer;
629 struct GNUNET_TIME_Relative latency;
631 GNUNET_MESH_receive_done (channel);
634 GNUNET_break (0 != peer->timestamp.abs_value_us);
635 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
636 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
637 get_index (peer), get_index (peer->dest),
638 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
640 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
642 peer->timestamp = GNUNET_TIME_absolute_get ();
643 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
648 peer->timestamp.abs_value_us = 0;
656 * Handlers, for diverse services
658 static struct GNUNET_MESH_MessageHandler handlers[] = {
659 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
660 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
666 * Method called whenever another peer has added us to a channel
667 * the other peer initiated.
669 * @param cls Closure.
670 * @param channel New handle to the channel.
671 * @param initiator Peer that started the channel.
672 * @param port Port this channel is connected to.
673 * @param options channel option flags
674 * @return Initial channel context for the channel
675 * (can be NULL -- that's not an error).
678 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
679 const struct GNUNET_PeerIdentity *initiator,
680 uint32_t port, enum GNUNET_MESH_ChannelOption options)
683 struct MeshPeer *peer;
685 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
686 GNUNET_assert (NULL != peer);
687 GNUNET_assert (peer == peers[n].incoming);
688 GNUNET_assert (peer->dest == &peers[n]);
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
690 n, get_index (peer), channel);
691 peers[n].incoming_ch = channel;
697 * Function called whenever an inbound channel is destroyed. Should clean up
698 * any associated state.
700 * @param cls closure (set from GNUNET_MESH_connect)
701 * @param channel connection to the other end (henceforth invalid)
702 * @param channel_ctx place where local state associated
703 * with the channel is stored
706 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
710 struct MeshPeer *peer = &peers[n];
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 "Channel %p disconnected at peer %ld\n", channel, n);
714 if (peer->ch == channel)
719 static struct MeshPeer *
720 select_random_peer (struct MeshPeer *peer)
726 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
727 } while (NULL != peers[r].incoming);
728 peers[r].incoming = peer;
734 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
736 * Testcase continues when the root receives confirmation of connected peers,
737 * on callback funtion ch.
739 * @param cls Closure (unsued).
740 * @param tc Task Context.
743 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
745 enum GNUNET_MESH_ChannelOption flags;
748 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
751 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
753 flags = GNUNET_MESH_OPTION_DEFAULT;
754 for (i = 0; i < PING_PEERS; i++)
757 peers[i].dest = select_random_peer (&peers[i]);
758 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
761 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
762 i, get_index (peers[i].dest), peers[i].ch);
763 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
766 peers_running = TOTAL_PEERS;
767 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
772 * Callback to be called when the requested peer information is available
774 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
775 * @param op the operation this callback corresponds to
776 * @param pinfo the result; will be NULL if the operation has failed
777 * @param emsg error message if the operation has failed;
778 * NULL if the operation is successfull
781 peer_id_cb (void *cls,
782 struct GNUNET_TESTBED_Operation *op,
783 const struct GNUNET_TESTBED_PeerInformation *pinfo,
788 if (NULL == pinfo || NULL != emsg)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
791 abort_test (__LINE__);
794 peers[n].id = *(pinfo->result.id);
795 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
796 n, GNUNET_i2s (&peers[n].id));
797 GNUNET_break (GNUNET_OK ==
798 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
799 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
801 GNUNET_TESTBED_operation_done (peers[n].op);
805 if (p_ids < TOTAL_PEERS)
807 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
808 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
813 * test main: start test when all peers are connected
815 * @param cls Closure.
816 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
817 * @param num_peers Number of peers that are running.
818 * @param testbed_peers Array of peers.
819 * @param meshes Handle to each of the MESHs of the peers.
823 struct GNUNET_MESH_TEST_Context *ctx,
824 unsigned int num_peers,
825 struct GNUNET_TESTBED_Peer **testbed_peers,
826 struct GNUNET_MESH_Handle **meshes)
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
833 GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
834 GNUNET_assert (TOTAL_PEERS == num_peers);
835 peers_running = num_peers;
836 testbed_handles = testbed_peers;
837 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
838 &disconnect_mesh_peers,
840 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
841 &shutdown_task, NULL);
842 for (i = 0; i < TOTAL_PEERS; i++)
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
845 peers[i].up = GNUNET_YES;
846 peers[i].mesh = meshes[i];
848 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
849 GNUNET_TESTBED_PIT_IDENTITY,
850 &peer_id_cb, (void *) i);
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
853 /* Continues from pi_cb -> do_test */
858 * Main: start profiler.
861 main (int argc, char *argv[])
863 static uint32_t ports[2];
864 const char *config_file;
866 config_file = "test_mesh.conf";
868 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
869 GNUNET_assert (NULL != ids);
871 test_finished = GNUNET_NO;
874 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
875 &tmain, NULL, /* tmain cls */
876 &incoming_channel, &channel_cleaner,
881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
882 "FAILED! (%d/%d)\n", ok, ok_goal);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
889 /* end of gnunet-mesh-profiler.c */