- only ping from select peers, do not shut them down
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file mesh/gnunet-mesh-profiler.c
22  *
23  * @brief Profiler for mesh experiments.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include "gnunet_statistics_service.h"
30
31
32 #define PING 1
33 #define PONG 2
34
35 /**
36  * How many peers to run
37  */
38 #define TOTAL_PEERS 10
39
40 /**
41  * How many peers do pinging
42  */
43 #define PING_PEERS 1
44
45
46 /**
47  * Duration of each round.
48  */
49 #define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50
51 /**
52  * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
53  */
54 #define PING_PERIOD 2000
55
56 /**
57  * How long until we give up on connecting the peers?
58  */
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
60
61 /**
62  * Time to wait for stuff that should be rather fast
63  */
64 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
65
66 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
67
68 struct MeshPeer
69 {
70   /**
71    * Testbed Operation (to get peer id, etc).
72    */
73   struct GNUNET_TESTBED_Operation *op;
74
75   /**
76    * Peer ID.
77    */
78   struct GNUNET_PeerIdentity id;
79
80   /**
81    * Mesh handle for the root peer
82    */
83   struct GNUNET_MESH_Handle *mesh;
84
85   /**
86    * Channel handle for the root peer
87    */
88   struct GNUNET_MESH_Channel *ch;
89
90   /**
91    * Channel handle for the dest peer
92    */
93   struct GNUNET_MESH_Channel *incoming_ch;
94
95   /**
96    * Number of payload packes sent
97    */
98   int data_sent;
99
100   /**
101    * Number of payload packets received
102    */
103   int data_received;
104
105   int up;
106
107   struct MeshPeer *dest;
108   struct MeshPeer *incoming;
109   GNUNET_SCHEDULER_TaskIdentifier ping_task;
110   struct GNUNET_TIME_Absolute timestamp;
111 };
112
113 /**
114  * GNUNET_PeerIdentity -> MeshPeer
115  */
116 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
117
118 /**
119  * Testbed peer handles.
120  */
121 static struct GNUNET_TESTBED_Peer **testbed_handles;
122
123 /**
124  * Testbed Operation (to get stats).
125  */
126 static struct GNUNET_TESTBED_Operation *stats_op;
127
128 /**
129  * How many events have happened
130  */
131 static int ok;
132
133 /**
134  * Number of events expected to conclude the test successfully.
135  */
136 static int ok_goal;
137
138 /**
139  * Size of each test packet
140  */
141 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
142
143 /**
144  * Operation to get peer ids.
145  */
146 struct MeshPeer peers[TOTAL_PEERS];
147
148 /**
149  * Peer ids counter.
150  */
151 static unsigned int p_ids;
152
153 /**
154  * Total number of currently running peers.
155  */
156 static unsigned long long peers_running;
157
158 /**
159  * Test context (to shut down).
160  */
161 static struct GNUNET_MESH_TEST_Context *test_ctx;
162
163 /**
164  * Task called to shutdown test.
165  */
166 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
167
168 /**
169  * Task called to disconnect peers, before shutdown.
170  */
171 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
172
173 /**
174  * Task to perform tests
175  */
176 static GNUNET_SCHEDULER_TaskIdentifier test_task;
177
178
179 /**
180  * Flag to notify callbacks not to generate any new traffic anymore.
181  */
182 static int test_finished;
183
184 /**
185  * Calculate a random delay.
186  *
187  * @param max Exclusive maximum, in ms.
188  *
189  * @return A time between 0 a max-1 ms.
190  */
191 static struct GNUNET_TIME_Relative
192 delay_ms_rnd (unsigned int max)
193 {
194   unsigned int rnd;
195
196   rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
197   return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
198 }
199
200
201 /**
202  * Get the index of a peer in the peers array.
203  *
204  * @param peer Peer whose index to get.
205  *
206  * @return Index of peer in peers.
207  */
208 static unsigned int
209 get_index (struct MeshPeer *peer)
210 {
211   return peer - peers;
212 }
213
214
215 /**
216  * Show the results of the test (banwidth acheived) and log them to GAUGER
217  */
218 static void
219 show_end_data (void)
220 {
221 }
222
223
224 /**
225  * Shut down peergroup, clean up.
226  *
227  * @param cls Closure (unused).
228  * @param tc Task Context.
229  */
230 static void
231 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 {
233   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
234   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
235 }
236
237
238 /**
239  * Disconnect from mesh services af all peers, call shutdown.
240  *
241  * @param cls Closure (unused).
242  * @param tc Task Context.
243  */
244 static void
245 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   long line = (long) cls;
248   unsigned int i;
249
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "disconnecting mesh service, called from line %ld\n", line);
252   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
253   for (i = 0; i < TOTAL_PEERS; i++)
254   {
255     if (NULL != peers[i].op)
256       GNUNET_TESTBED_operation_done (peers[i].op);
257
258     if (peers[i].up != GNUNET_YES)
259       continue;
260
261     if (NULL != peers[i].ch)
262     {
263       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: channel %p\n", i, peers[i].ch);
264       GNUNET_MESH_channel_destroy (peers[i].ch);
265     }
266     if (NULL != peers[i].incoming_ch)
267     {
268       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u: incoming channel %p\n",
269                   i, peers[i].incoming_ch);
270       GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
271     }
272   }
273   GNUNET_MESH_TEST_cleanup (test_ctx);
274   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
275   {
276     GNUNET_SCHEDULER_cancel (shutdown_handle);
277   }
278   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
279 }
280
281
282 /**
283  * Finish test normally: schedule disconnect and shutdown
284  *
285  * @param line Line in the code the abort is requested from (__LINE__).
286  */
287 static void
288 abort_test (long line)
289 {
290   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
291   {
292     GNUNET_SCHEDULER_cancel (disconnect_task);
293     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
294                                                 (void *) line);
295   }
296 }
297
298 /**
299  * Stats callback. Finish the stats testbed operation and when all stats have
300  * been iterated, shutdown the test.
301  *
302  * @param cls closure
303  * @param op the operation that has been finished
304  * @param emsg error message in case the operation has failed; will be NULL if
305  *          operation has executed successfully.
306  */
307 static void
308 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
309 {
310   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
311   GNUNET_TESTBED_operation_done (stats_op);
312
313   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
314     GNUNET_SCHEDULER_cancel (disconnect_task);
315   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
316                                               (void *) __LINE__);
317
318 }
319
320
321 /**
322  * Process statistic values.
323  *
324  * @param cls closure
325  * @param peer the peer the statistic belong to
326  * @param subsystem name of subsystem that created the statistic
327  * @param name the name of the datum
328  * @param value the current value
329  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
330  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
331  */
332 static int
333 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
334                 const char *subsystem, const char *name,
335                 uint64_t value, int is_persistent)
336 {
337   uint32_t i;
338
339   i = GNUNET_TESTBED_get_index (peer);
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
341               i, subsystem, name, value);
342
343   return GNUNET_OK;
344 }
345
346
347 /**
348  * Task check that keepalives were sent and received.
349  *
350  * @param cls Closure (NULL).
351  * @param tc Task Context.
352  */
353 static void
354 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
355 {
356   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
357     return;
358
359   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
360   stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
361                                             NULL, NULL,
362                                             stats_iterator, stats_cont, NULL);
363 }
364
365
366 /**
367  * @brief Finish profiler normally. Signal finish and start collecting stats.
368  *
369  * @param cls Closure (unused).
370  * @param tc Task context.
371  */
372 static void
373 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
374 {
375   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
376     return;
377
378   test_finished = GNUNET_YES;
379   show_end_data();
380   GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
381 }
382
383 /**
384  * Set the total number of running peers.
385  *
386  * @param target Desired number of running peers.
387  */
388 static void
389 adjust_running_peers (unsigned int target)
390 {
391   struct GNUNET_TESTBED_Operation *op;
392   unsigned int delta;
393   unsigned int run;
394   unsigned int i;
395   unsigned int r;
396
397   GNUNET_assert (target <= TOTAL_PEERS);
398
399   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
400   if (target > peers_running)
401   {
402     delta = target - peers_running;
403     run = GNUNET_YES;
404   }
405   else
406   {
407     delta = peers_running - target;
408     run = GNUNET_NO;
409   }
410
411   for (i = 0; i < delta; i++)
412   {
413     do {
414       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
415                                     TOTAL_PEERS - PING_PEERS);
416       r += PING_PEERS;
417     } while (peers[r].up == run || NULL != peers[r].incoming);
418     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
419                 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
420
421     if (GNUNET_SCHEDULER_NO_TASK != peers[r].ping_task)
422       GNUNET_SCHEDULER_cancel (peers[r].ping_task);
423     peers[r].ping_task = GNUNET_SCHEDULER_NO_TASK;
424
425     peers[r].up = run;
426
427     if (NULL != peers[r].ch)
428       GNUNET_MESH_channel_destroy (peers[r].ch);
429     peers[r].ch = NULL;
430     if (NULL != peers[r].dest)
431     {
432       if (NULL != peers[r].dest->incoming_ch)
433         GNUNET_MESH_channel_destroy (peers[r].dest->incoming_ch);
434       peers[r].dest->incoming_ch = NULL;
435     }
436     GNUNET_MESH_disconnect (peers[r].mesh);
437     peers[r].mesh = NULL;
438
439     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
440                                              "mesh", NULL, NULL, run);
441     GNUNET_break (NULL != op);
442     peers_running += run ? 1 : -1;
443     GNUNET_assert (peers_running > 0);
444   }
445 }
446
447
448 /**
449  * @brief Move to next round.
450  *
451  * @param cls Closure (round #).
452  * @param tc Task context.
453  */
454 static void
455 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
456 {
457   long round = (long) cls;
458
459   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
460     return;
461
462   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
463   if (0.0 == rounds[round])
464   {
465     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
466     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
467     return;
468   }
469   adjust_running_peers (rounds[round] * TOTAL_PEERS);
470
471   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
472 }
473
474
475 /**
476  * Transmit ready callback.
477  *
478  * @param cls Closure (peer for PING, NULL for PONG).
479  * @param size Size of the tranmist buffer.
480  * @param buf Pointer to the beginning of the buffer.
481  *
482  * @return Number of bytes written to buf.
483  */
484 static size_t
485 tmt_rdy (void *cls, size_t size, void *buf);
486
487
488 /**
489  * @brief Send a ping to destination
490  *
491  * @param cls Closure (peer).
492  * @param tc Task context.
493  */
494 static void
495 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
496 {
497   struct MeshPeer *peer = (struct MeshPeer *) cls;
498
499   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
500
501   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
502       || GNUNET_YES == test_finished
503       || 0 != peer->timestamp.abs_value_us)
504     return;
505
506   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
507               get_index (peer), get_index (peer->dest));
508
509   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
510                                      GNUNET_TIME_UNIT_FOREVER_REL,
511                                      size_payload, &tmt_rdy, peer);
512 }
513
514 /**
515  * @brief Reply with a pong to origin.
516  *
517  * @param cls Closure (peer).
518  * @param tc Task context.
519  */
520 static void
521 pong (struct GNUNET_MESH_Channel *channel)
522 {
523   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
524                                      GNUNET_TIME_UNIT_FOREVER_REL,
525                                      size_payload, &tmt_rdy, NULL);
526 }
527
528
529 /**
530  * Transmit ready callback
531  *
532  * @param cls Closure (peer for PING, NULL for PONG).
533  * @param size Size of the buffer we have.
534  * @param buf Buffer to copy data to.
535  */
536 static size_t
537 tmt_rdy (void *cls, size_t size, void *buf)
538 {
539   struct MeshPeer *peer = (struct MeshPeer *) cls;
540   struct GNUNET_MessageHeader *msg = buf;
541   uint32_t *data;
542
543   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
544   if (size < size_payload || NULL == buf)
545   {
546     GNUNET_break (0);
547     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548                 "size %u, buf %p, data_sent %u, data_received %u\n",
549                 size, buf, peer->data_sent, peer->data_received);
550
551     return 0;
552   }
553   msg->size = htons (size);
554   if (NULL == peer)
555   {
556     msg->type = htons (PONG);
557     return sizeof (*msg);
558   }
559
560   msg->type = htons (PING);
561   data = (uint32_t *) &msg[1];
562   *data = htonl (peer->data_sent);
563   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
564   peer->data_sent++;
565   peer->timestamp = GNUNET_TIME_absolute_get ();
566   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
567                                                   &ping, peer);
568
569   return size_payload;
570 }
571
572
573 /**
574  * Function is called whenever a PING message is received.
575  *
576  * @param cls closure (peer #, set from GNUNET_MESH_connect)
577  * @param channel connection to the other end
578  * @param channel_ctx place to store local state associated with the channel
579  * @param message the actual message
580  * @return GNUNET_OK to keep the connection open,
581  *         GNUNET_SYSERR to close it (signal serious error)
582  */
583 int
584 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
585               void **channel_ctx,
586               const struct GNUNET_MessageHeader *message)
587 {
588   long n = (long) cls;
589
590   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
591   GNUNET_MESH_receive_done (channel);
592   if (GNUNET_NO == test_finished)
593     pong (channel);
594
595   return GNUNET_OK;
596 }
597
598
599 /**
600  * Function is called whenever a PONG message is received.
601  *
602  * @param cls closure (peer #, set from GNUNET_MESH_connect)
603  * @param channel connection to the other end
604  * @param channel_ctx place to store local state associated with the channel
605  * @param message the actual message
606  * @return GNUNET_OK to keep the connection open,
607  *         GNUNET_SYSERR to close it (signal serious error)
608  */
609 int
610 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
611               void **channel_ctx,
612               const struct GNUNET_MessageHeader *message)
613 {
614   long n = (long) cls;
615   struct MeshPeer *peer;
616   struct GNUNET_TIME_Relative latency;
617
618   GNUNET_MESH_receive_done (channel);
619   peer = &peers[n];
620
621   GNUNET_break (0 != peer->timestamp.abs_value_us);
622   latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
623   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
624               get_index (peer), get_index (peer->dest),
625               GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
626
627   if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
628   {
629     peer->timestamp = GNUNET_TIME_absolute_get ();
630     peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
631                                                     &ping, peer);
632   }
633   else
634   {
635     peer->timestamp.abs_value_us = 0;
636   }
637
638   return GNUNET_OK;
639 }
640
641
642 /**
643  * Handlers, for diverse services
644  */
645 static struct GNUNET_MESH_MessageHandler handlers[] = {
646   {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
647   {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
648   {NULL, 0, 0}
649 };
650
651
652 /**
653  * Method called whenever another peer has added us to a channel
654  * the other peer initiated.
655  *
656  * @param cls Closure.
657  * @param channel New handle to the channel.
658  * @param initiator Peer that started the channel.
659  * @param port Port this channel is connected to.
660  * @param options channel option flags
661  * @return Initial channel context for the channel
662  *         (can be NULL -- that's not an error).
663  */
664 static void *
665 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
666                  const struct GNUNET_PeerIdentity *initiator,
667                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
668 {
669   long n = (long) cls;
670   struct MeshPeer *peer;
671
672   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
673   GNUNET_assert (NULL != peer);
674   GNUNET_assert (peer == peers[n].incoming);
675   GNUNET_assert (peer->dest == &peers[n]);
676   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
677               n, get_index (peer), channel);
678   peers[n].incoming_ch = channel;
679
680   return NULL;
681 }
682
683 /**
684  * Function called whenever an inbound channel is destroyed.  Should clean up
685  * any associated state.
686  *
687  * @param cls closure (set from GNUNET_MESH_connect)
688  * @param channel connection to the other end (henceforth invalid)
689  * @param channel_ctx place where local state associated
690  *                   with the channel is stored
691  */
692 static void
693 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
694                  void *channel_ctx)
695 {
696   long n = (long) cls;
697   struct MeshPeer *peer = &peers[n];
698
699   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
700               "Channel %p disconnected at peer %ld\n", channel, n);
701   if (peer->ch == channel)
702     peer->ch = NULL;
703 }
704
705
706 static struct MeshPeer *
707 select_random_peer (struct MeshPeer *peer)
708 {
709   unsigned int r;
710
711   do
712   {
713     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
714   } while (NULL != peers[r].incoming);
715   peers[r].incoming = peer;
716
717   return &peers[r];
718 }
719
720 /**
721  * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
722  *
723  * Testcase continues when the root receives confirmation of connected peers,
724  * on callback funtion ch.
725  *
726  * @param cls Closure (unsued).
727  * @param tc Task Context.
728  */
729 static void
730 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
731 {
732   enum GNUNET_MESH_ChannelOption flags;
733   unsigned long i;
734
735   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
736     return;
737
738   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
739
740   flags = GNUNET_MESH_OPTION_DEFAULT;
741   for (i = 0; i < PING_PEERS; i++)
742   {
743
744     peers[i].dest = select_random_peer (&peers[i]);
745     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
746                                               &peers[i].dest->id,
747                                               1, flags);
748     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
749                 i, get_index (peers[i].dest), peers[i].ch);
750     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
751                                                        &ping, &peers[i]);
752   }
753   peers_running = TOTAL_PEERS;
754   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
755 }
756
757
758 /**
759  * Callback to be called when the requested peer information is available
760  *
761  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
762  * @param op the operation this callback corresponds to
763  * @param pinfo the result; will be NULL if the operation has failed
764  * @param emsg error message if the operation has failed;
765  *             NULL if the operation is successfull
766  */
767 static void
768 peer_id_cb (void *cls,
769        struct GNUNET_TESTBED_Operation *op,
770        const struct GNUNET_TESTBED_PeerInformation *pinfo,
771        const char *emsg)
772 {
773   long n = (long) cls;
774
775   if (NULL == pinfo || NULL != emsg)
776   {
777     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
778     abort_test (__LINE__);
779     return;
780   }
781   peers[n].id = *(pinfo->result.id);
782   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u  id: %s\n",
783               n, GNUNET_i2s (&peers[n].id));
784   GNUNET_break (GNUNET_OK ==
785                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
786                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
787
788   GNUNET_TESTBED_operation_done (peers[n].op);
789   peers[n].op = NULL;
790
791   p_ids++;
792   if (p_ids < TOTAL_PEERS)
793     return;
794   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
795   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
796                                             &start_test, NULL);
797 }
798
799 /**
800  * test main: start test when all peers are connected
801  *
802  * @param cls Closure.
803  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
804  * @param num_peers Number of peers that are running.
805  * @param testbed_peers Array of peers.
806  * @param meshes Handle to each of the MESHs of the peers.
807  */
808 static void
809 tmain (void *cls,
810        struct GNUNET_MESH_TEST_Context *ctx,
811        unsigned int num_peers,
812        struct GNUNET_TESTBED_Peer **testbed_peers,
813        struct GNUNET_MESH_Handle **meshes)
814 {
815   unsigned long i;
816
817   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
818   ok = 0;
819   test_ctx = ctx;
820   GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
821   GNUNET_assert (TOTAL_PEERS == num_peers);
822   peers_running = num_peers;
823   testbed_handles = testbed_peers;
824   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
825                                                   &disconnect_mesh_peers,
826                                                   (void *) __LINE__);
827   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
828                                                   &shutdown_task, NULL);
829   for (i = 0; i < TOTAL_PEERS; i++)
830   {
831     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
832     peers[i].up = GNUNET_YES;
833     peers[i].mesh = meshes[i];
834     peers[i].op =
835       GNUNET_TESTBED_peer_get_information (testbed_handles[i],
836                                            GNUNET_TESTBED_PIT_IDENTITY,
837                                            &peer_id_cb, (void *) i);
838   }
839   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
840   /* Continues from pi_cb -> do_test */
841 }
842
843
844 /**
845  * Main: start profiler.
846  */
847 int
848 main (int argc, char *argv[])
849 {
850   static uint32_t ports[2];
851   const char *config_file;
852
853   config_file = "test_mesh.conf";
854
855   ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
856   GNUNET_assert (NULL != ids);
857   p_ids = 0;
858   test_finished = GNUNET_NO;
859   ports[0] = 1;
860   ports[1] = 0;
861   GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
862                         &tmain, NULL, /* tmain cls */
863                         &incoming_channel, &channel_cleaner,
864                         handlers, ports);
865
866   if (ok_goal > ok)
867   {
868     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
869                 "FAILED! (%d/%d)\n", ok, ok_goal);
870     return 1;
871   }
872   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
873   return 0;
874 }
875
876 /* end of gnunet-mesh-profiler.c */
877