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
91 GNUNET_SCHEDULER_TaskIdentifier ping_task;
95 * Testbed peer handles.
97 struct GNUNET_TESTBED_Peer **testbed_handles;
100 * Testbed Operation (to get stats).
102 struct GNUNET_TESTBED_Operation *stats_op;
105 * How many events have happened
110 * Number of events expected to conclude the test successfully.
115 * Size of each test packet
117 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
120 * Operation to get peer ids.
122 struct MeshPeer peers[TOTAL_PEERS];
130 * Is the setup initialized?
132 static int initialized;
135 * Total number of currently running peers.
137 static unsigned long long peers_running;
140 * Test context (to shut down).
142 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;
160 * Time we started the data transmission (after channel has been established
163 static struct GNUNET_TIME_Absolute start_time;
168 static unsigned int ka_sent;
169 static unsigned int ka_received;
172 * Calculate a random delay.
174 * @param max Exclusive maximum, in ms.
176 * @return A time between 0 a max-1 ms.
178 static struct GNUNET_TIME_Relative
179 delay_ms_rnd (unsigned int max)
183 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
184 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
189 * Get the index of a peer in the peers array.
191 * @param peer Peer whose index to get.
193 * @return Index of peer in peers.
196 get_index (struct MeshPeer *peer)
203 * Show the results of the test (banwidth acheived) and log them to GAUGER
208 static struct GNUNET_TIME_Absolute end_time;
209 static struct GNUNET_TIME_Relative total_time;
211 end_time = GNUNET_TIME_absolute_get();
212 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
213 FPRINTF (stderr, "Test time %s\n",
214 GNUNET_STRINGS_relative_time_to_string (total_time,
216 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
217 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
218 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
219 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
224 * Shut down peergroup, clean up.
226 * @param cls Closure (unused).
227 * @param tc Task Context.
230 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
233 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
238 * Disconnect from mesh services af all peers, call shutdown.
240 * @param cls Closure (unused).
241 * @param tc Task Context.
244 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 long line = (long) cls;
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "disconnecting mesh service of peers, called from line %ld\n",
252 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
253 for (i = 0; i < TOTAL_PEERS; i++)
255 GNUNET_TESTBED_operation_done (peers[i].op);
256 GNUNET_MESH_channel_destroy (peers[i].ch);
257 GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
259 GNUNET_MESH_TEST_cleanup (test_ctx);
260 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
262 GNUNET_SCHEDULER_cancel (shutdown_handle);
264 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
269 * Abort test: schedule disconnect and shutdown immediately
271 * @param line Line in the code the abort is requested from (__LINE__).
274 abort_test (long line)
276 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
278 GNUNET_SCHEDULER_cancel (disconnect_task);
279 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
286 * Stats callback. Finish the stats testbed operation and when all stats have
287 * been iterated, shutdown the test.
290 * @param op the operation that has been finished
291 * @param emsg error message in case the operation has failed; will be NULL if
292 * operation has executed successfully.
295 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
298 GNUNET_TESTBED_operation_done (stats_op);
300 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
301 GNUNET_SCHEDULER_cancel (disconnect_task);
302 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
309 * Process statistic values.
312 * @param peer the peer the statistic belong to
313 * @param subsystem name of subsystem that created the statistic
314 * @param name the name of the datum
315 * @param value the current value
316 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
317 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
320 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
321 const char *subsystem, const char *name,
322 uint64_t value, int is_persistent)
324 static const char *s_sent = "# keepalives sent";
325 static const char *s_recv = "# keepalives received";
328 i = GNUNET_TESTBED_get_index (peer);
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u - %s [%s]: %llu\n",
330 i, subsystem, name, value);
331 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
334 if (0 == strncmp(s_recv, name, strlen (s_recv)) && 4 == i)
337 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " sent: %u, received: %u\n",
338 ka_sent, ka_received);
339 if (ka_sent < 2 || ka_sent > ka_received + 1)
348 * Task check that keepalives were sent and received.
350 * @param cls Closure (NULL).
351 * @param tc Task Context.
354 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
359 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
360 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
361 stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
363 stats_iterator, stats_cont, NULL);
368 * Transmit ready callback.
370 * @param cls Closure (peer).
371 * @param size Size of the tranmist buffer.
372 * @param buf Pointer to the beginning of the buffer.
374 * @return Number of bytes written to buf.
377 tmt_rdy (void *cls, size_t size, void *buf);
381 * Task to schedule a new data transmission.
383 * @param cls Closure (peer).
384 * @param tc Task Context.
387 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
389 struct MeshPeer *peer = (struct MeshPeer *) cls;
390 struct GNUNET_MESH_TransmitHandle *th;
391 struct GNUNET_MESH_Channel *channel;
393 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
399 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
400 GNUNET_TIME_UNIT_FOREVER_REL,
401 size_payload, &tmt_rdy, peer);
408 * Transmit ready callback
410 * @param cls Closure (peer).
411 * @param size Size of the buffer we have.
412 * @param buf Buffer to copy data to.
415 tmt_rdy (void *cls, size_t size, void *buf)
417 struct MeshPeer *peer = (struct MeshPeer *) cls;
418 struct GNUNET_MessageHeader *msg = buf;
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
422 if (size < size_payload || NULL == buf)
424 GNUNET_break (ok >= ok_goal - 2);
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "size %u, buf %p, data_sent %u, data_received %u\n",
427 size, buf, peer->data_sent, peer->data_received);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
432 msg->size = htons (size);
433 msg->type = htons ((long) cls);
434 data = (uint32_t *) &msg[1];
435 *data = htonl (peer->data_sent);
436 if (GNUNET_NO == initialized)
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "sending initializer\n");
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 " Sent packet %d\n", peer->data_sent);
446 if (peer->data_sent < TOTAL_PACKETS)
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 " Scheduling packet %d\n", peer->data_sent + 1);
450 GNUNET_SCHEDULER_add_now (&data_task, peer);
459 * Function is called whenever a message is received.
461 * @param cls closure (set from GNUNET_MESH_connect)
462 * @param channel connection to the other end
463 * @param channel_ctx place to store local state associated with the channel
464 * @param message the actual message
465 * @return GNUNET_OK to keep the connection open,
466 * GNUNET_SYSERR to close it (signal serious error)
469 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
471 const struct GNUNET_MessageHeader *message)
473 // long n = (long) cls;
475 GNUNET_MESH_receive_done (channel);
478 if (GNUNET_NO == initialized)
480 initialized = GNUNET_YES;
481 start_time = GNUNET_TIME_absolute_get ();
482 GNUNET_SCHEDULER_add_now (&data_task, NULL);
484 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
485 GNUNET_TIME_UNIT_FOREVER_REL,
486 size_payload, &tmt_rdy, (void *) 1L);
488 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
490 GNUNET_SCHEDULER_cancel (disconnect_task);
491 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
492 &disconnect_mesh_peers,
501 * Handlers, for diverse services
503 static struct GNUNET_MESH_MessageHandler handlers[] = {
504 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
510 * Method called whenever another peer has added us to a channel
511 * the other peer initiated.
513 * @param cls Closure.
514 * @param channel New handle to the channel.
515 * @param initiator Peer that started the channel.
516 * @param port Port this channel is connected to.
517 * @param options channel option flags
518 * @return Initial channel context for the channel
519 * (can be NULL -- that's not an error).
522 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
523 const struct GNUNET_PeerIdentity *initiator,
524 uint32_t port, enum GNUNET_MESH_ChannelOption options)
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
529 "Incoming channel from %s to peer %ld\n",
530 GNUNET_i2s (initiator), n);
532 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
533 peers[n].incoming_ch = channel;
535 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
537 GNUNET_SCHEDULER_cancel (disconnect_task);
538 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
539 &disconnect_mesh_peers,
547 * Function called whenever an inbound channel is destroyed. Should clean up
548 * any associated state.
550 * @param cls closure (set from GNUNET_MESH_connect)
551 * @param channel connection to the other end (henceforth invalid)
552 * @param channel_ctx place where local state associated
553 * with the channel is stored
556 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
561 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
562 "Incoming channel disconnected at peer %ld\n", n);
563 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
565 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
567 GNUNET_SCHEDULER_cancel (disconnect_task);
568 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
575 * @brief Send data to destination
577 * @param cls Closure (peer).
578 * @param tc Task context.
581 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 struct MeshPeer *peer = (struct MeshPeer *) cls;
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u -> %u\n",
585 get_index (peer), peer->dest);
587 GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
588 GNUNET_TIME_UNIT_FOREVER_REL,
589 size_payload, &tmt_rdy, (void *) 1L);
594 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
596 * Testcase continues when the root receives confirmation of connected peers,
597 * on callback funtion ch.
599 * @param cls Closure (unsued).
600 * @param tc Task Context.
603 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
605 enum GNUNET_MESH_ChannelOption flags;
608 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
611 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
613 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
614 GNUNET_SCHEDULER_cancel (disconnect_task);
615 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
616 &disconnect_mesh_peers,
619 flags = GNUNET_MESH_OPTION_DEFAULT;
620 for (i = 0; i < TOTAL_PEERS; i++)
622 peers[i].dest = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
624 peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
625 &peers[peers[i].dest].id,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u => %u\n", i, peers[i].dest);
628 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd(2000),
635 * Callback to be called when the requested peer information is available
637 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
638 * @param op the operation this callback corresponds to
639 * @param pinfo the result; will be NULL if the operation has failed
640 * @param emsg error message if the operation has failed;
641 * NULL if the operation is successfull
644 peer_id_cb (void *cls,
645 struct GNUNET_TESTBED_Operation *op,
646 const struct GNUNET_TESTBED_PeerInformation *pinfo,
651 if (NULL == pinfo || NULL != emsg)
653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
654 abort_test (__LINE__);
657 peers[n].id = *(pinfo->result.id);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u id: %s\n",
659 n, GNUNET_i2s (&peers[n].id));
661 if (p_ids < TOTAL_PEERS)
663 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
664 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
669 * test main: start test when all peers are connected
671 * @param cls Closure.
672 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
673 * @param num_peers Number of peers that are running.
674 * @param testbed_peers Array of peers.
675 * @param meshes Handle to each of the MESHs of the peers.
679 struct GNUNET_MESH_TEST_Context *ctx,
680 unsigned int num_peers,
681 struct GNUNET_TESTBED_Peer **testbed_peers,
682 struct GNUNET_MESH_Handle **meshes)
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
689 GNUNET_assert (TOTAL_PEERS == num_peers);
690 peers_running = num_peers;
691 testbed_handles = testbed_handles;
692 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
693 &disconnect_mesh_peers,
695 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
696 &shutdown_task, NULL);
697 for (i = 0; i < TOTAL_PEERS; i++)
699 peers[i].mesh = meshes[i];
701 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
702 GNUNET_TESTBED_PIT_IDENTITY,
703 &peer_id_cb, (void *) i);
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
706 /* Continues from pi_cb -> do_test */
711 * Main: start profiler.
714 main (int argc, char *argv[])
716 initialized = GNUNET_NO;
717 static uint32_t ports[2];
718 const char *config_file;
720 config_file = "test_mesh.conf";
725 GNUNET_MESH_TEST_run ("mesh_profiler", config_file, TOTAL_PEERS,
726 &tmain, NULL, /* tmain cls */
727 &incoming_channel, &channel_cleaner,
732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
733 "FAILED! (%d/%d)\n", ok, ok_goal);
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
740 /* end of test_mesh_small.c */