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 * How long until we give up on connecting the peers?
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
46 * Time to wait for stuff that should be rather fast
48 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54 * Testbed Operation (to get peer id, etc).
56 struct GNUNET_TESTBED_Operation *op;
61 struct GNUNET_PeerIdentity id;
64 * Mesh handle for the root peer
66 struct GNUNET_MESH_Handle *mesh;
69 * Channel handle for the root peer
71 struct GNUNET_MESH_Channel *ch;
74 * Channel handle for the dest peer
76 struct GNUNET_MESH_Channel *incoming_ch;
79 * Number of payload packes sent
84 * Number of payload packets received
88 struct MeshPeer *dest;
89 struct MeshPeer *incoming;
90 GNUNET_SCHEDULER_TaskIdentifier ping_task;
91 struct GNUNET_TIME_Absolute timestamp;
95 * GNUNET_PeerIdentity -> MeshPeer
97 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
100 * Testbed peer handles.
102 static struct GNUNET_TESTBED_Peer **testbed_handles;
105 * Testbed Operation (to get stats).
107 static struct GNUNET_TESTBED_Operation *stats_op;
110 * How many events have happened
115 * Number of events expected to conclude the test successfully.
120 * Size of each test packet
122 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
125 * Operation to get peer ids.
127 struct MeshPeer peers[TOTAL_PEERS];
132 static unsigned int p_ids;
135 * Total number of currently running peers.
137 static unsigned long long peers_running;
140 * Test context (to shut down).
142 static struct GNUNET_MESH_TEST_Context *test_ctx;
145 * Task called to shutdown test.
147 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
150 * Task called to disconnect peers, before shutdown.
152 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
155 * Task to perform tests
157 static GNUNET_SCHEDULER_TaskIdentifier test_task;
161 * Flag to notify callbacks not to generate any new traffic anymore.
163 static int test_finished;
166 * Calculate a random delay.
168 * @param max Exclusive maximum, in ms.
170 * @return A time between 0 a max-1 ms.
172 static struct GNUNET_TIME_Relative
173 delay_ms_rnd (unsigned int max)
177 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
178 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
183 * Get the index of a peer in the peers array.
185 * @param peer Peer whose index to get.
187 * @return Index of peer in peers.
190 get_index (struct MeshPeer *peer)
197 * Show the results of the test (banwidth acheived) and log them to GAUGER
206 * Shut down peergroup, clean up.
208 * @param cls Closure (unused).
209 * @param tc Task Context.
212 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
215 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
220 * Disconnect from mesh services af all peers, call shutdown.
222 * @param cls Closure (unused).
223 * @param tc Task Context.
226 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 long line = (long) cls;
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "disconnecting mesh service of peers, called from line %ld\n",
234 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
235 for (i = 0; i < TOTAL_PEERS; i++)
237 GNUNET_TESTBED_operation_done (peers[i].op);
238 GNUNET_MESH_channel_destroy (peers[i].ch);
239 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
241 GNUNET_MESH_TEST_cleanup (test_ctx);
242 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
244 GNUNET_SCHEDULER_cancel (shutdown_handle);
246 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
251 * Finish test normally: schedule disconnect and shutdown
253 * @param line Line in the code the abort is requested from (__LINE__).
256 abort_test (long line)
258 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
260 GNUNET_SCHEDULER_cancel (disconnect_task);
261 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
267 * Stats callback. Finish the stats testbed operation and when all stats have
268 * been iterated, shutdown the test.
271 * @param op the operation that has been finished
272 * @param emsg error message in case the operation has failed; will be NULL if
273 * operation has executed successfully.
276 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
278 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
279 GNUNET_TESTBED_operation_done (stats_op);
281 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
282 GNUNET_SCHEDULER_cancel (disconnect_task);
283 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
290 * Process statistic values.
293 * @param peer the peer the statistic belong to
294 * @param subsystem name of subsystem that created the statistic
295 * @param name the name of the datum
296 * @param value the current value
297 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
298 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
301 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
302 const char *subsystem, const char *name,
303 uint64_t value, int is_persistent)
307 i = GNUNET_TESTBED_get_index (peer);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
309 i, subsystem, name, value);
316 * Task check that keepalives were sent and received.
318 * @param cls Closure (NULL).
319 * @param tc Task Context.
322 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
327 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
329 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
331 stats_iterator, stats_cont, NULL);
336 * @brief Finish profiler normally.
338 * @param cls Closure (unused).
339 * @param tc Task context.
342 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
344 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
347 test_finished = GNUNET_YES;
349 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
355 * Transmit ready callback.
357 * @param cls Closure (peer for PING, NULL for PONG).
358 * @param size Size of the tranmist buffer.
359 * @param buf Pointer to the beginning of the buffer.
361 * @return Number of bytes written to buf.
364 tmt_rdy (void *cls, size_t size, void *buf);
368 * @brief Send a ping to destination
370 * @param cls Closure (peer).
371 * @param tc Task context.
374 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
376 struct MeshPeer *peer = (struct MeshPeer *) cls;
378 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
380 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
381 || GNUNET_YES == test_finished
382 || 0 != peer->timestamp.abs_value_us)
385 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
386 get_index (peer), get_index (peer->dest));
388 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
389 GNUNET_TIME_UNIT_FOREVER_REL,
390 size_payload, &tmt_rdy, peer);
394 * @brief Reply with a pong to origin.
396 * @param cls Closure (peer).
397 * @param tc Task context.
400 pong (struct GNUNET_MESH_Channel *channel)
402 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
403 GNUNET_TIME_UNIT_FOREVER_REL,
404 size_payload, &tmt_rdy, NULL);
409 * Transmit ready callback
411 * @param cls Closure (peer for PING, NULL for PONG).
412 * @param size Size of the buffer we have.
413 * @param buf Buffer to copy data to.
416 tmt_rdy (void *cls, size_t size, void *buf)
418 struct MeshPeer *peer = (struct MeshPeer *) cls;
419 struct GNUNET_MessageHeader *msg = buf;
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
424 if (size < size_payload || NULL == buf)
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "size %u, buf %p, data_sent %u, data_received %u\n",
429 size, buf, peer->data_sent, peer->data_received);
433 msg->size = htons (size);
436 msg->type = htons (PONG);
437 return sizeof (*msg);
440 msg->type = htons (PING);
441 data = (uint32_t *) &msg[1];
442 *data = htonl (peer->data_sent);
443 if (0 == peer->data_sent)
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: initializer\n");
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
454 peer->timestamp = GNUNET_TIME_absolute_get ();
455 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (s * 1000),
463 * Function is called whenever a PING message is received.
465 * @param cls closure (peer #, set from GNUNET_MESH_connect)
466 * @param channel connection to the other end
467 * @param channel_ctx place to store local state associated with the channel
468 * @param message the actual message
469 * @return GNUNET_OK to keep the connection open,
470 * GNUNET_SYSERR to close it (signal serious error)
473 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
475 const struct GNUNET_MessageHeader *message)
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
480 GNUNET_MESH_receive_done (channel);
481 if (GNUNET_NO == test_finished)
489 * Function is called whenever a PONG message is received.
491 * @param cls closure (peer #, set from GNUNET_MESH_connect)
492 * @param channel connection to the other end
493 * @param channel_ctx place to store local state associated with the channel
494 * @param message the actual message
495 * @return GNUNET_OK to keep the connection open,
496 * GNUNET_SYSERR to close it (signal serious error)
499 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
501 const struct GNUNET_MessageHeader *message)
504 struct MeshPeer *peer;
505 struct GNUNET_TIME_Relative latency;
507 GNUNET_MESH_receive_done (channel);
510 GNUNET_break (0 != peer->timestamp.abs_value_us);
511 latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
512 FPRINTF (stderr, "%ld latency: %s\n",
513 n, GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
515 if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
517 peer->timestamp = GNUNET_TIME_absolute_get ();
518 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
523 peer->timestamp.abs_value_us = 0;
531 * Handlers, for diverse services
533 static struct GNUNET_MESH_MessageHandler handlers[] = {
534 {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
535 {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
541 * Method called whenever another peer has added us to a channel
542 * the other peer initiated.
544 * @param cls Closure.
545 * @param channel New handle to the channel.
546 * @param initiator Peer that started the channel.
547 * @param port Port this channel is connected to.
548 * @param options channel option flags
549 * @return Initial channel context for the channel
550 * (can be NULL -- that's not an error).
553 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
554 const struct GNUNET_PeerIdentity *initiator,
555 uint32_t port, enum GNUNET_MESH_ChannelOption options)
558 struct MeshPeer *peer;
560 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
561 GNUNET_assert (NULL != peer);
562 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
563 peers[n].incoming_ch = channel;
565 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
567 GNUNET_SCHEDULER_cancel (disconnect_task);
568 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
569 &disconnect_mesh_peers,
577 * Function called whenever an inbound channel is destroyed. Should clean up
578 * any associated state.
580 * @param cls closure (set from GNUNET_MESH_connect)
581 * @param channel connection to the other end (henceforth invalid)
582 * @param channel_ctx place where local state associated
583 * with the channel is stored
586 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
592 "Incoming channel disconnected at peer %ld\n", n);
593 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
595 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
597 GNUNET_SCHEDULER_cancel (disconnect_task);
598 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
604 static struct MeshPeer *
605 select_random_peer (struct MeshPeer *peer)
611 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
612 } while (NULL != peers[r].incoming);
613 peers[r].incoming = peer;
619 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
621 * Testcase continues when the root receives confirmation of connected peers,
622 * on callback funtion ch.
624 * @param cls Closure (unsued).
625 * @param tc Task Context.
628 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
630 enum GNUNET_MESH_ChannelOption flags;
633 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
636 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
638 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
639 GNUNET_SCHEDULER_cancel (disconnect_task);
640 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
641 &disconnect_mesh_peers,
644 flags = GNUNET_MESH_OPTION_DEFAULT;
645 for (i = 0; i < TOTAL_PEERS; i++)
648 peers[i].dest = select_random_peer (&peers[i]);
649 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
652 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u\n",
653 i, get_index (peers[i].dest));
654 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
661 * Callback to be called when the requested peer information is available
663 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
664 * @param op the operation this callback corresponds to
665 * @param pinfo the result; will be NULL if the operation has failed
666 * @param emsg error message if the operation has failed;
667 * NULL if the operation is successfull
670 peer_id_cb (void *cls,
671 struct GNUNET_TESTBED_Operation *op,
672 const struct GNUNET_TESTBED_PeerInformation *pinfo,
677 if (NULL == pinfo || NULL != emsg)
679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
680 abort_test (__LINE__);
683 peers[n].id = *(pinfo->result.id);
684 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
685 n, GNUNET_i2s (&peers[n].id));
686 GNUNET_break (GNUNET_OK ==
687 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
690 if (p_ids < TOTAL_PEERS)
692 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
693 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
698 * test main: start test when all peers are connected
700 * @param cls Closure.
701 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
702 * @param num_peers Number of peers that are running.
703 * @param testbed_peers Array of peers.
704 * @param meshes Handle to each of the MESHs of the peers.
708 struct GNUNET_MESH_TEST_Context *ctx,
709 unsigned int num_peers,
710 struct GNUNET_TESTBED_Peer **testbed_peers,
711 struct GNUNET_MESH_Handle **meshes)
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
718 GNUNET_assert (TOTAL_PEERS == num_peers);
719 peers_running = num_peers;
720 testbed_handles = testbed_peers;
721 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
722 &finish_profiler, NULL);
723 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
724 &disconnect_mesh_peers,
726 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
727 &shutdown_task, NULL);
728 for (i = 0; i < TOTAL_PEERS; i++)
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
731 peers[i].mesh = meshes[i];
733 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
734 GNUNET_TESTBED_PIT_IDENTITY,
735 &peer_id_cb, (void *) i);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
738 /* Continues from pi_cb -> do_test */
743 * Main: start profiler.
746 main (int argc, char *argv[])
748 static uint32_t ports[2];
749 const char *config_file;
751 config_file = "test_mesh.conf";
753 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
754 GNUNET_assert (NULL != ids);
756 test_finished = GNUNET_NO;
759 GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
760 &tmain, NULL, /* tmain cls */
761 &incoming_channel, &channel_cleaner,
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 "FAILED! (%d/%d)\n", ok, ok_goal);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
774 /* end of gnunet-mesh-profiler.c */