- dont disconnect from mesh service
[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
437     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
438                                              "mesh", NULL, NULL, run);
439     GNUNET_break (NULL != op);
440     peers_running += run ? 1 : -1;
441     GNUNET_assert (peers_running > 0);
442   }
443 }
444
445
446 /**
447  * @brief Move to next round.
448  *
449  * @param cls Closure (round #).
450  * @param tc Task context.
451  */
452 static void
453 next_rnd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
454 {
455   long round = (long) cls;
456
457   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
458     return;
459
460   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
461   if (0.0 == rounds[round])
462   {
463     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n");
464     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
465     return;
466   }
467   adjust_running_peers (rounds[round] * TOTAL_PEERS);
468
469   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, (void *) (round + 1));
470 }
471
472
473 /**
474  * Transmit ready callback.
475  *
476  * @param cls Closure (peer for PING, NULL for PONG).
477  * @param size Size of the tranmist buffer.
478  * @param buf Pointer to the beginning of the buffer.
479  *
480  * @return Number of bytes written to buf.
481  */
482 static size_t
483 tmt_rdy (void *cls, size_t size, void *buf);
484
485
486 /**
487  * @brief Send a ping to destination
488  *
489  * @param cls Closure (peer).
490  * @param tc Task context.
491  */
492 static void
493 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 {
495   struct MeshPeer *peer = (struct MeshPeer *) cls;
496
497   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
498
499   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
500       || GNUNET_YES == test_finished
501       || 0 != peer->timestamp.abs_value_us)
502     return;
503
504   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
505               get_index (peer), get_index (peer->dest));
506
507   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
508                                      GNUNET_TIME_UNIT_FOREVER_REL,
509                                      size_payload, &tmt_rdy, peer);
510 }
511
512 /**
513  * @brief Reply with a pong to origin.
514  *
515  * @param cls Closure (peer).
516  * @param tc Task context.
517  */
518 static void
519 pong (struct GNUNET_MESH_Channel *channel)
520 {
521   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
522                                      GNUNET_TIME_UNIT_FOREVER_REL,
523                                      size_payload, &tmt_rdy, NULL);
524 }
525
526
527 /**
528  * Transmit ready callback
529  *
530  * @param cls Closure (peer for PING, NULL for PONG).
531  * @param size Size of the buffer we have.
532  * @param buf Buffer to copy data to.
533  */
534 static size_t
535 tmt_rdy (void *cls, size_t size, void *buf)
536 {
537   struct MeshPeer *peer = (struct MeshPeer *) cls;
538   struct GNUNET_MessageHeader *msg = buf;
539   uint32_t *data;
540
541   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
542   if (size < size_payload || NULL == buf)
543   {
544     GNUNET_break (0);
545     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546                 "size %u, buf %p, data_sent %u, data_received %u\n",
547                 size, buf, peer->data_sent, peer->data_received);
548
549     return 0;
550   }
551   msg->size = htons (size);
552   if (NULL == peer)
553   {
554     msg->type = htons (PONG);
555     return sizeof (*msg);
556   }
557
558   msg->type = htons (PING);
559   data = (uint32_t *) &msg[1];
560   *data = htonl (peer->data_sent);
561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
562   peer->data_sent++;
563   peer->timestamp = GNUNET_TIME_absolute_get ();
564   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
565                                                   &ping, peer);
566
567   return size_payload;
568 }
569
570
571 /**
572  * Function is called whenever a PING message is received.
573  *
574  * @param cls closure (peer #, set from GNUNET_MESH_connect)
575  * @param channel connection to the other end
576  * @param channel_ctx place to store local state associated with the channel
577  * @param message the actual message
578  * @return GNUNET_OK to keep the connection open,
579  *         GNUNET_SYSERR to close it (signal serious error)
580  */
581 int
582 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
583               void **channel_ctx,
584               const struct GNUNET_MessageHeader *message)
585 {
586   long n = (long) cls;
587
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
589   GNUNET_MESH_receive_done (channel);
590   if (GNUNET_NO == test_finished)
591     pong (channel);
592
593   return GNUNET_OK;
594 }
595
596
597 /**
598  * Function is called whenever a PONG message is received.
599  *
600  * @param cls closure (peer #, set from GNUNET_MESH_connect)
601  * @param channel connection to the other end
602  * @param channel_ctx place to store local state associated with the channel
603  * @param message the actual message
604  * @return GNUNET_OK to keep the connection open,
605  *         GNUNET_SYSERR to close it (signal serious error)
606  */
607 int
608 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
609               void **channel_ctx,
610               const struct GNUNET_MessageHeader *message)
611 {
612   long n = (long) cls;
613   struct MeshPeer *peer;
614   struct GNUNET_TIME_Relative latency;
615
616   GNUNET_MESH_receive_done (channel);
617   peer = &peers[n];
618
619   GNUNET_break (0 != peer->timestamp.abs_value_us);
620   latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
621   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u latency: %s\n",
622               get_index (peer), get_index (peer->dest),
623               GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
624
625   if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
626   {
627     peer->timestamp = GNUNET_TIME_absolute_get ();
628     peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
629                                                     &ping, peer);
630   }
631   else
632   {
633     peer->timestamp.abs_value_us = 0;
634   }
635
636   return GNUNET_OK;
637 }
638
639
640 /**
641  * Handlers, for diverse services
642  */
643 static struct GNUNET_MESH_MessageHandler handlers[] = {
644   {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
645   {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
646   {NULL, 0, 0}
647 };
648
649
650 /**
651  * Method called whenever another peer has added us to a channel
652  * the other peer initiated.
653  *
654  * @param cls Closure.
655  * @param channel New handle to the channel.
656  * @param initiator Peer that started the channel.
657  * @param port Port this channel is connected to.
658  * @param options channel option flags
659  * @return Initial channel context for the channel
660  *         (can be NULL -- that's not an error).
661  */
662 static void *
663 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
664                  const struct GNUNET_PeerIdentity *initiator,
665                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
666 {
667   long n = (long) cls;
668   struct MeshPeer *peer;
669
670   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
671   GNUNET_assert (NULL != peer);
672   GNUNET_assert (peer == peers[n].incoming);
673   GNUNET_assert (peer->dest == &peers[n]);
674   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u %p\n",
675               n, get_index (peer), channel);
676   peers[n].incoming_ch = channel;
677
678   return NULL;
679 }
680
681 /**
682  * Function called whenever an inbound channel is destroyed.  Should clean up
683  * any associated state.
684  *
685  * @param cls closure (set from GNUNET_MESH_connect)
686  * @param channel connection to the other end (henceforth invalid)
687  * @param channel_ctx place where local state associated
688  *                   with the channel is stored
689  */
690 static void
691 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
692                  void *channel_ctx)
693 {
694   long n = (long) cls;
695   struct MeshPeer *peer = &peers[n];
696
697   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698               "Channel %p disconnected at peer %ld\n", channel, n);
699   if (peer->ch == channel)
700     peer->ch = NULL;
701 }
702
703
704 static struct MeshPeer *
705 select_random_peer (struct MeshPeer *peer)
706 {
707   unsigned int r;
708
709   do
710   {
711     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
712   } while (NULL != peers[r].incoming);
713   peers[r].incoming = peer;
714
715   return &peers[r];
716 }
717
718 /**
719  * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
720  *
721  * Testcase continues when the root receives confirmation of connected peers,
722  * on callback funtion ch.
723  *
724  * @param cls Closure (unsued).
725  * @param tc Task Context.
726  */
727 static void
728 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
729 {
730   enum GNUNET_MESH_ChannelOption flags;
731   unsigned long i;
732
733   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
734     return;
735
736   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
737
738   flags = GNUNET_MESH_OPTION_DEFAULT;
739   for (i = 0; i < PING_PEERS; i++)
740   {
741
742     peers[i].dest = select_random_peer (&peers[i]);
743     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
744                                               &peers[i].dest->id,
745                                               1, flags);
746     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u %p\n",
747                 i, get_index (peers[i].dest), peers[i].ch);
748     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
749                                                        &ping, &peers[i]);
750   }
751   peers_running = TOTAL_PEERS;
752   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_rnd, NULL);
753 }
754
755
756 /**
757  * Callback to be called when the requested peer information is available
758  *
759  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
760  * @param op the operation this callback corresponds to
761  * @param pinfo the result; will be NULL if the operation has failed
762  * @param emsg error message if the operation has failed;
763  *             NULL if the operation is successfull
764  */
765 static void
766 peer_id_cb (void *cls,
767        struct GNUNET_TESTBED_Operation *op,
768        const struct GNUNET_TESTBED_PeerInformation *pinfo,
769        const char *emsg)
770 {
771   long n = (long) cls;
772
773   if (NULL == pinfo || NULL != emsg)
774   {
775     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
776     abort_test (__LINE__);
777     return;
778   }
779   peers[n].id = *(pinfo->result.id);
780   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u  id: %s\n",
781               n, GNUNET_i2s (&peers[n].id));
782   GNUNET_break (GNUNET_OK ==
783                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
784                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
785
786   GNUNET_TESTBED_operation_done (peers[n].op);
787   peers[n].op = NULL;
788
789   p_ids++;
790   if (p_ids < TOTAL_PEERS)
791     return;
792   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
793   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
794                                             &start_test, NULL);
795 }
796
797 /**
798  * test main: start test when all peers are connected
799  *
800  * @param cls Closure.
801  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
802  * @param num_peers Number of peers that are running.
803  * @param testbed_peers Array of peers.
804  * @param meshes Handle to each of the MESHs of the peers.
805  */
806 static void
807 tmain (void *cls,
808        struct GNUNET_MESH_TEST_Context *ctx,
809        unsigned int num_peers,
810        struct GNUNET_TESTBED_Peer **testbed_peers,
811        struct GNUNET_MESH_Handle **meshes)
812 {
813   unsigned long i;
814
815   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
816   ok = 0;
817   test_ctx = ctx;
818   GNUNET_assert (TOTAL_PEERS > 2 * PING_PEERS);
819   GNUNET_assert (TOTAL_PEERS == num_peers);
820   peers_running = num_peers;
821   testbed_handles = testbed_peers;
822   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
823                                                   &disconnect_mesh_peers,
824                                                   (void *) __LINE__);
825   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
826                                                   &shutdown_task, NULL);
827   for (i = 0; i < TOTAL_PEERS; i++)
828   {
829     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
830     peers[i].up = GNUNET_YES;
831     peers[i].mesh = meshes[i];
832     peers[i].op =
833       GNUNET_TESTBED_peer_get_information (testbed_handles[i],
834                                            GNUNET_TESTBED_PIT_IDENTITY,
835                                            &peer_id_cb, (void *) i);
836   }
837   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
838   /* Continues from pi_cb -> do_test */
839 }
840
841
842 /**
843  * Main: start profiler.
844  */
845 int
846 main (int argc, char *argv[])
847 {
848   static uint32_t ports[2];
849   const char *config_file;
850
851   config_file = "test_mesh.conf";
852
853   ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
854   GNUNET_assert (NULL != ids);
855   p_ids = 0;
856   test_finished = GNUNET_NO;
857   ports[0] = 1;
858   ports[1] = 0;
859   GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
860                         &tmain, NULL, /* tmain cls */
861                         &incoming_channel, &channel_cleaner,
862                         handlers, ports);
863
864   if (ok_goal > ok)
865   {
866     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
867                 "FAILED! (%d/%d)\n", ok, ok_goal);
868     return 1;
869   }
870   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
871   return 0;
872 }
873
874 /* end of gnunet-mesh-profiler.c */
875