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 GNUNET_TESTBED_operation_done (peers[i].op);
251 GNUNET_MESH_channel_destroy (peers[i].ch);
252 if (NULL != peers[i].incoming_ch)
253 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
255 GNUNET_MESH_TEST_cleanup (test_ctx);
256 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
258 GNUNET_SCHEDULER_cancel (shutdown_handle);
260 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
265 * Finish test normally: schedule disconnect and shutdown
267 * @param line Line in the code the abort is requested from (__LINE__).
270 abort_test (long line)
272 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
274 GNUNET_SCHEDULER_cancel (disconnect_task);
275 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
281 * Stats callback. Finish the stats testbed operation and when all stats have
282 * been iterated, shutdown the test.
285 * @param op the operation that has been finished
286 * @param emsg error message in case the operation has failed; will be NULL if
287 * operation has executed successfully.
290 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
293 GNUNET_TESTBED_operation_done (stats_op);
295 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
296 GNUNET_SCHEDULER_cancel (disconnect_task);
297 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
304 * Process statistic values.
307 * @param peer the peer the statistic belong to
308 * @param subsystem name of subsystem that created the statistic
309 * @param name the name of the datum
310 * @param value the current value
311 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
312 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
315 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
316 const char *subsystem, const char *name,
317 uint64_t value, int is_persistent)
321 i = GNUNET_TESTBED_get_index (peer);
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
323 i, subsystem, name, value);
330 * Task check that keepalives were sent and received.
332 * @param cls Closure (NULL).
333 * @param tc Task Context.
336 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
341 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
342 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
343 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
345 stats_iterator, stats_cont, NULL);
350 * @brief Finish profiler normally. Signal finish and start collecting stats.
352 * @param cls Closure (unused).
353 * @param tc Task context.
356 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
358 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
361 test_finished = GNUNET_YES;
363 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
367 * Set the total number of running peers.
369 * @param target Desired number of running peers.
372 adjust_running_peers (unsigned int target)
374 struct GNUNET_TESTBED_Operation *op;
380 GNUNET_assert (target <= TOTAL_PEERS);
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
383 if (target > peers_running)
385 delta = target - peers_running;
390 delta = peers_running - target;
394 for (i = 0; i < delta; i++)
397 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
398 } while (peers[r].up == run);
399 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
400 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
401 GNUNET_SCHEDULER_cancel (peers[r].ping_task);
402 peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
403 peers[r].up = GNUNET_NO;
404 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
405 "mesh", NULL, NULL, run);
406 GNUNET_break (NULL != op);
412 * @brief Move to next round.
414 * @param cls Closure (round #).
415 * @param tc Task context.
418 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
420 long round = (long) cls;
422 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
426 if (0.0 == rounds[round])
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
429 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
432 adjust_running_peers (rounds[round] * TOTAL_PEERS);
434 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
439 * Transmit ready callback.
441 * @param cls Closure (peer for PING, NULL for PONG).
442 * @param size Size of the tranmist buffer.
443 * @param buf Pointer to the beginning of the buffer.
445 * @return Number of bytes written to buf.
448 tmt_rdy (void *cls, size_t size, void *buf);
452 * @brief Send a ping to destination
454 * @param cls Closure (peer).
455 * @param tc Task context.
458 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
460 struct MeshPeer *peer = (struct MeshPeer *) cls;
462 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
464 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
465 || GNUNET_YES == test_finished
466 || 0 != peer->timestamp.abs_value_us)
469 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
470 get_index (peer), get_index (peer->dest));
472 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
473 GNUNET_TIME_UNIT_FOREVER_REL,
474 size_payload, &tmt_rdy, peer);
478 * @brief Reply with a pong to origin.
480 * @param cls Closure (peer).
481 * @param tc Task context.
484 pong (struct GNUNET_MESH_Channel *channel)
486 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
487 GNUNET_TIME_UNIT_FOREVER_REL,
488 size_payload, &tmt_rdy, NULL);
493 * Transmit ready callback
495 * @param cls Closure (peer for PING, NULL for PONG).
496 * @param size Size of the buffer we have.
497 * @param buf Buffer to copy data to.
500 tmt_rdy (void *cls, size_t size, void *buf)
502 struct MeshPeer *peer = (struct MeshPeer *) cls;
503 struct GNUNET_MessageHeader *msg = buf;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
507 if (size < size_payload || NULL == buf)
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "size %u, buf %p, data_sent %u, data_received %u\n",
512 size, buf, peer->data_sent, peer->data_received);
516 msg->size = htons (size);
519 msg->type = htons (PONG);
520 return sizeof (*msg);
523 msg->type = htons (PING);
524 data = (uint32_t *) &msg[1];
525 *data = htonl (peer->data_sent);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
528 peer->timestamp = GNUNET_TIME_absolute_get ();
529 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
537 * Function is called whenever a PING message is received.
539 * @param cls closure (peer #, set from GNUNET_MESH_connect)
540 * @param channel connection to the other end
541 * @param channel_ctx place to store local state associated with the channel
542 * @param message the actual message
543 * @return GNUNET_OK to keep the connection open,
544 * GNUNET_SYSERR to close it (signal serious error)
547 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
549 const struct GNUNET_MessageHeader *message)
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
554 GNUNET_MESH_receive_done (channel);
555 if (GNUNET_NO == test_finished)
563 * Function is called whenever a PONG message is received.
565 * @param cls closure (peer #, set from GNUNET_MESH_connect)
566 * @param channel connection to the other end
567 * @param channel_ctx place to store local state associated with the channel
568 * @param message the actual message
569 * @return GNUNET_OK to keep the connection open,
570 * GNUNET_SYSERR to close it (signal serious error)
573 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
575 const struct GNUNET_MessageHeader *message)
578 struct MeshPeer *peer;
579 struct GNUNET_TIME_Relative latency;
581 GNUNET_MESH_receive_done (channel);
584 GNUNET_break (0 != peer->timestamp.abs_value_us);
585 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
587 get_index (peer), get_index (peer->dest),
588 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
590 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
592 peer->timestamp = GNUNET_TIME_absolute_get ();
593 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
598 peer->timestamp.abs_value_us = 0;
606 * Handlers, for diverse services
608 static struct GNUNET_MESH_MessageHandler handlers[] = {
609 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
610 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
616 * Method called whenever another peer has added us to a channel
617 * the other peer initiated.
619 * @param cls Closure.
620 * @param channel New handle to the channel.
621 * @param initiator Peer that started the channel.
622 * @param port Port this channel is connected to.
623 * @param options channel option flags
624 * @return Initial channel context for the channel
625 * (can be NULL -- that's not an error).
628 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
629 const struct GNUNET_PeerIdentity *initiator,
630 uint32_t port, enum GNUNET_MESH_ChannelOption options)
633 struct MeshPeer *peer;
635 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
636 GNUNET_assert (NULL != peer);
637 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
638 peers[n].incoming_ch = channel;
640 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
642 GNUNET_SCHEDULER_cancel (disconnect_task);
643 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
644 &disconnect_mesh_peers,
652 * Function called whenever an inbound channel is destroyed. Should clean up
653 * any associated state.
655 * @param cls closure (set from GNUNET_MESH_connect)
656 * @param channel connection to the other end (henceforth invalid)
657 * @param channel_ctx place where local state associated
658 * with the channel is stored
661 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
665 struct MeshPeer *peer = &peers[n];
667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
668 "Incoming channel disconnected at peer %ld\n", n);
669 if (peer->ch == channel)
674 static struct MeshPeer *
675 select_random_peer (struct MeshPeer *peer)
681 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
682 } while (NULL != peers[r].incoming);
683 peers[r].incoming = peer;
689 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
691 * Testcase continues when the root receives confirmation of connected peers,
692 * on callback funtion ch.
694 * @param cls Closure (unsued).
695 * @param tc Task Context.
698 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
700 enum GNUNET_MESH_ChannelOption flags;
703 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
706 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
708 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
709 GNUNET_SCHEDULER_cancel (disconnect_task);
710 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
711 &disconnect_mesh_peers,
714 flags = GNUNET_MESH_OPTION_DEFAULT;
715 for (i = 0; i < TOTAL_PEERS; i++)
718 peers[i].dest = select_random_peer (&peers[i]);
719 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
722 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u\n",
723 i, get_index (peers[i].dest));
724 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
727 peers_running = TOTAL_PEERS;
728 GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
733 * Callback to be called when the requested peer information is available
735 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
736 * @param op the operation this callback corresponds to
737 * @param pinfo the result; will be NULL if the operation has failed
738 * @param emsg error message if the operation has failed;
739 * NULL if the operation is successfull
742 peer_id_cb (void *cls,
743 struct GNUNET_TESTBED_Operation *op,
744 const struct GNUNET_TESTBED_PeerInformation *pinfo,
749 if (NULL == pinfo || NULL != emsg)
751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
752 abort_test (__LINE__);
755 peers[n].id = *(pinfo->result.id);
756 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
757 n, GNUNET_i2s (&peers[n].id));
758 GNUNET_break (GNUNET_OK ==
759 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
762 if (p_ids < TOTAL_PEERS)
764 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
765 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
770 * test main: start test when all peers are connected
772 * @param cls Closure.
773 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
774 * @param num_peers Number of peers that are running.
775 * @param testbed_peers Array of peers.
776 * @param meshes Handle to each of the MESHs of the peers.
780 struct GNUNET_MESH_TEST_Context *ctx,
781 unsigned int num_peers,
782 struct GNUNET_TESTBED_Peer **testbed_peers,
783 struct GNUNET_MESH_Handle **meshes)
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
790 GNUNET_assert (TOTAL_PEERS == num_peers);
791 peers_running = num_peers;
792 testbed_handles = testbed_peers;
793 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
794 &disconnect_mesh_peers,
796 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
797 &shutdown_task, NULL);
798 for (i = 0; i < TOTAL_PEERS; i++)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
801 peers[i].up = GNUNET_YES;
802 peers[i].mesh = meshes[i];
804 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
805 GNUNET_TESTBED_PIT_IDENTITY,
806 &peer_id_cb, (void *) i);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
809 /* Continues from pi_cb -> do_test */
814 * Main: start profiler.
817 main (int argc, char *argv[])
819 static uint32_t ports[2];
820 const char *config_file;
822 config_file = "test_mesh.conf";
824 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
825 GNUNET_assert (NULL != ids);
827 test_finished = GNUNET_NO;
830 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
831 &tmain, NULL, /* tmain cls */
832 &incoming_channel, &channel_cleaner,
837 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
838 "FAILED! (%d/%d)\n", ok, ok_goal);
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
845 /* end of gnunet-mesh-profiler.c */