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"
34 * How namy messages to send
36 #define TOTAL_PACKETS 1000
39 * How namy peers to run
41 #define TOTAL_PEERS 1000
44 * How long until we give up on connecting the peers?
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49 * Time to wait for stuff that should be rather fast
51 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55 * How many events have happened
60 * Number of events expected to conclude the test successfully.
65 * Size of each test packet
67 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
70 * Operation to get peer ids.
72 struct GNUNET_TESTBED_Operation *t_op[TOTAL_PEERS];
77 struct GNUNET_PeerIdentity *p_id[TOTAL_PEERS];
80 * Mesh handle for the root peer
82 static struct GNUNET_MESH_Handle *mesh_h[TOTAL_PEERS];
85 * Channel handle for the root peer
87 static struct GNUNET_MESH_Channel *ch;
90 * Channel handle for the dest peer
92 static struct GNUNET_MESH_Channel *incoming_ch;
100 * Is the setup initialized?
102 static int initialized;
105 * Number of payload packes sent
107 static int data_sent;
110 * Number of payload packets received
112 static int data_received;
115 * Number of payload packed explicitly (app level) acknowledged
120 * Total number of currently running peers.
122 static unsigned long long peers_running;
125 * Test context (to shut down).
127 struct GNUNET_MESH_TEST_Context *test_ctx;
130 * Task called to shutdown test.
132 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
135 * Task called to disconnect peers, before shutdown.
137 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
140 * Task to perform tests
142 static GNUNET_SCHEDULER_TaskIdentifier test_task;
145 * Time we started the data transmission (after channel has been established
148 static struct GNUNET_TIME_Absolute start_time;
150 static struct GNUNET_TESTBED_Peer **testbed_peers;
155 static struct GNUNET_STATISTICS_Handle *stats;
156 static struct GNUNET_STATISTICS_GetHandle *stats_get;
157 static struct GNUNET_TESTBED_Operation *stats_op;
158 static unsigned int ka_sent;
159 static unsigned int ka_received;
163 * Show the results of the test (banwidth acheived) and log them to GAUGER
168 static struct GNUNET_TIME_Absolute end_time;
169 static struct GNUNET_TIME_Relative total_time;
171 end_time = GNUNET_TIME_absolute_get();
172 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
173 FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
174 FPRINTF (stderr, "Test time %s\n",
175 GNUNET_STRINGS_relative_time_to_string (total_time,
177 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
178 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
179 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
180 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
181 GAUGER ("MESH", test_name,
182 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
188 * Shut down peergroup, clean up.
190 * @param cls Closure (unused).
191 * @param tc Task Context.
194 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
197 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
202 * Disconnect from mesh services af all peers, call shutdown.
204 * @param cls Closure (unused).
205 * @param tc Task Context.
208 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 long line = (long) cls;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "disconnecting mesh service of peers, called from line %ld\n",
216 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
217 for (i = 0; i < 2; i++)
219 GNUNET_TESTBED_operation_done (t_op[i]);
223 GNUNET_MESH_channel_destroy (ch);
226 if (NULL != incoming_ch)
228 GNUNET_MESH_channel_destroy (incoming_ch);
231 GNUNET_MESH_TEST_cleanup (test_ctx);
232 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
234 GNUNET_SCHEDULER_cancel (shutdown_handle);
236 if (NULL != stats_get)
237 GNUNET_STATISTICS_get_cancel (stats_get);
238 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
243 * Abort test: schedule disconnect and shutdown immediately
245 * @param line Line in the code the abort is requested from (__LINE__).
248 abort_test (long line)
250 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (disconnect_task);
253 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
260 * Stats callback. Finish the stats testbed operation and when all stats have
261 * been iterated, shutdown the test.
264 * @param op the operation that has been finished
265 * @param emsg error message in case the operation has failed; will be NULL if
266 * operation has executed successfully.
269 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
271 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
272 GNUNET_TESTBED_operation_done (stats_op);
274 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
275 GNUNET_SCHEDULER_cancel (disconnect_task);
276 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
283 * Process statistic values.
286 * @param peer the peer the statistic belong to
287 * @param subsystem name of subsystem that created the statistic
288 * @param name the name of the datum
289 * @param value the current value
290 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
291 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
294 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
295 const char *subsystem, const char *name,
296 uint64_t value, int is_persistent)
298 static const char *s_sent = "# keepalives sent";
299 static const char *s_recv = "# keepalives received";
302 i = GNUNET_TESTBED_get_index (peer);
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u - %s [%s]: %llu\n",
304 i, subsystem, name, value);
305 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
308 if (0 == strncmp(s_recv, name, strlen (s_recv)) && 4 == i)
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " sent: %u, received: %u\n",
312 ka_sent, ka_received);
313 if (ka_sent < 2 || ka_sent > ka_received + 1)
322 * Task check that keepalives were sent and received.
324 * @param cls Closure (NULL).
325 * @param tc Task Context.
328 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
330 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
333 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
334 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
335 GNUNET_MESH_channel_destroy (ch);
336 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_peers,
338 stats_iterator, stats_cont, NULL);
343 * Transmit ready callback.
345 * @param cls Closure (message type).
346 * @param size Size of the tranmist buffer.
347 * @param buf Pointer to the beginning of the buffer.
349 * @return Number of bytes written to buf.
352 tmt_rdy (void *cls, size_t size, void *buf);
356 * Task to schedule a new data transmission.
358 * @param cls Closure (peer #).
359 * @param tc Task Context.
362 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
364 struct GNUNET_MESH_TransmitHandle *th;
365 struct GNUNET_MESH_Channel *channel;
367 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
371 if (GNUNET_YES == test_backwards)
373 channel = incoming_ch;
379 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
380 GNUNET_TIME_UNIT_FOREVER_REL,
381 size_payload, &tmt_rdy, (void *) 1L);
384 unsigned long i = (unsigned long) cls;
386 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
389 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
390 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
391 &data_task, (void *)1UL);
396 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
397 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
398 GNUNET_TIME_UNIT_MILLISECONDS,
400 &data_task, (void *)i);
407 * Transmit ready callback
409 * @param cls Closure (message type).
410 * @param size Size of the buffer we have.
411 * @param buf Buffer to copy data to.
414 tmt_rdy (void *cls, size_t size, void *buf)
416 struct GNUNET_MessageHeader *msg = buf;
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "tmt_rdy called, filling buffer\n");
421 if (size < size_payload || NULL == buf)
423 GNUNET_break (ok >= ok_goal - 2);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "size %u, buf %p, data_sent %u, data_received %u\n",
426 size, buf, data_sent, data_received);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
431 msg->size = htons (size);
432 msg->type = htons ((long) cls);
433 data = (uint32_t *) &msg[1];
434 *data = htonl (data_sent);
435 if (GNUNET_NO == initialized)
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "sending initializer\n");
440 else if (SPEED == test)
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 " Sent packet %d\n", data_sent);
445 if (data_sent < TOTAL_PACKETS)
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 " Scheduling packet %d\n", data_sent + 1);
449 GNUNET_SCHEDULER_add_now (&data_task, NULL);
458 * Function is called whenever a message is received.
460 * @param cls closure (set from GNUNET_MESH_connect)
461 * @param channel connection to the other end
462 * @param channel_ctx place to store local state associated with the channel
463 * @param message the actual message
464 * @return GNUNET_OK to keep the connection open,
465 * GNUNET_SYSERR to close it (signal serious error)
468 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
470 const struct GNUNET_MessageHeader *message)
472 long client = (long) cls;
473 long expected_target_client;
478 GNUNET_MESH_receive_done (channel);
482 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
484 GNUNET_SCHEDULER_cancel (disconnect_task);
485 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
486 &disconnect_mesh_peers,
494 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
498 "Leaf client %li got a message.\n",
505 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
506 data = (uint32_t *) &message[1];
507 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
508 if (SPEED == test && GNUNET_YES == test_backwards)
510 expected_target_client = 0L;
514 expected_target_client = 4L;
517 if (GNUNET_NO == initialized)
519 initialized = GNUNET_YES;
520 start_time = GNUNET_TIME_absolute_get ();
523 GNUNET_assert (4L == client);
524 GNUNET_SCHEDULER_add_now (&data_task, NULL);
529 if (client == expected_target_client) // Normally 4
532 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
533 if (SPEED != test || (ok_goal - 2) == ok)
535 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
536 GNUNET_TIME_UNIT_FOREVER_REL,
537 size_payload, &tmt_rdy, (void *) 1L);
542 if (data_received < TOTAL_PACKETS)
548 if (test == SPEED_ACK || test == SPEED)
551 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", data_ack);
552 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
553 GNUNET_TIME_UNIT_FOREVER_REL,
554 size_payload, &tmt_rdy, (void *) 1L);
555 if (data_ack < TOTAL_PACKETS && SPEED != test)
557 if (ok == 2 && SPEED == test)
561 if (test == P2P_SIGNAL)
563 GNUNET_MESH_channel_destroy (incoming_ch);
568 GNUNET_MESH_channel_destroy (ch);
573 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
575 GNUNET_SCHEDULER_cancel (disconnect_task);
576 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
577 &disconnect_mesh_peers,
586 * Handlers, for diverse services
588 static struct GNUNET_MESH_MessageHandler handlers[] = {
589 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
595 * Method called whenever another peer has added us to a channel
596 * the other peer initiated.
598 * @param cls Closure.
599 * @param channel New handle to the channel.
600 * @param initiator Peer that started the channel.
601 * @param port Port this channel is connected to.
602 * @param options channel option flags
603 * @return Initial channel context for the channel
604 * (can be NULL -- that's not an error).
607 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
608 const struct GNUNET_PeerIdentity *initiator,
609 uint32_t port, enum GNUNET_MESH_ChannelOption options)
611 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
612 "Incoming channel from %s to peer %d\n",
613 GNUNET_i2s (initiator), (long) cls);
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
616 if ((long) cls == 4L)
617 incoming_ch = channel;
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 "Incoming channel for unknown client %lu\n", (long) cls);
624 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
626 GNUNET_SCHEDULER_cancel (disconnect_task);
627 if (KEEPALIVE == test)
629 struct GNUNET_TIME_Relative delay;
630 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS , 5);
632 GNUNET_SCHEDULER_add_delayed (delay, &collect_stats, NULL);
635 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
636 &disconnect_mesh_peers,
644 * Function called whenever an inbound channel is destroyed. Should clean up
645 * any associated state.
647 * @param cls closure (set from GNUNET_MESH_connect)
648 * @param channel connection to the other end (henceforth invalid)
649 * @param channel_ctx place where local state associated
650 * with the channel is stored
653 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
659 "Incoming channel disconnected at peer %d\n", i);
660 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
662 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
664 GNUNET_SCHEDULER_cancel (disconnect_task);
665 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
672 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
674 * Testcase continues when the root receives confirmation of connected peers,
675 * on callback funtion ch.
677 * @param cls Closure (unsued).
678 * @param tc Task Context.
681 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
683 enum GNUNET_MESH_ChannelOption flags;
685 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
688 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
690 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
691 GNUNET_SCHEDULER_cancel (disconnect_task);
692 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
693 &disconnect_mesh_peers,
696 flags = GNUNET_MESH_OPTION_DEFAULT;
697 ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
703 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
704 GNUNET_TIME_UNIT_FOREVER_REL,
705 size_payload, &tmt_rdy, (void *) 1L);
710 * Callback to be called when the requested peer information is available
712 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
713 * @param op the operation this callback corresponds to
714 * @param pinfo the result; will be NULL if the operation has failed
715 * @param emsg error message if the operation has failed;
716 * NULL if the operation is successfull
720 struct GNUNET_TESTBED_Operation *op,
721 const struct GNUNET_TESTBED_PeerInformation *pinfo,
726 if (NULL == pinfo || NULL != emsg)
728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
729 abort_test (__LINE__);
732 p_id[i] = pinfo->result.id;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u id: %s\n",
734 i, GNUNET_i2s (p_id[i]));
736 if (p_ids < TOTAL_PEERS)
738 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
739 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
744 * test main: start test when all peers are connected
746 * @param cls Closure.
747 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
748 * @param num_peers Number of peers that are running.
749 * @param peers Array of peers.
750 * @param meshes Handle to each of the MESHs of the peers.
754 struct GNUNET_MESH_TEST_Context *ctx,
755 unsigned int num_peers,
756 struct GNUNET_TESTBED_Peer **peers,
757 struct GNUNET_MESH_Handle **meshes)
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
764 GNUNET_assert (TOTAL_PEERS == num_peers);
765 peers_running = num_peers;
766 testbed_peers = peers;
767 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
768 &disconnect_mesh_peers,
770 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
771 &shutdown_task, NULL);
772 for (i = 0; i < TOTAL_PEERS; i++)
774 t_op[i] = GNUNET_TESTBED_peer_get_information (peers[i],
775 GNUNET_TESTBED_PIT_IDENTITY,
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
779 /* Continues from pi_cb -> do_test */
784 * Main: start profiler.
787 main (int argc, char *argv[])
789 initialized = GNUNET_NO;
790 static uint32_t ports[2];
791 const char *config_file;
793 config_file = "test_mesh.conf";
798 GNUNET_MESH_TEST_run ("mesh_profiler", config_file, TOTAL_PEERS,
799 &tmain, NULL, /* tmain cls */
800 &incoming_channel, &channel_cleaner,
805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
806 "FAILED! (%d/%d)\n", ok, ok_goal);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
813 /* end of test_mesh_small.c */