2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/gnunet-cadet-profiler.c
23 * @brief Profiler for cadet experiments.
27 #include "cadet_test_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_statistics_service.h"
37 * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
39 #define PING_PERIOD 500
42 * How long until we give up on connecting the peers?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
47 * Time to wait for stuff that should be rather fast
49 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
52 * Total number of rounds.
54 #define number_rounds sizeof(rounds)/sizeof(rounds[0])
57 * Ratio of peers active. First round always is 1.0.
59 static float rounds[] = {0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0};
62 * Message type for pings.
64 struct CadetPingMessage
67 * Header. Type PING/PONG.
69 struct GNUNET_MessageHeader header;
77 * Time the message was sent.
79 struct GNUNET_TIME_AbsoluteNBO timestamp;
84 uint32_t round_number;
93 * Testbed Operation (to get peer id, etc).
95 struct GNUNET_TESTBED_Operation *op;
100 struct GNUNET_PeerIdentity id;
103 * Cadet handle for the root peer
105 struct GNUNET_CADET_Handle *cadet;
108 * Channel handle for the root peer
110 struct GNUNET_CADET_Channel *ch;
113 * Channel handle for the dest peer
115 struct GNUNET_CADET_Channel *incoming_ch;
118 * Channel handle for a warmup channel.
120 struct GNUNET_CADET_Channel *warmup_ch;
123 * Number of payload packes sent
128 * Number of payload packets received
138 * Destinaton to ping.
140 struct CadetPeer *dest;
143 * Incoming channel for pings.
145 struct CadetPeer *incoming;
148 * Task to do the next ping.
150 struct GNUNET_SCHEDULER_Task * ping_task;
152 float mean[number_rounds];
153 float var[number_rounds];
154 unsigned int pongs[number_rounds];
155 unsigned int pings[number_rounds];
160 * Duration of each round.
162 static struct GNUNET_TIME_Relative round_time;
165 * GNUNET_PeerIdentity -> CadetPeer
167 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
170 * Testbed peer handles.
172 static struct GNUNET_TESTBED_Peer **testbed_handles;
175 * Testbed Operation (to get stats).
177 static struct GNUNET_TESTBED_Operation *stats_op;
180 * Operation to get peer ids.
182 struct CadetPeer *peers;
187 static unsigned int p_ids;
190 * Total number of peers.
192 static unsigned long long peers_total;
195 * Number of currently running peers.
197 static unsigned long long peers_running;
200 * Number of peers doing pings.
202 static unsigned long long peers_pinging;
205 * Test context (to shut down).
207 static struct GNUNET_CADET_TEST_Context *test_ctx;
210 * Task called to shutdown test.
212 static struct GNUNET_SCHEDULER_Task * shutdown_handle;
215 * Task called to disconnect peers, before shutdown.
217 static struct GNUNET_SCHEDULER_Task * disconnect_task;
220 * Task to perform tests
222 static struct GNUNET_SCHEDULER_Task * test_task;
227 static unsigned int current_round;
230 * Do preconnect? (Each peer creates a tunnel to one other peer).
232 static int do_warmup;
237 static unsigned int peers_warmup;
240 * Flag to notify callbacks not to generate any new traffic anymore.
242 static int test_finished;
246 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
248 * Testcase continues when the root receives confirmation of connected peers,
249 * on callback funtion ch.
251 * @param cls Closure (unsued).
252 * @param tc Task Context.
255 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
259 * Calculate a random delay.
261 * @param max Exclusive maximum, in ms.
263 * @return A time between 0 a max-1 ms.
265 static struct GNUNET_TIME_Relative
266 delay_ms_rnd (unsigned int max)
270 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
271 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
276 * Get the index of a peer in the peers array.
278 * @param peer Peer whose index to get.
280 * @return Index of peer in peers.
283 get_index (struct CadetPeer *peer)
290 * Show the results of the test (banwidth acheived) and log them to GAUGER
295 struct CadetPeer *peer;
299 for (i = 0; i < number_rounds; i++)
301 for (j = 0; j < peers_pinging; j++)
305 "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n",
306 i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)),
307 peer->pings[i], peer->pongs[i]);
314 * Shut down peergroup, clean up.
316 * @param cls Closure (unused).
317 * @param tc Task Context.
320 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
323 shutdown_handle = NULL;
328 * Disconnect from cadet services af all peers, call shutdown.
330 * @param cls Closure (unused).
331 * @param tc Task Context.
334 disconnect_cadet_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 long line = (long) cls;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "disconnecting cadet service, called from line %ld\n", line);
341 disconnect_task = NULL;
342 for (i = 0; i < peers_total; i++)
344 if (NULL != peers[i].op)
345 GNUNET_TESTBED_operation_done (peers[i].op);
347 if (peers[i].up != GNUNET_YES)
350 if (NULL != peers[i].ch)
352 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
353 GNUNET_CADET_channel_destroy (peers[i].ch);
355 if (NULL != peers[i].warmup_ch)
357 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: warmup channel %p\n",
358 i, peers[i].warmup_ch);
359 GNUNET_CADET_channel_destroy (peers[i].warmup_ch);
361 if (NULL != peers[i].incoming_ch)
363 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
364 i, peers[i].incoming_ch);
365 GNUNET_CADET_channel_destroy (peers[i].incoming_ch);
368 GNUNET_CADET_TEST_cleanup (test_ctx);
369 if (NULL != shutdown_handle)
371 GNUNET_SCHEDULER_cancel (shutdown_handle);
373 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
378 * Finish test normally: schedule disconnect and shutdown
380 * @param line Line in the code the abort is requested from (__LINE__).
383 abort_test (long line)
385 if (disconnect_task != NULL)
387 GNUNET_SCHEDULER_cancel (disconnect_task);
388 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
394 * Stats callback. Finish the stats testbed operation and when all stats have
395 * been iterated, shutdown the test.
398 * @param op the operation that has been finished
399 * @param emsg error message in case the operation has failed; will be NULL if
400 * operation has executed successfully.
403 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
405 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
406 GNUNET_TESTBED_operation_done (stats_op);
408 if (NULL != disconnect_task)
409 GNUNET_SCHEDULER_cancel (disconnect_task);
410 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
417 * Process statistic values.
420 * @param peer the peer the statistic belong to
421 * @param subsystem name of subsystem that created the statistic
422 * @param name the name of the datum
423 * @param value the current value
424 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
425 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
428 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
429 const char *subsystem, const char *name,
430 uint64_t value, int is_persistent)
434 i = GNUNET_TESTBED_get_index (peer);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
436 i, subsystem, name, value);
443 * Task check that keepalives were sent and received.
445 * @param cls Closure (NULL).
446 * @param tc Task Context.
449 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
454 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
455 stats_op = GNUNET_TESTBED_get_statistics (peers_total, testbed_handles,
457 stats_iterator, stats_cont, NULL);
462 * @brief Finish profiler normally. Signal finish and start collecting stats.
464 * @param cls Closure (unused).
465 * @param tc Task context.
468 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
473 test_finished = GNUNET_YES;
475 GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
479 * Set the total number of running peers.
481 * @param target Desired number of running peers.
484 adjust_running_peers (unsigned int target)
486 struct GNUNET_TESTBED_Operation *op;
492 GNUNET_assert (target <= peers_total);
494 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
495 if (target > peers_running)
497 delta = target - peers_running;
502 delta = peers_running - target;
506 for (i = 0; i < delta; i++)
509 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
510 peers_total - peers_pinging);
512 } while (peers[r].up == run || NULL != peers[r].incoming);
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
514 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
516 if (NULL != peers[r].ping_task)
517 GNUNET_SCHEDULER_cancel (peers[r].ping_task);
518 peers[r].ping_task = NULL;
522 if (NULL != peers[r].ch)
523 GNUNET_CADET_channel_destroy (peers[r].ch);
525 if (NULL != peers[r].dest)
527 if (NULL != peers[r].dest->incoming_ch)
528 GNUNET_CADET_channel_destroy (peers[r].dest->incoming_ch);
529 peers[r].dest->incoming_ch = NULL;
532 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
533 "cadet", NULL, NULL, run);
534 GNUNET_break (NULL != op);
535 peers_running += run ? 1 : -1;
536 GNUNET_assert (peers_running > 0);
542 * @brief Move to next round.
544 * @param cls Closure (round #).
545 * @param tc Task context.
548 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
553 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", current_round);
554 if (0.0 == rounds[current_round])
556 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
557 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
560 adjust_running_peers (rounds[current_round] * peers_total);
563 GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
568 * Transmit ping callback.
570 * @param cls Closure (peer for PING, NULL for PONG).
571 * @param size Size of the tranmist buffer.
572 * @param buf Pointer to the beginning of the buffer.
574 * @return Number of bytes written to buf.
577 tmt_rdy_ping (void *cls, size_t size, void *buf);
581 * Transmit pong callback.
583 * @param cls Closure (copy of PING message, to be freed).
584 * @param size Size of the buffer we have.
585 * @param buf Buffer to copy data to.
588 tmt_rdy_pong (void *cls, size_t size, void *buf)
590 struct CadetPingMessage *ping = cls;
591 struct CadetPingMessage *pong;
593 if (0 == size || NULL == buf)
598 pong = (struct CadetPingMessage *) buf;
599 memcpy (pong, ping, sizeof (*ping));
600 pong->header.type = htons (PONG);
603 return sizeof (*ping);
608 * @brief Send a ping to destination
610 * @param cls Closure (peer).
611 * @param tc Task context.
614 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
616 struct CadetPeer *peer = (struct CadetPeer *) cls;
618 peer->ping_task = NULL;
620 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)
621 || GNUNET_YES == test_finished)
624 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u (%u)\n",
625 get_index (peer), get_index (peer->dest), peer->data_sent);
627 GNUNET_CADET_notify_transmit_ready (peer->ch, GNUNET_NO,
628 GNUNET_TIME_UNIT_FOREVER_REL,
629 sizeof (struct CadetPingMessage),
630 &tmt_rdy_ping, peer);
634 * @brief Reply with a pong to origin.
636 * @param cls Closure (peer).
637 * @param tc Task context.
640 pong (struct GNUNET_CADET_Channel *channel, const struct CadetPingMessage *ping)
642 struct CadetPingMessage *copy;
644 copy = GNUNET_new (struct CadetPingMessage);
645 memcpy (copy, ping, sizeof (*ping));
646 GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
647 GNUNET_TIME_UNIT_FOREVER_REL,
648 sizeof (struct CadetPingMessage),
649 &tmt_rdy_pong, copy);
654 * Transmit ping callback
656 * @param cls Closure (peer).
657 * @param size Size of the buffer we have.
658 * @param buf Buffer to copy data to.
661 tmt_rdy_ping (void *cls, size_t size, void *buf)
663 struct CadetPeer *peer = (struct CadetPeer *) cls;
664 struct CadetPingMessage *msg = buf;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
667 if (size < sizeof (struct CadetPingMessage) || NULL == buf)
669 GNUNET_break (GNUNET_YES == test_finished);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "size %u, buf %p, data_sent %u, data_received %u\n",
672 size, buf, peer->data_sent, peer->data_received);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending: msg %d\n", peer->data_sent);
677 msg->header.size = htons (size);
678 msg->header.type = htons (PING);
679 msg->counter = htonl (peer->data_sent++);
680 msg->round_number = htonl (current_round);
681 msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
682 peer->pings[current_round]++;
683 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
686 return sizeof (struct CadetPingMessage);
691 * Function is called whenever a PING message is received.
693 * @param cls closure (peer #, set from GNUNET_CADET_connect)
694 * @param channel connection to the other end
695 * @param channel_ctx place to store local state associated with the channel
696 * @param message the actual message
697 * @return GNUNET_OK to keep the connection open,
698 * GNUNET_SYSERR to close it (signal serious error)
701 ping_handler (void *cls, struct GNUNET_CADET_Channel *channel,
703 const struct GNUNET_MessageHeader *message)
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
708 GNUNET_CADET_receive_done (channel);
709 if (GNUNET_NO == test_finished)
710 pong (channel, (struct CadetPingMessage *) message);
717 * Function is called whenever a PONG message is received.
719 * @param cls closure (peer #, set from GNUNET_CADET_connect)
720 * @param channel connection to the other end
721 * @param channel_ctx place to store local state associated with the channel
722 * @param message the actual message
723 * @return GNUNET_OK to keep the connection open,
724 * GNUNET_SYSERR to close it (signal serious error)
727 pong_handler (void *cls, struct GNUNET_CADET_Channel *channel,
729 const struct GNUNET_MessageHeader *message)
732 struct CadetPeer *peer;
733 struct CadetPingMessage *msg;
734 struct GNUNET_TIME_Absolute send_time;
735 struct GNUNET_TIME_Relative latency;
736 unsigned int r /* Ping round */;
739 GNUNET_CADET_receive_done (channel);
742 msg = (struct CadetPingMessage *) message;
744 send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp);
745 latency = GNUNET_TIME_absolute_get_duration (send_time);
746 r = ntohl (msg->round_number);
747 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n",
748 get_index (peer), get_index (peer->dest), ntohl (msg->counter),
749 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
751 /* Online variance calculation */
753 delta = latency.rel_value_us - peer->mean[r];
754 peer->mean[r] = peer->mean[r] + delta/peer->pongs[r];
755 peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]);
762 * Handlers, for diverse services
764 static struct GNUNET_CADET_MessageHandler handlers[] = {
765 {&ping_handler, PING, sizeof (struct CadetPingMessage)},
766 {&pong_handler, PONG, sizeof (struct CadetPingMessage)},
772 * Method called whenever another peer has added us to a channel
773 * the other peer initiated.
775 * @param cls Closure.
776 * @param channel New handle to the channel.
777 * @param initiator Peer that started the channel.
778 * @param port Port this channel is connected to.
779 * @param options channel option flags
780 * @return Initial channel context for the channel
781 * (can be NULL -- that's not an error).
784 incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
785 const struct GNUNET_PeerIdentity *initiator,
786 uint32_t port, enum GNUNET_CADET_ChannelOption options)
789 struct CadetPeer *peer;
791 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
792 GNUNET_assert (NULL != peer);
793 if (NULL == peers[n].incoming)
795 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %3u: %u <= %u\n",
796 peers_warmup, n, get_index (peer));
798 if (peers_warmup < peers_total)
800 if (NULL != test_task)
802 GNUNET_SCHEDULER_cancel (test_task);
803 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
808 GNUNET_assert (peer == peers[n].incoming);
809 GNUNET_assert (peer->dest == &peers[n]);
810 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
811 n, get_index (peer), channel);
812 peers[n].incoming_ch = channel;
818 * Function called whenever an inbound channel is destroyed. Should clean up
819 * any associated state.
821 * @param cls closure (set from GNUNET_CADET_connect)
822 * @param channel connection to the other end (henceforth invalid)
823 * @param channel_ctx place where local state associated
824 * with the channel is stored
827 channel_cleaner (void *cls, const struct GNUNET_CADET_Channel *channel,
831 struct CadetPeer *peer = &peers[n];
833 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
834 "Channel %p disconnected at peer %ld\n", channel, n);
835 if (peer->ch == channel)
841 * Select a random peer that has no incoming channel
843 * @param peer ID of the peer connecting. NULL if irrelevant (warmup).
845 * @return Random peer not yet connected to.
847 static struct CadetPeer *
848 select_random_peer (struct CadetPeer *peer)
854 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total);
855 } while (NULL != peers[r].incoming);
856 peers[r].incoming = peer;
862 * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
864 * Testcase continues when the root receives confirmation of connected peers,
865 * on callback funtion ch.
867 * @param cls Closure (unsued).
868 * @param tc Task Context.
871 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
873 enum GNUNET_CADET_ChannelOption flags;
877 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
880 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
882 flags = GNUNET_CADET_OPTION_DEFAULT;
883 for (i = 0; i < peers_pinging; i++)
885 peers[i].dest = select_random_peer (&peers[i]);
886 peers[i].ch = GNUNET_CADET_channel_create (peers[i].cadet, NULL,
889 if (NULL == peers[i].ch)
891 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i);
892 GNUNET_CADET_TEST_cleanup (test_ctx);
895 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
896 i, get_index (peers[i].dest), peers[i].ch);
897 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
900 peers_running = peers_total;
901 if (NULL != disconnect_task)
902 GNUNET_SCHEDULER_cancel (disconnect_task);
904 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(round_time,
906 &disconnect_cadet_peers,
908 GNUNET_SCHEDULER_add_delayed (round_time, &next_rnd, NULL);
913 * Do warmup: create some channels to spread information about the topology.
918 struct CadetPeer *peer;
921 for (i = 0; i < peers_total; i++)
923 peer = select_random_peer (NULL);
924 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
925 i, get_index (peer));
927 GNUNET_CADET_channel_create (peers[i].cadet, NULL, &peer->id,
928 1, GNUNET_CADET_OPTION_DEFAULT);
929 if (NULL == peers[i].warmup_ch)
931 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i);
932 GNUNET_CADET_TEST_cleanup (test_ctx);
939 * Callback to be called when the requested peer information is available
941 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
942 * @param op the operation this callback corresponds to
943 * @param pinfo the result; will be NULL if the operation has failed
944 * @param emsg error message if the operation has failed;
945 * NULL if the operation is successfull
948 peer_id_cb (void *cls,
949 struct GNUNET_TESTBED_Operation *op,
950 const struct GNUNET_TESTBED_PeerInformation *pinfo,
955 if (NULL == pinfo || NULL != emsg)
957 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
958 abort_test (__LINE__);
961 peers[n].id = *(pinfo->result.id);
962 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u id: %s\n",
963 n, GNUNET_i2s (&peers[n].id));
964 GNUNET_break (GNUNET_OK ==
965 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
966 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
968 GNUNET_TESTBED_operation_done (peers[n].op);
972 if (p_ids < peers_total)
974 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
977 struct GNUNET_TIME_Relative delay;
980 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
982 test_task = GNUNET_SCHEDULER_add_delayed (delay, &start_test, NULL);
983 return; /* start_test from incoming_channel */
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n");
986 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
991 * test main: start test when all peers are connected
993 * @param cls Closure.
994 * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
995 * @param num_peers Number of peers that are running.
996 * @param testbed_peers Array of peers.
997 * @param cadetes Handle to each of the CADETs of the peers.
1001 struct GNUNET_CADET_TEST_Context *ctx,
1002 unsigned int num_peers,
1003 struct GNUNET_TESTBED_Peer **testbed_peers,
1004 struct GNUNET_CADET_Handle **cadetes)
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
1010 GNUNET_assert (peers_total == num_peers);
1011 peers_running = num_peers;
1012 testbed_handles = testbed_peers;
1013 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
1014 &disconnect_cadet_peers,
1016 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1017 &shutdown_task, NULL);
1018 for (i = 0; i < peers_total; i++)
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
1021 peers[i].up = GNUNET_YES;
1022 peers[i].cadet = cadetes[i];
1024 GNUNET_TESTBED_peer_get_information (testbed_handles[i],
1025 GNUNET_TESTBED_PIT_IDENTITY,
1026 &peer_id_cb, (void *) i);
1028 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "requested peer ids\n");
1029 /* Continues from pi_cb -> do_test */
1034 * Main: start profiler.
1037 main (int argc, char *argv[])
1039 static uint32_t ports[2];
1040 const char *config_file;
1042 config_file = ".profiler.conf";
1046 fprintf (stderr, "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", argv[0]);
1047 fprintf (stderr, "example: %s 30s 16 1 Y\n", argv[0]);
1051 if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[1], &round_time))
1053 fprintf (stderr, "%s is not a valid time\n", argv[1]);
1057 peers_total = atoll (argv[2]);
1058 if (2 > peers_total)
1060 fprintf (stderr, "%s peers is not valid (> 2)\n", argv[1]);
1063 peers = GNUNET_malloc (sizeof (struct CadetPeer) * peers_total);
1065 peers_pinging = atoll (argv[3]);
1067 if (peers_total < 2 * peers_pinging)
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1070 "not enough peers, total should be > 2 * peers_pinging\n");
1074 do_warmup = (5 > argc || argv[4][0] != 'N');
1076 ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, GNUNET_YES);
1077 GNUNET_assert (NULL != ids);
1079 test_finished = GNUNET_NO;
1082 GNUNET_CADET_TEST_run ("cadet-profiler", config_file, peers_total,
1083 &tmain, NULL, /* tmain cls */
1084 &incoming_channel, &channel_cleaner,
1086 GNUNET_free (peers);
1091 /* end of gnunet-cadet-profiler.c */