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/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"
33 * How namy messages to send
35 #define TOTAL_PACKETS 1000
38 * How namy peers to run
40 #define TOTAL_PEERS 1000
43 * How long until we give up on connecting the peers?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
48 * Time to wait for stuff that should be rather fast
50 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
56 * Testbed Operation (to get peer id, etc).
58 struct GNUNET_TESTBED_Operation *op;
63 struct GNUNET_PeerIdentity id;
66 * Mesh handle for the root peer
68 struct GNUNET_MESH_Handle *mesh;
71 * Channel handle for the root peer
73 struct GNUNET_MESH_Channel *ch;
76 * Channel handle for the dest peer
78 struct GNUNET_MESH_Channel *incoming_ch;
81 * Number of payload packes sent
86 * Number of payload packets received
90 struct MeshPeer *dest;
91 struct MeshPeer *incoming;
92 GNUNET_SCHEDULER_TaskIdentifier ping_task;
93 struct GNUNET_TIME_Absolute timestamp;
97 * GNUNET_PeerIdentity -> MeshPeer
99 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
102 * Testbed peer handles.
104 static struct GNUNET_TESTBED_Peer **testbed_handles;
107 * Testbed Operation (to get stats).
109 static struct GNUNET_TESTBED_Operation *stats_op;
112 * How many events have happened
117 * Number of events expected to conclude the test successfully.
122 * Size of each test packet
124 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
127 * Operation to get peer ids.
129 struct MeshPeer peers[TOTAL_PEERS];
134 static unsigned int p_ids;
137 * Total number of currently running peers.
139 static unsigned long long peers_running;
142 * Test context (to shut down).
144 static struct GNUNET_MESH_TEST_Context *test_ctx;
147 * Task called to shutdown test.
149 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
152 * Task called to disconnect peers, before shutdown.
154 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
157 * Task to perform tests
159 static GNUNET_SCHEDULER_TaskIdentifier test_task;
163 * Flag to notify callbacks not to generate any new traffic anymore.
165 static int test_finished;
168 * Calculate a random delay.
170 * @param max Exclusive maximum, in ms.
172 * @return A time between 0 a max-1 ms.
174 static struct GNUNET_TIME_Relative
175 delay_ms_rnd (unsigned int max)
179 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
180 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
185 * Get the index of a peer in the peers array.
187 * @param peer Peer whose index to get.
189 * @return Index of peer in peers.
192 get_index (struct MeshPeer *peer)
199 * Show the results of the test (banwidth acheived) and log them to GAUGER
208 * Shut down peergroup, clean up.
210 * @param cls Closure (unused).
211 * @param tc Task Context.
214 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
217 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
222 * Disconnect from mesh services af all peers, call shutdown.
224 * @param cls Closure (unused).
225 * @param tc Task Context.
228 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
230 long line = (long) cls;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "disconnecting mesh service of peers, called from line %ld\n",
236 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
237 for (i = 0; i < TOTAL_PEERS; i++)
239 GNUNET_TESTBED_operation_done (peers[i].op);
240 GNUNET_MESH_channel_destroy (peers[i].ch);
241 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
243 GNUNET_MESH_TEST_cleanup (test_ctx);
244 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
246 GNUNET_SCHEDULER_cancel (shutdown_handle);
248 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
253 * Finish test normally: schedule disconnect and shutdown
255 * @param line Line in the code the abort is requested from (__LINE__).
258 abort_test (long line)
260 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
262 GNUNET_SCHEDULER_cancel (disconnect_task);
263 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
269 * Stats callback. Finish the stats testbed operation and when all stats have
270 * been iterated, shutdown the test.
273 * @param op the operation that has been finished
274 * @param emsg error message in case the operation has failed; will be NULL if
275 * operation has executed successfully.
278 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
280 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
281 GNUNET_TESTBED_operation_done (stats_op);
283 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
284 GNUNET_SCHEDULER_cancel (disconnect_task);
285 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
292 * Process statistic values.
295 * @param peer the peer the statistic belong to
296 * @param subsystem name of subsystem that created the statistic
297 * @param name the name of the datum
298 * @param value the current value
299 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
300 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
303 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
304 const char *subsystem, const char *name,
305 uint64_t value, int is_persistent)
309 i = GNUNET_TESTBED_get_index (peer);
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u - %s [%s]: %llu\n",
311 i, subsystem, name, value);
318 * Task check that keepalives were sent and received.
320 * @param cls Closure (NULL).
321 * @param tc Task Context.
324 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
329 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
331 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
333 stats_iterator, stats_cont, NULL);
338 * @brief Finish profiler normally.
340 * @param cls Closure (unused).
341 * @param tc Task context.
344 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
349 test_finished = GNUNET_YES;
351 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
357 * Transmit ready callback.
359 * @param cls Closure (peer).
360 * @param size Size of the tranmist buffer.
361 * @param buf Pointer to the beginning of the buffer.
363 * @return Number of bytes written to buf.
366 tmt_rdy (void *cls, size_t size, void *buf);
370 * @brief Send data to destination
372 * @param cls Closure (peer).
373 * @param tc Task context.
376 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
378 struct MeshPeer *peer = (struct MeshPeer *) cls;
380 peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
381 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u -> %u\n",
385 get_index (peer), get_index (peer->dest));
387 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
388 GNUNET_TIME_UNIT_FOREVER_REL,
389 size_payload, &tmt_rdy, peer);
394 * Transmit ready callback
396 * @param cls Closure (peer).
397 * @param size Size of the buffer we have.
398 * @param buf Buffer to copy data to.
401 tmt_rdy (void *cls, size_t size, void *buf)
403 struct MeshPeer *peer = (struct MeshPeer *) cls;
404 struct GNUNET_MessageHeader *msg = buf;
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
409 if (size < size_payload || NULL == buf)
411 GNUNET_break (ok >= ok_goal - 2);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "size %u, buf %p, data_sent %u, data_received %u\n",
414 size, buf, peer->data_sent, peer->data_received);
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
419 msg->size = htons (size);
420 msg->type = htons ((long) cls);
421 data = (uint32_t *) &msg[1];
422 *data = htonl (peer->data_sent);
423 if (0 == peer->data_sent)
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: initializer\n");
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
434 peer->timestamp = GNUNET_TIME_absolute_get ();
435 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (s * 1000),
443 * Function is called whenever a message is received.
445 * @param cls closure (peer #, set from GNUNET_MESH_connect)
446 * @param channel connection to the other end
447 * @param channel_ctx place to store local state associated with the channel
448 * @param message the actual message
449 * @return GNUNET_OK to keep the connection open,
450 * GNUNET_SYSERR to close it (signal serious error)
453 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
455 const struct GNUNET_MessageHeader *message)
458 struct MeshPeer *peer;
459 struct GNUNET_TIME_Relative latency;
461 GNUNET_MESH_receive_done (channel);
464 GNUNET_assert (0 != peer->timestamp.abs_value_us);
465 latency = GNUNET_TIME_absolute_get_duration (peer->incoming->timestamp);
466 FPRINTF (stderr, "%u -> %ld latency: %s\n",
467 get_index (peer->incoming), n,
468 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
469 peer->timestamp.abs_value_us = 0;
476 * Handlers, for diverse services
478 static struct GNUNET_MESH_MessageHandler handlers[] = {
479 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
485 * Method called whenever another peer has added us to a channel
486 * the other peer initiated.
488 * @param cls Closure.
489 * @param channel New handle to the channel.
490 * @param initiator Peer that started the channel.
491 * @param port Port this channel is connected to.
492 * @param options channel option flags
493 * @return Initial channel context for the channel
494 * (can be NULL -- that's not an error).
497 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
498 const struct GNUNET_PeerIdentity *initiator,
499 uint32_t port, enum GNUNET_MESH_ChannelOption options)
502 struct MeshPeer *peer;
504 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
505 GNUNET_assert (NULL != peer);
506 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
507 peers[n].incoming_ch = channel;
509 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
511 GNUNET_SCHEDULER_cancel (disconnect_task);
512 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
513 &disconnect_mesh_peers,
521 * Function called whenever an inbound channel is destroyed. Should clean up
522 * any associated state.
524 * @param cls closure (set from GNUNET_MESH_connect)
525 * @param channel connection to the other end (henceforth invalid)
526 * @param channel_ctx place where local state associated
527 * with the channel is stored
530 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
536 "Incoming channel disconnected at peer %ld\n", n);
537 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
539 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
541 GNUNET_SCHEDULER_cancel (disconnect_task);
542 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
549 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
551 * Testcase continues when the root receives confirmation of connected peers,
552 * on callback funtion ch.
554 * @param cls Closure (unsued).
555 * @param tc Task Context.
558 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
560 enum GNUNET_MESH_ChannelOption flags;
563 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
566 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
568 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
569 GNUNET_SCHEDULER_cancel (disconnect_task);
570 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
571 &disconnect_mesh_peers,
574 flags = GNUNET_MESH_OPTION_DEFAULT;
575 for (i = 0; i < TOTAL_PEERS; i++)
578 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
579 peers[i].dest = &peers[r];
580 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u => %u\n", i, r);
584 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
591 * Callback to be called when the requested peer information is available
593 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
594 * @param op the operation this callback corresponds to
595 * @param pinfo the result; will be NULL if the operation has failed
596 * @param emsg error message if the operation has failed;
597 * NULL if the operation is successfull
600 peer_id_cb (void *cls,
601 struct GNUNET_TESTBED_Operation *op,
602 const struct GNUNET_TESTBED_PeerInformation *pinfo,
607 if (NULL == pinfo || NULL != emsg)
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
610 abort_test (__LINE__);
613 peers[n].id = *(pinfo->result.id);
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u id: %s\n",
615 n, GNUNET_i2s (&peers[n].id));
616 GNUNET_break (GNUNET_OK ==
617 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
620 if (p_ids < TOTAL_PEERS)
622 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
623 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
628 * test main: start test when all peers are connected
630 * @param cls Closure.
631 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
632 * @param num_peers Number of peers that are running.
633 * @param testbed_peers Array of peers.
634 * @param meshes Handle to each of the MESHs of the peers.
638 struct GNUNET_MESH_TEST_Context *ctx,
639 unsigned int num_peers,
640 struct GNUNET_TESTBED_Peer **testbed_peers,
641 struct GNUNET_MESH_Handle **meshes)
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
648 GNUNET_assert (TOTAL_PEERS == num_peers);
649 peers_running = num_peers;
650 testbed_handles = testbed_handles;
651 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
652 &finish_profiler, NULL);
653 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
654 &disconnect_mesh_peers,
656 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
657 &shutdown_task, NULL);
658 for (i = 0; i < TOTAL_PEERS; i++)
660 peers[i].mesh = meshes[i];
662 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
663 GNUNET_TESTBED_PIT_IDENTITY,
664 &peer_id_cb, (void *) i);
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
667 /* Continues from pi_cb -> do_test */
672 * Main: start profiler.
675 main (int argc, char *argv[])
677 static uint32_t ports[2];
678 const char *config_file;
680 config_file = "test_mesh.conf";
682 ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
683 GNUNET_assert (NULL != ids);
685 test_finished = GNUNET_NO;
688 GNUNET_MESH_TEST_run ("mesh_profiler", config_file, TOTAL_PEERS,
689 &tmain, NULL, /* tmain cls */
690 &incoming_channel, &channel_cleaner,
695 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
696 "FAILED! (%d/%d)\n", ok, ok_goal);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
703 /* end of test_mesh_small.c */