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)
57 * Testbed Operation (to get peer id, etc).
59 struct GNUNET_TESTBED_Operation *op;
62 * Testbed peer handle.
64 struct GNUNET_TESTBED_Peer *testbed_peer;
69 struct GNUNET_PeerIdentity *id;
72 * Mesh handle for the root peer
74 static struct GNUNET_MESH_Handle *mesh;
77 * Channel handle for the root peer
79 static struct GNUNET_MESH_Channel *ch;
82 * Channel handle for the dest peer
84 static struct GNUNET_MESH_Channel *incoming_ch;
89 * How many events have happened
94 * Number of events expected to conclude the test successfully.
99 * Size of each test packet
101 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
104 * Operation to get peer ids.
106 struct MeshPeer peers[TOTAL_PEERS];
114 * Is the setup initialized?
116 static int initialized;
119 * Number of payload packes sent
121 static int data_sent;
124 * Number of payload packets received
126 static int data_received;
129 * Number of payload packed explicitly (app level) acknowledged
134 * Total number of currently running peers.
136 static unsigned long long peers_running;
139 * Test context (to shut down).
141 struct GNUNET_MESH_TEST_Context *test_ctx;
144 * Task called to shutdown test.
146 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
149 * Task called to disconnect peers, before shutdown.
151 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
154 * Task to perform tests
156 static GNUNET_SCHEDULER_TaskIdentifier test_task;
159 * Time we started the data transmission (after channel has been established
162 static struct GNUNET_TIME_Absolute start_time;
167 static unsigned int ka_sent;
168 static unsigned int ka_received;
172 * Show the results of the test (banwidth acheived) and log them to GAUGER
177 static struct GNUNET_TIME_Absolute end_time;
178 static struct GNUNET_TIME_Relative total_time;
180 end_time = GNUNET_TIME_absolute_get();
181 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
182 FPRINTF (stderr, "Test time %s\n",
183 GNUNET_STRINGS_relative_time_to_string (total_time,
185 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
186 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
187 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
188 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
189 GAUGER ("MESH", test_name,
190 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
196 * Shut down peergroup, clean up.
198 * @param cls Closure (unused).
199 * @param tc Task Context.
202 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
205 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
210 * Disconnect from mesh services af all peers, call shutdown.
212 * @param cls Closure (unused).
213 * @param tc Task Context.
216 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 long line = (long) cls;
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "disconnecting mesh service of peers, called from line %ld\n",
224 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
225 for (i = 0; i < TOTAL_PEERS; i++)
227 GNUNET_TESTBED_operation_done (peers[i].op);
228 GNUNET_MESH_channel_destroy (peers[i].ch);
229 GNUNET_MESH_channel_destroy (peers[i].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");
372 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
373 GNUNET_TIME_UNIT_FOREVER_REL,
374 size_payload, &tmt_rdy, (void *) 1L);
381 * Transmit ready callback
383 * @param cls Closure (message type).
384 * @param size Size of the buffer we have.
385 * @param buf Buffer to copy data to.
388 tmt_rdy (void *cls, size_t size, void *buf)
390 struct GNUNET_MessageHeader *msg = buf;
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
394 if (size < size_payload || NULL == buf)
396 GNUNET_break (ok >= ok_goal - 2);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "size %u, buf %p, data_sent %u, data_received %u\n",
399 size, buf, data_sent, data_received);
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
404 msg->size = htons (size);
405 msg->type = htons ((long) cls);
406 data = (uint32_t *) &msg[1];
407 *data = htonl (data_sent);
408 if (GNUNET_NO == initialized)
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "sending initializer\n");
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 " Sent packet %d\n", data_sent);
418 if (data_sent < TOTAL_PACKETS)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 " Scheduling packet %d\n", data_sent + 1);
422 GNUNET_SCHEDULER_add_now (&data_task, NULL);
431 * Function is called whenever a message is received.
433 * @param cls closure (set from GNUNET_MESH_connect)
434 * @param channel connection to the other end
435 * @param channel_ctx place to store local state associated with the channel
436 * @param message the actual message
437 * @return GNUNET_OK to keep the connection open,
438 * GNUNET_SYSERR to close it (signal serious error)
441 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
443 const struct GNUNET_MessageHeader *message)
445 long client = (long) cls;
447 GNUNET_MESH_receive_done (channel);
450 if (GNUNET_NO == initialized)
452 initialized = GNUNET_YES;
453 start_time = GNUNET_TIME_absolute_get ();
454 GNUNET_SCHEDULER_add_now (&data_task, NULL);
456 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
457 GNUNET_TIME_UNIT_FOREVER_REL,
458 size_payload, &tmt_rdy, (void *) 1L);
460 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
462 GNUNET_SCHEDULER_cancel (disconnect_task);
463 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
464 &disconnect_mesh_peers,
473 * Handlers, for diverse services
475 static struct GNUNET_MESH_MessageHandler handlers[] = {
476 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
482 * Method called whenever another peer has added us to a channel
483 * the other peer initiated.
485 * @param cls Closure.
486 * @param channel New handle to the channel.
487 * @param initiator Peer that started the channel.
488 * @param port Port this channel is connected to.
489 * @param options channel option flags
490 * @return Initial channel context for the channel
491 * (can be NULL -- that's not an error).
494 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
495 const struct GNUNET_PeerIdentity *initiator,
496 uint32_t port, enum GNUNET_MESH_ChannelOption options)
500 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
501 "Incoming channel from %s to peer %ld\n",
502 GNUNET_i2s (initiator), n);
504 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
505 peers[n].incoming_ch = channel;
507 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
509 GNUNET_SCHEDULER_cancel (disconnect_task);
510 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
511 &disconnect_mesh_peers,
519 * Function called whenever an inbound channel is destroyed. Should clean up
520 * any associated state.
522 * @param cls closure (set from GNUNET_MESH_connect)
523 * @param channel connection to the other end (henceforth invalid)
524 * @param channel_ctx place where local state associated
525 * with the channel is stored
528 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
533 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
534 "Incoming channel disconnected at peer %ld\n", n);
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
537 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
539 GNUNET_SCHEDULER_cancel (disconnect_task);
540 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
547 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
549 * Testcase continues when the root receives confirmation of connected peers,
550 * on callback funtion ch.
552 * @param cls Closure (unsued).
553 * @param tc Task Context.
556 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
558 enum GNUNET_MESH_ChannelOption flags;
560 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
563 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
565 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
566 GNUNET_SCHEDULER_cancel (disconnect_task);
567 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
568 &disconnect_mesh_peers,
571 flags = GNUNET_MESH_OPTION_DEFAULT;
572 peers[0].ch = GNUNET_MESH_channel_create (peers[0].mesh, NULL,
573 &peers[TOTAL_PEERS -1].id,
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
580 GNUNET_MESH_notify_transmit_ready (peers[0].ch, GNUNET_NO,
581 GNUNET_TIME_UNIT_FOREVER_REL,
582 size_payload, &tmt_rdy, (void *) 1L);
587 * Callback to be called when the requested peer information is available
589 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
590 * @param op the operation this callback corresponds to
591 * @param pinfo the result; will be NULL if the operation has failed
592 * @param emsg error message if the operation has failed;
593 * NULL if the operation is successfull
597 struct GNUNET_TESTBED_Operation *op,
598 const struct GNUNET_TESTBED_PeerInformation *pinfo,
603 if (NULL == pinfo || NULL != emsg)
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
606 abort_test (__LINE__);
609 peers[n].id = pinfo->result.id;
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u id: %s\n",
611 i, GNUNET_i2s (p_id[i]));
613 if (p_ids < TOTAL_PEERS)
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
616 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
621 * test main: start test when all peers are connected
623 * @param cls Closure.
624 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
625 * @param num_peers Number of peers that are running.
626 * @param testbed_peers Array of peers.
627 * @param meshes Handle to each of the MESHs of the peers.
631 struct GNUNET_MESH_TEST_Context *ctx,
632 unsigned int num_peers,
633 struct GNUNET_TESTBED_Peer **testbed_peers,
634 struct GNUNET_MESH_Handle **meshes)
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
641 GNUNET_assert (TOTAL_PEERS == num_peers);
642 peers_running = num_peers;
643 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
644 &disconnect_mesh_peers,
646 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
647 &shutdown_task, NULL);
648 for (i = 0; i < TOTAL_PEERS; i++)
650 peers[i].testbed_peer = testbed_peers[i];
651 peers[i].mesh = meshes[i];
653 GNUNET_TESTBED_peer_get_information (peers[i].testbed_peer,
654 GNUNET_TESTBED_PIT_IDENTITY,
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
658 /* Continues from pi_cb -> do_test */
663 * Main: start profiler.
666 main (int argc, char *argv[])
668 initialized = GNUNET_NO;
669 static uint32_t ports[2];
670 const char *config_file;
672 config_file = "test_mesh.conf";
677 GNUNET_MESH_TEST_run ("mesh_profiler", config_file, TOTAL_PEERS,
678 &tmain, NULL, /* tmain cls */
679 &incoming_channel, &channel_cleaner,
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 "FAILED! (%d/%d)\n", ok, ok_goal);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
692 /* end of test_mesh_small.c */